Skip to content

Commit

Permalink
More unit tests for extract
Browse files Browse the repository at this point in the history
  • Loading branch information
melanieclarke committed Nov 25, 2024
1 parent cedf8a1 commit 421ee4f
Show file tree
Hide file tree
Showing 3 changed files with 440 additions and 10 deletions.
24 changes: 15 additions & 9 deletions jwst/extract_1d/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,22 +920,28 @@ def aperture_center(profile, dispaxis=1, middle_pix=None):
"""
weights = profile.copy()
weights[weights <= 0] = 0.0
if middle_pix is not None and np.sum(profile) > 0:
if middle_pix is not None:
spec_center = middle_pix
if dispaxis == HORIZONTAL:
slit_center = np.average(np.arange(profile.shape[0]),
weights=weights[:, middle_pix])
if np.sum(weights[:, middle_pix]) > 0:
slit_center = np.average(np.arange(profile.shape[0]),
weights=weights[:, middle_pix])
else:
slit_center = (profile.shape[0] - 1) / 2
else:
slit_center = np.average(np.arange(profile.shape[1]),
weights=weights[middle_pix, :])
if np.sum(weights[middle_pix, :]) > 0:
slit_center = np.average(np.arange(profile.shape[1]),
weights=weights[middle_pix, :])
else:
slit_center = (profile.shape[1] - 1) / 2
else:
yidx, xidx = np.mgrid[:profile.shape[0], :profile.shape[1]]
if np.sum(profile) > 0:
if np.sum(weights) > 0:
center_y = np.average(yidx, weights=weights)
center_x = np.average(xidx, weights=weights)
else:
center_y = profile.shape[0] // 2
center_x = profile.shape[1] // 2
center_y = (profile.shape[0] - 1) / 2
center_x = (profile.shape[1] - 1) / 2
if dispaxis == HORIZONTAL:
slit_center = center_y
spec_center = center_x
Expand Down Expand Up @@ -1053,7 +1059,7 @@ def location_from_wcs(input_model, slit):
dithra = slit.meta.dither.dithered_ra
dithdec = slit.meta.dither.dithered_dec
x_y = wcs.backward_transform(dithra, dithdec, middle_wl)
except AttributeError:
except (AttributeError, TypeError):
log.warning("Dithered pointing location not found in wcsinfo.")
return None, None, None
else:
Expand Down
67 changes: 66 additions & 1 deletion jwst/extract_1d/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,48 @@ def simple_wcs_function(x, y):

return ra, dec, wave

# Add a bounding box
simple_wcs_function.bounding_box = wcs_bbox_from_shape(shape)

# Add a few expected attributes, so they can be monkeypatched as needed
simple_wcs_function.get_transform = None
simple_wcs_function.backward_transform = None
simple_wcs_function.available_frames = []

return simple_wcs_function


@pytest.fixture()
def simple_wcs_transpose():
shape = (50, 50)
ycenter = shape[0] // 2.0

def simple_wcs_function(x, y):
""" Simple WCS for testing """
crpix2 = ycenter
crpix3 = 1.0
cdelt1 = 0.1
cdelt2 = 0.1
cdelt3 = -0.01

crval1 = 45.0
crval2 = 45.0
crval3 = 7.5

wave = (y + 1 - crpix3) * cdelt3 + crval3
ra = (y + 1 - crpix2) * cdelt1 + crval1
dec = np.full_like(ra, crval2 + 1 * cdelt2)

return ra, dec, wave

# Add a bounding box
simple_wcs_function.bounding_box = wcs_bbox_from_shape(shape)

# Add a few expected attributes, so they can be monkeypatched as needed
simple_wcs_function.get_transform = None
simple_wcs_function.backward_transform = None
simple_wcs_function.available_frames = []

return simple_wcs_function


Expand Down Expand Up @@ -73,8 +113,8 @@ def mock_nirspec_fs_one_slit(simple_wcs):
model.meta.exposure.nints = 1
model.meta.exposure.type = 'NRS_FIXEDSLIT'
model.meta.subarray.name = 'ALLSLITS'

model.source_type = 'EXTENDED'

model.meta.wcsinfo.dispersion_direction = 1
model.meta.wcs = simple_wcs

Expand Down Expand Up @@ -158,6 +198,31 @@ def mock_nirspec_bots(simple_wcs):
model.close()


@pytest.fixture()
def mock_miri_lrs_fs(simple_wcs_transpose):
model = dm.SlitModel()
model.meta.instrument.name = 'MIRI'
model.meta.instrument.detector = 'MIRIMAGE'
model.meta.observation.date = '2023-07-22'
model.meta.observation.time = '06:24:45.569'
model.meta.exposure.nints = 1
model.meta.exposure.type = 'MIR_LRS-FIXEDSLIT'
model.meta.subarray.name = 'FULL'
model.meta.target.source_type = 'EXTENDED'
model.meta.dither.dithered_ra = 45.0
model.meta.dither.dithered_ra = 45.0

model.meta.wcsinfo.dispersion_direction = 2
model.meta.wcs = simple_wcs_transpose

model.data = np.arange(50 * 50, dtype=float).reshape((50, 50))
model.var_poisson = model.data * 0.02
model.var_rnoise = model.data * 0.02
model.var_flat = model.data * 0.05
yield model
model.close()


@pytest.fixture()
def mock_miri_ifu(simple_wcs_ifu):
model = dm.IFUCubeModel()
Expand Down
Loading

0 comments on commit 421ee4f

Please sign in to comment.