def loadmath5py(filename): """ Similar to loadmat function, except that this function import h5py package to read v7.3 mat file can only supply the <filename>, <filename> can be a string or a Path object Return, example see loadmat function To do: 1. consider to selective read in? """ from h5py import File import numpy as np from RZutilpy.system import Path filename = Path(filename) if ~isinstance(filename, Path) else filename print('read the file' + filename.str) Fileobject = File(filename.str, 'r') keys = [] values = [] mat_contend = {} for k, v in Fileobject.items(): keys.append(k) values.append(v) mat_contend[k] = np.array(v) #del keys[0:3] # remove first 3 information keys. #del values[0:3] # delete first 3 values return mat_contend, keys, values
class TestMapping(BaseTest): """ Test if the registration of Group as a Mapping behaves as expected """ def setUp(self): data = ('a', 'b') self.f = File('foo.hdf5', 'w') self.grp = self.f.create_group('bar') self.attr = self.f.attrs.create('x', data) def TearDown(self): if self.f: self.close() def test_keys(self): key_1 = self.f.keys() self.assertIsInstance(repr(key_1), str) key_2 = self.grp.keys() self.assertIsInstance(repr(key_2), str) def test_values(self): value_1 = self.f.values() self.assertIsInstance(repr(value_1), str) value_2 = self.grp.values() self.assertIsInstance(repr(value_2), str) def test_items(self): item_1 = self.f.items() self.assertIsInstance(repr(item_1), str) item_2 = self.grp.items() self.assertIsInstance(repr(item_1), str)
def show_file_contents(input_file): f = File(input_file, 'r') for k, v in f.items(): print(k, v)
def print_datasets_info(h5py_file: h5py.File) -> None: for dataset_name, dataset in h5py_file.items(): print(dataset)