MDL-60944 analytics: Include trained ML models

This commit is contained in:
David Monllaó
2019-02-01 06:52:42 +01:00
parent e4453adc55
commit c70a7194f4
14 changed files with 513 additions and 232 deletions

View File

@@ -33,7 +33,7 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processor implements \core_analytics\classifier, \core_analytics\regressor {
class processor implements \core_analytics\classifier, \core_analytics\regressor, \core_analytics\packable {
/**
* The required version of the python package that performs all calculations.
@@ -263,6 +263,78 @@ class processor implements \core_analytics\classifier, \core_analytics\regresso
return $resultobj;
}
/**
* Exports the machine learning model.
*
* @throws \moodle_exception
* @param string $uniqueid The model unique id
* @param string $modeldir The directory that contains the trained model.
* @return string The path to the directory that contains the exported model.
*/
public function export(string $uniqueid, string $modeldir) : string {
// We include an exporttmpdir as we want to be sure that the file is not deleted after the
// python process finishes.
$exporttmpdir = make_request_directory('mlbackend_python_export');
$cmd = "{$this->pathtopython} -m moodlemlbackend.export " .
escapeshellarg($uniqueid) . ' ' .
escapeshellarg($modeldir) . ' ' .
escapeshellarg($exporttmpdir);
if (!PHPUNIT_TEST && CLI_SCRIPT) {
debugging($cmd, DEBUG_DEVELOPER);
}
$output = null;
$exitcode = null;
$exportdir = exec($cmd, $output, $exitcode);
if ($exitcode != 0) {
throw new \moodle_exception('errorexportmodelresult', 'analytics');
}
if (!$exportdir) {
throw new \moodle_exception('errorexportmodelresult', 'analytics');
}
return $exportdir;
}
/**
* Imports the provided machine learning model.
*
* @param string $uniqueid The model unique id
* @param string $modeldir The directory that will contain the trained model.
* @param string $importdir The directory that contains the files to import.
* @return bool Success
*/
public function import(string $uniqueid, string $modeldir, string $importdir) : bool {
$cmd = "{$this->pathtopython} -m moodlemlbackend.import " .
escapeshellarg($uniqueid) . ' ' .
escapeshellarg($modeldir) . ' ' .
escapeshellarg($importdir);
if (!PHPUNIT_TEST && CLI_SCRIPT) {
debugging($cmd, DEBUG_DEVELOPER);
}
$output = null;
$exitcode = null;
$success = exec($cmd, $output, $exitcode);
if ($exitcode != 0) {
throw new \moodle_exception('errorimportmodelresult', 'analytics');
}
if (!$success) {
throw new \moodle_exception('errorimportmodelresult', 'analytics');
}
return $success;
}
/**
* Train this processor regression model using the provided supervised learning dataset.
*