Skip to content

Commit

Permalink
added test to cover width
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Mar 25, 2024
1 parent 1a19983 commit f3d5315
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
19 changes: 10 additions & 9 deletions specreduce/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def mk_img_non_flat_trace(nrows=40, ncols=100, amp=10, stddev=2):
class TestMeasureCrossDispersionProfile():

@pytest.mark.parametrize('pixel', [None, 1, [1, 2, 3]])
def test_measure_cross_dispersion_profile(self, pixel):
@pytest.mark.parametrize('width', [10, 9])
def test_measure_cross_dispersion_profile(self, pixel, width):
"""
Basic test for `measure_cross_dispersion_profile`. Parametrized over
different options for `pixel` to test using all wavelengths, a single
Expand All @@ -65,24 +66,23 @@ def test_measure_cross_dispersion_profile(self, pixel):
# use a flat trace at trace_pos=10, a window of width 10 around the trace
# and use all 20 columns in image to create an average (median)
# cross dispersion profile
cdp = measure_cross_dispersion_profile(img, width=10, pixel=pixel)
cdp = measure_cross_dispersion_profile(img, width=width, pixel=pixel)

# make sure that if we fit a gaussian to the measured average profile,
# that we get out the same profile that was used to create the image.
# this should be exact since theres no noise in the image
fitter = fitting.LevMarLSQFitter()
mod = models.Gaussian1D()
fit_model = fitter(mod, np.arange(10), cdp)
fit_model = fitter(mod, np.arange(width), cdp)

assert fit_model.mean.value == mean
assert fit_model.mean.value == np.where(cdp == max(cdp))[0][0]
assert fit_model.stddev.value == stddev

# test passing in a FlatTrace, and check the profile
cdp = measure_cross_dispersion_profile(img, width=10, pixel=pixel,
trace=FlatTrace(img, 5))
fit_model = fitter(mod, np.arange(10), cdp)
assert fit_model.mean.value == mean
assert fit_model.stddev.value == stddev
cdp = measure_cross_dispersion_profile(img, width=width, pixel=pixel)
fit_model = fitter(mod, np.arange(width), cdp)
assert fit_model.mean.value == np.where(cdp == max(cdp))[0][0]
np.testing.assert_allclose(fit_model.stddev.value, stddev)

@pytest.mark.filterwarnings("ignore:Model is linear in parameters")
def test_cross_dispersion_profile_non_flat_trace(self):
Expand All @@ -107,6 +107,7 @@ def test_cross_dispersion_profile_non_flat_trace(self):
trace=trace_fit,
width=None,
pixel=pixel,
align_along_trace=False,
statistic='mean')
peak_loc = (np.where(profile == max(profile))[0][0])
assert peak_loc == peak_locs[i]
Expand Down
7 changes: 5 additions & 2 deletions specreduce/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0,
width=None, pixel=None, pixel_range=None,
statistic='median', align_along_trace=False):
statistic='median', align_along_trace=True):
"""
Return a 1D (quantity) array of the cross-dispersion profile measured at a
specified pixel value ('wavelength', but potentially before calibration),
Expand Down Expand Up @@ -39,6 +39,9 @@ def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0,
window around the trace used to measure the profile (this window moves with
the trace if trace is not flat).
By default, if a non-flat trace is used the image will be aligned along the
trace. This can be controlled with the 'align_along_trace' parameter.
Parameters
----------
image : `~astropy.nddata.NDData`-like or array-like, required
Expand Down Expand Up @@ -71,7 +74,7 @@ def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0,
Relevant only for non-flat traces. If True, "roll" each column so that
the trace sits in the central row before calculating average profile. This
will prevent any 'blurring' from averaging a non-flat trace at different
pixel/wavelengths. [default: False]
pixel/wavelengths. [default: True]
"""

Expand Down

0 comments on commit f3d5315

Please sign in to comment.