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 ----- 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.') diff --git a/specreduce/tests/test_extract.py b/specreduce/tests/test_extract.py index 323e27dd..69a626c5 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`). + # + 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() + + # 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(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(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(variance=err) + assert ext.unit == u.Unit() + + def test_horne_variance_errors(): trace = FlatTrace(image, 3.0)