/usr/lib/python3/dist-packages/photutils/background/tests/test_background_2d.py is in python3-photutils 0.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import itertools
import numpy as np
from numpy.testing import assert_allclose, assert_equal
import pytest
from ..core import MeanBackground
from ..background_2d import (BkgZoomInterpolator, BkgIDWInterpolator,
Background2D)
try:
import scipy # noqa
HAS_SCIPY = True
except ImportError:
HAS_SCIPY = False
try:
import matplotlib # noqa
HAS_MATPLOTLIB = True
except ImportError:
HAS_MATPLOTLIB = False
DATA = np.ones((100, 100))
BKG_RMS = np.zeros((100, 100))
BKG_MESH = np.ones((4, 4))
BKG_RMS_MESH = np.zeros((4, 4))
PADBKG_MESH = np.ones((5, 5))
PADBKG_RMS_MESH = np.zeros((5, 5))
FILTER_SIZES = [(1, 1), (3, 3)]
INTERPOLATORS = [BkgZoomInterpolator(), BkgIDWInterpolator()]
@pytest.mark.skipif('not HAS_SCIPY')
class TestBackground2D(object):
@pytest.mark.parametrize(('filter_size', 'interpolator'),
list(itertools.product(FILTER_SIZES,
INTERPOLATORS)))
def test_background(self, filter_size, interpolator):
b = Background2D(DATA, (25, 25), filter_size=filter_size,
interpolator=interpolator)
assert_allclose(b.background, DATA)
assert_allclose(b.background_rms, BKG_RMS)
assert_allclose(b.background_mesh, BKG_MESH)
assert_allclose(b.background_rms_mesh, BKG_RMS_MESH)
assert b.background_median == 1.0
assert b.background_rms_median == 0.0
@pytest.mark.parametrize('interpolator', INTERPOLATORS)
def test_background_nonconstant(self, interpolator):
data = np.copy(DATA)
data[25:50, 50:75] = 10.
bkg_low_res = np.copy(BKG_MESH)
bkg_low_res[1, 2] = 10.
b1 = Background2D(data, (25, 25), filter_size=(1, 1),
interpolator=interpolator)
assert_allclose(b1.background_mesh, bkg_low_res)
assert b1.background.shape == data.shape
b2 = Background2D(data, (25, 25), filter_size=(1, 1),
edge_method='pad', interpolator=interpolator)
assert_allclose(b2.background_mesh, bkg_low_res)
assert b2.background.shape == data.shape
def test_no_sigma_clipping(self):
data = np.copy(DATA)
data[10, 10] = 100.
b1 = Background2D(data, (25, 25), filter_size=(1, 1),
bkg_estimator=MeanBackground())
b2 = Background2D(data, (25, 25), filter_size=(1, 1), sigma_clip=None,
bkg_estimator=MeanBackground())
assert b2.background_mesh[0, 0] > b1.background_mesh[0, 0]
@pytest.mark.parametrize('filter_size', FILTER_SIZES)
def test_resizing(self, filter_size):
b1 = Background2D(DATA, (23, 22), filter_size=filter_size,
bkg_estimator=MeanBackground(), edge_method='crop')
b2 = Background2D(DATA, (23, 22), filter_size=filter_size,
bkg_estimator=MeanBackground(), edge_method='pad')
assert_allclose(b1.background, b2.background)
assert_allclose(b1.background_rms, b2.background_rms)
@pytest.mark.parametrize('box_size', ([(25, 25), (23, 22)]))
def test_background_mask(self, box_size):
"""
Test with an input mask. Note that box_size=(23, 22) tests the
resizing of the image and mask.
"""
data = np.copy(DATA)
data[25:50, 25:50] = 100.
mask = np.zeros_like(DATA, dtype=np.bool)
mask[25:50, 25:50] = True
b = Background2D(data, box_size, filter_size=(1, 1), mask=mask,
bkg_estimator=MeanBackground())
assert_allclose(b.background, DATA)
assert_allclose(b.background_rms, BKG_RMS)
# test edge crop with
b2 = Background2D(data, box_size, filter_size=(1, 1), mask=mask,
bkg_estimator=MeanBackground(), edge_method='crop')
assert_allclose(b2.background, DATA)
def test_mask(self):
data = np.copy(DATA)
data[25:50, 25:50] = 100.
mask = np.zeros_like(DATA, dtype=np.bool)
mask[25:50, 25:50] = True
b1 = Background2D(data, (25, 25), filter_size=(1, 1), mask=None,
bkg_estimator=MeanBackground())
assert_equal(b1.background_mesh, b1.background_mesh_ma)
assert_equal(b1.background_rms_mesh, b1.background_rms_mesh_ma)
assert not np.ma.is_masked(b1.mesh_nmasked)
b2 = Background2D(data, (25, 25), filter_size=(1, 1), mask=mask,
bkg_estimator=MeanBackground())
assert np.ma.count(b2.background_mesh_ma) < b2.nboxes
assert np.ma.count(b2.background_rms_mesh_ma) < b2.nboxes
assert np.ma.is_masked(b2.mesh_nmasked)
def test_completely_masked(self):
with pytest.raises(ValueError):
mask = np.ones_like(DATA, dtype=np.bool)
Background2D(DATA, (25, 25), mask=mask)
def test_zero_padding(self):
"""Test case where padding is added only on one axis."""
b = Background2D(DATA, (25, 22), filter_size=(1, 1))
assert_allclose(b.background, DATA)
assert_allclose(b.background_rms, BKG_RMS)
assert b.background_median == 1.0
assert b.background_rms_median == 0.0
def test_filter_threshold(self):
"""Only meshes greater than filter_threshold are filtered."""
data = np.copy(DATA)
data[25:50, 50:75] = 10.
b = Background2D(data, (25, 25), filter_size=(3, 3),
filter_threshold=9.)
assert_allclose(b.background, DATA)
assert_allclose(b.background_mesh, BKG_MESH)
b2 = Background2D(data, (25, 25), filter_size=(3, 3),
filter_threshold=11.) # no filtering
assert b2.background_mesh[1, 2] == 10
def test_filter_threshold_high(self):
"""No filtering because filter_threshold is too large."""
data = np.copy(DATA)
data[25:50, 50:75] = 10.
ref_data = np.copy(BKG_MESH)
ref_data[1, 2] = 10.
b = Background2D(data, (25, 25), filter_size=(3, 3),
filter_threshold=100.)
assert_allclose(b.background_mesh, ref_data)
def test_filter_threshold_nofilter(self):
"""No filtering because filter_size is (1, 1)."""
data = np.copy(DATA)
data[25:50, 50:75] = 10.
ref_data = np.copy(BKG_MESH)
ref_data[1, 2] = 10.
b = Background2D(data, (25, 25), filter_size=(1, 1),
filter_threshold=1.)
assert_allclose(b.background_mesh, ref_data)
def test_scalar_sizes(self):
b1 = Background2D(DATA, (25, 25), filter_size=(3, 3))
b2 = Background2D(DATA, 25, filter_size=3)
assert_allclose(b1.background, b2.background)
assert_allclose(b1.background_rms, b2.background_rms)
def test_exclude_percentile(self):
with pytest.raises(ValueError):
Background2D(DATA, (5, 5), exclude_percentile=-1)
with pytest.raises(ValueError):
Background2D(DATA, (5, 5), exclude_percentile=101)
def test_mask_badshape(self):
with pytest.raises(ValueError):
Background2D(DATA, (25, 25), filter_size=(1, 1),
mask=np.zeros((2, 2)))
def test_invalid_edge_method(self):
with pytest.raises(ValueError):
Background2D(DATA, (23, 22), filter_size=(1, 1),
edge_method='not_valid')
def test_invalid_mesh_idx_len(self):
with pytest.raises(ValueError):
bkg = Background2D(DATA, (25, 25), filter_size=(1, 1))
bkg._make_2d_array(np.arange(3))
@pytest.mark.skipif('not HAS_MATPLOTLIB')
def test_plot_meshes(self):
"""
This test should run without any errors, but there is no return
value.
"""
b = Background2D(DATA, (25, 25))
b.plot_meshes(outlines=True)
|