예제 #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");
         }
     }
 }
예제 #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));
     }
 }
예제 #3
0
 public function __construct($logistic_regression, $api = null, $storage = "storage")
 {
     $this->input_fields = array();
     $this->term_forms = array();
     $this->tag_clouds = array();
     $this->term_analysis = array();
     $this->items = array();
     $this->item_analysis = array();
     $this->categories = array();
     $this->data_field_types = array();
     $this->numeric_fields = array();
     $old_coefficients = false;
     if ($api == null) {
         $api = new BigML(null, null, null, $storage);
     }
     if (is_string($logistic_regression)) {
         if (file_exists($logistic_regression)) {
             $logistic_regression = json_decode(file_get_contents($logistic_regression));
         } else {
             if (!$api::_checkModelId($logistic_regression)) {
                 error_log("Wrong logistic regression id");
                 return null;
             } else {
                 $logistic_regression = $api::retrieve_resource($logistic_regression, $api::ONLY_MODEL);
             }
         }
     }
     if ($logistic_regression == null || !property_exists($logistic_regression, 'resource')) {
         error_log("Cannot create the Model instance. Could not find the 'logistic_regression' key in the resource");
         throw new Exception('Cannot create the logistic regression instance. Could not find the logistic regression key in the resource');
     }
     if (property_exists($logistic_regression, "object") && property_exists($logistic_regression->object, "status") && $logistic_regression->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The logistic_regression isn't finished yet");
     }
     if (property_exists($logistic_regression, "object") && $logistic_regression->object instanceof STDClass) {
         $logistic_regression = $logistic_regression->object;
     }
     if (property_exists($logistic_regression, "logistic_regression") && $logistic_regression->logistic_regression instanceof STDClass) {
         if ($logistic_regression->status->code == BigMLRequest::FINISHED) {
             $this->input_fields = property_exists($logistic_regression, "input_fields") ? $logistic_regression->input_fields : array();
             $this->dataset_field_types = property_exists($logistic_regression, "dataset_field_types") ? $logistic_regression->dataset_field_types : array();
             $objective_field = $logistic_regression->objective_fields;
             $logistic_regression_info = $logistic_regression->logistic_regression;
             $fields = property_exists($logistic_regression_info, "fields") ? $logistic_regression_info->fields : array();
             if (is_null($this->input_fields) or empty($this->input_fields)) {
                 $this->input_fields = array();
                 $fields_sorted_by_column_number = array();
                 foreach ($fields as $field_id => $field) {
                     $a[$field_id] = $field->column_number;
                 }
                 asort($fields_sorted_by_column_number);
                 foreach ($fields_sorted_by_column_number as $key => $value) {
                     array_push($this->input_fields, $key);
                 }
             }
             $this->coefficients = array();
             if (property_exists($logistic_regression_info, "coefficients")) {
                 $j = 0;
                 foreach ($logistic_regression_info->coefficients as $key => $coefficient) {
                     $this->coefficients[$coefficient[0]] = $coefficient[1];
                     if ($j == 0 and !is_array($coefficient[1])) {
                         $old_coefficients = true;
                     }
                     $j += 1;
                 }
             }
             $this->bias = property_exists($logistic_regression_info, "bias") ? $logistic_regression_info->bias : 0;
             $this->c = property_exists($logistic_regression_info, "c") ? $logistic_regression_info->c : null;
             $this->eps = property_exists($logistic_regression_info, "eps") ? $logistic_regression_info->eps : null;
             $this->lr_normalize = property_exists($logistic_regression_info, "normalize") ? $logistic_regression_info->normalize : null;
             $this->balance_fields = property_exists($logistic_regression_info, "balance_fields") ? $logistic_regression_info->balance_fields : null;
             $this->regularization = property_exists($logistic_regression_info, "regularization") ? $logistic_regression_info->regularization : null;
             $this->field_codings = property_exists($logistic_regression_info, "field_codings") ? $logistic_regression_info->field_codings : array();
             $this->missing_numerics = property_exists($logistic_regression_info, "missing_numerics") ? $logistic_regression_info->missing_numerics : false;
             $objective_id = extract_objective($objective_field);
             foreach ($fields as $field_id => $field) {
                 if ($field->optype == 'text') {
                     $this->term_forms[$field_id] = $field->summary->term_forms;
                     $this->tag_clouds[$field_id] = array();
                     # TODO revisar
                     foreach ($field->summary->tag_cloud as $tag => $value) {
                         array_push($this->tag_clouds[$field_id], $value[0]);
                     }
                     $this->term_analysis[$field_id] = $field->term_analysis;
                 } else {
                     if ($field->optype == 'items') {
                         $this->items[$field_id] = array();
                         foreach ($field->summary->items as $item => $value) {
                             array_push($this->items[$field_id], $value[0]);
                         }
                         $this->item_analysis[$field_id] = $field->item_analysis;
                     } else {
                         if ($field->optype == 'categorical') {
                             $this->categories[$field_id] = array();
                             foreach ($field->summary->categories as $key => $value) {
                                 array_push($this->categories[$field_id], $value[0]);
                             }
                         }
                     }
                 }
                 if ($this->missing_numerics && $field->optype == "numeric") {
                     $this->numeric_fields[$field_id] = true;
                 }
             }
             parent::__construct($fields, $objective_id);
             $this->field_codings = property_exists($logistic_regression_info, "field_codings") ? $logistic_regression_info->field_codings : array();
             $this->format_field_codings();
             foreach ($this->field_codings as $field_id => $field_coding) {
                 if (array_key_exists($field_id, $fields) && array_key_exists($field_id, $this->inverted_fields)) {
                     $this->field_codings[$this->inverted_fields[$field_id]] = $this->field_codings[$field_id];
                     unset($this->field_codings[$field_id]);
                 }
             }
             if ($old_coefficients) {
                 $this->map_coefficients();
             }
         } else {
             throw new Exception("The logistic regression isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the Model instance. Could not find the 'logistic regression' key in the resource:\n\n" . $logistic_regression);
     }
 }
예제 #4
0
 public function __construct($cluster, $api = null, $storage = "storage")
 {
     if ($api == null) {
         $api = new BigML(null, null, null, $storage);
     }
     if (is_string($cluster)) {
         if (!$api::_checkClusterId($cluster)) {
             error_log("Wrong cluster id");
             return null;
         }
         $cluster = $api::retrieve_resource($cluster, $api::ONLY_MODEL);
     }
     if (property_exists($cluster, "object") && property_exists($cluster->object, "status") && $cluster->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The cluster isn't finished yet");
     }
     if (property_exists($cluster, "object") && $cluster->object instanceof STDClass) {
         $cluster = $cluster->object;
     }
     if (property_exists($cluster, "clusters") && $cluster->clusters instanceof STDClass) {
         if ($cluster->status->code == BigMLRequest::FINISHED) {
             $clusters = $cluster->clusters->clusters;
             $this->centroids = array();
             foreach ($clusters as $centroid) {
                 array_push($this->centroids, new Centroid($centroid));
             }
             $this->scales = $cluster->scales;
             $this->term_forms = array();
             $this->tag_clouds = array();
             $this->term_analysis = array();
             $fields = $cluster->clusters->fields;
             $summary_fields = $cluster->summary_fields;
             foreach ($summary_fields as $field_id) {
                 unset($fields->{$field_id});
             }
             foreach ($fields as $field_id => $field) {
                 if ($field->optype == 'text') {
                     $this->term_forms[$field_id] = $field->summary->term_forms;
                     $this->tag_clouds[$field_id] = $field->summary->tag_cloud;
                     $this->term_analysis[$field_id] = $field->term_analysis;
                 }
             }
             parent::__construct($fields);
             foreach ($this->scales as $field_id => $field) {
                 if (!property_exists($this->fields, $field_id)) {
                     throw new Exception("Some fields are missing  to generate a local cluster. Please, provide a cluster with the complete list of fields.");
                 }
             }
         } else {
             throw new Exception("The cluster isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the Cluster instance. Could not  find the 'clusters' key in the resource:\n\n " . $cluster);
     }
 }
예제 #5
0
 public function __construct($association, $api = null)
 {
     $this->discretization = array();
     $this->field_discretizations = array();
     $this->items = array();
     $this->search_strategy = Association::DEFAULT_SEARCH_STRATEGY;
     $this->rules = array();
     #$SEARCH_STRATEGY_CODES = array("leverage"=> 0, "confidence"=> 1, "support"=> 2, "coverage" => 3, "lift" => 4);
     if (is_string($association)) {
         if (file_exists($association)) {
             $association = json_decode(file_get_contents($association));
         } else {
             if (!$api::_checkAssociationId($association)) {
                 error_log("Wrong association id");
                 return null;
             } else {
                 $association = $api::retrieve_resource($association, $api::ONLY_MODEL);
             }
         }
     }
     if ($association == null || !property_exists($association, 'resource')) {
         error_log("Cannot create the Association instance. Could not find the 'association' key in the resource");
         throw new Exception('Cannot create the association instance. Could not find the association key in the resource');
     }
     if (property_exists($association, "object") && property_exists($association->object, "status") && $association->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The association isn't finished yet");
     }
     if (property_exists($association, "object") && $association->object instanceof STDClass) {
         $association = $association->object;
         if (property_exists($association, "associations") && $association->associations instanceof STDClass) {
             if (property_exists($association, "status") && $association->status->code == BigMLRequest::FINISHED) {
                 $associations = $association->associations;
                 $fields = $associations->fields;
                 parent::__construct($fields);
                 $this->complement = property_exists($associations, "complement") ? $associations->complement : false;
                 $this->discretization = property_exists($associations, "discretization") ? $associations->discretization : array();
                 $this->field_discretizations = property_exists($associations, "field_discretizations") ? $associations->field_discretizations : array();
                 $items = property_exists($associations, "items") ? $associations->items : array();
                 foreach ($items as $index => $item) {
                     array_push($this->items, new Item($index, $item, $fields));
                 }
                 $this->k = property_exists($associations, "k") ? $associations->k : 100;
                 $this->max_lhs = property_exists($associations, "max_lhs") ? $associations->max_lhs : 4;
                 $this->min_coverage = property_exists($associations, "min_coverage") ? $associations->min_coverage : 0;
                 $this->min_leverage = property_exists($associations, "min_leverage") ? $associations->min_leverage : -1;
                 $this->min_strength = property_exists($associations, "min_strength") ? $associations->min_strength : 0;
                 $this->min_support = property_exists($associations, "min_support") ? $associations->min_support : 0;
                 $this->min_lift = property_exists($associations, "min_lift") ? $associations->min_lift : 0;
                 $this->prune = property_exists($associations, "prune") ? $associations->prune : true;
                 #$this->search_strategy = $SEARCH_STRATEGY_CODES[property_exists($associations, "search_strategy") ? $associations->search_strategy : Association::DEFAULT_SEARCH_STRATEGY];
                 $this->search_strategy = property_exists($associations, "search_strategy") ? $associations->search_strategy : $DEFAULT_SEARCH_STRATEGY;
                 $rules = property_exists($associations, "rules") ? $associations->rules : array();
                 foreach ($rules as $rule) {
                     array_push($this->rules, new AssociationRule($rule));
                 }
                 $this->significance_level = property_exists($associations, "significance_level") ? $associations->significance_level : 0.05;
             } else {
                 throw new Exception("The association isn't finished yet");
             }
         } else {
             throw new Exception("Cannot create the Association instance. Could not find the 'association' key in the resource:\n\n" . json_encode($association));
         }
     } else {
         throw new Exception("Cannot create the Association instance. Could not find the 'association' key in the resource:\n\n" . json_encode($association));
     }
 }
예제 #6
0
 public function __construct($cluster, $api = null, $storage = "storage")
 {
     if ($api == null) {
         $api = new BigML(null, null, null, $storage);
     }
     if (is_string($cluster)) {
         if (!$api::_checkClusterId($cluster)) {
             error_log("Wrong cluster id");
             return null;
         }
         $cluster = $api::retrieve_resource($cluster, $api::ONLY_MODEL);
     }
     if (property_exists($cluster, "object") && property_exists($cluster->object, "status") && $cluster->object->status->code != BigMLRequest::FINISHED) {
         throw new Exception("The cluster isn't finished yet");
     }
     if (property_exists($cluster, "object") && $cluster->object instanceof STDClass) {
         $cluster = $cluster->object;
     }
     if (property_exists($cluster, "clusters") && $cluster->clusters instanceof STDClass) {
         if ($cluster->status->code == BigMLRequest::FINISHED) {
             $the_clusters = $cluster->clusters;
             $cluster_global = array_key_exists("global", $the_clusters) ? $the_clusters->global : null;
             $clusters = $the_clusters->clusters;
             $this->centroids = array();
             foreach ($clusters as $centroid) {
                 array_push($this->centroids, new Centroid($centroid));
             }
             $this->cluster_global = $cluster_global;
             if (!is_null($cluster_global)) {
                 $this->cluster_global = new Centroid($cluster_global);
                 $this->cluster_global->name = GLOBAL_CLUSTER_LABEL;
                 $this->cluster_global->count = $this->cluster_global->distance->population;
             }
             $this->total_ss = $the_clusters->total_ss;
             $this->within_ss = $the_clusters->within_ss;
             if (!is_null($this->within_ss)) {
                 $this->within_ss = 0;
                 foreach ($this->centroids as $centroid) {
                     $this->within_ss += $centroid->distance->sum_squares;
                 }
             }
             $this->between_ss = $the_clusters->between_ss;
             $this->ratio_ss = $the_clusters->ratio_ss;
             $this->critical_value = array_key_exists("critical_value", $cluster) ? $cluster->critical_value : null;
             $this->k = $cluster->k;
             $this->scales = $cluster->scales;
             $this->term_forms = array();
             $this->tag_clouds = array();
             $this->term_analysis = array();
             $this->item_analysis = array();
             $this->items = array();
             $fields = $cluster->clusters->fields;
             $summary_fields = $cluster->summary_fields;
             foreach ($summary_fields as $field_id) {
                 unset($fields->{$field_id});
             }
             foreach ($fields as $field_id => $field) {
                 if ($field->optype == 'text') {
                     $this->term_forms[$field_id] = $field->summary->term_forms;
                     $this->tag_clouds[$field_id] = $field->summary->tag_cloud;
                     $this->term_analysis[$field_id] = $field->term_analysis;
                 } else {
                     if ($field->optype == 'items') {
                         $this->items[$field_id] = $field->summary->items;
                         $this->item_analysis[$field_id] = $field->item_analysis;
                     }
                 }
             }
             parent::__construct($fields);
             foreach ($this->scales as $field_id => $field) {
                 if (!property_exists($this->fields, $field_id)) {
                     throw new Exception("Some fields are missing  to generate a local cluster. Please, provide a cluster with the complete list of fields.");
                 }
             }
         } else {
             throw new Exception("The cluster isn't finished yet");
         }
     } else {
         throw new Exception("Cannot create the Cluster instance. Could not  find the 'clusters' key in the resource:\n\n " . $cluster);
     }
 }