@@ -133,14 +133,20 @@ def _to_trace(trace):
133
133
134
134
self .bkg_wimage = bkg_wimage
135
135
136
+ # mask user-highlighted and invalid values (if any) before taking stats
137
+ or_mask = (np .logical_or (~ np .isfinite (self .image .data ), self .image .mask )
138
+ if self .image .mask is not None
139
+ else ~ np .isfinite (self .image .data ))
140
+
136
141
if self .statistic == 'average' :
137
- self ._bkg_array = np .average (self .image .data ,
138
- weights = self .bkg_wimage ,
139
- axis = self .crossdisp_axis )
142
+ image_ma = np .ma .masked_array (self .image .data , mask = or_mask )
143
+ self ._bkg_array = np .ma .average (image_ma ,
144
+ weights = self .bkg_wimage ,
145
+ axis = self .crossdisp_axis ).data
140
146
elif self .statistic == 'median' :
141
- med_image = self . image . data . copy ( )
142
- med_image [ np .where (self .bkg_wimage ) == 0 ] = np . nan
143
- self ._bkg_array = np .nanmedian ( med_image , axis = self .crossdisp_axis )
147
+ med_mask = np . logical_or ( self . bkg_wimage == 0 , or_mask )
148
+ image_ma = np .ma . masked_array (self .image . data , mask = med_mask )
149
+ self ._bkg_array = np .ma . median ( image_ma , axis = self .crossdisp_axis ). data
144
150
else :
145
151
raise ValueError ("statistic must be 'average' or 'median'" )
146
152
@@ -232,7 +238,7 @@ def bkg_image(self, image=None):
232
238
233
239
Returns
234
240
-------
235
- Spectrum1D object with same shape as ``image``.
241
+ `~specutils. Spectrum1D` object with same shape as ``image``.
236
242
"""
237
243
image = self ._parse_image (image )
238
244
return Spectrum1D (np .tile (self ._bkg_array ,
@@ -261,11 +267,11 @@ def bkg_spectrum(self, image=None):
261
267
bkg_image = self .bkg_image (image )
262
268
263
269
try :
264
- return bkg_image .collapse (np .sum , axis = self .crossdisp_axis )
270
+ return bkg_image .collapse (np .nansum , axis = self .crossdisp_axis )
265
271
except u .UnitTypeError :
266
272
# can't collapse with a spectral axis in pixels because
267
273
# SpectralCoord only allows frequency/wavelength equivalent units...
268
- ext1d = np .sum (bkg_image .flux , axis = self .crossdisp_axis )
274
+ ext1d = np .nansum (bkg_image .flux , axis = self .crossdisp_axis )
269
275
return Spectrum1D (ext1d , bkg_image .spectral_axis )
270
276
271
277
def sub_image (self , image = None ):
@@ -280,7 +286,7 @@ def sub_image(self, image=None):
280
286
281
287
Returns
282
288
-------
283
- array with same shape as ``image``
289
+ `~specutils.Spectrum1D` object with same shape as ``image``.
284
290
"""
285
291
image = self ._parse_image (image )
286
292
@@ -313,11 +319,11 @@ def sub_spectrum(self, image=None):
313
319
sub_image = self .sub_image (image = image )
314
320
315
321
try :
316
- return sub_image .collapse (np .sum , axis = self .crossdisp_axis )
322
+ return sub_image .collapse (np .nansum , axis = self .crossdisp_axis )
317
323
except u .UnitTypeError :
318
324
# can't collapse with a spectral axis in pixels because
319
325
# SpectralCoord only allows frequency/wavelength equivalent units...
320
- ext1d = np .sum (sub_image .flux , axis = self .crossdisp_axis )
326
+ ext1d = np .nansum (sub_image .flux , axis = self .crossdisp_axis )
321
327
return Spectrum1D (ext1d , spectral_axis = sub_image .spectral_axis )
322
328
323
329
def __rsub__ (self , image ):
0 commit comments