def get_dataset(self, identifier): """Read a full dataset from the data store. Returns None if no dataset with the given identifier exists. Parameters ---------- identifier : string Unique dataset identifier Returns ------- vizier.datastore.mimir.dataset.MimirDatasetHandle """ # Return None if the dataset file does not exist dataset_file = self.get_dataset_file(identifier) if not os.path.isfile(dataset_file): return None annotations = DatasetMetadata.from_file( self.get_metadata_filename(identifier)) return MimirDatasetHandle.from_file(dataset_file, annotations=annotations)
def get_dataset(self, identifier): """Read a full dataset from the data store. Returns None if no dataset with the given identifier exists. Parameters ---------- identifier : string Unique dataset identifier Returns ------- vizier.datastore.fs.dataset.FileSystemDatasetHandle """ # Test if a subfolder for the given dataset identifier exists. If not # return None. dataset_dir = self.get_dataset_dir(identifier) if not os.path.isdir(dataset_dir): return None # Load the dataset handle return FileSystemDatasetHandle.from_file( descriptor_file=os.path.join(dataset_dir, DESCRIPTOR_FILE), data_file=os.path.join(dataset_dir, DATA_FILE), annotations=DatasetMetadata.from_file( self.get_metadata_filename(identifier)))
def get_annotations(self, identifier, column_id=None, row_id=None): """Get list of annotations for a resources of a given dataset. If only the column id is provided annotations for the identifier column will be returned. If only the row identifier is given all annotations for the specified row are returned. Otherwise, all annotations for the specified cell are returned. If both identifier are None all annotations for the dataset are returned. Parameters ---------- column_id: int, optional Unique column identifier row_id: int, optiona Unique row identifier Returns ------- vizier.datastore.annotation.dataset.DatasetMetadata """ # Test if a subfolder for the given dataset identifier exists. If not # return None. dataset_dir = self.get_dataset_dir(identifier) if not os.path.isdir(dataset_dir): return None annotations = DatasetMetadata.from_file( self.get_metadata_filename(identifier)) if column_id is None and row_id is None: return annotations elif column_id is None: return DatasetMetadata(rows=annotations.rows).filter(rows=[row_id]) elif row_id is None: return DatasetMetadata(columns=annotations.columns).filter( columns=[column_id]) else: return DatasetMetadata(cells=annotations.cells).filter( columns=[column_id], rows=[row_id])
def update_annotation(self, identifier, key, old_value=None, new_value=None, column_id=None, row_id=None): """Update the annotations for a component of the datasets with the given identifier. Returns the updated annotations or None if the dataset does not exist. Parameters ---------- identifier : string Unique dataset identifier column_id: int, optional Unique column identifier row_id: int, optional Unique row identifier key: string, optional Annotation key old_value: string, optional Previous annotation value whan updating an existing annotation. new_value: string, optional Updated annotation value Returns ------- bool """ # Raise ValueError if column id and row id are both None if column_id is None and row_id is None: raise ValueError('invalid dataset resource identifier') # Return None if the dataset is unknown dataset_dir = self.get_dataset_dir(identifier) if not os.path.isdir(dataset_dir): return None # Read annotations from file, Evaluate update statement and write result # back to file. metadata_filename = self.get_metadata_filename(identifier) annotations = DatasetMetadata.from_file(metadata_filename) # Get object annotations if column_id is None: elements = annotations.rows elif row_id is None: elements = annotations.columns else: elements = annotations.cells # Identify the type of operation: INSERT, DELETE or UPDATE if old_value is None and not new_value is None: elements.append( DatasetAnnotation(key=key, value=new_value, column_id=column_id, row_id=row_id)) elif not old_value is None and new_value is None: del_index = None for i in range(len(elements)): a = elements[i] if a.column_id == column_id and a.row_id == row_id: if a.key == key and a.value == old_value: del_index = i break if del_index is None: return False del elements[del_index] elif not old_value is None and not new_value is None: anno = None for a in elements: if a.column_id == column_id and a.row_id == row_id: if a.key == key and a.value == old_value: anno = a break if anno is None: return False anno.value = new_value else: raise ValueError('invalid modification operation') # Write modified annotations to file annotations.to_file(metadata_filename) return True