Skip to content

Commit

Permalink
move part of image parsing to its own function to avoid issue with cl…
Browse files Browse the repository at this point in the history
…assmethod, and fix issue with parsing quantities
  • Loading branch information
cshanahan1 committed Feb 8, 2024
1 parent 715361e commit b2e032f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 3 additions & 3 deletions specreduce/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from astropy.utils.decorators import deprecated_attribute
from specutils import Spectrum1D

from specreduce.core import _ImageParser
from specreduce.core import _ImageParser, _get_data_from_image
from specreduce.extract import _ap_weight_image
from specreduce.tracing import Trace, FlatTrace

Expand Down Expand Up @@ -183,7 +183,7 @@ def two_sided(cls, image, trace_object, separation, **kwargs):
crossdisp_axis : int
cross-dispersion axis
"""
image = cls._parse_image(cls, image)
image = _get_data_from_image(image) if image is not None else cls.image
kwargs['traces'] = [trace_object-separation, trace_object+separation]
return cls(image=image, **kwargs)

Expand Down Expand Up @@ -220,7 +220,7 @@ def one_sided(cls, image, trace_object, separation, **kwargs):
crossdisp_axis : int
cross-dispersion axis
"""
image = cls._parse_image(cls, image)
image = _get_data_from_image(image) if image is not None else cls.image
kwargs['traces'] = [trace_object+separation]
return cls(image=image, **kwargs)

Expand Down
21 changes: 15 additions & 6 deletions specreduce/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
__all__ = ['SpecreduceOperation']


def _get_data_from_image(image):
"""Extract data array from various input types for `image`.
Retruns `np.ndarray` of image data."""

if isinstance(image, u.quantity.Quantity):
img = image.value
if isinstance(image, np.ndarray):
img = image
else: # NDData, including CCDData and Spectrum1D
img = image.data
return img


class _ImageParser:
"""
Coerces images from accepted formats to Spectrum1D objects for
Expand All @@ -26,6 +39,7 @@ class _ImageParser:
- `~astropy.units.quantity.Quantity`
- `~numpy.ndarray`
"""

def _parse_image(self, image, disp_axis=1):
"""
Convert all accepted image types to a consistently formatted
Expand All @@ -50,12 +64,7 @@ def _parse_image(self, image, disp_axis=1):
# useful for Background's instance methods
return self.image

if isinstance(image, np.ndarray):
img = image
elif isinstance(image, u.quantity.Quantity):
img = image.value
else: # NDData, including CCDData and Spectrum1D
img = image.data
img = _get_data_from_image(image)

# mask and uncertainty are set as None when they aren't specified upon
# creating a Spectrum1D object, so we must check whether these
Expand Down
2 changes: 1 addition & 1 deletion specreduce/tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def mk_img(self, nrows=200, ncols=160):
index_arr = np.tile(np.arange(nrows), (ncols, 1))
img = col_model(index_arr.T) + noise

return img
return img * u.DN

def test_window_fit_trace(self):

Expand Down

0 comments on commit b2e032f

Please sign in to comment.