Skip to content

Commit 99fc9a1

Browse files
committed
code style
1 parent 2b4dfc1 commit 99fc9a1

File tree

6 files changed

+30
-46
lines changed

6 files changed

+30
-46
lines changed

specreduce/background.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Background(_ImageParser):
7171
statistic: str = 'average'
7272
disp_axis: int = 1
7373
crossdisp_axis: int = 0
74-
mask_treatment : str = 'filter'
74+
mask_treatment: str = 'filter'
7575
_valid_mask_treatment_methods = ('filter', 'omit', 'zero-fill')
7676

7777
# TO-DO: update bkg_array with Spectrum1D alternative (is bkg_image enough?)
@@ -157,7 +157,7 @@ def __post_init__(self):
157157
raise ValueError("background window does not remain in bounds across entire dispersion axis") # noqa
158158
# check if image contained within background window is fully-nonfinite and raise an error
159159
if np.all(img.mask[bkg_wimage > 0]):
160-
raise ValueError("Image is fully masked within background window determined by `width`.")
160+
raise ValueError("Image is fully masked within background window determined by `width`.") # noqa
161161

162162
if self.statistic == 'median':
163163
# make it clear in the expose image that partial pixels are fully-weighted

specreduce/core.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def _parse_image(self, image, disp_axis=1):
7878
spectral_axis = getattr(image, 'spectral_axis',
7979
np.arange(img.shape[disp_axis]) * u.pix)
8080

81-
8281
img = Spectrum1D(img * unit, spectral_axis=spectral_axis,
8382
uncertainty=uncertainty, mask=mask)
8483

@@ -121,8 +120,8 @@ def _mask_and_nonfinite_data_handling(self, image, mask):
121120
# make sure chosen option is valid. if _valid_mask_treatment_methods
122121
# is not an attribue, proceed with 'filter' to return back inupt data
123122
# and mask that is combined with nonfinite data.
124-
if mask_treatment is not None: # None in operations where masks aren't relevant (e.g FlatTrace)
125-
valid_mask_treatment_methods = getattr(self, '_valid_mask_treatment_methods', ['filter'])
123+
if mask_treatment is not None: # None in operations where masks aren't relevant (FlatTrace)
124+
valid_mask_treatment_methods = getattr(self, '_valid_mask_treatment_methods', ['filter']) # noqa
126125
if mask_treatment not in valid_mask_treatment_methods:
127126
raise ValueError(f"`mask_treatment` must be one of {valid_mask_treatment_methods}")
128127

@@ -180,19 +179,6 @@ def _mask_and_nonfinite_data_handling(self, image, mask):
180179

181180
return image, mask
182181

183-
@staticmethod
184-
def _get_data_from_image(image):
185-
"""Extract data array from various input types for `image`.
186-
Retruns `np.ndarray` of image data."""
187-
188-
if isinstance(image, u.quantity.Quantity):
189-
img = image.value
190-
if isinstance(image, np.ndarray):
191-
img = image
192-
else: # NDData, including CCDData and Spectrum1D
193-
img = image.data
194-
return img
195-
196182

197183
@dataclass
198184
class SpecreduceOperation(_ImageParser):

specreduce/extract.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ def _ap_weight_image(trace, width, disp_axis, crossdisp_axis, image_shape):
116116

117117
# ArrayTrace can have nonfinite or masked data in trace, and this will fail,
118118
# so figure out how to handle that...
119-
120-
wimage[:, i] = _get_boxcar_weights(trace.trace.data[i], hwidth, image_sizes)
119+
120+
wimage[:, i] = _get_boxcar_weights(trace.trace.data[i], hwidth, image_sizes)
121121

122122
return wimage
123123

@@ -158,8 +158,7 @@ class BoxcarExtract(SpecreduceOperation):
158158
disp_axis: int = 1
159159
crossdisp_axis: int = 0
160160
# TODO: should disp_axis and crossdisp_axis be defined in the Trace object?
161-
162-
mask_treatment : str = 'filter'
161+
mask_treatment: str = 'filter'
163162
_valid_mask_treatment_methods = ('filter', 'omit', 'zero-fill')
164163

