Skip to content

Commit

Permalink
simplifying query_step_status function
Browse files Browse the repository at this point in the history
  • Loading branch information
emolter committed Jun 29, 2024
1 parent 4f603fb commit 1fca536
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
27 changes: 26 additions & 1 deletion jwst/master_background/master_background_mos_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def process(self, data):
# If some type of background processing had already been done. Abort.
# UNLESS forcing is enacted.
if not self.force_subtract and \
'COMPLETE' in [query_step_status(data_model, "back_sub"), query_step_status(data_model, "master_background")]:
'COMPLETE' in [query_step_status_container(data_model, "back_sub"), \
query_step_status_container(data_model, "master_background")]:
self.log.info('Background subtraction has already occurred. Skipping.')
record_step_status(data, 'master_background', success=False)

Check warning on line 113 in jwst/master_background/master_background_mos_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/master_background/master_background_mos_step.py#L113

Added line #L113 was not covered by tests
return data
Expand Down Expand Up @@ -275,3 +276,27 @@ def _calc_master_background(self, data, user_background=None):
mb_multislit = self.flat_field(mb_multislit)

return master_background, mb_multislit


def query_step_status_container(model: datamodels.JwstDataModel, step_name: str):
'''Query the status of a step in a datamodel or container of datamodels
assuming the step status for the zeroth model is representative of all models.
This is true for the master background step but may not be generally applicable.
Parameters
----------
model : `~jwst.datamodels.JwstDataModel` instance
This is the datamodel or container of datamodels to check
step_name : str
The attribute in meta.cal_step to check
Returns
-------
status : str
The status of the step in meta.cal_step, typically 'COMPLETE' or 'SKIPPED'
'''
if isinstance(model, datamodels.ModelContainer):
return query_step_status(model[0], step_name)

Check warning on line 300 in jwst/master_background/master_background_mos_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/master_background/master_background_mos_step.py#L299-L300

Added lines #L299 - L300 were not covered by tests
else:
return query_step_status(model, step_name)

Check warning on line 302 in jwst/master_background/master_background_mos_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/master_background/master_background_mos_step.py#L302

Added line #L302 was not covered by tests
4 changes: 2 additions & 2 deletions jwst/pipeline/calwebb_spec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def process_exposure_product(
# as DO_NOT_USE or NON_SCIENCE.
resampled = self.pixel_replace(resampled)
resampled = self.cube_build(resampled)
if query_step_status(resampled, "cube_build") == 'COMPLETE':
if query_step_status(resampled[0], "cube_build") == 'COMPLETE':
self.save_model(resampled[0], suffix='s3d')
elif exp_type in ['MIR_LRS-SLITLESS']:
resampled = calibrated.copy()
Expand All @@ -347,7 +347,7 @@ def process_exposure_product(
# as DO_NOT_USE or NON_SCIENCE.
resampled = self.pixel_replace(resampled)
# Extract a 1D spectrum from the 2D/3D data
if exp_type in ['MIR_MRS', 'NRS_IFU'] and query_step_status(resampled, "cube_build") == 'SKIPPED':
if exp_type in ['MIR_MRS', 'NRS_IFU'] and query_step_status(resampled[0], "cube_build") == 'SKIPPED':
# Skip extract_1d for IFU modes where no cube was built
self.extract_1d.skip = True

Expand Down
20 changes: 3 additions & 17 deletions jwst/stpipe/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import os
import re
from collections.abc import Sequence
import numpy as np

# Configure logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -166,9 +165,9 @@ def record_step_status(datamodel, cal_step, success=True):
If True, then 'COMPLETE' is recorded. If False, then 'SKIPPED'
"""
if success:
status = 'COMPLETE'
status = COMPLETE
else:
status = 'SKIPPED'
status = SKIPPED

if isinstance(datamodel, Sequence):
for model in datamodel:
Expand All @@ -195,17 +194,4 @@ def query_step_status(datamodel, cal_step):
status : str
The status of the step in meta.cal_step, typically 'COMPLETE' or 'SKIPPED'
"""
if isinstance(datamodel, Sequence):
status = np.array([getattr(model.meta.cal_step, cal_step, NOT_SET) for model in datamodel])
if np.any(status == COMPLETE):
return COMPLETE
elif np.all(status == NOT_SET):
return NOT_SET
elif np.all(status == SKIPPED):
return SKIPPED
else:
msg = f"Unrecognized status in meta.cal_step.{cal_step}."
raise ValueError(msg)
return getattr(datamodel[0].meta.cal_step, cal_step, NOT_SET)
else:
return getattr(datamodel.meta.cal_step, cal_step, NOT_SET)
return getattr(datamodel.meta.cal_step, cal_step, NOT_SET)

0 comments on commit 1fca536

Please sign in to comment.