Skip to content

Commit d358277

Browse files
committed
Merge pull request #94 from crawfordsm/cosmicray_fixes
Fixes to cosmic ray tasks and closes #76, closes #57, and closes #17
2 parents 9dcf927 + d081889 commit d358277

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

ccdproc/ccdproc.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ def cosmicray_median(data, thresh, background=None, mbox=11):
614614
Parameters
615615
----------
616616
617-
ccd : numpy ndarray or Mask arary object
617+
ccd : numpy.ndarray or numpy.MaskedArary
618618
Data to have cosmic ray cleans
619619
620620
thresh : float
@@ -640,10 +640,22 @@ def cosmicray_median(data, thresh, background=None, mbox=11):
640640
A boolean ndarray with the cosmic rays identified
641641
642642
"""
643+
if not isinstance(data, np.ndarray):
644+
raise TypeError('data is not a ndarray object')
645+
646+
if background is None:
647+
background = data.std()
648+
else:
649+
if not isinstance(background, (float, np.ndarray)):
650+
raise TypeError('Background is not a float or ndarray')
643651

644652
# create the median image
645653
marr = ndimage.median_filter(data, size=(mbox, mbox))
646654

655+
# Only look at the data array
656+
if isinstance(data, np.ma.MaskedArray):
657+
data = data.data
658+
647659
# Find the residual image
648660
rarr = (data - marr) / background
649661

@@ -654,7 +666,7 @@ def cosmicray_median(data, thresh, background=None, mbox=11):
654666

655667

656668
@log_to_metadata
657-
def cosmicray_clean(ccddata, thresh, cr_func, crargs=(),
669+
def cosmicray_clean(ccd, thresh, cr_func, crargs=(),
658670
background=None, bargs=(), gbox=0, rbox=0):
659671
"""
660672
Cosmic ray clean a ccddata object. This process will apply a cosmic ray
@@ -665,7 +677,7 @@ def cosmicray_clean(ccddata, thresh, cr_func, crargs=(),
665677
Parameters
666678
----------
667679
668-
ccddata : CCDData object
680+
ccd : CCDData object
669681
Data to have cosmic ray cleans
670682
671683
thresh : float
@@ -718,10 +730,10 @@ def cosmicray_clean(ccddata, thresh, cr_func, crargs=(),
718730
"""
719731

720732
# make a masked array that will be used for all calculations
721-
if ccddata.mask is None:
722-
data = ccddata.data
733+
if ccd.mask is None:
734+
data = ccd.data
723735
else:
724-
data = np.ma.masked_array(ccddata.data, ccddata.mask)
736+
data = np.ma.masked_array(ccd.data, ccd.mask)
725737

726738
if background is None:
727739
background = sigma_func(data)
@@ -731,21 +743,26 @@ def cosmicray_clean(ccddata, thresh, cr_func, crargs=(),
731743
# identify the cosmic rays
732744
crarr = cr_func(data, thresh, background, *crargs)
733745

746+
#create new output array
747+
newccd = ccd.copy()
748+
734749
# upate the mask
735-
if ccddata.mask is None:
736-
ccddata.mask = crarr
750+
if newccd.mask is None:
751+
newccd.mask = crarr
737752
else:
738-
ccddata.mask = ccddata.mask + crarr.mask
753+
newccd.mask = newccd.mask + crarr.mask
739754

740755
# grow the pixels
741756
if gbox > 0:
742-
ccddata.mask = ndimage.maximum_filter(ccddata.mask, gbox)
757+
newccd.mask = ndimage.maximum_filter(newccd.mask, gbox)
743758

759+
#replace bad pixels in the image
744760
if rbox > 0:
745-
data = np.ma.masked_array(ccddata.data, (ccddata.mask == 0))
761+
data = np.ma.masked_array(newccd.data, (newccd.mask == 0))
746762
mdata = ndimage.median_filter(data, rbox)
747-
ccddata.data[ccddata.mask > 0] = mdata[ccddata.mask > 0]
748-
return ccddata
763+
newccd.data[newccd.mask > 0] = mdata[newccd.mask > 0]
764+
765+
return newccd
749766

750767

751768
class Keyword(object):

ccdproc/tests/test_cosmicray.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ def test_cosmicray_clean_scalar_background(ccd_data, background_type):
4343
assert abs(cc.data.std() - scale) < 0.1
4444
assert ((testdata - cc.data) > 0).sum() == NCRAYS
4545

46+
@pytest.mark.data_scale(DATA_SCALE)
47+
def test_cosmicray_clean_gbox(ccd_data):
48+
scale = DATA_SCALE # yuck. Maybe use pytest.parametrize?
49+
threshold = 5
50+
add_cosmicrays(ccd_data, scale, threshold, ncrays=NCRAYS)
51+
testdata = 1.0 * ccd_data.data
52+
cc = ccd_data # currently here because no copy command for NDData
53+
cc = cosmicray_clean(cc, 5.0, cosmicray_median, crargs=(11,),
54+
background=background_variance_box, bargs=(25,),
55+
rbox=0, gbox=5)
56+
data = np.ma.masked_array(cc.data, cc.mask)
57+
assert abs(data.std() - scale) < 0.1
58+
assert cc.mask.sum() > NCRAYS
59+
60+
4661

4762
@pytest.mark.data_scale(DATA_SCALE)
4863
def test_cosmicray_clean(ccd_data):
@@ -75,12 +90,34 @@ def test_cosmicray_clean_rbox_zero_replaces_no_pixels(ccd_data):
7590
def test_cosmicray_median(ccd_data):
7691
threshold = 5
7792
add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS)
78-
crarr = cosmicray_median(ccd_data, 5, mbox=11, background=DATA_SCALE)
93+
crarr = cosmicray_median(ccd_data.data, 5, mbox=11, background=DATA_SCALE)
94+
95+
# check the number of cosmic rays detected
96+
assert crarr.sum() == NCRAYS
97+
98+
@pytest.mark.data_scale(DATA_SCALE)
99+
def test_cosmicray_median_masked(ccd_data):
100+
threshold = 5
101+
add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS)
102+
data = np.ma.masked_array(ccd_data.data, (ccd_data.data>-1e6))
103+
crarr = cosmicray_median(data, 5, mbox=11, background=DATA_SCALE)
79104

80105
# check the number of cosmic rays detected
81106
assert crarr.sum() == NCRAYS
82107

83108

109+
@pytest.mark.data_scale(DATA_SCALE)
110+
def test_cosmicray_median_background_None(ccd_data):
111+
threshold = 5
112+
add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS)
113+
crarr = cosmicray_median(ccd_data.data, 5, mbox=11, background=None)
114+
115+
# check the number of cosmic rays detected
116+
assert crarr.sum() == NCRAYS
117+
118+
119+
120+
84121
def test_background_variance_box():
85122
with NumpyRNGContext(123):
86123
scale = 5.3

0 commit comments

Comments
 (0)