Skip to content

Commit

Permalink
Merge pull request #2261 from cta-observatory/fix_activity_meta
Browse files Browse the repository at this point in the history
Fix wrong activity meta being written into output files
  • Loading branch information
maxnoe committed Mar 16, 2023
2 parents c169e25 + 21a1727 commit 3bf6b17
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
41 changes: 29 additions & 12 deletions ctapipe/core/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def start_activity(self, activity_name=sys.executable):
self._activities.append(activity)
log.debug(f"started activity: {activity_name}")

def _get_current_or_start_activity(self):
if self.current_activity is None:
log.info(
"No activity has been explicitly started, starting new default activity."
" Consider calling Provenance().start_activity(<name>) explicitly."
)
self.start_activity()

return self.current_activity

def add_input_file(self, filename, role=None):
"""register an input to the current activity
Expand All @@ -96,11 +106,12 @@ def add_input_file(self, filename, role=None):
role: str
role this input file satisfies (optional)
"""
self.current_activity.register_input(abspath(filename), role=role)
activity = self._get_current_or_start_activity()
activity.register_input(abspath(filename), role=role)
log.debug(
"added input entity '{}' to activity: '{}'".format(
filename, self.current_activity.name
)
"added input entity '%s' to activity: '%s'",
filename,
activity.name,
)

def add_output_file(self, filename, role=None):
Expand All @@ -115,11 +126,12 @@ def add_output_file(self, filename, role=None):
role this output file satisfies (optional)
"""
self.current_activity.register_output(abspath(filename), role=role)
activity = self._get_current_or_start_activity()
activity.register_output(abspath(filename), role=role)
log.debug(
"added output entity '{}' to activity: '{}'".format(
filename, self.current_activity.name
)
"added output entity '%s' to activity: '%s'",
filename,
activity.name,
)

def add_config(self, config):
Expand All @@ -131,7 +143,13 @@ def add_config(self, config):
config: dict
configuration parameters
"""
self.current_activity.register_config(config)
activity = self._get_current_or_start_activity()
activity.register_config(config)
log.debug(
"added config entity '%s' to activity: '%s'",
config,
activity.name,
)

def finish_activity(self, status="completed", activity_name=None):
"""end the current activity"""
Expand All @@ -156,9 +174,8 @@ def activity(self, name):
@property
def current_activity(self):
if len(self._activities) == 0:
log.debug("No activity has been started... starting a default one")
self.start_activity()
return self._activities[-1] # current activity as at the top of stack
return None
return self._activities[-1] # current activity is at the top of stack

@property
def finished_activities(self):
Expand Down
10 changes: 7 additions & 3 deletions ctapipe/io/datawriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ def write_reference_metadata_headers(
instrument_info: meta.Instrument
instrument metadata
"""
activity = PROV.current_activity.provenance
category = "Sim" if is_simulation else "Other"
activity = PROV.current_activity
if activity is None and len(PROV.finished_activities) > 0:
# assume that we write provenance for a "just finished activity"
activity = PROV.finished_activities[-1]

activity_meta = meta.Activity.from_provenance(activity.provenance)
category = "Sim" if is_simulation else "Other"
reference = meta.Reference(
contact=contact_info,
product=meta.Product(
Expand All @@ -129,7 +133,7 @@ def write_reference_metadata_headers(
subtype="",
id_=",".join(str(x) for x in obs_ids),
),
activity=meta.Activity.from_provenance(activity),
activity=activity_meta,
instrument=instrument_info,
)

Expand Down
2 changes: 2 additions & 0 deletions ctapipe/io/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def from_provenance(cls, activity):
type_="software",
id_=activity["activity_uuid"],
start_time=activity["start"]["time_utc"],
stop_time=activity["stop"].get("time_utc", Time.now()),
software_name="ctapipe",
software_version=activity["system"]["ctapipe_version"],
)
Expand All @@ -157,6 +158,7 @@ def from_provenance(cls, activity):
type_ = Unicode("software")
id_ = Unicode()
start_time = AstroTime()
stop_time = AstroTime(allow_none=True, default_value=None)
software_name = Unicode("unknown")
software_version = Unicode("unknown")

Expand Down
1 change: 1 addition & 0 deletions ctapipe/tools/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def _info_system():
print("\n*** ctapipe system environment ***\n")

prov = Provenance()
prov.start_activity("ctapipe-info")
system_prov = prov.current_activity.provenance["system"]

for section in ["platform", "python"]:
Expand Down
1 change: 1 addition & 0 deletions docs/changes/2261.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure the correct activity metadata is written into output files.

0 comments on commit 3bf6b17

Please sign in to comment.