Skip to content

Commit

Permalink
Merge pull request #2319 from cta-observatory/hdf5_path_handling
Browse files Browse the repository at this point in the history
Do not use pathlib for hdf5 datasets, fixes #2318
  • Loading branch information
maxnoe committed May 10, 2023
2 parents 17d07c4 + ae4a2af commit 062fd96
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
16 changes: 1 addition & 15 deletions ctapipe/io/hdf5merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..instrument.subarray import SubarrayDescription
from ..utils.arrays import recarray_drop_columns
from . import metadata
from .hdf5tableio import DEFAULT_FILTERS, get_column_attrs, get_node_meta
from .hdf5tableio import DEFAULT_FILTERS, get_column_attrs, get_node_meta, split_h5path


class NodeType(enum.Enum):
Expand Down Expand Up @@ -78,20 +78,6 @@ class CannotMerge(IOError):
"""Raised when trying to merge incompatible files"""


def split_h5path(path):
"""
Split a path inside an hdf5 file into parent / child
"""
if not path.startswith("/"):
raise ValueError("Path must start with /")

head, _, tail = path.rstrip("/").rpartition("/")
if head == "":
head = "/"

return head, tail


class HDF5Merger(Component):
"""
Class to copy / append / merge ctapipe hdf5 files
Expand Down
22 changes: 17 additions & 5 deletions ctapipe/io/hdf5tableio.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
TimeColumnTransform,
)

__all__ = ["HDF5TableWriter", "HDF5TableReader"]
__all__ = ["HDF5TableWriter", "HDF5TableReader", "split_h5path"]

PYTABLES_TYPE_MAP = {
"float": tables.Float64Col,
Expand Down Expand Up @@ -48,6 +48,20 @@
)


def split_h5path(path):
"""
Split a path inside an hdf5 file into parent / child
"""
if not path.startswith("/"):
raise ValueError("Path must start with /")

head, _, tail = path.rstrip("/").rpartition("/")
if head == "":
head = "/"

return head, tail


def get_hdf5_attr(attrs, name, default=None):
if name in attrs:
return attrs[name]
Expand Down Expand Up @@ -390,10 +404,8 @@ def _setup_new_table(self, table_name, containers):
if table_name.startswith("/"):
raise ValueError("Table name must not start with '/'")

table_path = PurePath(self._group) / PurePath(table_name)
table_group = str(table_path.parent)
table_basename = table_path.stem
table_path = str(table_path)
table_path = f"{self._group.rstrip('/')}/{table_name}"
table_group, table_basename = split_h5path(table_path)

for container in containers:
meta.update(container.meta) # copy metadata from container
Expand Down
1 change: 1 addition & 0 deletions docs/changes/2319.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix HDF5Writer not working on windows due to using pathlib for hdf5 dataset names.

0 comments on commit 062fd96

Please sign in to comment.