async def mark_delete(self, id): """ Set the deletion flags of the experiment `id`, and all its children experiments, and return all these marked experiments. Args: id: The experiment ID. Returns: list[ObjectId]: The experiments marked to be deleted. """ id = validate_experiment_id(id) await self.ensure_indexes() return await self._mark_delete(id)
async def get(self, id): """ Get an experiment document by `id`. Args: id (str or ObjectId): ID of the experiment. Returns: dict or None: The experiment document, or :obj:`None` if the experiment does not exist or its deletion flag has been set. """ return from_database_experiment_doc( await self.collection.find_one( {'_id': validate_experiment_id(id), 'deleted': {'$ne': True}}))
async def update(self, id, doc_fields): """ Update an experiment document in ``[coll_name].runs``. Args: id (str or ObjectId): ID of the experiment. doc_fields: Fields of the experiment document to be updated. Raises: KeyError: If the experiment with `id` does not exist. """ id = validate_experiment_id(id) doc_fields = validate_experiment_doc( pop_experiment_id(dict(doc_fields or ()))) await self.ensure_indexes() if doc_fields: return await self._update(id, doc_fields)
async def complete_deletion(self, id_list): """ Complete the deletion on `id_list`. Args: id_list (list[ObjectId]): List of experiment documents to be actually deleted from MongoDB. Returns: The actual number of experiments having been deleted. """ async def deletion_task(id): return (await self.collection.delete_one({'_id': id})).deleted_count id_list = set(validate_experiment_id(i) for i in id_list) tasks = [deletion_task(i) for i in id_list] await self.ensure_indexes() return sum(await asyncio.gather(*tasks))
def get_path(self, experiment_id, experiment_doc=None): """ Get the path of the storage directory of the specified experiment. Args: experiment_id (str or ObjectId): The ID of the experiment. experiment_doc (None or dict): The experiment document from MongoDB. If specified, will use its "storage_dir" as the directory to open, instead of the default location. (default :obj:`None`) Returns: str: The path of the experiment storage directory. """ experiment_id = str(validate_experiment_id(experiment_id)) storage_dir = (experiment_doc or None) and \ experiment_doc.get('storage_dir', None) if storage_dir is None: r_id = ''.join(reversed(experiment_id)) storage_dir = '{}/{}/{}'.format(r_id[:2], r_id[2:4], r_id[4:]) storage_dir = os.path.abspath( os.path.join(self.storage_root, storage_dir)) return storage_dir