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

Configurable contact information for DataWriter #1757

Merged
merged 2 commits into from
Jul 23, 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
23 changes: 13 additions & 10 deletions ctapipe/io/datawriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pathlib
from collections import defaultdict
from typing import DefaultDict, Tuple
from traitlets import Instance

import numpy as np
import tables
Expand All @@ -20,11 +21,11 @@
from ..core import Component, Container, Field, Provenance, ToolConfigurationError
from ..core.traits import Bool, CaselessStrEnum, Float, Int, Path, Unicode
from ..instrument import SubarrayDescription
from ..io import EventSource, HDF5TableWriter, TableWriter
from ..io import metadata as meta
from ..io.datalevels import DataLevel
from ..io.simteleventsource import SimTelEventSource
from ..io.tableio import FixedPointColumnTransform, TelListToMaskTransform
from . import EventSource, HDF5TableWriter, TableWriter
from . import metadata as meta
from .datalevels import DataLevel
from .simteleventsource import SimTelEventSource
from .tableio import FixedPointColumnTransform, TelListToMaskTransform

__all__ = ["DataWriter", "DATA_MODEL_VERSION", "write_reference_metadata_headers"]

Expand All @@ -41,7 +42,7 @@


def write_reference_metadata_headers(
obs_ids, subarray, writer, is_simulation, data_levels
obs_ids, subarray, writer, is_simulation, data_levels, contact_info
):
"""
Attaches Core Provenence headers to an output HDF5 file.
Expand All @@ -65,7 +66,7 @@ def write_reference_metadata_headers(
category = "Sim" if is_simulation else "Other"

reference = meta.Reference(
contact=meta.Contact(name="", email="", organization="CTA Consortium"),
contact=contact_info,
product=meta.Product(
description="ctapipe Data Product",
data_category=category,
Expand Down Expand Up @@ -113,6 +114,7 @@ class DataWriter(Component):
"""

# pylint: disable=too-many-instance-attributes
contact_info = Instance(meta.Contact, kw={}).tag(config=True)

output_path = Path(
help="output filename", default_value=pathlib.Path("events.dl1.h5")
Expand Down Expand Up @@ -191,10 +193,9 @@ def __init__(self, event_source: EventSource, config=None, parent=None, **kwargs
"""
super().__init__(config=config, parent=parent, **kwargs)

# here we just set up data, but all real initializtion should be in
# setup(), which is called when the first event is read.

self.event_source = event_source
self.contact_info = meta.Contact(parent=self)

self._at_least_one_event = False
self._is_simulation = event_source.is_simulation
self._subarray: SubarrayDescription = event_source.subarray
Expand Down Expand Up @@ -258,12 +259,14 @@ def finish(self):
if self._writer:
if self.write_index_tables:
self._generate_indices()

write_reference_metadata_headers(
subarray=self._subarray,
obs_ids=self.event_source.obs_ids,
writer=self._writer,
is_simulation=self._is_simulation,
data_levels=self.datalevels,
contact_info=self.contact_info,
)
self._writer.close()
self._writer = None
Expand Down
18 changes: 13 additions & 5 deletions ctapipe/io/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

from astropy.time import Time
from tables import NaturalNameWarning
from traitlets import Enum, HasTraits, Instance, List, Unicode, default
from traitlets import Enum, Instance, List, Unicode, default, HasTraits
from traitlets.config import Configurable

from ..core.traits import AstroTime
from .datalevels import DataLevel
Expand All @@ -47,12 +48,15 @@
CONVERSIONS = {Time: lambda t: t.utc.iso, list: str}


class Contact(HasTraits):
class Contact(Configurable):
""" Contact information """

name = Unicode("unknown")
email = Unicode("unknown")
organization = Unicode("unknown")
name = Unicode("unknown").tag(config=True)
email = Unicode("unknown").tag(config=True)
organization = Unicode("unknown").tag(config=True)

def __repr__(self):
return f"Contact(name={self.name}, email={self.email}, organization={self.organization})"


class Product(HasTraits):
Expand Down Expand Up @@ -165,7 +169,11 @@ def _to_dict(hastraits_instance, prefix=""):
"""
res = {}

ignore = {"parent", "config"}
for k, trait in hastraits_instance.traits().items():
if k in ignore:
continue

key = (prefix + k.upper().replace("_", " ")).replace(" ", " ").strip()
val = trait.get(hastraits_instance)

Expand Down
37 changes: 37 additions & 0 deletions ctapipe/io/tests/test_datawriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import tables
from traitlets.config import Config
from astropy import units as u
from ctapipe.calib import CameraCalibrator
from ctapipe.instrument import SubarrayDescription
Expand Down Expand Up @@ -213,3 +214,39 @@ def test_dl1writer_no_events(tmpdir: Path):
with tables.open_file(output_path) as h5file:
assert h5file.get_node("/configuration/simulation/run") is not None
assert h5file.get_node("/simulation/service/shower_distribution") is not None


def test_metadata(tmpdir: Path):
output_path = Path(tmpdir / "metadata.dl1.h5")

dataset = "lst_prod3_calibration_and_mcphotons.simtel.zst"

config = Config(
{
"DataWriter": {
"Contact": {
"name": "Maximilian Nöthe",
"email": "[email protected]",
"organization": "TU Dortmund",
}
}
}
)

with EventSource(get_dataset_path(dataset)) as source:
with DataWriter(
event_source=source,
output_path=output_path,
write_parameters=True,
write_images=True,
config=config,
):
pass

assert output_path.exists()

with tables.open_file(output_path) as h5file:
meta = h5file.root._v_attrs
assert meta["CTA CONTACT NAME"] == "Maximilian Nöthe"
assert meta["CTA CONTACT EMAIL"] == "[email protected]"
assert meta["CTA CONTACT ORGANIZATION"] == "TU Dortmund"
5 changes: 5 additions & 0 deletions examples/stage1_config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"DataWriter": {
"Contact": {
"name": "YOUR NAME HERE",
"email": "[email protected]",
"organization": "CTA Consortium"
},
"overwrite": false,
"write_images": true,
"write_parameters": true,
Expand Down