Esempio n. 1
0
 public function __construct($anomaly, $api = null, $storage = "storage")
 {
     if ($api == null) {
         $api = new BigML(null, null, null, $storage);
     }
     if (is_string($anomaly)) {
         if (!$api::_checkAnomalyId($anomaly)) {
             error_log("Wrong anomaly id");
             return null;
         }
         $anomaly = $api::retrieve_resource($anomaly, $api::ONLY_MODEL);
     }
     if ($anomaly == null || !property_exists($anomaly, 'resource')) {
         error_log("Cannot create the Anomaly instance. Could not find the 'model' key in the resource");
         throw new Exception('Cannot create the Anomaly instance. Could not find the model key in the resource');
     }
     if (property_exists($anomaly, "object") && property_exists($anomaly->object, "status") && $anomaly->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The model isn't finished yet");
     }
     if (property_exists($anomaly, "object") && $anomaly->object instanceof STDClass) {
         $anomaly = $anomaly->object;
         $this->sample_size = $anomaly->sample_size;
         $this->input_fields = $anomaly->input_fields;
         $this->id_fields = $anomaly->id_fields;
     }
     if (property_exists($anomaly, "model") && $anomaly->model instanceof STDClass) {
         parent::__construct($anomaly->model->fields);
         if (property_exists($anomaly->model, "top_anomalies") && is_array($anomaly->model->top_anomalies)) {
             $this->mean_depth = $anomaly->model->mean_depth;
             if ($anomaly->status != null && ($anomaly->status->code = 5)) {
                 $this->expected_mean_depth = null;
                 if ($this->mean_depth == null || $this->sample_size == null) {
                     error_log("The anomaly data is not complete. Score will not be available");
                     throw new Exception('The anomaly data is not complete. Score will not be available');
                 } else {
                     $default_depth = 2 * (0.5772156649 + log($this->sample_size - 1) - floatval($this->sample_size - 1) / $this->sample_size);
                     $this->expected_mean_depth = min(array($this->mean_depth, $default_depth));
                 }
                 $iflorest = property_exists($anomaly->model, "trees") ? $anomaly->model->trees : array();
                 if ($iflorest != null && !empty($iflorest)) {
                     $this->iforest = array();
                     foreach ($iflorest as $anomaly_tree) {
                         array_push($this->iforest, new AnomalyTree($anomaly_tree->root, $this->fields));
                     }
                 }
                 $this->top_anomalies = $anomaly->model->top_anomalies;
             } else {
                 error_log("The anomaly isn't finished yet");
                 throw new Exception("The anomaly isn't finished yet");
             }
         } else {
             error_log("Cannot create the Anomaly instance. Could not find the 'top_anomalies' key in the resource");
             throw new Exception("Cannot create the Anomaly instance. Could not find the 'top_anomalies' key in the resource");
         }
     }
 }
Esempio n. 2
0
 public function __construct($model, $api = null)
 {
     if (check_model_structure($model)) {
         $this->resource_id = $model->resource;
     } else {
         if ($api == null) {
             $api = new BigML(null, null, null, $storage);
         }
         if (is_string($model)) {
             if (!$api::_checkModelId($model)) {
                 error_log("Wrong model id");
                 return null;
             }
             $model = $api::retrieve_resource($model, $api::ONLY_MODEL);
         }
     }
     if (property_exists($model, "object") && $model->object instanceof STDClass) {
         $model = $model->object;
     }
     if (property_exists($model, "model") && $model->model instanceof STDClass) {
         if ($model->status->code == BigMLRequest::FINISHED) {
             if (property_exists($model->model, "model_fields")) {
                 foreach ($model->model->model_fields as $key => $value) {
                     if (!property_exists($model->model->fields, $key)) {
                         throw new Exception("Some fields are missing to generate a local model " . $key . "  Please, provide a model with the complete list of fields.");
                     }
                     if (property_exists($model->model->fields->{$key}, "summary")) {
                         $model->model->model_fields->{$key}->summary = $model->model->fields->{$key}->summary;
                     }
                     $model->model->model_fields->{$key}->name = $model->model->fields->{$key}->name;
                 }
             }
             parent::__construct($model->model->model_fields, extract_objective($model->objective_fields));
             $this->description = $model->description;
             $this->field_importance = property_exists($model->model, "importance") ? $model->model->importance : null;
             if ($this->field_importance != null) {
                 $fields_importance = array();
                 foreach ($this->field_importance as $field => $value) {
                     if (property_exists($model->model->model_fields, $value[0])) {
                         array_push($fields_importance, $value);
                     }
                 }
                 $this->field_importance = $fields_importance;
             }
             if (property_exists($model, "locale" && $model->locale != null)) {
                 $this->locale = $model->locale;
             }
         } else {
             throw new Exception("The model isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the BaseModel instance. Could not  find the 'model' key in the resource:\n\n " . print_r($model));
     }
 }