if ($trainresults->status == 0) {
$output .= $OUTPUT->notification(get_string('trainingprocessfinished', 'tool_analytics'),
\core\output\notification::NOTIFY_SUCCESS);
- } else if ($trainresults->status === \core_analytics\model::NO_DATASET) {
+ } else if ($trainresults->status === \core_analytics\model::NO_DATASET ||
+ $trainresults->status === \core_analytics\model::NOT_ENOUGH_DATA) {
$output .= $OUTPUT->notification(get_string('nodatatotrain', 'tool_analytics'),
\core\output\notification::NOTIFY_WARNING);
} else {
- $output .= $OUTPUT->notification(get_string('generalerror', 'analytics', $trainresults->status),
+ $output .= $OUTPUT->notification(get_string('generalerror', 'tool_analytics', $trainresults->status),
\core\output\notification::NOTIFY_ERROR);
}
}
if ($predictresults->status == 0) {
$output .= $OUTPUT->notification(get_string('predictionprocessfinished', 'tool_analytics'),
\core\output\notification::NOTIFY_SUCCESS);
- } else if ($predictresults->status === \core_analytics\model::NO_DATASET) {
+ } else if ($predictresults->status === \core_analytics\model::NO_DATASET ||
+ $predictresults->status === \core_analytics\model::NOT_ENOUGH_DATA) {
$output .= $OUTPUT->notification(get_string('nodatatopredict', 'tool_analytics'),
\core\output\notification::NOTIFY_WARNING);
} else {
- $output .= $OUTPUT->notification(get_string('generalerror', 'analytics', $predictresults->status),
+ $output .= $OUTPUT->notification(get_string('generalerror', 'tool_analytics', $predictresults->status),
\core\output\notification::NOTIFY_ERROR);
}
}
return get_string('errorprocessornotready', 'analytics', $isready);
}
+ $currentvalue = get_config('analytics', 'predictionsprocessor');
+ if (!empty($currentvalue) && $currentvalue != str_replace('\\\\', '\\', $data)) {
+ // Clear all models data.
+ $models = \core_analytics\manager::get_all_models();
+ foreach ($models as $model) {
+ $model->clear();
+ }
+ }
+
return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
}
}
}
/**
- * Should the model callback be triggered?
+ * This method determines if a prediction is interesing for the model or not.
*
* @param mixed $predictedvalue
* @param float $predictionscore
}
/**
- * Should the model callback be triggered?
+ * This method determines if a prediction is interesing for the model or not.
+ *
+ * This method internally calls ignored_predicted_classes to skip classes
+ * flagged by the target as not important for users.
*
* @param mixed $predictedvalue
* @param float $predictionscore
}
/**
- * Should the model callback be triggered?
+ * This method determines if a prediction is interesing for the model or not.
*
* @param mixed $predictedvalue
* @param float $predictionscore
/**
* Model with low prediction accuracy.
*/
- const EVALUATE_LOW_SCORE = 4;
+ const LOW_SCORE = 4;
/**
* Not enough data to evaluate the model properly.
*/
- const EVALUATE_NOT_ENOUGH_DATA = 8;
+ const NOT_ENOUGH_DATA = 8;
/**
* Invalid analysable for the time splitting method.
$this->model->indicators !== $indicatorsstr) {
// Delete generated predictions before changing the model version.
- $this->clear_model();
+ $this->clear();
// It needs to be reset as the version changes.
$this->uniqueid = null;
\core_analytics\manager::check_can_manage_models();
- $this->clear_model();
+ $this->clear();
- // Method self::clear_model is already clearing the current model version.
+ // Method self::clear is already clearing the current model version.
$predictor = \core_analytics\manager::get_predictions_processor();
$predictor->delete_output_dir($this->get_output_dir(array(), true));
$result->status = $predictorresult->status;
$result->info = $predictorresult->info;
+ if ($result->status !== self::OK) {
+ return $result;
+ }
+
$this->flag_file_as_used($samplesfile, 'trained');
// Mark the model as trained if it wasn't.
$result->predictions = $this->format_predictor_predictions($predictorresult);
}
+ if ($result->status !== self::OK) {
+ return $result;
+ }
+
if ($result->predictions) {
$samplecontexts = $this->execute_prediction_callbacks($result->predictions, $indicatorcalculations);
}
// Here we will store all predictions' contexts, this will be used to limit which users will see those predictions.
$samplecontexts = array();
+ $records = array();
foreach ($predictions as $uniquesampleid => $prediction) {
- if ($this->get_target()->triggers_callback($prediction->prediction, $prediction->predictionscore)) {
+ // The unique sample id contains both the sampleid and the rangeindex.
+ list($sampleid, $rangeindex) = $this->get_time_splitting()->infer_sample_info($uniquesampleid);
- // The unique sample id contains both the sampleid and the rangeindex.
- list($sampleid, $rangeindex) = $this->get_time_splitting()->infer_sample_info($uniquesampleid);
+ if ($this->get_target()->triggers_callback($prediction->prediction, $prediction->predictionscore)) {
- // Store the predicted values.
+ // Prepare the record to store the predicted values.
list($record, $samplecontext) = $this->prepare_prediction_record($sampleid, $rangeindex, $prediction->prediction,
$prediction->predictionscore, json_encode($indicatorcalculations[$uniquesampleid]));
}
// Delete generated predictions before changing the model version.
- $this->clear_model();
+ $this->clear();
// It needs to be reset as the version changes.
$this->uniqueid = null;
$outputdir = rtrim($CFG->dataroot, '/') . DIRECTORY_SEPARATOR . 'models';
}
- // Append model id
+ // Append model id.
$outputdir .= DIRECTORY_SEPARATOR . $this->model->id;
if (!$onlymodelid) {
// Append version + subdirs.
*
* @return void
*/
- private function clear_model() {
+ public function clear() {
global $DB;
+ \core_analytics\manager::check_can_manage_models();
+
// Delete current model version stored stuff.
$predictor = \core_analytics\manager::get_predictions_processor();
$predictor->clear_model($this->get_unique_id(), $this->get_output_dir());
$modelversionoutputdir = $this->model->get_output_dir();
$this->assertTrue(is_dir($modelversionoutputdir));
- // Update to an empty time splitting method to force clear_model execution.
- $this->model->update(1, false, '');
+ // Update to an empty time splitting method to force model::clear execution.
+ $this->model->clear();
$this->assertFalse(is_dir($modelversionoutputdir));
- // Restore previous time splitting method.
- $this->model->enable('\core\analytics\time_splitting\no_splitting');
-
// Check that most of the stuff got deleted.
$this->assertEquals(1, $DB->count_records('analytics_models', array('id' => $this->modelobj->id)));
$this->assertEquals(1, $DB->count_records('analytics_models_log', array('modelid' => $this->modelobj->id)));
return $this->add_prediction_processors($cases);
}
+ /**
+ * Test the system classifiers returns.
+ *
+ * This test checks that all mlbackend plugins in the system are able to return proper status codes
+ * even under weird situations.
+ *
+ * @dataProvider provider_ml_classifiers_return
+ * @param int $success
+ * @param int $nsamples
+ * @param int $classes
+ * @param string $predictionsprocessorclass
+ * @return void
+ */
+ public function test_ml_classifiers_return($success, $nsamples, $classes, $predictionsprocessorclass) {
+ $this->resetAfterTest();
+
+ $predictionsprocessor = \core_analytics\manager::get_predictions_processor($predictionsprocessorclass, false);
+ if ($predictionsprocessor->is_ready() !== true) {
+ $this->markTestSkipped('Skipping ' . $predictionsprocessorclass . ' as the predictor is not ready.');
+ }
+
+ if ($nsamples % count($classes) != 0) {
+ throw new \coding_exception('The number of samples should be divisible by the number of classes');
+ }
+ $samplesperclass = $nsamples / count($classes);
+
+ // Metadata (we pass 2 classes even if $classes only provides 1 class samples as we want to test
+ // what the backend does in this case.
+ $dataset = "nfeatures,targetclasses,targettype" . PHP_EOL;
+ $dataset .= "3,\"[0,1]\",\"discrete\"" . PHP_EOL;
+
+ // Headers.
+ $dataset .= "feature1,feature2,feature3,target" . PHP_EOL;
+ foreach ($classes as $class) {
+ for ($i = 0; $i < $samplesperclass; $i++) {
+ $dataset .= "1,0,1,$class" . PHP_EOL;
+ }
+ }
+
+ $trainingfile = array(
+ 'contextid' => \context_system::instance()->id,
+ 'component' => 'analytics',
+ 'filearea' => 'labelled',
+ 'itemid' => 123,
+ 'filepath' => '/',
+ 'filename' => 'whocares.csv'
+ );
+ $fs = get_file_storage();
+ $dataset = $fs->create_file_from_string($trainingfile, $dataset);
+
+ // Training should work correctly if at least 1 sample of each class is included.
+ $dir = make_request_directory();
+ $result = $predictionsprocessor->train_classification('whatever', $dataset, $dir);
+
+ switch ($success) {
+ case 'yes':
+ $this->assertEquals(\core_analytics\model::OK, $result->status);
+ break;
+ case 'no':
+ $this->assertNotEquals(\core_analytics\model::OK, $result->status);
+ break;
+ case 'maybe':
+ default:
+ // We just check that an object is returned so we don't have an empty check,
+ // what we really want to check is that an exception was not thrown.
+ $this->assertInstanceOf(\stdClass::class, $result);
+ }
+ }
+
+ /**
+ * test_ml_classifiers_return provider
+ *
+ * We can not be very specific here as test_ml_classifiers_return only checks that
+ * mlbackend plugins behave and expected and control properly backend errors even
+ * under weird situations.
+ *
+ * @return array
+ */
+ public function provider_ml_classifiers_return() {
+ // Using verbose options as the first argument for readability.
+ $cases = array(
+ '1-samples' => array('maybe', 1, [0]),
+ '2-samples-same-class' => array('maybe', 2, [0]),
+ '2-samples-different-classes' => array('yes', 2, [0, 1]),
+ '4-samples-different-classes' => array('yes', 4, [0, 1])
+ );
+
+ // We need to test all system prediction processors.
+ return $this->add_prediction_processors($cases);
+ }
/**
* Basic test to check that prediction processors work as expected.
'expectedresults' => array(
// The course duration is too much to be processed by in weekly basis.
'\core\analytics\time_splitting\weekly' => \core_analytics\model::NO_DATASET,
- '\core\analytics\time_splitting\single_range' => \core_analytics\model::EVALUATE_LOW_SCORE,
- '\core\analytics\time_splitting\quarters' => \core_analytics\model::EVALUATE_LOW_SCORE,
+ '\core\analytics\time_splitting\single_range' => \core_analytics\model::LOW_SCORE,
+ '\core\analytics\time_splitting\quarters' => \core_analytics\model::LOW_SCORE,
)
),
'good' => array(
$string['onlycli'] = 'Analytics processes execution via command line only';
$string['onlycliinfo'] = 'Analytics processes like evaluating models, training machine learning algorithms or getting predictions can take some time, they will run as cron tasks and they can be forced via command line. Disable this setting if you want your site managers to be able to run these processes manually via web interface';
$string['predictionsprocessor'] = 'Predictions processor';
-$string['predictionsprocessor_help'] = 'Prediction processors are the machine learning backends that process the datasets generated by calculating models\' indicators and targets.';
+$string['predictionsprocessor_help'] = 'A predictions processor is the machine-learning backend that processes the datasets generated by calculating models\' indicators and targets. All trained algorithms and predictions will be deleted if you change to another predictions processor.';
$string['processingsitecontents'] = 'Processing site contents';
$string['successfullyanalysed'] = 'Successfully analysed';
$string['timesplittingmethod'] = 'Time-splitting method';
$samples[] = array_slice($sampledata, 0, $metadata['nfeatures']);
$targets[] = intval($data[$metadata['nfeatures']]);
- if (count($samples) === self::BATCH_SIZE) {
+ $nsamples = count($samples);
+ if ($nsamples === self::BATCH_SIZE) {
// Training it batches to avoid running out of memory.
$classifier->partialTrain($samples, $targets, array(0, 1));
$samples = array();
$targets = array();
}
+ if (empty($morethan1sample) && $nsamples > 1) {
+ $morethan1sample = true;
+ }
}
fclose($fh);
+ if (empty($morethan1sample)) {
+ $resultobj = new \stdClass();
+ $resultobj->status = \core_analytics\model::NO_DATASET;
+ $resultobj->info = array();
+ return $resultobj;
+ }
+
// Train the remaining samples.
if ($samples) {
$classifier->partialTrain($samples, $targets, array(0, 1));
}
if (!empty($notenoughdata)) {
$resultobj = new \stdClass();
- $resultobj->status = \core_analytics\model::EVALUATE_NOT_ENOUGH_DATA;
+ $resultobj->status = \core_analytics\model::NOT_ENOUGH_DATA;
$resultobj->score = 0;
$resultobj->info = array(get_string('errornotenoughdata', 'mlbackend_php'));
return $resultobj;
// If each iteration results varied too much we need more data to confirm that this is a valid model.
if ($modeldev > $maxdeviation) {
- $resultobj->status = $resultobj->status + \core_analytics\model::EVALUATE_NOT_ENOUGH_DATA;
+ $resultobj->status = $resultobj->status + \core_analytics\model::NOT_ENOUGH_DATA;
$a = new \stdClass();
$a->deviation = $modeldev;
$a->accepteddeviation = $maxdeviation;
}
if ($resultobj->score < \core_analytics\model::MIN_SCORE) {
- $resultobj->status = $resultobj->status + \core_analytics\model::EVALUATE_LOW_SCORE;
+ $resultobj->status = $resultobj->status + \core_analytics\model::LOW_SCORE;
$a = new \stdClass();
$a->score = $resultobj->score;
$a->minscore = \core_analytics\model::MIN_SCORE;
/**
* The required version of the python package that performs all calculations.
*/
- const REQUIRED_PIP_PACKAGE_VERSION = '0.0.2';
+ const REQUIRED_PIP_PACKAGE_VERSION = '0.0.3';
/**
* The path to the Python bin.
}
if ($exitcode != 0) {
- throw new \moodle_exception('errorpredictionsprocessor', 'analytics', '', implode(', ', $resultobj->errors));
+ $errors = $resultobj->errors;
+ if (is_array($errors)) {
+ $errors = implode(', ', $errors);
+ }
+ $resultobj->info = array(get_string('errorpredictionsprocessor', 'analytics', $errors));
}
return $resultobj;
}
if ($exitcode != 0) {
- throw new \moodle_exception('errorpredictionsprocessor', 'analytics', '', implode(', ', $resultobj->errors));
+ $errors = $resultobj->errors;
+ if (is_array($errors)) {
+ $errors = implode(', ', $errors);
+ }
+ $resultobj->info = array(get_string('errorpredictionsprocessor', 'analytics', $errors));
}
return $resultobj;