Skip to content

Commit

Permalink
Merge pull request #31 from mwcraig/tests-100-pct
Browse files Browse the repository at this point in the history
This pull requests brings test coverage close to 100%
  • Loading branch information
mwcraig committed Mar 20, 2014
2 parents fd16da0 + 6a80b78 commit 6d4d132
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .coveragerc
@@ -0,0 +1,7 @@
[run]
branch = True
omit =
ccdproc/__init__*
ccdproc/conftest.py
ccdproc/*tests/*
ccdproc/*version*
7 changes: 2 additions & 5 deletions ccdproc/ccdproc.py
@@ -1,13 +1,10 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# This module implements the base CCDPROC functions
import inspect
import numpy as np

from ccddata import CCDData, electrons
from ccddata import CCDData

from astropy import units as u
from astropy.units.quantity import Quantity
from astropy.nddata.nduncertainty import StdDevUncertainty
from astropy.modeling import fitting
from astropy import stats

Expand Down Expand Up @@ -47,7 +44,7 @@ def subtract_overscan(ccd, overscan, median=False, model=None):
ccd : CCDData object
CCDData object with overscan subtracted
"""
if not (isinstance(ccd, CCDData) or isinstance(overscan, np.ndarray)):
if not (isinstance(ccd, CCDData) or isinstance(ccd, np.ndarray)):
raise TypeError('ccddata is not a CCDData or ndarray object')
if not (isinstance(overscan, CCDData) or isinstance(overscan, np.ndarray)):
raise TypeError('overscan is not a CCDData or ndarray object')
Expand Down
23 changes: 21 additions & 2 deletions ccdproc/tests/test_ccddata.py
Expand Up @@ -4,11 +4,10 @@
import numpy as np
from astropy.io import fits

from numpy.testing import assert_array_equal
from astropy.tests.helper import pytest
from astropy.utils import NumpyRNGContext

from ..ccddata import CCDData, electrons, fromFITS
from ..ccddata import CCDData, electrons, fromFITS, toFITS


def test_ccddata_empty():
Expand Down Expand Up @@ -86,6 +85,26 @@ def test_create_variance():
assert cd.uncertainty.array.size == 100
assert cd.uncertainty.array.dtype == np.dtype(float)


def test_setting_bad_uncertainty_raises_error():
cd = CCDData(np.ones((100, 100)))
with pytest.raises(TypeError):
# Uncertainty is supposed to be an instance of NDUncertainty
cd.uncertainty = 10


def test_create_variance_with_bad_image_units_raises_error():
cd = CCDData(np.ones((100, 100)))
with pytest.raises(TypeError):
cd.create_variance(10)


def test_toFITS():
cd = CCDData(np.ones((100, 100)), meta={'observer': 'Edwin Hubble'})
fits_hdulist = toFITS(cd)
assert isinstance(fits_hdulist, fits.HDUList)


if __name__ == '__main__':
test_ccddata_empty()
test_ccddata_simple()
Expand Down
8 changes: 6 additions & 2 deletions ccdproc/tests/test_ccdproc.py
Expand Up @@ -10,7 +10,7 @@
from astropy.tests.helper import pytest
from astropy.utils import NumpyRNGContext

from ..ccddata import CCDData, electrons, adu, fromFITS, toFITS
from ..ccddata import CCDData, electrons, adu
from ..ccdproc import *

# tests for overscan
Expand Down Expand Up @@ -64,7 +64,11 @@ def test_subtract_overscan_model():
assert abs(ccd.data.mean()) < 0.1


def test_sutract_overscan_ccd_failt():
def test_subtract_overscan_ccd_fails():
# do we get an error if the *image* is neither an nor an array?
with pytest.raises(TypeError):
subtract_overscan(3, np.zeros((5, 5)))
# do we get an error if the *overscan* is not an image or an array?
with pytest.raises(TypeError):
subtract_overscan(np.zeros((10, 10)), 3, median=False, model=None)

Expand Down
43 changes: 40 additions & 3 deletions ccdproc/tests/test_cosmicray.py
Expand Up @@ -4,11 +4,11 @@
import numpy as np
from astropy.io import fits

from numpy.testing import assert_array_equal
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest
from astropy.utils import NumpyRNGContext

from ..ccddata import CCDData, electrons, fromFITS, toFITS
from ..ccddata import CCDData, toFITS
from ..ccdproc import *

import os
Expand All @@ -21,7 +21,7 @@ def writeout(cd, outfile):
hdu.writeto(outfile)


def test_cosmicray_clean_scalarbackground():
def test_cosmicray_clean_scalar_background():
with NumpyRNGContext(125):
scale = 5.3
size = 100
Expand All @@ -40,6 +40,25 @@ def test_cosmicray_clean_scalarbackground():
assert (testdata - cc.data > 0).sum() == ncrays


def test_cosmicray_clean_no_background():
with NumpyRNGContext(125):
scale = 5.3
size = 100
data = np.random.normal(loc=0, size=(size, size), scale=scale)
cd = CCDData(data, meta=fits.header.Header())
ncrays = 30
crrays = np.random.random_integers(0, size - 1, size=(ncrays, 2))
crflux = 10 * scale * np.random.random(30) + 5 * scale
for i in range(ncrays):
y, x = crrays[i]
cd.data[y, x] = crflux[i]
testdata = 1.0 * cd.data
cc = cosmicray_clean(cd, 5, cosmicray_median, crargs=(11,),
background=None, bargs=(), rbox=11, gbox=0)
assert abs(cc.data.std() - scale) < 0.1
assert (testdata - cc.data > 0).sum() == ncrays


def test_cosmicray_clean():
with NumpyRNGContext(125):
scale = 5.3
Expand All @@ -62,6 +81,24 @@ def test_cosmicray_clean():
assert (testdata - cc.data > 0).sum() == 30


def test_cosmicray_clean_rbox_zero_replaces_no_pixels():
with NumpyRNGContext(125):
scale = 5.3
size = 100
data = np.random.normal(loc=0, size=(size, size), scale=scale)
cd = CCDData(data, meta=fits.header.Header())
ncrays = 30
crrays = np.random.random_integers(0, size - 1, size=(ncrays, 2))
crflux = 10 * scale * np.random.random(30) + 5 * scale
for i in range(ncrays):
y, x = crrays[i]
cd.data[y, x] = crflux[i]
testdata = 1.0 * cd.data
cc = cosmicray_clean(cd, 5, cosmicray_median, crargs=(11,),
background=scale, bargs=(), rbox=0, gbox=0)
assert_allclose(cc, testdata)


def test_cosmicray_median():
with NumpyRNGContext(125):
scale = 5.3
Expand Down

0 comments on commit 6d4d132

Please sign in to comment.