Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
99b45a9
save the bkgd mask even if step is exists early
penaguerrero Aug 25, 2025
7db7d83
testing the datamodel changes
penaguerrero Aug 26, 2025
aee20aa
Merge branch 'main' into bkgdfix
penaguerrero Aug 26, 2025
9c02c7f
testing the datamodel changes
penaguerrero Aug 26, 2025
e57bc66
setting BKGD_SCL keyword to 1.0 and step status to FAIL
penaguerrero Aug 26, 2025
8f9a469
Merge branch 'main' into bkgdfix
penaguerrero Aug 26, 2025
2045efa
changing test to include the FAIL status
penaguerrero Aug 26, 2025
2214e10
testing FAILED as step status
penaguerrero Aug 26, 2025
e770eb0
testing FAILED as step status
penaguerrero Aug 26, 2025
798a21d
adding test for infinite sclaing factor
penaguerrero Aug 26, 2025
191b0a1
Merge branch 'main' into bkgdfix
penaguerrero Aug 26, 2025
8e7a82a
clenup of copies
penaguerrero Aug 27, 2025
48fbcab
more cleaning up and testing for shallow copies
penaguerrero Aug 27, 2025
9d8c262
Merge branch 'main' into bkgdfix
penaguerrero Aug 27, 2025
db66f48
additional shallow copy tests
penaguerrero Aug 27, 2025
03ca267
additional shallow copy tests
penaguerrero Aug 27, 2025
f0a3cf6
fix for miri test
penaguerrero Aug 28, 2025
fc4f20a
setting scaling factor to 0.0 in case of step satus set to FAILED
penaguerrero Aug 28, 2025
aeb06b9
reverting to main
penaguerrero Aug 28, 2025
517c006
test for scaling factor 0.0 if FAILED
penaguerrero Aug 28, 2025
eb87bc5
add information to changes file
penaguerrero Aug 28, 2025
e7a1260
Delete changes/9722.background.rst
penaguerrero Aug 28, 2025
e85d84c
add information to changes file
penaguerrero Aug 28, 2025
0171965
Update changes/9772.background.rst
penaguerrero Aug 29, 2025
3efe118
Merge branch 'main' into bkgdfix
penaguerrero Aug 29, 2025
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
2 changes: 1 addition & 1 deletion jwst/background/background_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def process(self, step_input, input_bkg_list=None):
if result is None:
result = input_model.copy()
result.meta.cal_step.bkg_subtract = "SKIPPED"
else:
elif not result.meta.cal_step.bkg_subtract:
result.meta.cal_step.bkg_subtract = "COMPLETE"

elif input_model.meta.exposure.type == "NIS_SOSS":
Expand Down
24 changes: 17 additions & 7 deletions jwst/background/background_sub_wfss.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,29 @@ def subtract_wfss_bkg(
log.warning("No source_catalog found in input.meta.")
got_catalog = False

# Make a copy of the input model to save the mask
result = input_model.copy()

# Create a mask from the source catalog, True where there are no sources,
# i.e. in regions we can use as background.
if got_catalog:
bkg_mask = _mask_from_source_cat(input_model, wl_range_name, mmag_extract)
if not _sufficient_background_pixels(input_model.dq, bkg_mask, bkg_ref.data):
log.warning("Not enough background pixels to work with.")
log.warning("Step will be SKIPPED.")
log.warning("Step will be FAILED.")
# Save the mask in expected data type for the datamodel and set
# other keywords appropriately for this case
result.mask = bkg_mask.astype(np.uint32)
result.meta.background.scaling_factor = 1.0
result.meta.cal_step.bkg_subtract = "FAILED"
bkg_ref.close()
return None
return result
else:
bkg_mask = np.ones(input_model.data.shape, dtype=bool)

# save the mask in expected data type for the datamodel
result.mask = bkg_mask.astype(np.uint32)

# compute scaling factor for the reference background image
log.info("Starting iterative outlier rejection for background subtraction.")
rescaler = _ScalingFactorComputer(**rescaler_kwargs)
Expand All @@ -99,19 +110,18 @@ def subtract_wfss_bkg(
if not np.isfinite(factor):
log.warning(
"Could not determine a finite scaling factor between reference background and data."
" Step will be SKIPPED."
" Step will be FAILED."
)
result.meta.background.scaling_factor = 1.0
result.meta.cal_step.bkg_subtract = "FAILED"
bkg_ref.close()
return None
return result

# extract the derived factor and apply it to the unmasked, non-outlier-rejected data
subtract_this = factor * bkg_ref.data
result = input_model.copy()
result.data = input_model.data - subtract_this
result.dq = np.bitwise_or(input_model.dq, bkg_ref.dq)
result.meta.background.scaling_factor = factor
# save the mask in expected data type for the datamodel
result.mask = bkg_mask.astype(np.uint32)

log.info(f"Average of scaled background image = {np.nanmean(subtract_this):.3e}")
log.info(f"Scaling factor = {factor:.5e}")
Expand Down
20 changes: 19 additions & 1 deletion jwst/background/tests/test_background_wfss.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,23 @@ def test_bkg_fail(monkeypatch, caplog, make_nrc_wfss_datamodel):

result = BackgroundStep.call(model)
assert result is not model
assert result.meta.cal_step.bkg_subtract == "SKIPPED"
assert result.meta.cal_step.bkg_subtract == "FAILED"
assert "Not enough background pixels" in caplog.text


def test_infinite_factor(monkeypatch, caplog, make_nrc_wfss_datamodel):
"""Test for infinite scaling factor."""
model = make_nrc_wfss_datamodel.copy()

# Mock an infinite scaling factor
monkeypatch.setattr(
background_sub_wfss._ScalingFactorComputer, "err_weighted_mean", lambda *args: np.nan
)

result = BackgroundStep.call(model)
assert result is not model
assert result.meta.cal_step.bkg_subtract == "FAILED"
assert (
"Could not determine a finite scaling factor between reference background and data"
in caplog.text
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"scikit-image>=0.20.0",
"scipy>=1.14.1",
"spherical-geometry>=1.3",
"stdatamodels @ git+https://github.com/spacetelescope/stdatamodels.git@main",
"stdatamodels @ git+https://github.com/penaguerrero/stdatamodels.git@change_bkgd_scl_type",
"stcal @ git+https://github.com/spacetelescope/stcal.git@main",
"stpipe @ git+https://github.com/spacetelescope/stpipe.git@main",
"stsci.imagestats>=1.6.3",
Expand Down
Loading