From 2c7f03ca28fecdbdc5cf8a7c5e0a7282fdbd4885 Mon Sep 17 00:00:00 2001 From: Steven Crawford Date: Wed, 23 Jul 2014 17:26:52 +0200 Subject: [PATCH] Removed variance names from CR tasks --- ccdproc/core.py | 106 ++++++++++++++++---------------- ccdproc/tests/test_cosmicray.py | 48 +++++++-------- 2 files changed, 76 insertions(+), 78 deletions(-) diff --git a/ccdproc/core.py b/ccdproc/core.py index 3a820079..6490ae28 100644 --- a/ccdproc/core.py +++ b/ccdproc/core.py @@ -19,7 +19,7 @@ from .utils.slices import slice_from_string from .log_meta import log_to_metadata -__all__ = ['background_variance_box', 'background_variance_filter', +__all__ = ['background_deviation_box', 'background_deviation_filter', 'cosmicray_median', 'cosmicray_lacosmic', 'create_variance', 'flat_correct', 'gain_correct', 'rebin', 'sigma_func', 'subtract_bias', 'subtract_dark', 'subtract_overscan', @@ -28,8 +28,8 @@ # The dictionary below is used to translate actual function names to names # that are FITS compliant, i.e. 8 characters or less. _short_names = { - 'background_variance_box': 'bakvarbx', - 'background_variance_filter': 'bakvfilt', + 'background_deviation_box': 'bakdevbx', + 'background_deviation_filter': 'bakdfilt', 'cosmicray_median': 'crmedian', 'create_variance': 'creatvar', 'flat_correct': 'flatcor', @@ -116,7 +116,7 @@ def subtract_overscan(ccd, overscan=None, overscan_axis=1, fits_section=None, Parameters ---------- ccd : `~ccdproc.ccddata.CCDData` - Data to have variance frame corrected + Data to have overscan frame corrected overscan : `~ccdproc.ccddata.CCDData` Slice from `ccd` that contains the overscan. Must provide either @@ -412,7 +412,7 @@ def gain_correct(ccd, gain, gain_unit=None): Parameters ---------- ccd : `~ccdproc.ccddata.CCDData` - Data to have variance frame corrected + Data to have gain corrected gain : `~astropy.units.Quantity` or `~ccdproc.ccdproc.Keyword` gain value for the image expressed in electrons per adu @@ -562,7 +562,7 @@ def transform_image(ccd, transform_func, **kwargs): def sigma_func(arr): """ - Robust method for calculating the variance of an array. ``sigma_func`` uses + Robust method for calculating the deviation of an array. ``sigma_func`` uses the median absolute deviation to determine the variance. Parameters @@ -623,20 +623,20 @@ def setbox(x, y, mbox, xmax, ymax): return x1, x2, y1, y2 -def background_variance_box(data, bbox): +def background_deviation_box(data, bbox): """ - Determine the background variance with a box size of bbox. The algorithm - steps through the image and calculates the variance within each box. - It returns an array with the pixels in each box filled with the variance + Determine the background deviation with a box size of bbox. The algorithm + steps through the image and calculates the deviation within each box. + It returns an array with the pixels in each box filled with the deviation value. Parameters ---------- data : `~numpy.ndarray` or `~numpy.ma.MaskedArray` - Data to measure background variance + Data to measure background deviation bbox : int - Box size for calculating background variance + Box size for calculating background deviation Raises ------ @@ -646,7 +646,7 @@ def background_variance_box(data, bbox): Returns ------- background : `~numpy.ndarray` or `~numpy.ma.MaskedArray` - An array with the measured background variance in each pixel + An array with the measured background deviation in each pixel """ # Check to make sure the background box is an appropriate size @@ -665,18 +665,18 @@ def background_variance_box(data, bbox): return barr -def background_variance_filter(data, bbox): +def background_deviation_filter(data, bbox): """ - Determine the background variance for each pixel from a box with size of + Determine the background deviation for each pixel from a box with size of bbox. Parameters ---------- data : `~numpy.ndarray` - Data to measure background variance + Data to measure background deviation bbox : int - Box size for calculating background variance + Box size for calculating background deviation Raises ------ @@ -686,7 +686,7 @@ def background_variance_filter(data, bbox): Returns ------- background : `~numpy.ndarray` or `~numpy.ma.MaskedArray` - An array with the measured background variance in each pixel + An array with the measured background deviation in each pixel """ # Check to make sure the background box is an appropriate size @@ -831,7 +831,7 @@ def _blkavg(data, newshape): return eval(''.join(evList)) -def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, +def cosmicray_lacosmic(ccd, error_image=None, thresh=5, fthresh=5, gthresh=1.5, b_factor=2, mbox=5, min_limit=0.01, gbox=0, rbox=0, f_conv=np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]])): @@ -847,8 +847,8 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, ccd: `~ccdproc.CCDData` or `numpy.ndarray` Data to have cosmic ray cleaned - variance_image : `numpy.ndarray` - Background variance level. It should have the same shape as data + error_image : `numpy.ndarray` + Error level in the image. It should have the same shape as data as data. This is the same as the noise array in lacosmic.cl thresh : float @@ -913,11 +913,11 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, 1) Given an numpy.ndarray object, the syntax for running cosmicrar_lacosmic would be: - >>> newdata, mask = cosmicray_clean(data, variance_image=variance_image, + >>> newdata, mask = cosmicray_clean(data, error_image=error_image, thresh=5, mbox=11, rbox=11, gbox=5) - where the variance is an array that is the same shape as data but - includes the pixel variance. This would return a data array, newdata, + where the error is an array that is the same shape as data but + includes the pixel error. This would return a data array, newdata, with the bad pixels replaced by the local median from a box of 11 pixels; and it would return a mask indicating the bad pixels. @@ -935,10 +935,10 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, if isinstance(ccd, np.ndarray): data = ccd - if not isinstance(variance_image, np.ndarray): - raise TypeError('variance_image is not a ndarray object') - if data.shape != variance_image.shape: - raise ValueError('variance_image is not the same shape as data') + if not isinstance(error_image, np.ndarray): + raise TypeError('error_image is not a ndarray object') + if data.shape != error_image.shape: + raise ValueError('error_image is not the same shape as data') #set up a copy of the array and original shape shape = data.shape @@ -955,7 +955,7 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, ldata = _blkavg(ldata, shape) #median the noise image - med_noise = ndimage.median_filter(variance_image, size=(mbox, mbox)) + med_noise = ndimage.median_filter(error_image, size=(mbox, mbox)) #create S/N image sndata = ldata / med_noise / b_factor @@ -974,9 +974,7 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, # set a minimum value for all pixels so no divide by zero problems fdata[fdata < min_limit] = min_limit - fdata = sndata * masks / fdata - maskf = (fdata > fthresh) #make the list of cosmic rays crarr = masks * (fdata > fthresh) @@ -1000,14 +998,14 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, elif isinstance(ccd, CCDData): - #set up the variance image - if variance_image is None and ccd.uncertainty is not None: - variance_image = ccd.uncertainty.array - if ccd.data.shape != variance_image.shape: - raise ValueError('variance_image is not the same shape as data') + #set up the error image + if error_image is None and ccd.uncertainty is not None: + error_image = ccd.uncertainty.array + if ccd.data.shape != error_image.shape: + raise ValueError('error_image is not the same shape as data') data, crarr = cosmicray_lacosmic(ccd.data, - variance_image=variance_image, + error_image=error_image, thresh=thresh, fthresh=fthresh, gthresh=gthresh, b_factor=b_factor, mbox=mbox, min_limit=min_limit, @@ -1026,7 +1024,7 @@ def cosmicray_lacosmic(ccd, variance_image=None, thresh=5, fthresh=5, raise TypeError('ccddata is not a CCDData or ndarray object') -def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, +def cosmicray_median(ccd, error_image=None, thresh=5, mbox=11, gbox=0, rbox=0): """ Identify cosmic rays through median technique. The median technique @@ -1042,8 +1040,8 @@ def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, thresh : float Threshhold for detecting cosmic rays - variance_image : None, float, or `~numpy.ndarray` - Background variance level. If None, the task will use the standard + error_image : None, float, or `~numpy.ndarray` + Error level. If None, the task will use the standard deviation of the data. If an ndarray, it should have the same shape as data. @@ -1080,11 +1078,11 @@ def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, 1) Given an numpy.ndarray object, the syntax for running cosmicray_lacosmic would be: - >>> newdata, mask = cosmicray_lacosmic(data, variance_image=variance, + >>> newdata, mask = cosmicray_lacosmic(data, error_image=error, thresh=5, mbox=11, rbox=11, gbox=5) - where variance is an array that is the same shape as data but - includes the pixel variance. This would return a data array, newdata, + where error is an array that is the same shape as data but + includes the pixel error. This would return a data array, newdata, with the bad pixels replaced by the local median from a box of 11 pixels; and it would return a mask indicating the bad pixels. @@ -1102,11 +1100,11 @@ def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, if isinstance(ccd, np.ndarray): data = ccd - if variance_image is None: - variance_image = data.std() + if error_image is None: + error_image = data.std() else: - if not isinstance(variance_image, (float, np.ndarray)): - raise TypeError('variance_image is not a float or ndarray') + if not isinstance(error_image, (float, np.ndarray)): + raise TypeError('error_image is not a float or ndarray') # create the median image marr = ndimage.median_filter(data, size=(mbox, mbox)) @@ -1116,7 +1114,7 @@ def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, data = data.data # Find the residual image - rarr = (data - marr) / variance_image + rarr = (data - marr) / error_image # identify all sources crarr = (rarr > thresh) @@ -1135,13 +1133,13 @@ def cosmicray_median(ccd, variance_image=None, thresh=5, mbox=11, gbox=0, return ndata, crarr elif isinstance(ccd, CCDData): - #set up the variance image - if variance_image is None and ccd.uncertainty is not None: - variance_image = ccd.uncertainty.array - if ccd.data.shape != variance_image.shape: - raise ValueError('variance_image is not the same shape as data') + #set up the error image + if error_image is None and ccd.uncertainty is not None: + error_image = ccd.uncertainty.array + if ccd.data.shape != error_image.shape: + raise ValueError('error_image is not the same shape as data') - data, crarr = cosmicray_median(ccd.data, variance_image=variance_image, + data, crarr = cosmicray_median(ccd.data, error_image=error_image, thresh=thresh, mbox=mbox, gbox=gbox, rbox=rbox) diff --git a/ccdproc/tests/test_cosmicray.py b/ccdproc/tests/test_cosmicray.py index c18c10a9..a928578b 100644 --- a/ccdproc/tests/test_cosmicray.py +++ b/ccdproc/tests/test_cosmicray.py @@ -47,8 +47,8 @@ def test_cosmicray_lacosmic_gbox(ccd_data): scale = DATA_SCALE # yuck. Maybe use pytest.parametrize? threshold = 5 add_cosmicrays(ccd_data, scale, threshold, ncrays=NCRAYS) - variance = ccd_data.data*0.0+DATA_SCALE - data, crarr = cosmicray_lacosmic(ccd_data.data, variance_image=variance, + error = ccd_data.data*0.0+DATA_SCALE + data, crarr = cosmicray_lacosmic(ccd_data.data, error_image=error, thresh=5, mbox=11, rbox=0, gbox=5) data = np.ma.masked_array(data, crarr) assert abs(data.std() - scale) < 0.1 @@ -60,8 +60,8 @@ def test_cosmicray_lacosmic_rbox(ccd_data): scale = DATA_SCALE # yuck. Maybe use pytest.parametrize? threshold = 5 add_cosmicrays(ccd_data, scale, threshold, ncrays=NCRAYS) - variance = ccd_data.data*0.0+DATA_SCALE - data, crarr = cosmicray_lacosmic(ccd_data.data, variance_image=variance, + error = ccd_data.data*0.0+DATA_SCALE + data, crarr = cosmicray_lacosmic(ccd_data.data, error_image=error, thresh=5, mbox=11, rbox=21, gbox=5) assert data[crarr].mean() < ccd_data.data[crarr].mean() assert crarr.sum() > NCRAYS @@ -88,7 +88,7 @@ def test_cosmicray_lacosmic_check_data(ccd_data): @pytest.mark.data_scale(DATA_SCALE) -def test_cosmicray_lacosmic_check_variance_image(ccd_data): +def test_cosmicray_lacosmic_check_error_image(ccd_data): with pytest.raises(TypeError): noise = DATA_SCALE * np.ones_like(ccd_data.data) cosmicray_lacosmic(ccd_data.data, 10, thresh=5, @@ -108,7 +108,7 @@ def test_cosmicray_lacosmic_check_shape(ccd_data): def test_cosmicray_median_check_data(ccd_data): with pytest.raises(TypeError): ndata, crarr = cosmicray_median(10, thresh=5, mbox=11, - variance_image=DATA_SCALE) + error_image=DATA_SCALE) @pytest.mark.data_scale(DATA_SCALE) @@ -116,7 +116,7 @@ def test_cosmicray_median(ccd_data): threshold = 5 add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS) ndata, crarr = cosmicray_median(ccd_data.data, thresh=5, mbox=11, - variance_image=DATA_SCALE) + error_image=DATA_SCALE) # check the number of cosmic rays detected assert crarr.sum() == NCRAYS @@ -128,7 +128,7 @@ def test_cosmicray_median_ccddata(ccd_data): add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS) ccd_data.uncertainty = ccd_data.data*0.0+DATA_SCALE nccd = cosmicray_median(ccd_data, thresh=5, mbox=11, - variance_image=None) + error_image=None) # check the number of cosmic rays detected assert nccd.mask.sum() == NCRAYS @@ -140,7 +140,7 @@ def test_cosmicray_median_masked(ccd_data): add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS) data = np.ma.masked_array(ccd_data.data, (ccd_data.data > -1e6)) ndata, crarr = cosmicray_median(data, thresh=5, mbox=11, - variance_image=DATA_SCALE) + error_image=DATA_SCALE) # check the number of cosmic rays detected assert crarr.sum() == NCRAYS @@ -151,7 +151,7 @@ def test_cosmicray_median_background_None(ccd_data): threshold = 5 add_cosmicrays(ccd_data, DATA_SCALE, threshold, ncrays=NCRAYS) data, crarr = cosmicray_median(ccd_data.data, thresh=5, mbox=11, - variance_image=None) + error_image=None) # check the number of cosmic rays detected assert crarr.sum() == NCRAYS @@ -162,8 +162,8 @@ def test_cosmicray_median_gbox(ccd_data): scale = DATA_SCALE # yuck. Maybe use pytest.parametrize? threshold = 5 add_cosmicrays(ccd_data, scale, threshold, ncrays=NCRAYS) - variance = ccd_data.data*0.0+DATA_SCALE - data, crarr = cosmicray_median(ccd_data.data, variance_image=variance, + error = ccd_data.data*0.0+DATA_SCALE + data, crarr = cosmicray_median(ccd_data.data, error_image=error, thresh=5, mbox=11, rbox=0, gbox=5) data = np.ma.masked_array(data, crarr) assert crarr.sum() > NCRAYS @@ -175,47 +175,47 @@ def test_cosmicray_median_rbox(ccd_data): scale = DATA_SCALE # yuck. Maybe use pytest.parametrize? threshold = 5 add_cosmicrays(ccd_data, scale, threshold, ncrays=NCRAYS) - variance = ccd_data.data*0.0+DATA_SCALE - data, crarr = cosmicray_median(ccd_data.data, variance_image=variance, + error = ccd_data.data*0.0+DATA_SCALE + data, crarr = cosmicray_median(ccd_data.data, error_image=error, thresh=5, mbox=11, rbox=21, gbox=5) assert data[crarr].mean() < ccd_data.data[crarr].mean() assert crarr.sum() > NCRAYS @pytest.mark.data_scale(DATA_SCALE) -def test_cosmicray_median_background_error(ccd_data): +def test_cosmicray_median_background_deviation(ccd_data): with pytest.raises(TypeError): crarr = cosmicray_median(ccd_data.data, thresh=5, mbox=11, - variance_image='blank') + error_image='blank') -def test_background_variance_box(): +def test_background_deviation_box(): with NumpyRNGContext(123): scale = 5.3 cd = np.random.normal(loc=0, size=(100, 100), scale=scale) - bd = background_variance_box(cd, 25) + bd = background_deviation_box(cd, 25) assert abs(bd.mean() - scale) < 0.10 -def test_background_variance_box_fail(): +def test_background_deviation_box_fail(): with NumpyRNGContext(123): scale = 5.3 cd = np.random.normal(loc=0, size=(100, 100), scale=scale) with pytest.raises(ValueError): - background_variance_box(cd, 0.5) + background_deviation_box(cd, 0.5) -def test_background_variance_filter(): +def test_background_deviation_filter(): with NumpyRNGContext(123): scale = 5.3 cd = np.random.normal(loc=0, size=(100, 100), scale=scale) - bd = background_variance_filter(cd, 25) + bd = background_deviation_filter(cd, 25) assert abs(bd.mean() - scale) < 0.10 -def test_background_variance_filter_fail(): +def test_background_deviation_filter_fail(): with NumpyRNGContext(123): scale = 5.3 cd = np.random.normal(loc=0, size=(100, 100), scale=scale) with pytest.raises(ValueError): - background_variance_filter(cd, 0.5) + background_deviation_filter(cd, 0.5)