Skip to content

Commit

Permalink
Merge pull request #118 from ojustino/correct-default-mask
Browse files Browse the repository at this point in the history
Correct default mask for array images in HorneExtract
  • Loading branch information
ojustino authored Aug 18, 2022
2 parents 37d4502 + e295641 commit abb5725
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
2 changes: 1 addition & 1 deletion specreduce/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')

Expand Down
32 changes: 31 additions & 1 deletion specreduce/tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit abb5725

Please sign in to comment.