Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 274
added serialize run functionality#459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
4118a9652e301b3209892050a572d92e9f2ec82219File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| from collections import OrderedDict | ||
| import errno | ||
| import json | ||
| import pickle | ||
| import sys | ||
| import time | ||
| import numpy as np | ||
| import arff | ||
| import os | ||
| import xmltodict | ||
| import openml | ||
| @@ -65,6 +68,96 @@ def __str__(self): | ||
| def _repr_pretty_(self, pp, cycle): | ||
| pp.text(str(self)) | ||
| @classmethod | ||
| def from_filesystem(cls, folder): | ||
| """ | ||
| The inverse of the to_filesystem method. Instantiates an OpenMLRun | ||
| object based on files stored on the file system. | ||
| Parameters | ||
| ---------- | ||
| folder : str | ||
| a path leading to the folder where the results | ||
| are stored | ||
| Returns | ||
| ------- | ||
| run : OpenMLRun | ||
| the re-instantiated run object | ||
| """ | ||
| if not os.path.isdir(folder): | ||
| raise ValueError('Could not find folder') | ||
| description_path = os.path.join(folder, 'description.xml') | ||
| predictions_path = os.path.join(folder, 'predictions.arff') | ||
| trace_path = os.path.join(folder, 'trace.arff') | ||
| model_path = os.path.join(folder, 'model.pkl') | ||
| if not os.path.isfile(description_path): | ||
| raise ValueError('Could not find description.xml') | ||
| if not os.path.isfile(predictions_path): | ||
| raise ValueError('Could not find predictions.arff') | ||
| if not os.path.isfile(model_path): | ||
| raise ValueError('Could not find model.pkl') | ||
| with open(description_path, 'r') as fp: | ||
| run = openml.runs.functions._create_run_from_xml(fp.read(), from_server=False) | ||
| with open(predictions_path, 'r') as fp: | ||
| predictions = arff.load(fp) | ||
| run.data_content = predictions['data'] | ||
| with open(model_path, 'rb') as fp: | ||
| run.model = pickle.load(fp) | ||
| if os.path.isfile(trace_path): | ||
| with open(trace_path, 'r') as fp: | ||
| trace = arff.load(fp) | ||
| run.trace_attributes = trace['attributes'] | ||
| run.trace_content = trace['data'] | ||
| return run | ||
| def to_filesystem(self, output_directory): | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please add a docstring, here, too? | ||
| """ | ||
| The inverse of the from_filesystem method. Serializes a run | ||
| on the filesystem, to be uploaded later. | ||
| Parameters | ||
| ---------- | ||
| folder : str | ||
| a path leading to the folder where the results | ||
| will be stored. Should be empty | ||
| """ | ||
| if self.data_content is None or self.model is None: | ||
| raise ValueError('Run should have been executed (and contain model / predictions)') | ||
| try: | ||
| os.makedirs(output_directory) | ||
| except OSError as e: | ||
| if e.errno == errno.EEXIST: | ||
| pass | ||
| else: | ||
| raise e | ||
| if not os.listdir(output_directory) == []: | ||
| raise ValueError('Output directory should be empty') | ||
| run_xml = self._create_description_xml() | ||
| predictions_arff = arff.dumps(self._generate_arff_dict()) | ||
| with open(os.path.join(output_directory, 'description.xml'), 'w') as f: | ||
| f.write(run_xml) | ||
| with open(os.path.join(output_directory, 'predictions.arff'), 'w') as f: | ||
| f.write(predictions_arff) | ||
| with open(os.path.join(output_directory, 'model.pkl'), 'wb') as f: | ||
| pickle.dump(self.model, f) | ||
| if self.trace_content is not None: | ||
| trace_arff = arff.dumps(self._generate_trace_arff_dict()) | ||
| with open(os.path.join(output_directory, 'trace.arff'), 'w') as f: | ||
| f.write(trace_arff) | ||
| def _generate_arff_dict(self): | ||
| """Generates the arff dictionary for uploading predictions to the server. | ||
| @@ -109,11 +202,11 @@ def _generate_trace_arff_dict(self): | ||
| Contains information about the optimization trace. | ||
| """ | ||
| if self.trace_content is None or len(self.trace_content) == 0: | ||
| raise ValueError('No trace content avaiable.') | ||
| raise ValueError('No trace content available.') | ||
| if len(self.trace_attributes) != len(self.trace_content[0]): | ||
| raise ValueError('Trace_attributes and trace_content not compatible') | ||
| arff_dict = {} | ||
| arff_dict = dict() | ||
| arff_dict['attributes'] = self.trace_attributes | ||
| arff_dict['data'] = self.trace_content | ||
| arff_dict['relation'] = 'openml_task_' + str(self.task_id) + '_predictions' | ||
| @@ -447,7 +540,8 @@ def _to_dict(taskid, flow_id, setup_string, error_message, parameter_settings, | ||
| description['oml:run']['oml:parameter_setting'] = parameter_settings | ||
| if tags is not None: | ||
| description['oml:run']['oml:tag'] = tags # Tags describing the run | ||
| if fold_evaluations is not None or sample_evaluations is not None: | ||
| if (fold_evaluations is not None and len(fold_evaluations) > 0) or \ | ||
| (sample_evaluations is not None and len(sample_evaluations) > 0): | ||
| description['oml:run']['oml:output_data'] = dict() | ||
| description['oml:run']['oml:output_data']['oml:evaluation'] = list() | ||
| if fold_evaluations is not None: | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a docstring?