Skip to content

Commit

Permalink
some more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-janssen committed Jun 6, 2024
1 parent d0ee66e commit 8796e4f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
22 changes: 12 additions & 10 deletions h5io_browser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,11 @@ def read_dict_from_hdf(
dict: The loaded data. Can be of any type supported by ``write_hdf5``.
"""
with h5py.File(file_name, "r") as hdf:
if recursive:
nodes_lst = _get_hdf_content(
hdf=hdf[h5_path], recursive=recursive, only_nodes=True
)
else:
nodes_lst = [h5_path]
nodes_lst = _get_hdf_content(
hdf=hdf[h5_path], recursive=recursive, only_nodes=True
)
if not recursive and len(nodes_lst) == 0:
nodes_lst += [h5_path]
if len(nodes_lst) > 0 and nodes_lst[0] != "/":
if not nested:
return {
Expand Down Expand Up @@ -174,12 +173,15 @@ def _get_nested_dict_item(key, value, h5_path):
dict: hierarchical dictionary
"""
groups = key[len(h5_path) :].split("/")
if len(groups) > 1 and groups[0] == "":
if len(groups) > 0 and groups[0] == "":
del groups[0]
nested_dict = value
for g in groups[::-1]:
nested_dict = {g: nested_dict}
return nested_dict
if len(groups) > 0:
for g in groups[::-1]:
nested_dict = {g: nested_dict}
return nested_dict
else:
return {key.split("/")[-1]: nested_dict}


def _merge_nested_dict(main_dict, add_dict):
Expand Down
65 changes: 47 additions & 18 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ def test_read_dict_hierarchical(self):
file_name=self.file_name, h5_path=self.h5_path, recursive=True
),
)
self.assertEqual(
self.data_hierarchical,
read_dict_from_hdf(
file_name=self.file_name, h5_path=self.h5_path, recursive=1
),
)
self.assertEqual({}, read_dict_from_hdf(file_name=self.file_name, h5_path="/"))
self.assertEqual(
{
k: v
for k, v in self.data_hierarchical.items()
if "/data_hierarchical/c" in k
},
read_dict_from_hdf(
file_name=self.file_name,
h5_path=posixpath.join(self.h5_path, "c"),
recursive=True,
),
)
self.assertEqual(
{"/data_hierarchical/a": [1, 2]},
read_dict_from_hdf(
file_name=self.file_name, h5_path=posixpath.join(self.h5_path, "a")
),
)
self.assertEqual(
{"/data_hierarchical/b": 3},
read_dict_from_hdf(
file_name=self.file_name, h5_path=posixpath.join(self.h5_path, "b")
),
)

def test_read_dict_hierarchical_nested(self):
self.assertEqual(
{"a": [1, 2], "b": 3, "c": {"d": 4, "e": 5}},
read_dict_from_hdf(
Expand Down Expand Up @@ -103,34 +136,30 @@ def test_read_dict_hierarchical(self):
),
)
self.assertEqual(
self.data_hierarchical,
read_dict_from_hdf(
file_name=self.file_name, h5_path=self.h5_path, recursive=1
),
)
self.assertEqual({}, read_dict_from_hdf(file_name=self.file_name, h5_path="/"))
self.assertEqual(
{
k: v
for k, v in self.data_hierarchical.items()
if "/data_hierarchical/c" in k
},
{'a': [1, 2], 'b': 3},
read_dict_from_hdf(
file_name=self.file_name,
h5_path=posixpath.join(self.h5_path, "c"),
recursive=True,
h5_path=self.h5_path,
recursive=False,
nested=True,
),
)
self.assertEqual(
{"/data_hierarchical/a": [1, 2]},
{'a': [1, 2]},
read_dict_from_hdf(
file_name=self.file_name, h5_path=posixpath.join(self.h5_path, "a")
file_name=self.file_name,
h5_path=posixpath.join(self.h5_path, "a"),
recursive=False,
nested=True,
),
)
self.assertEqual(
{"/data_hierarchical/b": 3},
{'b': 3},
read_dict_from_hdf(
file_name=self.file_name, h5_path=posixpath.join(self.h5_path, "b")
file_name=self.file_name,
h5_path=posixpath.join(self.h5_path, "b"),
recursive=False,
nested=True,
),
)

Expand Down

0 comments on commit 8796e4f

Please sign in to comment.