Пример #1
0
    def test_disable_scalars_as_attribs(self):
        d = {"foo": 3}
        fname = os.path.join(temp_path,
                             "test_write_scalar_as_not_attribs.hdf5")
        with h5py.File(fname, "w") as f:
            write_dict_hierarchy(f, d, scalars_as_attribs=False)

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f)
            np.testing.assert_allclose(f["foo"][()], d["foo"])
Пример #2
0
    def test_scalars_as_attribs_by_default(self):
        d = {"foo": 3}
        fname = os.path.join(temp_path, "test_write_scalar_attribs.hdf5")
        with h5py.File(fname, "w") as f:
            write_dict_hierarchy(f, d)

        with h5py.File(fname, "r") as f:
            self.assertNotIn("foo", f)
            self.assertIn("foo", f.attrs)
            self.assertAlmostEqual(f.attrs["foo"], d["foo"])
Пример #3
0
    def test_write_string_when_scalars_as_attribs_is_true(self):
        d = {"foo": "bar"}

        fname = os.path.join(temp_path, "test_write_str.hdf5")
        with h5py.File(fname, "w") as f:
            write_dict_hierarchy(f, d, scalars_as_attribs=True)

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f.attrs)
            self.assertEqual(f.attrs["foo"].decode(), "bar")
Пример #4
0
    def test_hierarchy(self):
        d1 = {"subfoo": [1, 2.3]}
        d = {"d1": d1}
        fname = os.path.join(temp_path, "test_write_hierarchical.hdf5")
        with h5py.File(fname, "w") as f:
            write_dict_hierarchy(f, d, scalars_as_attribs=False)

        with h5py.File(fname, "r") as f:
            self.assertIn("d1", f)
            self.assertIn("subfoo", f["d1"])
            np.testing.assert_allclose(f["d1/subfoo"][()], d1["subfoo"])
Пример #5
0
    def setUp(self):
        self.d1 = {"foo": [1, 2, 3], "bar": 0.3}
        self.d = {
            "foo": [0.2, 0.3],
            "bar": [0.1, 0.2, 0.3],
            "foobar": "foobar",
            "dict": self.d1,
        }

        self.fname = os.path.join(temp_path, "test_write_read_roundtrip.hdf5")
        with h5py.File(self.fname, "w") as f:
            write_dict_hierarchy(f, self.d, scalars_as_attribs=False)
Пример #6
0
    def test_write_flat(self):
        d = {"foo": np.array([1, 5]), "bar": [1, 2, 3]}
        fname = os.path.join(temp_path, "test_write_flat.hdf5")
        with h5py.File(fname, "w") as f:
            write_dict_hierarchy(f, d)

        with h5py.File(fname, "r") as f:
            self.assertIn("foo", f)
            self.assertIn("bar", f)

            np.testing.assert_allclose(f["foo"][()], d["foo"])
            np.testing.assert_allclose(f["bar"][()], d["bar"])