Skip to content

Commit

Permalink
MDL-59995 core_admin: added setting to define Python bin path
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Sep 12, 2017
1 parent 32f9550 commit 391663c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions admin/settings/server.php
Expand Up @@ -12,6 +12,8 @@
$temp->add(new admin_setting_configexecutable('aspellpath', new lang_string('aspellpath', 'admin'), new lang_string('edhelpaspellpath'), ''));
$temp->add(new admin_setting_configexecutable('pathtodot', new lang_string('pathtodot', 'admin'), new lang_string('pathtodot_help', 'admin'), ''));
$temp->add(new admin_setting_configexecutable('pathtogs', new lang_string('pathtogs', 'admin'), new lang_string('pathtogs_help', 'admin'), '/usr/bin/gs'));
$temp->add(new admin_setting_configexecutable('pathtopython', new lang_string('pathtopython', 'admin'),
new lang_string('pathtopythondesc', 'admin'), ''));
$ADMIN->add('server', $temp);


Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Expand Up @@ -835,6 +835,8 @@
$string['pathtopsql'] = 'Path to psql';
$string['pathtopsqldesc'] = 'This is only necessary to enter if you have more than one psql on your system (for example if you have more than one version of postgresql installed)';
$string['pathtopsqlinvalid'] = 'Invalid path to psql - either wrong path or not executable';
$string['pathtopython'] = 'Path to Python';
$string['pathtopythondesc'] = 'Path to your executable Python binary.';
$string['pcreunicodewarning'] = 'It is strongly recommended to use PCRE PHP extension that is compatible with Unicode characters.';
$string['perfdebug'] = 'Performance info';
$string['performance'] = 'Performance';
Expand Down
33 changes: 28 additions & 5 deletions lib/mlbackend/python/classes/processor.php
Expand Up @@ -40,15 +40,38 @@ class processor implements \core_analytics\classifier, \core_analytics\regresso
*/
const REQUIRED_PIP_PACKAGE_VERSION = '0.0.2';

/**
* The path to the Python bin.
*
* @var string
*/
protected $pathtopython;

/**
* The constructor.
*/
public function __construct() {
global $CFG;

// Set the python location if there is a value.
if (!empty($CFG->pathtopython)) {
$this->pathtopython = $CFG->pathtopython;
}
}

/**
* Is the plugin ready to be used?.
*
* @return bool
* @return bool|string Returns true on success, a string detailing the error otherwise
*/
public function is_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 = 'python -m moodlemlbackend.version';
$cmd = "{$this->pathtopython} -m moodlemlbackend.version";

$output = null;
$exitcode = null;
Expand Down Expand Up @@ -84,7 +107,7 @@ public function train_classification($uniqueid, \stored_file $dataset, $outputdi
// Obtain the physical route to the file.
$datasetpath = $this->get_file_path($dataset);

$cmd = 'python -m moodlemlbackend.training ' .
$cmd = "{$this->pathtopython} -m moodlemlbackend.training " .
escapeshellarg($uniqueid) . ' ' .
escapeshellarg($outputdir) . ' ' .
escapeshellarg($datasetpath);
Expand Down Expand Up @@ -125,7 +148,7 @@ public function classify($uniqueid, \stored_file $dataset, $outputdir) {
// Obtain the physical route to the file.
$datasetpath = $this->get_file_path($dataset);

$cmd = 'python -m moodlemlbackend.prediction ' .
$cmd = "{$this->pathtopython} -m moodlemlbackend.prediction " .
escapeshellarg($uniqueid) . ' ' .
escapeshellarg($outputdir) . ' ' .
escapeshellarg($datasetpath);
Expand Down Expand Up @@ -168,7 +191,7 @@ public function evaluate_classification($uniqueid, $maxdeviation, $niterations,
// Obtain the physical route to the file.
$datasetpath = $this->get_file_path($dataset);

$cmd = 'python -m moodlemlbackend.evaluation ' .
$cmd = "{$this->pathtopython} -m moodlemlbackend.evaluation " .
escapeshellarg($uniqueid) . ' ' .
escapeshellarg($outputdir) . ' ' .
escapeshellarg($datasetpath) . ' ' .
Expand Down
1 change: 1 addition & 0 deletions lib/mlbackend/python/lang/en/mlbackend_python.php
Expand Up @@ -25,3 +25,4 @@
$string['packageinstalledshouldbe'] = '"moodlemlbackend" python package should be updated. The required version is "{$a->required}" and the installed version is "{$a->installed}"';
$string['pluginname'] = 'Python machine learning backend';
$string['pythonpackagenotinstalled'] = '"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.';

0 comments on commit 391663c

Please sign in to comment.