Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move some image parsing logic #209

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading