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

Allow tables in arbitrarily deep hdf5 groups #1068

Merged
merged 2 commits into from
May 20, 2019
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
33 changes: 21 additions & 12 deletions ctapipe/io/hdf5tableio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementations of TableWriter and -Reader for HDF5 files"""
import enum
from functools import partial
from pathlib import PurePath

import numpy as np
import tables
Expand Down Expand Up @@ -78,7 +79,13 @@ class HDF5TableWriter(TableWriter):
"""

def __init__(
self, filename, group_name, add_prefix=False, mode="w", root_uep="/", **kwargs
self,
filename,
group_name="",
add_prefix=False,
mode="w",
root_uep="/",
**kwargs,
):

super().__init__(add_prefix=add_prefix)
Expand All @@ -91,14 +98,7 @@ def __init__(
kwargs.update(mode=mode, root_uep=root_uep)

self.open(filename, **kwargs)

if root_uep + group_name in self._h5file:

self._group = self._h5file.get_node(root_uep + group_name)

else:

self._group = self._h5file.create_group(root_uep, group_name)
self._group = "/" + group_name

self.log.debug("h5file: %s", self._h5file)

Expand Down Expand Up @@ -205,20 +205,29 @@ def transform(enum_value):
def _setup_new_table(self, table_name, containers):
""" set up the table. This is called the first time `write()`
is called on a new table """
self.log.debug("Initializing table '%s'", table_name)
self.log.debug("Initializing table '%s' in group '%s'", table_name, self._group)
meta = self._create_hdf5_table_schema(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

for container in containers:
meta.update(container.meta) # copy metadata from container

table = self._h5file.create_table(
where=self._group,
name=table_name,
where=table_group,
name=table_basename,
title="Storage of {}".format(
",".join(c.__class__.__name__ for c in containers)
),
description=self._schemas[table_name],
createparents=True,
)
self.log.debug("CREATED TABLE: %s", table)
for key, val in meta.items():
table.attrs[key] = val

Expand Down
Loading