first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for mlbackend_python.
*
* @package mlbackend_python
* @copyright 2018 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mlbackend_python\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for mlbackend_python implementing null_provider.
*
* @copyright 2018 David Monllao
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+747
View File
@@ -0,0 +1,747 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Python predictions processor
*
* @package mlbackend_python
* @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mlbackend_python;
defined('MOODLE_INTERNAL') || die();
/**
* Python predictions processor.
*
* @package mlbackend_python
* @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, \core_analytics\packable {
/**
* The required version of the python package that performs all calculations.
*/
const REQUIRED_PIP_PACKAGE_VERSION = '3.0.5';
/**
* The python package is installed in a server.
* @var bool
*/
protected $useserver;
/**
* The path to the Python bin.
*
* @var string
*/
protected $pathtopython;
/**
* Remote server host
* @var string
*/
protected $host;
/**
* Remote server port
* @var int
*/
protected $port;
/**
* Whether to use http or https.
* @var bool
*/
protected $secure;
/**
* Server username.
* @var string
*/
protected $username;
/**
* Server password for $this->username.
* @var string
*/
protected $password;
/**
* The constructor.
*
*/
public function __construct() {
global $CFG;
$config = get_config('mlbackend_python');
$this->useserver = !empty($config->useserver);
if (!$this->useserver) {
// Set the python location if there is a value.
if (!empty($CFG->pathtopython)) {
$this->pathtopython = $CFG->pathtopython;
}
} else {
$this->host = $config->host ?? '';
$this->port = $config->port ?? '';
$this->secure = $config->secure ?? false;
$this->username = $config->username ?? '';
$this->password = $config->password ?? '';
}
}
/**
* Is the plugin ready to be used?.
*
* @return bool|string Returns true on success, a string detailing the error otherwise
*/
public function is_ready() {
if (!$this->useserver) {
return $this->is_webserver_ready();
} else {
return $this->is_python_server_ready();
}
}
/**
* Checks if the python package is available in the web server executing this script.
*
* @return bool|string Returns true on success, a string detailing the error otherwise
*/
protected function is_webserver_ready() {
if (empty($this->pathtopython)) {
$settingurl = new \moodle_url('/admin/settings.php', array('section' => 'systempaths'));
return get_string('pythonpathnotdefined', 'mlbackend_python', $settingurl->out());
}
// Check the installed pip package version.
$cmd = "{$this->pathtopython} -m moodlemlbackend.version";
$output = null;
$exitcode = null;
// Execute it sending the standard error to $output.
$result = exec($cmd . ' 2>&1', $output, $exitcode);
if ($exitcode != 0) {
return get_string('pythonpackagenotinstalled', 'mlbackend_python', $cmd);
}
$vercheck = self::check_pip_package_version($result);
return $this->version_check_return($result, $vercheck);
}
/**
* Checks if the server can be accessed.
*
* @return bool|string True or an error string.
*/
protected function is_python_server_ready() {
if (empty($this->host) || empty($this->port) || empty($this->username) || empty($this->password)) {
return get_string('errornoconfigdata', 'mlbackend_python');
}
// Connection is allowed to use 'localhost' and other potentially blocked hosts/ports.
$curl = new \curl(['ignoresecurity' => true]);
$responsebody = $curl->get($this->get_server_url('version')->out(false));
if ($curl->info['http_code'] !== 200) {
return get_string('errorserver', 'mlbackend_python', $this->server_error_str($curl->info['http_code'], $responsebody));
}
$vercheck = self::check_pip_package_version($responsebody);
return $this->version_check_return($responsebody, $vercheck);
}
/**
* Delete the model version output directory.
*
* @throws \moodle_exception
* @param string $uniqueid
* @param string $modelversionoutputdir
* @return null
*/
public function clear_model($uniqueid, $modelversionoutputdir) {
if (!$this->useserver) {
remove_dir($modelversionoutputdir);
} else {
// Use the server.
$url = $this->get_server_url('deletemodel');
list($responsebody, $httpcode) = $this->server_request($url, 'post', ['uniqueid' => $uniqueid]);
}
}
/**
* Delete the model output directory.
*
* @throws \moodle_exception
* @param string $modeloutputdir
* @param string $uniqueid
* @return null
*/
public function delete_output_dir($modeloutputdir, $uniqueid) {
if (!$this->useserver) {
remove_dir($modeloutputdir);
} else {
$url = $this->get_server_url('deletemodel');
list($responsebody, $httpcode) = $this->server_request($url, 'post', ['uniqueid' => $uniqueid]);
}
}
/**
* Trains a machine learning algorithm with the provided dataset.
*
* @param string $uniqueid
* @param \stored_file $dataset
* @param string $outputdir
* @return \stdClass
*/
public function train_classification($uniqueid, \stored_file $dataset, $outputdir) {
if (!$this->useserver) {
// Use the local file system.
list($result, $exitcode) = $this->exec_command('training', [$uniqueid, $outputdir,
$this->get_file_path($dataset)], 'errornopredictresults');
} else {
// Use the server.
$requestparams = ['uniqueid' => $uniqueid, 'dirhash' => $this->hash_dir($outputdir),
'dataset' => $dataset];
$url = $this->get_server_url('training');
list($result, $httpcode) = $this->server_request($url, 'post', $requestparams);
}
if (!$resultobj = json_decode($result)) {
throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
}
if ($resultobj->status != 0) {
$resultobj = $this->format_error_info($resultobj);
}
return $resultobj;
}
/**
* Classifies the provided dataset samples.
*
* @param string $uniqueid
* @param \stored_file $dataset
* @param string $outputdir
* @return \stdClass
*/
public function classify($uniqueid, \stored_file $dataset, $outputdir) {
if (!$this->useserver) {
// Use the local file system.
list($result, $exitcode) = $this->exec_command('prediction', [$uniqueid, $outputdir,
$this->get_file_path($dataset)], 'errornopredictresults');
} else {
// Use the server.
$requestparams = ['uniqueid' => $uniqueid, 'dirhash' => $this->hash_dir($outputdir),
'dataset' => $dataset];
$url = $this->get_server_url('prediction');
list($result, $httpcode) = $this->server_request($url, 'post', $requestparams);
}
if (!$resultobj = json_decode($result)) {
throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
}
if ($resultobj->status != 0) {
$resultobj = $this->format_error_info($resultobj);
}
return $resultobj;
}
/**
* Evaluates this processor classification model using the provided supervised learning dataset.
*
* @param string $uniqueid
* @param float $maxdeviation
* @param int $niterations
* @param \stored_file $dataset
* @param string $outputdir
* @param string $trainedmodeldir
* @return \stdClass
*/
public function evaluate_classification($uniqueid, $maxdeviation, $niterations, \stored_file $dataset,
$outputdir, $trainedmodeldir) {
global $CFG;
if (!$this->useserver) {
// Use the local file system.
$datasetpath = $this->get_file_path($dataset);
$params = [$uniqueid, $outputdir, $datasetpath, \core_analytics\model::MIN_SCORE,
$maxdeviation, $niterations];
if ($trainedmodeldir) {
$params[] = $trainedmodeldir;
}
list($result, $exitcode) = $this->exec_command('evaluation', $params, 'errornopredictresults');
if (!$resultobj = json_decode($result)) {
throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
}
} else {
// Use the server.
$requestparams = ['uniqueid' => $uniqueid, 'minscore' => \core_analytics\model::MIN_SCORE,
'maxdeviation' => $maxdeviation, 'niterations' => $niterations,
'dirhash' => $this->hash_dir($outputdir), 'dataset' => $dataset];
if ($trainedmodeldir) {
$requestparams['trainedmodeldirhash'] = $this->hash_dir($trainedmodeldir);
}
$url = $this->get_server_url('evaluation');
list($result, $httpcode) = $this->server_request($url, 'post', $requestparams);
if (!$resultobj = json_decode($result)) {
throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
}
// We need an extra request to get the resources generated during the evaluation process.
// Directory to temporarly store the evaluation log zip returned by the server.
$evaluationtmpdir = make_request_directory();
$evaluationzippath = $evaluationtmpdir . DIRECTORY_SEPARATOR . 'evaluationlog.zip';
$requestparams = ['uniqueid' => $uniqueid, 'dirhash' => $this->hash_dir($outputdir),
'runid' => $resultobj->runid];
$url = $this->get_server_url('evaluationlog');
list($result, $httpcode) = $this->server_request($url, 'download_one', $requestparams,
['filepath' => $evaluationzippath]);
$rundir = $outputdir . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $resultobj->runid;
if (!mkdir($rundir, $CFG->directorypermissions, true)) {
throw new \moodle_exception('errorexportmodelresult', 'analytics');
}
$zip = new \zip_packer();
$success = $zip->extract_to_pathname($evaluationzippath, $rundir, null, null, true);
if (!$success) {
$a = 'The evaluation files can not be exported to ' . $rundir;
throw new \moodle_exception('errorpredictionsprocessor', 'analytics', '', $a);
}
$resultobj->dir = $rundir;
}
$resultobj = $this->add_extra_result_info($resultobj);
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 {
$exporttmpdir = make_request_directory();
if (!$this->useserver) {
// Use the local file system.
// We include an exporttmpdir as we want to be sure that the file is not deleted after the
// python process finishes.
list($exportdir, $exitcode) = $this->exec_command('export', [$uniqueid, $modeldir, $exporttmpdir],
'errorexportmodelresult');
if ($exitcode != 0) {
throw new \moodle_exception('errorexportmodelresult', 'analytics');
}
} else {
// Use the server.
$requestparams = ['uniqueid' => $uniqueid, 'dirhash' => $this->hash_dir($modeldir)];
$exportzippath = $exporttmpdir . DIRECTORY_SEPARATOR . 'export.zip';
$url = $this->get_server_url('export');
list($result, $httpcode) = $this->server_request($url, 'download_one', $requestparams,
['filepath' => $exportzippath]);
$exportdir = make_request_directory();
$zip = new \zip_packer();
$success = $zip->extract_to_pathname($exportzippath, $exportdir, null, null, true);
if (!$success) {
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 {
if (!$this->useserver) {
// Use the local file system.
list($result, $exitcode) = $this->exec_command('import', [$uniqueid, $modeldir, $importdir],
'errorimportmodelresult');
if ($exitcode != 0) {
throw new \moodle_exception('errorimportmodelresult', 'analytics');
}
} else {
// Use the server.
// Zip the $importdir to send a single file.
$importzipfile = $this->zip_dir($importdir);
if (!$importzipfile) {
// There was an error zipping the directory.
throw new \moodle_exception('errorimportmodelresult', 'analytics');
}
$requestparams = ['uniqueid' => $uniqueid, 'dirhash' => $this->hash_dir($modeldir),
'importzip' => curl_file_create($importzipfile, null, 'import.zip')];
$url = $this->get_server_url('import');
list($result, $httpcode) = $this->server_request($url, 'post', $requestparams);
}
return (bool)$result;
}
/**
* Train this processor regression model using the provided supervised learning dataset.
*
* @throws new \coding_exception
* @param string $uniqueid
* @param \stored_file $dataset
* @param string $outputdir
* @return \stdClass
*/
public function train_regression($uniqueid, \stored_file $dataset, $outputdir) {
throw new \coding_exception('This predictor does not support regression yet.');
}
/**
* Estimates linear values for the provided dataset samples.
*
* @throws new \coding_exception
* @param string $uniqueid
* @param \stored_file $dataset
* @param mixed $outputdir
* @return void
*/
public function estimate($uniqueid, \stored_file $dataset, $outputdir) {
throw new \coding_exception('This predictor does not support regression yet.');
}
/**
* Evaluates this processor regression model using the provided supervised learning dataset.
*
* @throws new \coding_exception
* @param string $uniqueid
* @param float $maxdeviation
* @param int $niterations
* @param \stored_file $dataset
* @param string $outputdir
* @param string $trainedmodeldir
* @return \stdClass
*/
public function evaluate_regression($uniqueid, $maxdeviation, $niterations, \stored_file $dataset,
$outputdir, $trainedmodeldir) {
throw new \coding_exception('This predictor does not support regression yet.');
}
/**
* Returns the path to the dataset file.
*
* @param \stored_file $file
* @return string
*/
protected function get_file_path(\stored_file $file) {
// From moodle filesystem to the local file system.
// This is not ideal, but there is no read access to moodle filesystem files.
return $file->copy_content_to_temp('core_analytics');
}
/**
* Check that the given package version can be used and return the error status.
*
* When evaluating the version, we assume the sematic versioning scheme as described at
* https://semver.org/.
*
* @param string $actual The actual Python package version
* @param string $required The required version of the package
* @return int -1 = actual version is too low, 1 = actual version too high, 0 = actual version is ok
*/
public static function check_pip_package_version($actual, $required = self::REQUIRED_PIP_PACKAGE_VERSION) {
if (empty($actual)) {
return -1;
}
if (version_compare($actual, $required, '<')) {
return -1;
}
$parts = explode('.', $required);
$requiredapiver = reset($parts);
$parts = explode('.', $actual);
$actualapiver = reset($parts);
if ($requiredapiver > 0 || $actualapiver > 1) {
if (version_compare($actual, $requiredapiver + 1, '>=')) {
return 1;
}
}
return 0;
}
/**
* Executes the specified module.
*
* @param string $modulename
* @param array $params
* @param string $errorlangstr
* @return array [0] is the result body and [1] the exit code.
*/
protected function exec_command(string $modulename, array $params, string $errorlangstr) {
$cmd = $this->pathtopython . ' -m moodlemlbackend.' . $modulename . ' ';
foreach ($params as $param) {
$cmd .= escapeshellarg($param) . ' ';
}
if (!PHPUNIT_TEST && CLI_SCRIPT) {
debugging($cmd, DEBUG_DEVELOPER);
}
$output = null;
$exitcode = null;
$result = exec($cmd, $output, $exitcode);
if (!$result) {
throw new \moodle_exception($errorlangstr, 'analytics');
}
return [$result, $exitcode];
}
/**
* Formats the errors and info in a single info string.
*
* @param \stdClass $resultobj
* @return \stdClass
*/
private function format_error_info(\stdClass $resultobj) {
if (!empty($resultobj->errors)) {
$errors = $resultobj->errors;
if (is_array($errors)) {
$errors = implode(', ', $errors);
}
} else if (!empty($resultobj->info)) {
// Show info if no errors are returned.
$errors = $resultobj->info;
if (is_array($errors)) {
$errors = implode(', ', $errors);
}
}
$resultobj->info = array(get_string('errorpredictionsprocessor', 'analytics', $errors));
return $resultobj;
}
/**
* Returns the url to the python ML server.
*
* @param string|null $path
* @return \moodle_url
*/
private function get_server_url(?string $path = null) {
$protocol = !empty($this->secure) ? 'https' : 'http';
$url = $protocol . '://' . rtrim($this->host, '/');
if (!empty($this->port)) {
$url .= ':' . $this->port;
}
if ($path) {
$url .= '/' . $path;
}
return new \moodle_url($url);
}
/**
* Sends a request to the python ML server.
*
* @param \moodle_url $url The requested url in the python ML server
* @param string $method The curl method to use
* @param array $requestparams Curl request params
* @param array|null $options Curl request options
* @return array [0] for the response body and [1] for the http code
*/
protected function server_request($url, string $method, array $requestparams, ?array $options = null) {
if ($method !== 'post' && $method !== 'get' && $method !== 'download_one') {
throw new \coding_exception('Incorrect request method provided. Only "get", "post" and "download_one"
actions are available.');
}
// Connection is allowed to use 'localhost' and other potentially blocked hosts/ports.
$curl = new \curl(['ignoresecurity' => true]);
$authorization = $this->username . ':' . $this->password;
$curl->setHeader('Authorization: Basic ' . base64_encode($authorization));
$responsebody = $curl->{$method}($url, $requestparams, $options);
if ($curl->info['http_code'] !== 200) {
throw new \moodle_exception('errorserver', 'mlbackend_python', '',
$this->server_error_str($curl->info['http_code'], $responsebody));
}
return [$responsebody, $curl->info['http_code']];
}
/**
* Adds extra information to results info.
*
* @param \stdClass $resultobj
* @return \stdClass
*/
protected function add_extra_result_info(\stdClass $resultobj): \stdClass {
if (!empty($resultobj->dir)) {
$dir = $resultobj->dir . DIRECTORY_SEPARATOR . 'tensor';
$resultobj->info[] = get_string('tensorboardinfo', 'mlbackend_python', $dir);
}
return $resultobj;
}
/**
* Returns the proper return value for the version checking.
*
* @param string $actual Actual moodlemlbackend version
* @param int $vercheck Version checking result
* @return true|string Returns true on success, a string detailing the error otherwise
*/
private function version_check_return($actual, $vercheck) {
if ($vercheck === 0) {
return true;
}
if ($actual) {
$a = [
'installed' => $actual,
'required' => self::REQUIRED_PIP_PACKAGE_VERSION,
];
if ($vercheck < 0) {
return get_string('packageinstalledshouldbe', 'mlbackend_python', $a);
} else if ($vercheck > 0) {
return get_string('packageinstalledtoohigh', 'mlbackend_python', $a);
}
}
if (!$this->useserver) {
$cmd = "{$this->pathtopython} -m moodlemlbackend.version";
} else {
// We can't not know which is the python bin in the python ML server, the most likely
// value is 'python'.
$cmd = "python -m moodlemlbackend.version";
}
return get_string('pythonpackagenotinstalled', 'mlbackend_python', $cmd);
}
/**
* Hashes the provided dir as a string.
*
* @param string $dir Directory path
* @return string Hash
*/
private function hash_dir(string $dir) {
return md5($dir);
}
/**
* Zips the provided directory.
*
* @param string $dir Directory path
* @return string The zip filename
*/
private function zip_dir(string $dir) {
$ziptmpdir = make_request_directory();
$ziptmpfile = $ziptmpdir . DIRECTORY_SEPARATOR . 'mlbackend.zip';
$files = get_directory_list($dir);
$zipfiles = [];
foreach ($files as $file) {
$fullpath = $dir . DIRECTORY_SEPARATOR . $file;
// Use the relative path to the file as the path in the zip.
$zipfiles[$file] = $fullpath;
}
$zip = new \zip_packer();
if (!$zip->archive_to_pathname($zipfiles, $ziptmpfile)) {
return false;
}
return $ziptmpfile;
}
/**
* Error string for httpcode !== 200
*
* @param int $httpstatuscode The HTTP status code
* @param string $responsebody The body of the response
*/
private function server_error_str(int $httpstatuscode, string $responsebody): string {
return 'HTTP status code ' . $httpstatuscode . ': ' . $responsebody;
}
}
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'mlbackend_python'
*
* @package mlbackend_python
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['errornoconfigdata'] = 'The server configuration is not complete.';
$string['errorserver'] = 'Server error {$a}';
$string['host'] = 'Host';
$string['hostdesc'] = 'Host';
$string['packageinstalledshouldbe'] = 'The moodlemlbackend Python package should be updated. The required version is "{$a->required}" and the installed version is "{$a->installed}".';
$string['packageinstalledtoohigh'] = 'The moodlemlbackend Python package is not compatible with this version of Moodle. The required version is "{$a->required}" or higher as long as it is API-compatible. The installed version "{$a->installed}" is too high.';
$string['pluginname'] = 'Python machine learning backend';
$string['port'] = 'Port';
$string['portdesc'] = 'Port';
$string['privacy:metadata'] = 'The Python machine learning backend plugin does not store any personal data.';
$string['pythonpackagenotinstalled'] = 'The moodlemlbackend Python package is not installed or there is a problem with it. Please execute "{$a}" from command line interface for more info.';
$string['pythonpathnotdefined'] = 'The path to your executable Python binary has not been defined. Please visit "{$a}" to set it.';
$string['serversettingsinfo'] = 'If \'Use a server\' is enabled, the server settings will be displayed.';
$string['username'] = 'Username';
$string['usernamedesc'] = 'String of characters used as a username to communicate between the Moodle server and the Python server.';
$string['password'] = 'Password';
$string['passworddesc'] = 'String of characters used as a password to communicate between the Moodle server and the Python server.';
$string['secure'] = 'Use HTTPS';
$string['securedesc'] = 'Whether to use HTTP or HTTPS.';
$string['useserver'] = 'Use a server';
$string['useserverdesc'] = 'The machine learning backend Python package is not installed on the web server but on a different server.';
$string['tensorboardinfo'] = 'Launch TensorBoard from command line by typing tensorboard --logdir=\'{$a}\' in your web server.';
+54
View File
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Administration settings definitions for mlbackend_python.
*
* @package mlbackend_python
* @copyright 2019 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
$info = $OUTPUT->notification(get_string('serversettingsinfo', 'mlbackend_python'), 'info');
$settings->add(new admin_setting_heading('mlbackend_python/serversettingsinfo', '', $info));
$settings->add(new admin_setting_configcheckbox('mlbackend_python/useserver', get_string('useserver', 'mlbackend_python'),
get_string('useserverdesc', 'mlbackend_python'), 0));
$settings->add(new admin_setting_configtext('mlbackend_python/host', get_string('host', 'mlbackend_python'),
get_string('host', 'mlbackend_python'), '', PARAM_HOST));
$settings->hide_if('mlbackend_python/host', 'mlbackend_python/useserver', 'neq', '1');
$settings->add(new admin_setting_configtext('mlbackend_python/port', get_string('port', 'mlbackend_python'),
get_string('port', 'mlbackend_python'), '', PARAM_INT));
$settings->hide_if('mlbackend_python/port', 'mlbackend_python/useserver', 'neq', '1');
$settings->add(new admin_setting_configcheckbox('mlbackend_python/secure', get_string('secure', 'mlbackend_python'),
get_string('securedesc', 'mlbackend_python'), 0));
$settings->hide_if('mlbackend_python/secure', 'mlbackend_python/useserver', 'neq', '1');
$settings->add(new admin_setting_configtext('mlbackend_python/username', get_string('username', 'mlbackend_python'),
get_string('usernamedesc', 'mlbackend_python'), 'default', PARAM_ALPHANUMEXT));
$settings->hide_if('mlbackend_python/username', 'mlbackend_python/useserver', 'neq', '1');
$settings->add(new admin_setting_configtext('mlbackend_python/password', get_string('password', 'mlbackend_python'),
get_string('passworddesc', 'mlbackend_python'), '', PARAM_ALPHANUMEXT));
$settings->hide_if('mlbackend_python/password', 'mlbackend_python/useserver', 'neq', '1');
}
@@ -0,0 +1,176 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace mlbackend_python;
/**
* Unit tests for the {@link \mlbackend_python\processor} class.
*
* @package mlbackend_python
* @category test
* @copyright 2019 David Mudrák <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class processor_test extends \advanced_testcase {
/**
* Test implementation of the {@link \mlbackend_python\processor::check_pip_package_version()} method.
*
* @dataProvider check_pip_package_versions
* @param string $actual A sample of the actual package version
* @param string $required A sample of the required package version
* @param int $result Expected value returned by the tested method
*/
public function test_check_pip_package_version($actual, $required, $result): void {
$this->assertSame($result, \mlbackend_python\processor::check_pip_package_version($actual, $required));
}
/**
* Check that the {@link \mlbackend_python\processor::check_pip_package_version()} can be called with single argument.
*/
public function test_check_pip_package_version_default(): void {
$this->assertSame(-1, \mlbackend_python\processor::check_pip_package_version('0.0.1'));
$this->assertSame(0, \mlbackend_python\processor::check_pip_package_version(
\mlbackend_python\processor::REQUIRED_PIP_PACKAGE_VERSION));
}
/**
* Provides data samples for the {@link self::test_check_pip_package_version()}.
*
* @return array
*/
public function check_pip_package_versions() {
return [
// Exact match.
[
'0.0.5',
'0.0.5',
0,
],
[
'1.0.0',
'1.0.0',
0,
],
// Actual version higher than required, yet still API compatible.
[
'1.0.3',
'1.0.1',
0,
],
[
'2.1.3',
'2.0.0',
0,
],
[
'1.1.5',
'1.1',
0,
],
[
'2.0.3',
'2',
0,
],
// Actual version not high enough to meet the requirements.
[
'0.0.5',
'1.0.0',
-1,
],
[
'0.37.0',
'1.0.0',
-1,
],
[
'0.0.5',
'0.37.0',
-1,
],
[
'2.0.0',
'2.0.2',
-1,
],
[
'2.7.0',
'3.0',
-1,
],
[
'2.8.9-beta1',
'3.0',
-1,
],
[
'1.1.0-rc1',
'1.1.0',
-1,
],
// Actual version too high and no longer API compatible.
[
'2.0.0',
'1.0.0',
1,
],
[
'3.1.5',
'2.0',
1,
],
[
'3.0.0',
'1.0',
1,
],
[
'2.0.0',
'0.0.5',
1,
],
[
'3.0.2',
'0.37.0',
1,
],
// Zero major version requirement is fulfilled with 1.x API (0.x are not considered stable APIs).
[
'1.0.0',
'0.0.5',
0,
],
[
'1.8.6',
'0.37.0',
0,
],
// Empty version is never good enough.
[
'',
'1.0.0',
-1,
],
[
'0.0.0',
'0.37.0',
-1,
],
];
}
}
+7
View File
@@ -0,0 +1,7 @@
This files describes API changes in the mlbackend_python code, the
information provided here is intended especially for developers.
=== 3.8 ===
* The phi coefficient (Matthews' correlation coefficient) has been replaced by
the F1 score as the main accuracy metric.
+29
View File
@@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version details.
*
* @package mlbackend_python
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'mlbackend_python'; // Full name of the plugin (used for diagnostics).