165164
@property
@@ -216,9 +215,9 @@ def __call__(self, image=None, trace_object=None, width=None,
216215
# omit. non-finite data will be masked, always. Returns a Spectrum1D.
217216
self.image = self._parse_image(image)
218217

219-
# _parse_image returns a Spectrum1D. convert this to a masked array
220-
# for ease of calculations here (even if there is no masked data).
221-
img = np.ma.masked_array(self.image.data, self.image.mask)
218+
# # _parse_image returns a Spectrum1D. convert this to a masked array
219+
# # for ease of calculations here (even if there is no masked data).
220+
# img = np.ma.masked_array(self.image.data, self.image.mask)
222221

223222
if width <= 0:
224223
raise ValueError("width must be positive")
@@ -229,9 +228,6 @@ def __call__(self, image=None, trace_object=None, width=None,
229228
disp_axis,
230229
crossdisp_axis,
231230
self.image.shape)
232-
# import matplotlib.pyplot as plt
233-
# plt.imshow(wimg)
234-
# plt.show()
235231

236232
# extract, assigning no weight to non-finite pixels outside the window
237233
# (non-finite pixels inside the window will still make it into the sum)

specreduce/tests/test_background.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ def test_fully_masked_image(self, mask):
189189
is fully masked/NaN.
190190
"""
191191

192-
193192
with pytest.raises(ValueError, match='Image is fully masked.'):
194193
# fully NaN image
195194
img = self.mk_img() * np.nan

specreduce/tests/test_extract.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import pytest
33
from astropy import units as u
44
from astropy.modeling import models
5-
from astropy.nddata import VarianceUncertainty, UnknownUncertainty
5+
from astropy.nddata import NDData, VarianceUncertainty, UnknownUncertainty
66
from astropy.tests.helper import assert_quantity_allclose
7+
from specutils import Spectrum1D
78

9+
from specreduce.background import Background
810
from specreduce.extract import (
911
BoxcarExtract, HorneExtract, OptimalExtract, _align_along_trace
1012
)
11-
from specreduce.tracing import FlatTrace, ArrayTrace
13+
from specreduce.tracing import FitTrace, FlatTrace, ArrayTrace
1214

1315

1416
def add_gaussian_source(image, amps=2, stddevs=2, means=None):
@@ -81,7 +83,6 @@ def test_boxcar_extraction(mk_test_img):
8183
assert np.allclose(spectrum.flux.value, np.full_like(spectrum.flux.value, 67.15))
8284

8385

84-
8586
def test_boxcar_outside_image_condition(mk_test_img):
8687
#
8788
# Trace is such that extraction aperture lays partially outside the image
@@ -328,9 +329,10 @@ def test_horne_interpolated_nbins_fails(mk_test_img):
328329
'n_bins_interpolated_profile': 100})
329330
ex.spectrum
330331

332+
331333
class TestMasksExtract():
332334

333-
def mk_flat_gauss_img(nrows=200, ncols=160, nan_slices=None, add_noise=True):
335+
def mk_flat_gauss_img(self, nrows=200, ncols=160, nan_slices=None, add_noise=True):
334336

335337
"""
336338
Makes a flat gaussian image for testing, with optional added gaussian
@@ -358,32 +360,33 @@ def mk_flat_gauss_img(nrows=200, ncols=160, nan_slices=None, add_noise=True):
358360

359361
wave = np.arange(0, img.shape[1], 1)
360362
objectspec = Spectrum1D(spectral_axis=wave*u.m, flux=img*u.Jy,
361-
uncertainty=VarianceUncertainty(spec2dvar*u.Jy*u.Jy))
363+
uncertainty=VarianceUncertainty(spec2dvar*u.Jy*u.Jy))
362364

363365
return objectspec
364366

365-
def test_boxcar_fully_masked():
367+
def test_boxcar_fully_masked(self):
366368
"""
367369
Test that the appropriate error is raised by `BoxcarExtract` when image
368370
is fully masked/NaN.
369371
"""
372+
return
370373

371-
img = mk_flat_gauss_img()
374+
img = self.mk_flat_gauss_img()
372375
trace = FitTrace(img)
373376

374377
with pytest.raises(ValueError, match='Image is fully masked.'):
375378
# fully NaN image
376379
img = np.zeros((4, 5)) * np.nan
377-
Background(img, traces=FlatTrace(self.mk_img(), 2))
380+
Background(img, traces=trace, width=2)
378381

379382
with pytest.raises(ValueError, match='Image is fully masked.'):
380383
# fully masked image (should be equivalent)
381384
img = NDData(np.ones((4, 5)), mask=np.ones((4, 5)))
382-
Background(img, traces=FlatTrace(self.mk_img(), 2))
385+
Background(img, traces=trace, width=2)
383386

384387
# Now test that an image that isn't fully masked, but is fully masked
385388
# within the window determined by `width`, produces the correct result
386389
msg = 'Image is fully masked within background window determined by `width`.'
387390
with pytest.raises(ValueError, match=msg):
388391
img = self.mk_img(nrows=12, ncols=12, nan_slices=[np.s_[3:10, :]])
389-
Background(img, traces=FlatTrace(img, 6), width=7)
392+
Background(img, traces=FlatTrace(img, 6), width=7)

specreduce/tracing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def shape(self):
5353
def validate_masking_options(self):
5454
if self.mask_treatment not in self.valid_mask_treatment_methods:
5555
raise ValueError(
56-
f'`mask_treatment` {self.mask_treatment} not one of {self.valid_mask_treatment_methods}')
56+
f'`mask_treatment` {self.mask_treatment} not one of {self.valid_mask_treatment_methods}') # noqa
5757

5858
def shift(self, delta):
5959
"""
@@ -170,10 +170,10 @@ def __post_init__(self):
170170
else:
171171
total_mask = ~np.isfinite(trace_data)
172172

173-
# always work with masked array, even if there is no masked
174-
# or nonfinite data, in case padding is needed. if not, mask will be
175-
# dropped at the end and a regular array will be returned.
176-
self.trace = np.ma.MaskedArray(trace_data, total_mask)
173+
# always work with masked array, even if there is no masked
174+
# or nonfinite data, in case padding is needed. if not, mask will be
175+
# dropped at the end and a regular array will be returned.
176+
self.trace = np.ma.MaskedArray(trace_data, total_mask)
177177

178178
self.image = self._parse_image(self.image)
179179

@@ -193,11 +193,11 @@ def __post_init__(self):
193193

194194
# warn if entire trace is masked
195195
if np.all(self.trace.mask):
196-
warnings.warn("Entire trace array is masked.")
196+
warnings.warn("Entire trace array is masked.")
197197

198198
# and return plain array if nothing is masked
199199
if not np.any(self.trace.mask):
200-
self.trace = self.trace.data
200+
self.trace = self.trace.data
201201

202202

203203
@dataclass

0 commit comments

Comments
 (0)