Skip to content

Commit

Permalink
move test from RT to unit
Browse files Browse the repository at this point in the history
  • Loading branch information
tapastro committed Oct 29, 2024
1 parent c49d072 commit da58617
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
23 changes: 15 additions & 8 deletions jwst/background/background_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,20 @@ def average_background(input_model, bkg_list, sigma, maxiters):
return avg_bkg


def sufficient_background_pixels(dq_array, bkg_mask, min_pixels=100):
"""Count number of good pixels for background use.
Check DQ flags of pixels selected for bkg use - XOR the DQ values with
the DO_NOT_USE flag to flip the DO_NOT_USE bit. Then count the number
of pixels that AND with the DO_NOT_USE flag, i.e. initially did not have
the DO_NOT_USE bit set.
"""
return np.count_nonzero((dq_array[bkg_mask]
^ pixel['DO_NOT_USE'])
& pixel['DO_NOT_USE']
) > min_pixels


def subtract_wfss_bkg(input_model, bkg_filename, wl_range_name, mmag_extract=None):
"""Scale and subtract a background reference image from WFSS/GRISM data.
Expand Down Expand Up @@ -310,14 +324,7 @@ def subtract_wfss_bkg(input_model, bkg_filename, wl_range_name, mmag_extract=Non
# 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)
# Check DQ flags of pixels selected for bkg use - XOR the DQ values with
# the DO_NOT_USE flag to flip the DO_NOT_USE bit. Then count the number
# of pixels that AND with the DO_NOT_USE flag, i.e. initially did not have
# the DO_NOT_USE bit set.
if np.count_nonzero((input_model.dq[bkg_mask]
^ pixel['DO_NOT_USE'])
& pixel['DO_NOT_USE']
) < 100:
if not sufficient_background_pixels(input_model.dq, bkg_mask):
log.warning("Not enough background pixels to work with.")
log.warning("Step will be SKIPPED.")
return None
Expand Down
24 changes: 23 additions & 1 deletion jwst/background/tests/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
from numpy.testing import assert_allclose

from stdatamodels.jwst import datamodels
from stdatamodels.jwst.datamodels.dqflags import pixel

from jwst.assign_wcs import AssignWcsStep
from jwst.background import BackgroundStep
from jwst.stpipe import Step
from jwst.background.background_sub import robust_mean, mask_from_source_cat, no_NaN
from jwst.background.background_sub import (robust_mean, mask_from_source_cat,
no_NaN, sufficient_background_pixels)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -403,3 +405,23 @@ def test_no_nan():

# Make sure arrays are equal.
assert np.array_equal(model.data, result.data)


def test_sufficient_background_pixels():
model = datamodels.ImageModel(data=np.zeros((2048, 2048)),
dq=np.zeros((2048, 2048)))
refpix_flags = pixel['DO_NOT_USE'] | pixel['REFERENCE_PIXEL']
model.dq[:4, :] = refpix_flags
model.dq[-4:, :] = refpix_flags
model.dq[:, :4] = refpix_flags
model.dq[:, -4:] = refpix_flags

bkg_mask = np.ones((2048, 2048), dtype=bool)
# With full array minux refpix available for bkg, should be sufficient
assert sufficient_background_pixels(model.dq, bkg_mask)

bkg_mask[4: -4, :] = 0
bkg_mask[:, 4: -4] = 0
# Now mask out entire array, mocking full source coverage of detector -
# no pixels should be available for bkg
assert not sufficient_background_pixels(model.dq, bkg_mask)
46 changes: 0 additions & 46 deletions jwst/regtest/test_nircam_wfss_spec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ def run_pipeline(rtdata_module):
return rtdata


@pytest.fixture(scope="module")
def run_pipeline_skip_bkg(rtdata_module):
"""Run the calwebb_spec2 pipeline on a single NIRSpec WFSS exposure."""

rtdata = rtdata_module

# Get the input data; load individual data files first, load ASN file last
rtdata.get_data("nircam/wfss/jw01076-o101_t002_nircam_clear-f356w_spoof_cat.ecsv")
rtdata.get_data("nircam/wfss/jw01076101001_02101_00003_nrcalong_rate.fits")
rtdata.get_data("nircam/wfss/jw01076-o101_20220403t120233_spec2_002_spoof_asn.json")

# Run the calwebb_spec2 pipeline; save results from intermediate steps
args = ["calwebb_spec2", rtdata.input,
"--output_file='skip_bkgsub'",
"--steps.bkg_subtract.skip=False"]
Step.from_cmdline(args)

return rtdata


@pytest.mark.bigdata
@pytest.mark.parametrize("suffix", [
"assign_wcs", "bsub", "extract_2d", "flat_field", "srctype",
Expand All @@ -71,29 +51,3 @@ def test_nircam_wfss_spec2(run_pipeline, fitsdiff_default_kwargs, suffix):
# Compare the results
diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs)
assert diff.identical, diff.report()


@pytest.mark.bigdata
@pytest.mark.parametrize("suffix", [
"cal", "x1d"])
def test_nircam_wfss_spec2_nobkg(run_pipeline_skip_bkg,
fitsdiff_default_kwargs, suffix):
"""Test of calwebb_spec2 on NIRCam WFSS with skipped
background subtraction.
In WFSS background subtraction, pixels in source cutouts are masked
out of the background calculation. Test the case where no pixels are
available for background calculation.
"""

# Run the pipeline and retrieve outputs
rtdata = run_pipeline_skip_bkg
output = f"skip_bkgsub_{suffix}.fits"
rtdata.output = output

# Get the truth files
rtdata.get_truth("truth/test_nircam_wfss_spec2/" + output)

# Compare the results
diff = FITSDiff(rtdata.output, rtdata.truth, **fitsdiff_default_kwargs)
assert diff.identical, diff.report()

0 comments on commit da58617

Please sign in to comment.