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

JP-3773: Store logs from calibration pipeline in datamodel #8857

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 27 additions & 3 deletions jwst/stpipe/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
from functools import wraps
import logging
import warnings
import time

from stdatamodels.jwst.datamodels import JwstDataModel
from stdatamodels.jwst import datamodels
from stpipe import crds_client
from stpipe import Step
from stpipe import Pipeline
from stpipe import crds_client, Step, Pipeline

from .. import __version_commit__, __version__
from ..lib.suffix import remove_suffix

_LOG_FORMATTER = logging.Formatter(
"%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
_LOG_FORMATTER.converter = time.gmtime


log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand All @@ -25,6 +30,8 @@ class JwstStep(Step):
output_ext = string(default='.fits') # Output file type
"""

_log_records_formatter = _LOG_FORMATTER

@classmethod
def _datamodels_open(cls, init, **kwargs):
return datamodels.open(init, **kwargs)
Expand Down Expand Up @@ -92,6 +99,12 @@ def finalize_result(self, result, reference_files_used):
if self.parent is None:
log.info(f"Results used CRDS context: {result.meta.ref_file.crds.context_used}")

if hasattr(result, 'cal_logs'):
tmpdict = result.cal_logs.instance
else:
tmpdict = dict()
tmpdict[self.class_alias] = '\n'.join(self._log_records)
result.cal_logs = tmpdict
Comment on lines +102 to +107
Copy link
Collaborator

@braingram braingram Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if hasattr(result, 'cal_logs'):
tmpdict = result.cal_logs.instance
else:
tmpdict = dict()
tmpdict[self.class_alias] = '\n'.join(self._log_records)
result.cal_logs = tmpdict
if not hasattr(result, 'cal_logs'):
result.cal_logs = {}
setattr(result.cal_logs, self.class_alias, self._log_records)

Some code golf and switching the cal_log to a list of strings.


def remove_suffix(self, name):
return remove_suffix(name)
Expand Down Expand Up @@ -124,3 +137,14 @@ class JwstPipeline(Pipeline, JwstStep):
def finalize_result(self, result, reference_files_used):
if isinstance(result, JwstDataModel):
log.info(f"Results used CRDS context: {crds_client.get_context_used(result.crds_observatory)}")

if hasattr(result, 'cal_logs'):
tmpdict = result.cal_logs.instance
else:
tmpdict = dict()
tmpdict[self.class_alias] = '\n'.join(self._log_records)

for _, step in self.step_defs.items():
if step.class_alias in tmpdict.keys():
del tmpdict[step.class_alias]
result.cal_logs = tmpdict
Comment on lines +141 to +150
Copy link
Collaborator

@braingram braingram Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if hasattr(result, 'cal_logs'):
tmpdict = result.cal_logs.instance
else:
tmpdict = dict()
tmpdict[self.class_alias] = '\n'.join(self._log_records)
for _, step in self.step_defs.items():
if step.class_alias in tmpdict.keys():
del tmpdict[step.class_alias]
result.cal_logs = tmpdict
if not hasattr(result, 'cal_logs'):
result.cal_logs = {}
for _, step in self.step_defs.items():
if hasattr(result.cal_logs, step.class_alias):
delattr(result.cal_logs, step.class_alias)
setattr(result.cal_logs, self.class_alias, self._log_records)

Also switching cal_logs to a list of strings and removing tmpdict.

Loading