Skip to content

Commit

Permalink
Clarify docs, add log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
melanieclarke committed Jan 21, 2025
1 parent 0eb37e8 commit 190f948
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/jwst/extract_1d/description.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ are used instead:
residuals of the scene modeled by the PSF at that location. This option is
strongly recommended if `model_nod_pair` is True, since the negative nod location is less
reliably estimated than the positive trace location.
* `subtract_background`: Unlike box extraction, the background levels can be modeled and removed
* `subtract_background`: Unlike during box extraction, the background levels can be modeled and removed
during optimal extraction without explicitly setting a background region. It is recommended to
set this parameter to True if background subtraction was skipped prior to extraction. Set this
parameter to False if a negative nod trace is present but not modeled (`model_nod_pair = False`).
Expand Down
8 changes: 6 additions & 2 deletions jwst/extract_1d/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,15 +371,19 @@ def get_extract_parameters(ref_dict, input_model, slitname, sp_order, meta,
extract_params['model_nod_pair'] = model_nod_pair

# Check for a valid PSF file
if extract_params['psf'] == 'N/A':
extraction_type = str(extraction_type).lower()
if extract_params['psf'] == 'N/A' and extraction_type != 'box':
log.warning("No PSF file available. Setting extraction type to 'box'.")
extraction_type = 'box'

# Set the extraction type to 'box' or 'optimal'
if str(extraction_type).lower() == 'none':
if extraction_type == 'none':
if extract_params['use_source_posn']:
extract_params['extraction_type'] = 'optimal'
else:
extract_params['extraction_type'] = 'box'
log.info(f"Using extraction type '{extract_params['extraction_type']}' "
f"for use_source_posn = {extract_params['use_source_posn']}")
else:
extract_params['extraction_type'] = extraction_type

Expand Down
28 changes: 14 additions & 14 deletions jwst/extract_1d/psf_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ def _make_cutout_profile(xidx, yidx, psf_subpix, psf_data, dispaxis,
return [sprofile, nod_profile * -1]


def _profile_residual(param, cutout, cutout_var, xidx, yidx, psf_subpix, psf_data,
dispaxis, fit_bkg=True):
def _profile_residual(shifts_to_optimize, cutout, cutout_var, xidx, yidx,
psf_subpix, psf_data, dispaxis, fit_bkg=True):
"""Residual function to minimize for optimizing trace locations."""
if len(param) > 1:
nod_offset = param[1]
if len(shifts_to_optimize) > 1:
nod_offset = shifts_to_optimize[1]
else:
nod_offset = None
sprofiles = _make_cutout_profile(xidx, yidx, psf_subpix, psf_data, dispaxis,
extra_shift=param[0], nod_offset=nod_offset)
extra_shift=shifts_to_optimize[0],
nod_offset=nod_offset)
extract_kwargs = {'extraction_type': 'optimal',
'fit_bkg': fit_bkg,
'bkg_fit_type': 'poly',
Expand All @@ -160,14 +161,13 @@ def psf_profile(input_model, trace, wl_array, psf_ref_name,
optimize_shifts=True, model_nod_pair=True):
"""Create a spatial profile from a PSF reference.
This is intended to provide PSF-based profiles for point sources,
for slit-like data containing one positive trace and, optionally,
one negative trace resulting from nod subtraction. The location of
the positive trace should be provided in the `trace` input parameter;
the negative trace location will be guessed from the input metadata.
If a negative trace is modeled, it is recommended that `optimize_shifts`
also be set to True, to improve the initial guess for the trace
location.
Provides PSF-based profiles for point sources in slit-like data containing
one positive trace and, optionally, one negative trace resulting from nod
subtraction. The location of the positive trace should be provided in the
`trace` input parameter; the negative trace location will be guessed from
the input metadata. If a negative trace is modeled, it is recommended that
`optimize_shifts` also be set to True, to improve the initial guess for the
trace location.
Parameters
----------
Expand All @@ -184,7 +184,7 @@ def psf_profile(input_model, trace, wl_array, psf_ref_name,
psf_ref_name : str
PSF reference filename.
optimize_shifts : bool, optional
If True, the spatial location of the trace will be optimized via
If True, the spatial location of the trace will be optimized by
minimizing the residuals in a scene model compared to the data in
the first integration of `input_model`.
model_nod_pair : bool, optional
Expand Down
13 changes: 11 additions & 2 deletions jwst/extract_1d/tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,14 @@ def test_get_extract_parameters_smoothing_bad_value(

@pytest.mark.parametrize('use_source', [None, True, False])
def test_get_extract_parameters_extraction_type_none(
mock_nirspec_fs_one_slit, extract1d_ref_dict, use_source):
mock_nirspec_fs_one_slit, extract1d_ref_dict, use_source, log_watcher):
input_model = mock_nirspec_fs_one_slit

log_watcher.message = "Using extraction type"
params = ex.get_extract_parameters(
extract1d_ref_dict, input_model, 'slit1', 1, input_model.meta,
extraction_type=None, use_source_posn=use_source, psf_ref_name='available')
log_watcher.assert_seen()

# Extraction type is set to optimal if use_source_posn is True
if use_source is None or use_source is True:
Expand All @@ -259,12 +262,18 @@ def test_get_extract_parameters_extraction_type_none(

@pytest.mark.parametrize('extraction_type', [None, 'box', 'optimal'])
def test_get_extract_parameters_no_psf(
mock_nirspec_fs_one_slit, extract1d_ref_dict, extraction_type):
mock_nirspec_fs_one_slit, extract1d_ref_dict, extraction_type, log_watcher):
input_model = mock_nirspec_fs_one_slit

log_watcher.message = "Setting extraction type to 'box'"
params = ex.get_extract_parameters(
extract1d_ref_dict, input_model, 'slit1', 1, input_model.meta,
extraction_type=extraction_type, psf_ref_name='N/A')

# Warning message issued if extraction type was not already 'box'
if extraction_type != 'box':
log_watcher.assert_seen()

# Extraction type is always box if no psf is available
assert params['extraction_type'] == 'box'

Expand Down

0 comments on commit 190f948

Please sign in to comment.