Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix joining of paths in hdf file (was OS-dependent) #42

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions h5grove/content.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Dict, Generic, Optional, Sequence, TypeVar, Union
import h5py
import numpy as np
import os

from .models import Selection

Expand All @@ -13,6 +12,7 @@
attr_metadata,
get_array_stats,
get_entity_from_file,
hdf_path_join,
parse_slice,
sorted_dict,
)
Expand Down Expand Up @@ -189,9 +189,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"