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

Add option single_ob to HDF5Merger for merging chunks of the same ob #2436

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
44 changes: 32 additions & 12 deletions src/ctapipe/io/hdf5merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@
True, help="Whether to include processing statistics in merged output"
).tag(config=True)

single_ob = traits.Bool(
False,
help=(
"If true, input files are assumed to be multiple chunks from the same"
" observation block and the ob / sb blocks will only be copied from "
" the first input file"
),
).tag(config=True)

def __init__(self, output_path=None, **kwargs):
# enable using output_path as posarg
if output_path not in {None, traits.Undefined}:
Expand Down Expand Up @@ -207,6 +216,8 @@
# this will update _merged_obs_ids from existing input file
self._check_obs_ids(self.h5file)

self._n_merged = 0

def __call__(self, other: str | Path | tables.File):
"""
Append file ``other`` to the output file
Expand All @@ -217,7 +228,7 @@

with exit_stack:
# first file to be merged
if self.meta is None:
if self._n_merged == 0:
self.meta = self._read_meta(other)
self.data_model_version = self.meta.product.data_model_version
metadata.write_to_hdf5(self.meta.to_dict(), self.h5file)
Expand All @@ -233,6 +244,7 @@
self.log.info(
"Updated required nodes to %s", sorted(self.required_nodes)
)
self._n_merged += 1
finally:
self._update_meta()

Expand Down Expand Up @@ -288,26 +300,34 @@
f" check for duplicated obs_ids. Tried: {keys}"
)

duplicated = self._merged_obs_ids.intersection(obs_ids)
if len(duplicated) > 0:
msg = f"Input file {other.filename} contains obs_ids already included in output file: {duplicated}"
raise CannotMerge(msg)
if self.single_ob and len(self._merged_obs_ids) > 0:
different = self._merged_obs_ids.symmetric_difference(obs_ids)
if len(different) > 0:
msg = f"Input file {other.filename} contains different obs_ids than already merged ({self._merged_obs_ids}) for single_ob=True: {different}"
raise CannotMerge(msg)
else:
duplicated = self._merged_obs_ids.intersection(obs_ids)
if len(duplicated) > 0:
msg = f"Input file {other.filename} contains obs_ids already included in output file: {duplicated}"
raise CannotMerge(msg)

self._merged_obs_ids.update(obs_ids)

def _append(self, other):

Check failure on line 316 in src/ctapipe/io/hdf5merger.py

View check run for this annotation

CTAO-DPPS-SonarQube / ctapipe Sonarqube Results

src/ctapipe/io/hdf5merger.py#L316

Refactor this function to reduce its Cognitive Complexity from 55 to the 15 allowed.
self._check_obs_ids(other)

# Configuration
self._append_subarray(other)

config_keys = [
"/configuration/observation/scheduling_block",
"/configuration/observation/observation_block",
]
for key in config_keys:
if key in other.root:
self._append_table(other, other.root[key])
# in case of "single_ob", we only copy sb/ob blocks for the first file
if not self.single_ob or self._n_merged == 0:
config_keys = [
"/configuration/observation/scheduling_block",
"/configuration/observation/observation_block",
]
for key in config_keys:
if key in other.root:
self._append_table(other, other.root[key])

key = "/configuration/telescope/pointing"
if key in other.root:
Expand Down
4 changes: 4 additions & 0 deletions src/ctapipe/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class MergeTool(Tool):
}

flags = {
"single-ob": (
{"HDF5Merger": {"single_ob": True}},
"Only copy observation config of first file to be merged.",
),
"progress": (
{"MergeTool": {"progress_bar": True}},
"Show a progress bar for all given input files",
Expand Down
Loading