From 6a546e33f147845058ac7da5f9c37a0b2dd58d49 Mon Sep 17 00:00:00 2001 From: ojustino Date: Mon, 15 Aug 2022 11:58:08 -0400 Subject: [PATCH 1/4] Get mask object from numpy masked_invalid --- specreduce/extract.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specreduce/extract.py b/specreduce/extract.py index 3a22a5e4..62d28bd5 100644 --- a/specreduce/extract.py +++ b/specreduce/extract.py @@ -333,7 +333,7 @@ def __call__(self, image=None, trace_object=None, # check optional arguments, filling them in if absent if mask is None: - mask = np.ma.masked_invalid(image) + mask = np.ma.masked_invalid(image).mask elif image.shape != mask.shape: raise ValueError('image and mask shapes must match.') From 75b3f083a4c019e65ed3a83d7898d4e89fae1a5e Mon Sep 17 00:00:00 2001 From: ojustino Date: Mon, 15 Aug 2022 14:50:25 -0400 Subject: [PATCH 2/4] Updated changelog --- CHANGES.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index db294356..dafb2dac 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,8 +10,13 @@ API Changes ^^^^^^^^^^^ - ``BoxcarExtract`` and ``HorneExtract`` now accept parameters (and require the image and trace) - at initialization, and allow overriding any input parameters when calling. [#117] + at initialization, and allow overriding any input parameters when calling [#117] +Bug Fixes +^^^^^^^^^ + +- Corrected the default mask created in ``HorneExtract``/``OptimalExtract`` + when a user doesn't specify one and gives their image as a numpy array [#118] 1.0.0 ----- From ada5936b067cd76b985d6853b2c6bd5597f1477e Mon Sep 17 00:00:00 2001 From: ojustino Date: Mon, 15 Aug 2022 19:12:15 -0400 Subject: [PATCH 3/4] Wrote tests for Horne use with array-like images --- specreduce/tests/test_extract.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/specreduce/tests/test_extract.py b/specreduce/tests/test_extract.py index 323e27dd..19b333dd 100644 --- a/specreduce/tests/test_extract.py +++ b/specreduce/tests/test_extract.py @@ -4,7 +4,7 @@ import astropy.units as u from astropy.nddata import CCDData -from specreduce.extract import BoxcarExtract, HorneExtract +from specreduce.extract import BoxcarExtract, HorneExtract, OptimalExtract from specreduce.tracing import FlatTrace, ArrayTrace @@ -79,6 +79,36 @@ def test_boxcar_array_trace(): assert np.allclose(spectrum.flux.value, np.full_like(spectrum.flux.value, 75.)) +def test_horne_array_validation(): + # + # Test HorneExtract scenarios specific to its use with an image of + # type `~numpy.ndarray` (instead of the default `~astropy.nddata.NDData`). + # + extract = OptimalExtract() # alias of Horne; behavior shouldn't change + trace = FlatTrace(image, 15.0) + + # an array-type image must come with a variance argument + with pytest.raises(ValueError, match=r'.*array.*variance.*specified.*'): + ext = extract(image.data, trace) + + # an array-type image must have the same dimensions as its variance argument + with pytest.raises(ValueError, match=r'.*shapes must match.*'): + err = np.ones_like(image[0]) + ext = extract(image.data, trace, variance=err) + + # an array-type image must have the same dimensions as its mask argument + with pytest.raises(ValueError, match=r'.*shapes must match.*'): + err = np.ones_like(image) + mask = np.zeros_like(image[0]) + ext = extract(image.data, trace, variance=err, mask=mask) + + # an array-type image given without mask and unit arguments is fine + # and produces a unitless result + err = np.ones_like(image) + ext = extract(image.data, trace, variance=err) + assert ext.unit == u.Unit() + + def test_horne_variance_errors(): trace = FlatTrace(image, 3.0) From e295641cf841a723f1dd1a25563a8af808f191bd Mon Sep 17 00:00:00 2001 From: ojustino Date: Thu, 18 Aug 2022 14:14:11 -0400 Subject: [PATCH 4/4] Adjusted test to new extraction API --- specreduce/tests/test_extract.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/specreduce/tests/test_extract.py b/specreduce/tests/test_extract.py index 19b333dd..69a626c5 100644 --- a/specreduce/tests/test_extract.py +++ b/specreduce/tests/test_extract.py @@ -84,28 +84,28 @@ def test_horne_array_validation(): # Test HorneExtract scenarios specific to its use with an image of # type `~numpy.ndarray` (instead of the default `~astropy.nddata.NDData`). # - extract = OptimalExtract() # alias of Horne; behavior shouldn't change trace = FlatTrace(image, 15.0) + extract = OptimalExtract(image.data, trace) # equivalent to HorneExtract # an array-type image must come with a variance argument with pytest.raises(ValueError, match=r'.*array.*variance.*specified.*'): - ext = extract(image.data, trace) + ext = extract() # an array-type image must have the same dimensions as its variance argument with pytest.raises(ValueError, match=r'.*shapes must match.*'): err = np.ones_like(image[0]) - ext = extract(image.data, trace, variance=err) + ext = extract(variance=err) # an array-type image must have the same dimensions as its mask argument with pytest.raises(ValueError, match=r'.*shapes must match.*'): err = np.ones_like(image) mask = np.zeros_like(image[0]) - ext = extract(image.data, trace, variance=err, mask=mask) + ext = extract(variance=err, mask=mask) # an array-type image given without mask and unit arguments is fine # and produces a unitless result err = np.ones_like(image) - ext = extract(image.data, trace, variance=err) + ext = extract(variance=err) assert ext.unit == u.Unit()