Skip to content

Commit

Permalink
Use simpler checks for invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
ojustino committed Dec 5, 2022
1 parent 6b2ffb0 commit ddb0f7b
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion specreduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _parse_image(self, image, disp_axis=1):
if getattr(image, 'mask', None) is not None:
mask = image.mask
else:
mask = np.ma.masked_invalid(img).mask
mask = ~np.isfinite(img)

if getattr(image, 'uncertainty', None) is not None:
uncertainty = image.uncertainty
Expand Down
4 changes: 2 additions & 2 deletions specreduce/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def _parse_image(self, image,
elif mask is not None:
pass
else:
mask = np.ma.masked_invalid(img).mask
mask = ~np.isfinite(img)

if img.shape != mask.shape:
raise ValueError('image and mask shapes must match.')
Expand Down Expand Up @@ -486,7 +486,7 @@ def __call__(self, image=None, trace_object=None,

# mask any previously uncaught invalid values
or_mask = np.logical_or(mask,
np.ma.masked_invalid(self.image.data).mask)
~np.isfinite(self.image.data))
img = np.ma.masked_array(self.image.data, or_mask)
mask = img.mask

Expand Down
3 changes: 2 additions & 1 deletion specreduce/tests/test_image_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from specreduce.tracing import FlatTrace
from specutils import Spectrum1D, SpectralAxis


# fetch test image
fn = download_file('https://stsci.box.com/shared/static/exnkul627fcuhy5akf2gswytud5tazmw.fits',
cache=True)
Expand Down Expand Up @@ -96,7 +97,7 @@ def test_parse_horne():
# requires a variance, so it's chosen here to be on equal footing
# with the general case
defaults = {'variance': unc_def,
'mask': np.ma.masked_invalid(img).mask,
'mask': ~np.isfinite(img),
'unit': getattr(img, 'unit', u.DN)}

col[key] = HorneExtract._parse_image(object, img, **defaults)
Expand Down
4 changes: 2 additions & 2 deletions specreduce/tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ def test_fit_trace():
try:
FitTrace(img_win_nans, guess=guess, window=window)
except ValueError as e:
print(f"All-NaN window error message: {e}")
print(f"(expected) All-NaN window error message: {e}")
else:
raise RuntimeError('Trace was erroneously calculated on all-NaN window')

# error on trace of all-nan image
try:
FitTrace(img_all_nans)
except ValueError as e:
print(f"All-NaN image error message: {e}")
print(f"(expected) All-NaN image error message: {e}")
else:
raise RuntimeError('Trace was erroneously calculated on all-NaN image')

Expand Down
3 changes: 1 addition & 2 deletions specreduce/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def __post_init__(self):
self.image = self._parse_image(self.image)

# mask any previously uncaught invalid values
or_mask = np.logical_or(self.image.mask,
np.ma.masked_invalid(self.image.data).mask)
or_mask = np.logical_or(self.image.mask, ~np.isfinite(self.image.data))
img = np.ma.masked_array(self.image.data, or_mask)

# validate arguments
Expand Down

0 comments on commit ddb0f7b

Please sign in to comment.