Skip to content

Commit

Permalink
fix code style and documentation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LiFaytheGoblin committed Feb 5, 2024
1 parent 832ec13 commit 61bd563
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 25 deletions.
6 changes: 5 additions & 1 deletion classes/dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public function get_file_type(): string {
}

/**
* Restore the data for a model version to the cache from the stored CSV files.
*
* @param array $options
* @return void
*/
Expand All @@ -128,7 +130,9 @@ public function restore_raw_data(array $options): void {
$this->data = dataset_helper::build_from_csv($filehandle, $options['analysisintervalkey']);
}

/** Validate the evidence's options.
/**
* Validate the evidence's options.
*
* @param array $options
*/
public function validate_restore_options(array $options) : void {
Expand Down
23 changes: 19 additions & 4 deletions classes/dataset_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ public static function replace_rows_in_dataset(array $dataset, array $newrows) :
* @return array[]
*/
public static function merge(array $dataseta, array $datasetb) : array {
if (count($dataseta) < 1) return $datasetb;
if (count($datasetb) < 1) return $dataseta;
if (count($dataseta) < 1) {
return $datasetb;
}
if (count($datasetb) < 1) {
return $dataseta;
}

$analysisintervalkeya = self::get_analysisintervalkey($dataseta);
$analysisintervalkeyb = self::get_analysisintervalkey($datasetb);
Expand All @@ -211,8 +215,12 @@ public static function merge(array $dataseta, array $datasetb) : array {
* @return array
*/
public static function diff(array $dataseta, array $datasetb) : array {
if (count($dataseta) < 1) return $datasetb;
if (count($datasetb) < 1) return $dataseta;
if (count($dataseta) < 1) {
return $datasetb;
}
if (count($datasetb) < 1) {
return $dataseta;
}

$sampleidsb = self::get_sampleids_used_in_dataset($datasetb);
$rowsa = self::get_rows($dataseta);
Expand Down Expand Up @@ -362,6 +370,13 @@ public static function validate(array $dataset) : void {
}
}

/**
* Transform a dataset (a two dimensional array) into a string which can be
* stored as a CSV. Elements at id '0' are treated as the header.
*
* @param array $dataset
* @return string
*/
public static function serialize(array $dataset) : string {
$str = '';
$columns = null;
Expand Down
10 changes: 7 additions & 3 deletions classes/evidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,20 @@ public function set_raw_data(mixed $data): void {
*
* @param array $options
*/
public abstract function restore_raw_data(array $options): void;
abstract public function restore_raw_data(array $options): void;

/**
* Validates the $options array passed to restore_raw_data.
*
* @param array $options
*/
public abstract function validate_restore_options(array $options): void;

abstract public function validate_restore_options(array $options): void;

/**
* Return the file that contains the evidence data.
*
* @return mixed
*/
public function get_file() {
if (!$this->serializedfilelocation) {
throw new LogicException('No serialized file available to restore data from.');
Expand Down
14 changes: 12 additions & 2 deletions classes/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ protected function get_file_type(): string {
}

/**
* @param array $options
* Restore the model from the stored .ser file.
*
* @param array $options (can be empty - not used)
* @return void
*/
public function restore_raw_data(array $options): void {
$file = $this->get_file();
$this->data = unserialize($file->get_content());
}

public function validate_restore_options(array $options): void {}
/**
* No validation is needed for the restore options of the model class,
* since that parameter is not used.
*
* @param array $options
* @return void
*/
public function validate_restore_options(array $options): void {
}
}
7 changes: 5 additions & 2 deletions classes/model_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ public static function create_scaffold_and_get_for_config(int $configid): int {
* @return model_version
* @throws Exception
*/
public static function create(int $versionid, ?array $contexts = null, ?string $dataset = null, ?bool $anonymous = true): model_version {
public static function create(int $versionid, ?array $contexts = null, ?string $dataset = null,
?bool $anonymous = true): model_version {
$version = new model_version($versionid);

try {
Expand Down Expand Up @@ -597,7 +598,9 @@ public function get_shuffled_data(mixed $existingtrainingdataset, mixed $existin
// So the second part needs to be the remaining data, but shuffled.
if ($existingtrainingdataset !== false && !$existingtestdataset) {
$trainingdata = $this->get_single_evidence('training_dataset'); // First part of reconstructed data.
if (!isset($trainingdata)) $trainingdata = [];
if (!isset($trainingdata)) {
$trainingdata = [];
}
$remainingdata = dataset_helper::diff($data, $trainingdata); // Second part of reconstructed data.
$remainingdatashuffled = dataset_helper::get_shuffled($remainingdata);
return dataset_helper::merge($trainingdata, $remainingdatashuffled);
Expand Down
4 changes: 3 additions & 1 deletion classes/task/version_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public function execute() {
mtrace('Finished version ' . $data->versionid . '.');
}

/*
/**
* Check whether the version creation task is active for a specific version.
*
* @param int $versionid
*/
public static function is_active($versionid) {
global $DB;
Expand Down
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/lala/db" VERSION="2024012213" COMMENT="XMLDB file for Moodle admin/tool/lala"
<XMLDB PATH="admin/tool/lala/db" VERSION="2024020508" COMMENT="XMLDB file for Moodle admin/tool/lala"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand Down
2 changes: 1 addition & 1 deletion tests/evidence_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
require_once(__DIR__ . '/fixtures/test_dataset_evidence.php');

/**
* Dataset test.
* Evidence test.
*
* @package tool_lala
* @copyright 2023 Linda Fernsel <fernsel@htw-berlin.de>
Expand Down
20 changes: 18 additions & 2 deletions tests/fixtures/test_evidence.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,30 @@ protected function get_file_type(): string {
return self::FILETYPE;
}

/** Validate the $options -
/**
* Validate the $options -
* @param array $options
*/
public function validate_collect_options(array $options): void {
if (isset($this->data)) throw new Exception('Already collected.');
}

public function restore_raw_data(): void {
/**
* Return the test data.
*
* @param array $options
* @return void
*/
public function restore_raw_data(array $options): void {
// TODO: Implement restore_raw_data() method.
}

/**
* Validate the $options param - this is formality, since restore_raw_data is mocked.
*
* @param array $options
* @return void
*/
public function validate_restore_options(array $options): void {
}
}
11 changes: 7 additions & 4 deletions tests/model_version_complete_creation_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ public function test_model_version_complete_creation_modeldeleted(): void {
*
* @dataProvider tool_lala_model_creation_parameters_provider
* @covers ::tool_lala_model_version
* @param bool $anonymous
*/
public function test_model_version_complete_creation_resume_after_data_gathering(bool $anonymous): void {
// Generate test data.
Expand Down Expand Up @@ -299,7 +300,7 @@ public function test_model_version_complete_creation_resume_after_data_gathering
$this->assertTrue($hasmodel);
$haspredictionsdatasetincache = $this->version->evidence_in_cache('predictions_dataset');
$this->assertTrue($haspredictionsdatasetincache);
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$this->assertTrue(isset($predictionsdataset));
$this->assertTrue(count($predictionsdataset) > 0);
}
Expand All @@ -309,6 +310,7 @@ public function test_model_version_complete_creation_resume_after_data_gathering
*
* @dataProvider tool_lala_model_creation_parameters_provider
* @covers ::tool_lala_model_version
* @param bool $anonymous
*/
public function test_model_version_complete_creation_resume_after_data_split(bool $anonymous): void {
// Generate test data.
Expand Down Expand Up @@ -336,7 +338,7 @@ public function test_model_version_complete_creation_resume_after_data_split(boo
$this->assertTrue($hasmodel);
$haspredictionsdataset = $this->version->has_evidence('predictions_dataset');
$this->assertTrue($haspredictionsdataset);
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$this->assertTrue(isset($predictionsdataset));
$this->assertTrue(count($predictionsdataset) > 0);
}
Expand All @@ -346,8 +348,9 @@ public function test_model_version_complete_creation_resume_after_data_split(boo
*
* @dataProvider tool_lala_model_creation_parameters_provider
* @covers ::tool_lala_model_version
* @param bool $anonymous
*/
public function test_model_version_complete_creation_resume_after_training($anonymous): void {
public function test_model_version_complete_creation_resume_after_training(bool $anonymous): void {
// Generate test data.
test_course_with_students::create($this->getDataGenerator());

Expand All @@ -371,7 +374,7 @@ public function test_model_version_complete_creation_resume_after_training($anon
// Now the data is all there.
$haspredictionsdataset = $this->version->has_evidence('predictions_dataset');
$this->assertTrue($haspredictionsdataset);
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$predictionsdataset = $this->version->get_single_evidence('predictions_dataset');
$this->assertTrue(isset($predictionsdataset));
$this->assertTrue(count($predictionsdataset) > 0);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/model_version_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function test_model_version_has_evidence(): void {

$hasdatasetincache = $version->evidence_in_cache('dataset');
$this->assertTrue($hasdatasetincache);
$hasdataindb = $version->evidence_in_db('dataset');
$hasdataindb = $version->evidence_in_db('dataset');
$this->assertTrue($hasdataindb);

// Finish the model version automatically.
Expand All @@ -139,7 +139,7 @@ public function test_model_version_has_evidence(): void {
$this->assertTrue($hasdataset);
$hasdatasetincache = $version->evidence_in_cache('dataset');
$this->assertFalse($hasdatasetincache);
$hasdataindb = $version->evidence_in_db('dataset');
$hasdataindb = $version->evidence_in_db('dataset');
$this->assertTrue($hasdataindb);
}

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'tool_lala';
$plugin->release = '4.0.0';
$plugin->version = 2024012213;
$plugin->release = '4.0.1';
$plugin->version = 2024020508;
$plugin->requires = 2022041900;
$plugin->maturity = MATURITY_BETA;

0 comments on commit 61bd563

Please sign in to comment.