Skip to content

Commit

Permalink
Merge branch 'MDL-60375_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Oct 19, 2017
2 parents a0245c6 + 28c97d1 commit b469834
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
10 changes: 6 additions & 4 deletions analytics/classes/dataset_manager.php
Expand Up @@ -157,16 +157,18 @@ public function store($data) {
];

// Delete previous and old (we already checked that previous copies are not recent) evaluation files for this analysable.
$select = " = {$filerecord['itemid']} AND filepath = :filepath";
$fs->delete_area_files_select($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'],
$select, array('filepath' => $filerecord['filepath']));
if ($this->evaluation) {
$select = " = {$filerecord['itemid']} AND filepath = :filepath";
$fs->delete_area_files_select($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'],
$select, array('filepath' => $filerecord['filepath']));
}

// Write all this stuff to a tmp file.
$filepath = make_request_directory() . DIRECTORY_SEPARATOR . $filerecord['filename'];
$fh = fopen($filepath, 'w+');
if (!$fh) {
$this->close_process();
throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $tmpfilepath);
throw new \moodle_exception('errorcannotwritedataset', 'analytics', '', $filepath);
}
foreach ($data as $line) {
fputcsv($fh, $line);
Expand Down
2 changes: 1 addition & 1 deletion analytics/classes/local/analyser/base.php
Expand Up @@ -514,7 +514,7 @@ protected function process_time_splitting($timesplitting, $analysable, $includet
}
}

if (!empty($target)) {
if (!empty($includetarget)) {
$filearea = \core_analytics\dataset_manager::LABELLED_FILEAREA;
} else {
$filearea = \core_analytics\dataset_manager::UNLABELLED_FILEAREA;
Expand Down
32 changes: 20 additions & 12 deletions analytics/tests/prediction_test.php
Expand Up @@ -165,6 +165,12 @@ public function test_ml_training_and_prediction($timesplittingid, $predictedrang
$this->assertCount($ncourses * 2, $samples);
$this->assertEquals(1, $DB->count_records('analytics_used_files',
array('modelid' => $model->get_id(), 'action' => 'trained')));
// Check that analysable files for training are stored under labelled filearea.
$fs = get_file_storage();
$this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));
$this->assertEmpty($fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));

$courseparams = $params + array('shortname' => 'aaaaaa', 'fullname' => 'aaaaaa', 'visible' => 0);
$course1 = $this->getDataGenerator()->create_course($courseparams);
Expand Down Expand Up @@ -199,6 +205,12 @@ public function test_ml_training_and_prediction($timesplittingid, $predictedrang
$this->assertEquals(2, $DB->count_records('analytics_predictions',
array('modelid' => $model->get_id())));

// Check that analysable files to get predictions are stored under unlabelled filearea.
$this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));
$this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));

// No new generated files nor records as there are no new courses available.
$model->predict();
$predictedranges = $DB->get_records('analytics_predict_samples', array('modelid' => $model->get_id()));
Expand Down Expand Up @@ -234,25 +246,21 @@ public function test_ml_training_and_prediction($timesplittingid, $predictedrang
array('modelid' => $model->get_id(), 'action' => 'predicted')));
$this->assertEquals(4, $DB->count_records('analytics_predictions',
array('modelid' => $model->get_id())));
$this->assertCount(1, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));
$this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));

// New visible course (for training).
$course5 = $this->getDataGenerator()->create_course(array('shortname' => 'aaa', 'fullname' => 'aa'));
$course6 = $this->getDataGenerator()->create_course();
$result = $model->train();
$this->assertEquals(2, $DB->count_records('analytics_used_files',
array('modelid' => $model->get_id(), 'action' => 'trained')));

// Update one of the courses to not visible, it should be used again for prediction.
$course5->visible = 0;
update_course($course5);

$model->predict();
$this->assertEquals(1, $DB->count_records('analytics_predict_samples',
array('modelid' => $model->get_id())));
$this->assertEquals(2, $DB->count_records('analytics_used_files',
array('modelid' => $model->get_id(), 'action' => 'predicted')));
$this->assertEquals(4, $DB->count_records('analytics_predictions',
array('modelid' => $model->get_id())));
$this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::LABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));
$this->assertCount(2, $fs->get_directory_files(\context_system::instance()->id, 'analytics',
\core_analytics\dataset_manager::UNLABELLED_FILEAREA, $model->get_id(), '/analysable/', true, false));

set_config('enabled_stores', '', 'tool_log');
get_log_manager(true);
Expand Down
17 changes: 17 additions & 0 deletions lib/db/upgrade.php
Expand Up @@ -2749,5 +2749,22 @@ function xmldb_main_upgrade($oldversion) {
// Main savepoint reached.
upgrade_main_savepoint(true, 2017101300.01);
}

if ($oldversion < 2017101900.01) {

$fs = get_file_storage();
$models = $DB->get_records('analytics_models');
foreach ($models as $model) {
$files = $fs->get_directory_files(\context_system::instance()->id, 'analytics', 'unlabelled', $model->id,
'/analysable/', true, true);
foreach ($files as $file) {
$file->delete();
}
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2017101900.01);
}

return true;
}

0 comments on commit b469834

Please sign in to comment.