Exports the model training data.

@param int $modelid
@param string $timesplittingid
@return \stored_file|false
504153
public static function export_training_data($modelid, $timesplittingid) {

        $fs = get_file_storage();

        $contextid = \context_system::instance()->id;
        $filepath = '/timesplitting/' . \core_analytics\analysis::clean_time_splitting_id($timesplittingid) . '/';

        $files = $fs->get_directory_files($contextid, 'analytics', self::LABELLED_FILEAREA, $modelid,
            $filepath, true, false);

        // Discard evaluation files.
        foreach ($files as $key => $file) {
            if ($file->get_filename() === self::EVALUATION_FILENAME) {
                unset($files[$key]);
            }
        }

        if (empty($files)) {
            return false;
        }

        return self::merge_datasets($files, $modelid, $timesplittingid, self::EXPORT_FILEAREA);
    }
56921
public function train_classification($uniqueid, \stored_file $dataset, $outputdir) {

        $modelfilepath = $this->get_model_filepath($outputdir);

        $modelmanager = new ModelManager();

        if (file_exists($modelfilepath)) {
            $classifier = $modelmanager->restoreFromFile($modelfilepath);
        } else {
            $classifier = new \Phpml\Classification\Linear\LogisticRegression(self::TRAIN_ITERATIONS, Normalizer::NORM_L2);
        }

        $fh = $dataset->get_content_file_handle();

        // The first lines are var names and the second one values.
        $metadata = $this->extract_metadata($fh);

        // Skip headers.
        fgets($fh);

        $samples = array();
        $targets = array();
        while (($data = fgetcsv($fh)) !== false) {
            $sampledata = array_map('floatval', $data);
            $samples[] = array_slice($sampledata, 0, $metadata['nfeatures']);
            $targets[] = intval($data[$metadata['nfeatures']]);

            $nsamples = count($samples);
            if ($nsamples === self::BATCH_SIZE) {
                // Training it batches to avoid running out of memory.

                $classifier->partialTrain($samples, $targets, array(0, 1));
                $samples = array();
                $targets = array();
            }
            if (empty($morethan1sample) && $nsamples > 1) {
                $morethan1sample = true;
            }
        }
        fclose($fh);

        if (empty($morethan1sample)) {
            $resultobj = new \stdClass();
            $resultobj->status = \core_analytics\model::NO_DATASET;
            $resultobj->info = array();
            return $resultobj;
        }

        // Train the remaining samples.
        if ($samples) {
            $classifier->partialTrain($samples, $targets, array(0, 1));
        }

        $resultobj = new \stdClass();
        $resultobj->status = \core_analytics\model::OK;
        $resultobj->info = array();

        // Store the trained model.
        $modelmanager->saveToFile($classifier, $modelfilepath);

        return $resultobj;
    }
672020
public function getExportOutputResourceAttachment($executionId, $exportId, $attachmentName)
    {
        $url = $this->makeUrl($executionId, false, false, true, false, $exportId, true, $attachmentName);
        $response = $this->service->prepAndSend($url, array(200), 'GET', null, true, 'application/json', '*/*');

        return $response;
    }
744812
public static void exportCSVSpark(String directory, String delimiter, int outputSplits,
                    JavaRDD<List<Writable>> data) {
        exportCSVSpark(directory, delimiter, null, outputSplits, data);
    }
398548
function AggregateModel (id, modelInitValues) {
  if (!id || !_.isString(id)) {
    var err = new Error('No id injected!');
    debug(err);
    throw err;
  }

  this.id = id;

  if (modelInitValues instanceof AggregateModel) {
    this.attributes = modelInitValues.toJSON();
  } else {
    this.attributes = _.cloneDeep(modelInitValues || {});
  }

  this.attributes.id = this.id;
  this.attributes._destroyed = this.attributes._destroyed || false;
  this.attributes._revisions = this.attributes._revisions || {};
  this.attributes._revision = this.attributes._revision || 0;

  this.uncommittedEvents = [];
}
866054
private function getActiveModel(ModelIdInterface $modelId)
    {
        if (!Database::getInstance()->tableExists($modelId->getDataProviderName())) {
            return null;
        }
        $dataProvider = $this->itemContainer->getEnvironment()->getDataProvider();

        return $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));
    }
1099068
public function setModel($id, $modelId)
    {
        return $this->put($this->getPath().'/'.rawurlencode($id).'/idModel', array('value' => $modelId));
    }
445082
private function insertModelIntoDatabase(ModelInterface $model, $timestamp = 0)
    {
        $data = $this->convertModelToDataPropertyArray($model, $timestamp);
        if ($this->getIdGenerator()) {
            $model->setId($this->getIdGenerator()->generate());
            $data[$this->idProperty] = $model->getId();
        }

        $this->connection->insert($this->source, $data);

        $insertId = $this->connection->lastInsertId($this->source);

        if (('' !== $insertId) && !isset($data[$this->idProperty])) {
            $model->setId($insertId);
        }
    }
