From b2e032fecd514f7089228ac18b48141b1c8edcb7 Mon Sep 17 00:00:00 2001 From: Clare Shanahan Date: Thu, 8 Feb 2024 00:48:04 -0500 Subject: [PATCH] move part of image parsing to its own function to avoid issue with classmethod, and fix issue with parsing quantities --- specreduce/background.py | 6 +++--- specreduce/core.py | 21 +++++++++++++++------ specreduce/tests/test_tracing.py | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/specreduce/background.py b/specreduce/background.py index 77c6a98..aa0c037 100644 --- a/specreduce/background.py +++ b/specreduce/background.py @@ -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 @@ -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) @@ -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) diff --git a/specreduce/core.py b/specreduce/core.py index a6ed2a2..41d0eb2 100644 --- a/specreduce/core.py +++ b/specreduce/core.py @@ -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 @@ -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 @@ -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 diff --git a/specreduce/tests/test_tracing.py b/specreduce/tests/test_tracing.py index 7ecc32b..a9aa115 100644 --- a/specreduce/tests/test_tracing.py +++ b/specreduce/tests/test_tracing.py @@ -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):