From da586177f9bb9ad7944bdc98f1f88aab59e866f6 Mon Sep 17 00:00:00 2001 From: Tyler Pauly Date: Tue, 29 Oct 2024 12:22:09 -0400 Subject: [PATCH] move test from RT to unit --- jwst/background/background_sub.py | 23 +++++++----- jwst/background/tests/test_background.py | 24 ++++++++++++- jwst/regtest/test_nircam_wfss_spec2.py | 46 ------------------------ 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/jwst/background/background_sub.py b/jwst/background/background_sub.py index a84c4a816e..9edd641c23 100755 --- a/jwst/background/background_sub.py +++ b/jwst/background/background_sub.py @@ -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. @@ -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 diff --git a/jwst/background/tests/test_background.py b/jwst/background/tests/test_background.py index 7e895b7ddd..00b04c93a8 100644 --- a/jwst/background/tests/test_background.py +++ b/jwst/background/tests/test_background.py @@ -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") @@ -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) diff --git a/jwst/regtest/test_nircam_wfss_spec2.py b/jwst/regtest/test_nircam_wfss_spec2.py index efa0609257..f135a92fd7 100644 --- a/jwst/regtest/test_nircam_wfss_spec2.py +++ b/jwst/regtest/test_nircam_wfss_spec2.py @@ -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", @@ -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()