def read(self, h5_object=None): """ Reads all available NSID main datasets or the specified h5_object Parameters ---------- h5_object : h5py.Dataset or h5py.Group HDF5 Dataset to read or the HDF5 group under which to read all datasets Returns ------- sidpy.Dataset or list of sidpy.Dataset objects Datasets present in the provided file """ if h5_object is None: return self.read_all(recursive=True) if not isinstance(h5_object, (h5py.Group, h5py.Dataset)): raise TypeError('Provided h5_object was not a h5py.Dataset or ' 'h5py.Group object but was of type: {}' ''.format(type(h5_object))) self.__validate_obj_in_same_file(h5_object) if isinstance(h5_object, h5py.Dataset): return read_h5py_dataset(h5_object) else: return self.read_all(parent=h5_object)
def test_attrs_datatype(self): data_types = [ 'UNKNOWN', 'SPECTRUM', 'LINE_PLOT', 'LINE_PLOT_FAMILY', 'IMAGE', 'IMAGE_MAP', 'IMAGE_STACK', 'SPECTRAL_IMAGE', 'IMAGE_4D' ] for dt in data_types: _meta = {"data_type": dt} h5file = make_simple_nsid_dataset(_meta) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset.data_type.name == dt)
def test_valid_nsid_h5_dset(self): base = { 'quantity': 'Current', 'units': 'nA', # 'pyNSID_version': 'version', 'title': 'Current-Voltage spectroscopy measurement', 'data_type': 'SPECTRAL_IMAGE', 'modality': 'cAFM', 'source': 'Asylum Research Cypher' } h5file = make_simple_nsid_dataset(base) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) # Validate the base attributes first for key, expected in base.items(): actual = getattr(dset, key)
def read_all(self, recursive=True, parent=None): """ Reads all HDF5 datasets formatted according to NSID specifications. Parameters ---------- recursive : bool, default = True We might just remove this kwarg parent : h5py.Group, Default = None HDF5 group under which to read all available datasets. By default, all datasets within the HDF5 file are read. Returns ------- sidpy.Dataset or list of sidpy.Dataset objects Datasets present in the provided file """ if parent is None: h5_group = self._h5_file else: if not isinstance(parent, h5py.Group): raise TypeError('parent should be a h5py.Group object') self.__validate_obj_in_same_file(parent) h5_group = parent if recursive: list_of_main = self._main_dsets else: list_of_main = [] for key in h5_group: if isinstance(h5_group[key], h5py.Dataset): if check_if_main(h5_group[key]): list_of_main.append(h5_group[key]) # Go through each of the identified list_of_datasets = [] for dset in list_of_main: list_of_datasets.append(read_h5py_dataset(dset)) return list_of_datasets
def test_dims(self) -> None: h5file = make_simple_nsid_dataset() dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset._axes[0].name == 'a0') self.assertTrue(dset._axes[1].name == 'b0')
def test_attrs_source(self) -> None: _meta = {"source": "src"} h5file = make_simple_nsid_dataset(_meta) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset.source == 'src')
def test_attrs_modality(self) -> None: _meta = {"modality": "mod"} h5file = make_simple_nsid_dataset(_meta) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset.modality == 'mod')
def test_attrs_quantity(self): _meta = {"quantity": "Current"} h5file = make_simple_nsid_dataset(_meta) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset.quantity == 'Current')
def test_attrs_units(self) -> None: _meta = {"units": "nA"} h5file = make_simple_nsid_dataset(_meta) dset = read_h5py_dataset(h5file['MyGroup']['data']) self.assertIsInstance(dset, sidpy.Dataset) self.assertTrue(dset.units == 'nA')
def test_wrong_input_type(self) -> None: dataset = make_simple_h5_dataset() err_msg = 'can only read single Dataset' with self.assertRaises(TypeError) as context: _ = read_h5py_dataset(dataset) self.assertTrue(err_msg in str(context.exception))