Skip to content

Commit

Permalink
Merge ad7124e into f0958fe
Browse files Browse the repository at this point in the history
  • Loading branch information
mwcraig committed May 9, 2014
2 parents f0958fe + ad7124e commit d9255f8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
12 changes: 9 additions & 3 deletions ccdproc/ccdproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,23 @@ def subtract_overscan(ccd, overscan=None, fits_section=None,
if fits_section is not None:
overscan = ccd[slice_from_string(fits_section, fits_convention=True)]

# Assume axis with the smaller dimension is the one to aggregate over
oscan_axis = 1 if overscan.shape[0] > overscan.shape[1] else 0

if median:
oscan = np.median(overscan.data, axis=1)
oscan = np.median(overscan.data, axis=oscan_axis)
else:
oscan = np.mean(overscan.data, axis=1)
oscan = np.mean(overscan.data, axis=oscan_axis)

if model is not None:
of = fitting.LinearLSQFitter()
yarr = np.arange(len(oscan))
oscan = of(model, yarr, oscan)
oscan = oscan(yarr)
oscan = np.reshape(oscan, (oscan.size, 1))
if oscan_axis == 1:
oscan = np.reshape(oscan, (oscan.size, 1))
else:
oscan = np.reshape(oscan, (1, oscan.size))
else:
oscan = np.reshape(oscan, oscan.shape + (1,))

Expand Down
48 changes: 37 additions & 11 deletions ccdproc/tests/test_ccdproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,21 @@ def test_create_variance_keywords_must_have_unit(ccd_data):


# tests for overscan
@pytest.mark.parametrize('median,model', [
(False, None),
(True, None), ])
def test_subtract_overscan_mean(ccd_data, median, model):
@pytest.mark.parametrize('median,transpose', [
(False, False),
(False, True),
(True, False), ])
def test_subtract_overscan(ccd_data, median, transpose):
# create the overscan region
oscan = 300.
oscan_region = (slice(None), slice(0, 10)) # indices 0 through 9
fits_section = '[1:10, :]'
science_region = (slice(None), slice(10, None))
if transpose:
# Put overscan in first axis, not second, a test for #70
oscan_region = oscan_region[::-1]
fits_section = '[:, 1:10]'
science_region = science_region[::-1]
ccd_data.data[oscan_region] = oscan
# Add a fake sky background so the "science" part of the image has a
# different average than the "overscan" part.
Expand All @@ -86,8 +92,8 @@ def test_subtract_overscan_mean(ccd_data, median, model):
ccd_data.data[science_region] += oscan + sky
# Test once using the overscan argument to specify the overscan region
ccd_data_overscan = subtract_overscan(ccd_data,
overscan=ccd_data[:, 0:10],
median=median, model=model)
overscan=ccd_data[oscan_region],
median=median, model=None)
# Is the mean of the "science" region the sum of sky and the mean the
# "science" section had before backgrounds were added?
np.testing.assert_almost_equal(
Expand All @@ -100,7 +106,7 @@ def test_subtract_overscan_mean(ccd_data, median, model):
# with the fits_section
ccd_data_fits_section = subtract_overscan(ccd_data,
fits_section=fits_section,
median=median, model=model)
median=median, model=None)
# Is the mean of the "science" region the sum of sky and the mean the
# "science" section had before backgrounds were added?
np.testing.assert_almost_equal(
Expand All @@ -115,14 +121,34 @@ def test_subtract_overscan_mean(ccd_data, median, model):


# A more substantial test of overscan modeling
def test_subtract_overscan_model(ccd_data):
@pytest.mark.parametrize('transpose', [
True,
False])
def test_subtract_overscan_model(ccd_data, transpose):
# create the overscan region
size = ccd_data.shape[0]

oscan_region = (slice(None), slice(0, 10))
science_region = (slice(None), slice(10, None))

yscan, xscan = np.mgrid[0:size, 0:size] / 10.0 + 300.0
ccd_data.data = ccd_data.data + yscan
ccd_data = subtract_overscan(ccd_data, overscan=ccd_data[:, 0:10],

if transpose:
oscan_region = oscan_region[::-1]
science_region = science_region[::-1]
scan = xscan
else:
scan = yscan

original_mean = ccd_data.data[science_region].mean()

ccd_data.data[oscan_region] = 0. # only want overscan in that region
ccd_data.data = ccd_data.data + scan

ccd_data = subtract_overscan(ccd_data, overscan=ccd_data[oscan_region],
median=False, model=models.Polynomial1D(2))
assert abs(ccd_data.data.mean()) < 0.1
np.testing.assert_almost_equal(ccd_data.data[science_region].mean(),
original_mean)


def test_subtract_overscan_fails(ccd_data):
Expand Down

0 comments on commit d9255f8

Please sign in to comment.