Skip to content

Commit

Permalink
Merge pull request #2650 from cta-observatory/reference_reprs
Browse files Browse the repository at this point in the history
Update / add reprs for reference metadata objects
  • Loading branch information
kosack authored Nov 18, 2024
2 parents 329fb3b + be7f480 commit 1783546
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
3 changes: 3 additions & 0 deletions docs/changes/2650.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add ``__repr__`` methods to all objects that were missing
them in ``ctapipe.io.metadata``, update the existing ones
for consistency.
54 changes: 49 additions & 5 deletions src/ctapipe/io/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import os
import uuid
import warnings
from collections import OrderedDict, defaultdict
from collections import defaultdict
from contextlib import ExitStack

import tables
Expand Down Expand Up @@ -95,7 +95,12 @@ def default_name(self):
return ""

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


class Product(HasTraits):
Expand Down Expand Up @@ -133,6 +138,19 @@ def default_product_id(self):
"""default id is a UUID"""
return str(uuid.uuid4())

def __repr__(self):
return (
f"{self.__class__.__name__}("
f"id_='{self.id_}'"
f", description='{self.description}'"
f", creation_time='{self.creation_time.utc.isot}Z'"
f", data_category='{self.data_category}'"
f", data_levels='{','.join(dl.name for dl in self.data_levels)}'"
f", data_model_version='{self.data_model_version}'"
f", format='{self.format}'"
")"
)


class Process(HasTraits):
"""Process (top-level workflow) information"""
Expand All @@ -141,6 +159,15 @@ class Process(HasTraits):
subtype = Unicode("")
id_ = Unicode("")

def __repr__(self):
return (
f"{self.__class__.__name__}("
f"id_='{self.id_}'"
f", type_='{self.type_}'"
f", subtype='{self.subtype}'"
")"
)


class Activity(HasTraits):
"""Activity (tool) information"""
Expand Down Expand Up @@ -172,6 +199,23 @@ def default_time(self):
"""default time is now"""
return Time.now().iso

def __repr__(self):
if self.stop_time is not None:
stop_time = f"'{self.stop_time.utc.isot}Z'"
else:
stop_time = None
return (
f"{self.__class__.__name__}("
f"name='{self.name}'"
f", id_='{self.id_}'"
f", type_='{self.type_}'"
f", start_time='{self.start_time.utc.isot}Z'"
f", stop_time={stop_time}"
f", software_name='{self.software_name}'"
f", software_version='{self.software_version}'"
")"
)


class Instrument(Configurable):
"""Instrumental Context"""
Expand Down Expand Up @@ -208,8 +252,8 @@ class Instrument(Configurable):
def __repr__(self):
return (
f"{self.__class__.__name__}("
f"site={self.site}, class_={self.class_}, type_={self.type_}"
f", subtype={self.subtype}, version={self.version}, id_={self.id_}"
f"site='{self.site}', class_='{self.class_}', type_='{self.type_}'"
f", subtype='{self.subtype}', version='{self.version}', id_='{self.id_}'"
")"
)

Expand Down Expand Up @@ -253,7 +297,7 @@ def to_dict(self, fits=False):
"""
prefix = "CTA " if fits is False else "HIERARCH CTA "

meta = OrderedDict({prefix + "REFERENCE VERSION": "1"})
meta = {prefix + "REFERENCE VERSION": "1"}
meta.update(_to_dict(self.contact, prefix=prefix + "CONTACT "))
meta.update(_to_dict(self.product, prefix=prefix + "PRODUCT "))
meta.update(_to_dict(self.process, prefix=prefix + "PROCESS "))
Expand Down
9 changes: 9 additions & 0 deletions src/ctapipe/io/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ def test_read_hdf5_metadata(tmp_path):
metadata_out = meta.read_hdf5_metadata(file, path=metadata_path)

assert metadata_out == metadata_in


def test_reprs(reference):
assert isinstance(repr(reference), str)
assert isinstance(repr(reference.activity), str)
assert isinstance(repr(reference.product), str)
assert isinstance(repr(reference.contact), str)
assert isinstance(repr(reference.instrument), str)
assert isinstance(repr(reference.process), str)

0 comments on commit 1783546

Please sign in to comment.