2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Python predictions processor
20 * @package mlbackend_python
21 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace mlbackend_python;
27 defined('MOODLE_INTERNAL') || die();
30 * Python predictions processor.
32 * @package mlbackend_python
33 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class processor implements \core_analytics\predictor {
39 * The required version of the python package that performs all calculations.
41 const REQUIRED_PIP_PACKAGE_VERSION = '0.0.9';
44 * Is the plugin ready to be used?.
48 public function is_ready() {
50 // Check the installed pip package version.
51 $cmd = 'python -m moodleinspire.version';
55 // Execute it sending the standard error to $output.
56 $result = exec($cmd . ' 2>&1', $output, $exitcode);
58 if ($result === self::REQUIRED_PIP_PACKAGE_VERSION) {
63 return get_string('pythonpackagenotinstalled', 'mlbackend_python', $cmd);
67 $a = (object)array('installed' => $result, 'required' => self::REQUIRED_PIP_PACKAGE_VERSION);
68 return get_string('packageinstalledshouldbe', 'mlbackend_python', $a);
71 return get_string('pythonpackagenotinstalled', 'mlbackend_python', $cmd);
75 * Trains a machine learning algorithm with the provided dataset.
77 * @param string $uniqueid
78 * @param \stored_file $dataset
79 * @param string $outputdir
82 public function train($uniqueid, \stored_file $dataset, $outputdir) {
84 // Obtain the physical route to the file.
85 $datasetpath = $this->get_file_path($dataset);
87 $cmd = 'python -m moodleinspire.training ' .
88 escapeshellarg($uniqueid) . ' ' .
89 escapeshellarg($outputdir) . ' ' .
90 escapeshellarg($datasetpath);
92 if (!PHPUNIT_TEST && CLI_SCRIPT) {
93 debugging($cmd, DEBUG_DEVELOPER);
98 $result = exec($cmd, $output, $exitcode);
101 throw new \moodle_exception('errornopredictresults', 'analytics');
104 if (!$resultobj = json_decode($result)) {
105 throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
108 if ($exitcode != 0) {
109 throw new \moodle_exception('errorpredictionsprocessor', 'analytics', '', implode(', ', $resultobj->errors));
116 * Returns predictions for the provided dataset samples.
118 * @param string $uniqueid
119 * @param \stored_file $dataset
120 * @param string $outputdir
123 public function predict($uniqueid, \stored_file $dataset, $outputdir) {
125 // Obtain the physical route to the file.
126 $datasetpath = $this->get_file_path($dataset);
128 $cmd = 'python -m moodleinspire.prediction ' .
129 escapeshellarg($uniqueid) . ' ' .
130 escapeshellarg($outputdir) . ' ' .
131 escapeshellarg($datasetpath);
133 if (!PHPUNIT_TEST && CLI_SCRIPT) {
134 debugging($cmd, DEBUG_DEVELOPER);
139 $result = exec($cmd, $output, $exitcode);
142 throw new \moodle_exception('errornopredictresults', 'analytics');
145 if (!$resultobj = json_decode($result)) {
146 throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
149 if ($exitcode != 0) {
150 throw new \moodle_exception('errorpredictionsprocessor', 'analytics', '', implode(', ', $resultobj->errors));
157 * Evaluates the provided dataset.
159 * @param string $uniqueid
160 * @param float $maxdeviation
161 * @param int $niterations
162 * @param \stored_file $dataset
163 * @param string $outputdir
166 public function evaluate($uniqueid, $maxdeviation, $niterations, \stored_file $dataset, $outputdir) {
168 // Obtain the physical route to the file.
169 $datasetpath = $this->get_file_path($dataset);
171 $cmd = 'python -m moodleinspire.evaluation ' .
172 escapeshellarg($uniqueid) . ' ' .
173 escapeshellarg($outputdir) . ' ' .
174 escapeshellarg($datasetpath) . ' ' .
175 escapeshellarg(\core_analytics\model::MIN_SCORE) . ' ' .
176 escapeshellarg($maxdeviation) . ' ' .
177 escapeshellarg($niterations);
179 if (!PHPUNIT_TEST && CLI_SCRIPT) {
180 debugging($cmd, DEBUG_DEVELOPER);
185 $result = exec($cmd, $output, $exitcode);
188 throw new \moodle_exception('errornopredictresults', 'analytics');
191 if (!$resultobj = json_decode($result)) {
192 throw new \moodle_exception('errorpredictwrongformat', 'analytics', '', json_last_error_msg());
199 * Returns the path to the dataset file.
201 * @param \stored_file $file
204 protected function get_file_path(\stored_file $file) {
205 // From moodle filesystem to the local file system.
206 // This is not ideal, but there is no read access to moodle filesystem files.
207 return $file->copy_content_to_temp('core_analytics');