def open(self, filename, mode='r', uuid=None): assert uuid != self.uuid, "'uuid' may not be the same as this experiment's uuid. Set `uuid=None` to open a file with this experiment." assert '..' not in filename, filename if uuid is None: filepath = self.path / FILES_FOLDER / filename filepath.parent.mkdir(exist_ok=True) else: assert 'w' not in mode, mode assert 'a' not in mode, mode filepath = Path( self.parent_folder) / uuid / FILES_FOLDER / filename if not filepath.exists(): raise FileNotFoundError("File '{}' doesn't exist.".format( str(filepath))) other_experiment_metadata_json = utils.load_json( str(Path(self.parent_folder) / uuid / METADATA_JSON_FILENAME)) if other_experiment_metadata_json['status'] == 'running': raise ValueError( "Loading from a running experiment is not allowed. Other experiment's UUID: {}" .format(uuid)) with utils.UpdateJsonFile(str(self.path / METADATA_JSON_FILENAME)) as metadata: if uuid not in metadata['fileDependencies']: metadata['fileDependencies'][uuid] = [] if filename not in metadata['fileDependencies'][uuid]: metadata['fileDependencies'][uuid].append(filename) return open(str(filepath), mode)
def set_parameter(self, name, value): """Sets the parameter to the given value. Only one value can be recorded per parameter. You can overwrite a previously set parameter. """ json_path = self.path / METADATA_JSON_FILENAME with utils.UpdateJsonFile(str(json_path)) as metadata_json: metadata_json['parameters'][name] = value
def add_tags(id): experiment_json_path = Path( c.DEFAULT_PARENT_FOLDER) / id / c.METADATA_JSON_FILENAME with utils.UpdateJsonFile( str(experiment_json_path)) as experiment_json: for tag in request.json: if tag not in experiment_json['tags']: experiment_json['tags'].append(tag)
def save_notes(id): experiment_json_path = Path( c.DEFAULT_PARENT_FOLDER) / id / c.METADATA_JSON_FILENAME with utils.UpdateJsonFile( str(experiment_json_path)) as experiment_json: experiment_json['title'] = request.json['title'] experiment_json['notes'] = request.json['notes'] return id
def remove_tags(id): experiment_json_path = Path( c.DEFAULT_PARENT_FOLDER) / id / c.METADATA_JSON_FILENAME tags_to_remove = request.json with utils.UpdateJsonFile( str(experiment_json_path)) as experiment_json: tags = experiment_json['tags'] experiment_json['tags'] = list(set(tags) - set(tags_to_remove))
def save_text(id, text_id): if text_id not in ('title', 'description', 'conclusion'): raise ValueError('Invalid text_id: ' + text_id) experiment_json_path = Path( c.DEFAULT_PARENT_FOLDER) / id / c.METADATA_JSON_FILENAME with utils.UpdateJsonFile( str(experiment_json_path)) as experiment_json: experiment_json[text_id] = request.json return id
def add_tags(id): experiment_json_path = Path( c.DEFAULT_PARENT_FOLDER) / id / c.METADATA_JSON_FILENAME with utils.UpdateJsonFile( str(experiment_json_path)) as experiment_json: pattern = re.compile(c.TAG_REGEX_PATTERN) if not all(pattern.match(tag) for tag in request.json): return "Invalid tag(s). A tag can only include lower case ascii, 0-9 and hyphens.", 400 for tag in request.json: if tag not in experiment_json['tags']: experiment_json['tags'].append(tag) return id
def open(self, filename, mode='r', uuid=None): """Opens a file in the experiment's folder. Args: filename (str): A filename or path to a filename mode (str): The mode in which the file is opened. Supports the same modes as Python's built-in `open()` function. uuid (str, None): A previous experiment's id. If given, it will look for the filename in the previous experiment's saved files. Only supports 'r' mode when a uuid is given. Returns: A file object """ assert uuid != self.uuid, "'uuid' may not be the same as this experiment's uuid. Set `uuid=None` to open a file with this experiment." assert '..' not in filename, filename if uuid is None: filepath = self.path / FILES_FOLDER / filename filepath.parent.mkdir(exist_ok=True) else: assert 'r' in mode, mode filepath = Path( c.DEFAULT_PARENT_FOLDER) / uuid / FILES_FOLDER / filename if not filepath.exists(): raise FileNotFoundError("File '{}' doesn't exist.".format( str(filepath))) other_experiment_metadata_json = utils.load_json( str( Path(c.DEFAULT_PARENT_FOLDER) / uuid / METADATA_JSON_FILENAME)) if other_experiment_metadata_json['status'] == 'running': raise ValueError( "Loading from a running experiment is not allowed. Other experiment's UUID: {}" .format(uuid)) with utils.UpdateJsonFile(str(self.path / METADATA_JSON_FILENAME)) as metadata: if uuid not in metadata['fileDependencies']: metadata['fileDependencies'][uuid] = [] if filename not in metadata['fileDependencies'][uuid]: metadata['fileDependencies'][uuid].append(filename) return open(str(filepath), mode)
def __exit__(self, exc_type, exc_value, tb): reraise_exception = (exc_type is not None) and ( exc_type not in self.exceptions_to_ignore) if reraise_exception: traceback.print_exception(exc_type, exc_value, tb) self._close_streams() with utils.UpdateJsonFile(str(self.path / METADATA_JSON_FILENAME)) as metadata: metadata['status'] = 'failed' if reraise_exception else 'succeeded' metadata['endedDatetime'] = datetime.datetime.now().isoformat() if reraise_exception: metadata['exceptionType'] = exc_type.__name__ metadata['exceptionValue'] = str(exc_value) if exc_type is not None: return not reraise_exception
def set_parameter(self, name, value): json_path = self.path / METADATA_JSON_FILENAME with utils.UpdateJsonFile(str(json_path)) as metadata_json: metadata_json['parameters'][name] = value