956131
public function getSubscriptionUsageData(
        $subscriptionId,
        \DateTime $subscriptionStart = null,
        \DateTime $subscriptionEnd = null
    ) {
        $requestData = new GetUsageData\RequestData($subscriptionId);

        $requestData->setSubscriptionId($subscriptionId);
        $requestData->setSubscriptionStart($subscriptionStart);
        $requestData->setSubscriptionEnd($subscriptionEnd);

        $request = new GetUsageData\Request($requestData);

        return $this->sendRequest($request, GetUsageData\ApiResponse::class);
    }
1078413
protected function saveTo($fromOrgId, $fromRespId, $toOrgId, $toPatientId)
    {
        $model = $this->getModel();
        try {
            $model->move($fromOrgId, $fromRespId, $toOrgId, $toPatientId);    
        } catch (\Exception $exc) {
            $this->addMessage($exc->getMessage());
            return 0;
        }        
        
        return $model->getChanged();
    }
59477
protected function save_train_samples(array $sampleids, \core_analytics\local\time_splitting\base $timesplitting) {
        global $DB;

        $trainingsamples = new \stdClass();
        $trainingsamples->modelid = $this->analyser->get_modelid();
        $trainingsamples->analysableid = $timesplitting->get_analysable()->get_id();
        $trainingsamples->timesplitting = $timesplitting->get_id();

        $trainingsamples->sampleids = json_encode($sampleids);
        $trainingsamples->timecreated = time();

        $DB->insert_record('analytics_train_samples', $trainingsamples);
    }
376899
public function findWithTrashed($modelitemId)
    {
        $this->model = $this->model->withTrashed()->findOrFail($modelitemId);

        return $this->model;
    }
316419
protected function fetchModel(EnvironmentInterface $environment, ModelIdInterface $modelId)
    {
        $dataProvider = $environment->getDataProvider($modelId->getDataProviderName());
        $model        = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($modelId->getId()));

        if (!$model || !$model->getId()) {
            throw new DcGeneralRuntimeException(
                'Could not load model with id ' . $modelId->getSerialized()
            );
        }

        return $model;
    }
258747
public function addExport($data, $modelId = false)
    {

        $this->files   = $this->getFiles();
        $this->data    = $data;
        $this->modelId = $modelId;

        if ($model = $this->getModel()) {
            if ($this->model->getMeta('nested', false)) {
               $this->rowsPerBatch = $this->nestedRowsPerBatch;
            }
            
            $totalRows  = $this->getModelCount();
            $this->addFile();
            $this->addHeader($this->tempFilename . $this->fileExtension);
            $currentRow = 0;
            do {
                $filter['limit'] = array($this->rowsPerBatch, $currentRow);
                if ($this->batch) {
                    $this->batch->addTask('Export_ExportCommand', $data['type'], 'addRows', $data, $modelId, $this->tempFilename, $filter);
                } else {
                    $this->addRows($data, $modelId, $this->tempFilename, $filter);
                }
                $currentRow = $currentRow + $this->rowsPerBatch;
            } while ($currentRow < $totalRows);

            if ($this->batch) {
                $this->batch->addTask('Export_ExportCommand', $data['type'], 'addFooter', $this->tempFilename . $this->fileExtension, $modelId);
                $this->batch->setSessionVariable('files', $this->files);
            } else {
                $this->addFooter($this->tempFilename . $this->fileExtension, $modelId);
                $this->_session        = new \Zend_Session_Namespace(__CLASS__);
                $this->_session->files = $this->files;
            }
        }
    }
352121
public function getTrainingInputsByVersion(string $modelId, string $versionId)
    {
        $inputResult = $this->getRequest()->request(
            'GET',
            $this->getRequestUrl(sprintf('models/%s/versions/%s/inputs', $modelId, $versionId))
        );

        return $this->getInputsFromResult($inputResult);
    }
541900
protected function createAttachRecord($id, $timed)
    {
        $parentKey = $this->parentMap->getKeyName();

        $record = [];

        $record[$this->foreignKey] = $this->parent->getEntityAttribute($parentKey);

        $record[$this->otherKey] = $id;

        // If the record needs to have creation and update timestamps, we will make
        // them by calling the parent model's "freshTimestamp" method which will
        // provide us with a fresh timestamp in this model's preferred format.
        if ($timed) {
            $record = $this->setTimestampsOnAttach($record);
        }

        return $record;
    }
1221354
public function parse($modelId, $rawConfig, $allowEmpty = false) {
        $config = json_decode($rawConfig, true);

        if (!is_null($config)) {
            $this->_validate($config, $allowEmpty);
            return $config;
        } else throw new Exception("The {$modelId} configuration file contains invalid JSON. Don't forget the double quotes around array keys and string-like values. Or use jsonlint.com to validate your file.");
    }