def test_read_attribs_as_scalars(self): d_exp = {"foo": 3} fname = os.path.join(temp_path, "test_read_scalar_attribs.hdf5") with h5py.File(fname, "w") as f: f.attrs.create("foo", d_exp["foo"]) with h5py.File(fname, "r") as f: d = read_namespace_hierarchy(f) self.assertTrue(hasattr(d, "foo")) self.assertAlmostEqual(d.foo, d_exp["foo"])
def setUp(self): self.fname = os.path.join(temp_path, "test_read_seq.hdf5") self.n = 4 with h5py.File(self.fname, "w") as f: f.create_group("s") f["s"].attrs.create("_special_type", "set") f["s"].attrs.create("_len", self.n) f["s"].attrs.create("_0", 1) f["s"].attrs.create("_2", 2) f["s"].attrs.create("_4", 3) with h5py.File(self.fname, "r") as f: self.loaded = read_namespace_hierarchy(f)
def test_read_flat(self): d_exp = {"foo": np.array([-0.1, 5]), "bar": [1, 0.2, -3]} fname = os.path.join(temp_path, "test_read_flat.hdf5") with h5py.File(fname, "w") as f: f.create_dataset("foo", data=d_exp["foo"]) f.create_dataset("bar", data=d_exp["bar"]) with h5py.File(fname, "r") as f: d = read_namespace_hierarchy(f) self.assertTrue(hasattr(d, "foo")) self.assertTrue(hasattr(d, "bar")) np.testing.assert_allclose(d.foo, d_exp["foo"]) np.testing.assert_allclose(d.bar, d_exp["bar"])
def test_hierarchy(self): foo = [1, 2, 0.5] subfoo = [3, 2, 1] fname = os.path.join(temp_path, "test_read_hierarchical.hdf5") with h5py.File(fname, "w") as f: f.create_dataset("foo", data=foo) f_sub = f.create_group("sub") f_sub.create_dataset("subfoo", data=subfoo) with h5py.File(fname, "r") as f: d = read_namespace_hierarchy(f) self.assertTrue(hasattr(d, "foo")) self.assertTrue(hasattr(d, "sub")) self.assertTrue(hasattr(d.sub, "subfoo")) np.testing.assert_allclose(d.sub.subfoo, subfoo)
def test_conflict_attrib_name_with_dataset_name_with_and_without_attr( self): foo = [1, 2, 3] dataset_attr_foo = [2, 3, 1] attr_foo = -3.5 fname = os.path.join(temp_path, "test_read_scalar_attribs_conflict.hdf5") with h5py.File(fname, "w") as f: f.create_dataset("foo", data=foo) f.create_dataset("attr_foo", data=dataset_attr_foo) f.attrs.create("foo", attr_foo) with h5py.File(fname, "r") as f: d = read_namespace_hierarchy(f) self.assertTrue(hasattr(d, "attr_foo")) np.testing.assert_allclose(d.attr_foo, dataset_attr_foo)
def setUp(self): self.fname = os.path.join(temp_path, "test_read_seq.hdf5") self.seq = [3, "foo"] self.foo_attr = "fooooo" self.foo_dataset = np.array([-0.5, 0.0, 0.5]) with h5py.File(self.fname, "w") as f: f.create_group("s") f["s"].attrs.create("_special_type", "set") f["s"].attrs.create("_len", len(self.seq)) f["s"].attrs.create("_0", self.seq[0]) f["s"].attrs.create("_1", self.seq[1]) f["s"].attrs.create("foo_attr", self.foo_attr) f["s"].create_dataset("foo_dataset", data=self.foo_dataset) with h5py.File(self.fname, "r") as f: self.loaded = read_namespace_hierarchy(f)
def setUp(self): self.fname = os.path.join(temp_path, "test_read_seq.hdf5") self.seq = [3, "foo", np.array([1, 2, 5])] self.extra_attr = 5.0 self.extra_dataset = np.array([0.0, 0.5, 1.0]) with h5py.File(self.fname, "w") as f: for name, t in {"tpl": "tuple", "lst": "list"}.items(): f.create_group(name) f[name].attrs.create("_special_type", t) f[name].attrs.create("_len", len(self.seq)) f[name].attrs.create("_0", self.seq[0]) f[name].attrs.create("_1", self.seq[1]) f[name].create_dataset("_2", data=self.seq[2]) f[name].attrs.create("foo", self.extra_attr) f[name].create_dataset("bar", data=self.extra_dataset) with h5py.File(self.fname, "r") as f: self.loaded = read_namespace_hierarchy(f)
def setUp(self): self.fname = os.path.join(temp_path, "test_read_dict.hdf5") self.d = {"foo": "foo2", "bar": 3.5, 3: np.array([2, 3]), (2, 3): 5.0} self.foo_attr = "bar" self.foo_dataset = np.arange(5) with h5py.File(self.fname, "w") as f: f.create_group("d") f["d"].attrs.create("_special_type", "dict") f["d"].attrs.create("_len", len(self.d)) for i in range(len(self.d)): f["d"].create_group(f"_{i}") f[f"d/_{i}"].attrs.create("_special_type", "tuple") f[f"d/_{i}"].attrs.create("_len", 2) f["d/_0"].attrs.create("_0", "foo") f["d/_0"].attrs.create("_1", "foo2") f["d/_1"].attrs.create("_0", "bar") f["d/_1"].attrs.create("_1", 3.5) f["d/_2"].attrs.create("_0", 3) f["d/_2"].create_dataset("_1", data=self.d[3]) f["d/_3"].create_group("_0") f["d/_3/_0"].attrs.create("_special_type", "tuple") f["d/_3/_0"].attrs.create("_len", 2) f["d/_3/_0"].attrs.create("_0", 2) f["d/_3/_0"].attrs.create("_1", 3) f["d/_3"].attrs.create("_1", 5.0) f["d"].attrs.create("foo_attr", self.foo_attr) f["d"].create_dataset("foo_dataset", data=self.foo_dataset) with h5py.File(self.fname, "r") as f: self.loaded = read_namespace_hierarchy(f)