Example #1
0
    def setUp(self):
        self.sub_obj = SubObject(np.array([3.5, -0.5]))
        self.obj = AnObject("bar", 5.2, self.sub_obj)
        self.fname = os.path.join(temp_path, "test_obj_write.hdf5")

        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.obj)
Example #2
0
    def setUp(self):
        self.obj = SimpleNamespace(n_tpl=(1, 2, 3),
                                   v_tpl=("foo", 3),
                                   n_lst=[0, 1, 2],
                                   v_lst=["bar", 2.0])

        self.fname = os.path.join(temp_path,
                                  "test_obj_write_tuple_or_list.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.obj)
Example #3
0
    def test_writing_bool_array_works(self):
        foo = np.array([True, False, False, True])

        fname = os.path.join(temp_path, "test_obj_write_bool.hdf5")
        with h5py.File(fname, "w") as f:
            write_object_hierarchy(f, SimpleNamespace(foo=foo))

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f)
            np.testing.assert_allclose(f["foo"][()], foo)
Example #4
0
    def test_writing_non_numeric_array_yields_list(self):
        foo = np.array([None, (2, 3), 0.3], dtype=object)

        fname = os.path.join(temp_path,
                             "test_obj_write_non_numeric_array.hdf5")
        with h5py.File(fname, "w") as f:
            write_object_hierarchy(f, SimpleNamespace(foo=foo))

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f)
            self.assertTrue(isinstance(f["foo"], h5py.Group))
            self.assertEqual(f["foo"].attrs["_len"], len(foo))
Example #5
0
    def setUp(self):
        class NonStdSequence:
            def __len__(self):
                return 2

            def __getitem__(self, item: int) -> int:
                if item < len(self):
                    return item**2
                else:
                    raise IndexError()

        self.obj = SimpleNamespace(sub=NonStdSequence())

        self.fname = os.path.join(temp_path, "test_obj_write_non_std_seq.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.obj)
Example #6
0
    def setUp(self):
        class NoWriteSequence:
            def __init__(self):
                self.hdf_skip_contents = True
                self.foo = "foo"
                self.bar = np.asarray([0, 1, 1])

            def __getitem__(self, idx):
                if idx < 3:
                    return idx**3
                else:
                    raise IndexError

        self.seq = NoWriteSequence()
        self.fname = os.path.join(temp_path, "test_write_skip_contents.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.seq)
Example #7
0
    def test_silently_ignores_inaccessible_attributes(self):
        class AnotherObject:
            def __init__(self):
                self.foo = [1, 2, 3]

            @property
            def bar(self):
                raise KeyError

        obj = AnotherObject()
        fname = os.path.join(temp_path,
                             "test_obj_write_skip_inaccessible.hdf5")
        with h5py.File(fname, "w") as f:
            write_object_hierarchy(f, obj)

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f)
            self.assertNotIn("bar", f)
Example #8
0
    def setUp(self):
        class NonStdSequence:
            def __iter__(self):
                return NonStdIterator(self)

        class NonStdIterator:
            def __init__(self, obj):
                self._obj = obj
                self._idx = 0

            def __next__(self):
                if self._idx < 3:
                    value = self._idx**3
                    self._idx += 1
                    return value
                else:
                    raise StopIteration

        self.obj = SimpleNamespace(sub=NonStdSequence())

        self.fname = os.path.join(temp_path, "test_obj_write_non_std_seq.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.obj)
Example #9
0
def save_results(outfile: str, res: dict, force: bool):
    mode = "w" if force else "x"
    with h5py.File(outfile, mode) as f:
        write_object_hierarchy(f, res)
Example #10
0
    def setUp(self):
        self.obj = {1, 2, 0.3}

        self.fname = os.path.join(temp_path, "test_obj_write_set.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_object_hierarchy(f, self.obj)