Skip to content

Commit

Permalink
Fix joining of paths in hdf file (was OS-dependent)
Browse files Browse the repository at this point in the history
  • Loading branch information
loichuder committed Oct 22, 2021
1 parent f44bc76 commit 68cd2b1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
7 changes: 4 additions & 3 deletions h5grove/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
attr_metadata,
get_array_stats,
get_entity_from_file,
hdf_path_join,
parse_slice,
sorted_dict,
)
Expand Down Expand Up @@ -189,9 +190,9 @@ def __init__(self, path: str, h5py_entity: h5py.Group, h5file: h5py.File):

def _get_child_metadata_content(self, depth=0):
return [
create_content(self._h5file, os.path.join(self._path, child_path)).metadata(
depth
)
create_content(
self._h5file, hdf_path_join(self._path, child_path)
).metadata(depth)
for child_path in self._h5py_entity.keys()
]

Expand Down
7 changes: 7 additions & 0 deletions h5grove/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ def get_array_stats(data: np.ndarray) -> Dict[str, Union[float, int, None]]:
"mean": cast(np.mean(data)),
"std": cast(np.std(data)),
}


def hdf_path_join(prefix, suffix):
if prefix == "/":
return f"/{suffix}"

return f'{prefix.rstrip("/")}/{suffix}'
13 changes: 13 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from h5grove.utils import hdf_path_join


def test_root_path_join():
assert hdf_path_join("/", "child") == "/child"


def test_group_path_join():
assert hdf_path_join("/group1/group2", "data") == "/group1/group2/data"


def test_group_path_join_trailing():
assert hdf_path_join("/group1/group2/", "data") == "/group1/group2/data"

0 comments on commit 68cd2b1

Please sign in to comment.