/usr/lib/python3/dist-packages/photutils/utils/tests/test_interpolation.py is in python3-photutils 0.3-3.
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 212 213 214 215 216 217 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest
from .. import ShepardIDWInterpolator as idw
from .. import interpolate_masked_data, mask_to_mirrored_num
try:
import scipy
HAS_SCIPY = True
except ImportError:
HAS_SCIPY = False
SHAPE = (5, 5)
DATA = np.ones(SHAPE) * 2.0
MASK = np.zeros_like(DATA, dtype=bool)
MASK[2, 2] = True
ERROR = np.ones(SHAPE)
BACKGROUND = np.ones(SHAPE)
WRONG_SHAPE = np.ones((2, 2))
@pytest.mark.skipif('not HAS_SCIPY')
class TestShepardIDWInterpolator(object):
def setup_class(self):
np.random.seed(123)
self.x = np.random.random(100)
self.y = np.sin(self.x)
self.f = idw(self.x, self.y)
@pytest.mark.parametrize('positions', [0.4, np.arange(2, 5)*0.1])
def test_idw_1d(self, positions):
f = idw(self.x, self.y)
assert_allclose(f(positions), np.sin(positions), atol=1e-2)
def test_idw_weights(self):
weights = self.y * 0.1
f = idw(self.x, self.y, weights=weights)
pos = 0.4
assert_allclose(f(pos), np.sin(pos), atol=1e-2)
def test_idw_2d(self):
pos = np.random.rand(1000, 2)
val = np.sin(pos[:, 0] + pos[:, 1])
f = idw(pos, val)
x = 0.5
y = 0.6
assert_allclose(f([x, y]), np.sin(x + y), atol=1e-2)
def test_idw_3d(self):
val = np.ones((3, 3, 3))
pos = np.indices(val.shape)
f = idw(pos, val)
assert_allclose(f([0.5, 0.5, 0.5]), 1.0)
def test_no_coordinates(self):
with pytest.raises(ValueError):
idw([], 0)
def test_values_invalid_shape(self):
with pytest.raises(ValueError):
idw(self.x, 0)
def test_weights_invalid_shape(self):
with pytest.raises(ValueError):
idw(self.x, self.y, weights=10)
def test_weights_negative(self):
with pytest.raises(ValueError):
idw(self.x, self.y, weights=-self.y)
def test_n_neighbors_one(self):
assert_allclose(self.f(0.5, n_neighbors=1), 0.48103656)
def test_n_neighbors_negative(self):
with pytest.raises(ValueError):
self.f(0.5, n_neighbors=-1)
def test_conf_dist_negative(self):
assert_allclose(self.f(0.5, conf_dist=-1),
self.f(0.5, conf_dist=None))
def test_dtype_none(self):
result = self.f(0.5, dtype=None)
assert result.dtype.type == np.float64
def test_positions_0d_nomatch(self):
"""test when position ndim doesn't match coordinates ndim"""
pos = np.random.rand(10, 2)
val = np.sin(pos[:, 0] + pos[:, 1])
f = idw(pos, val)
with pytest.raises(ValueError):
f(0.5)
def test_positions_1d_nomatch(self):
"""test when position ndim doesn't match coordinates ndim"""
pos = np.random.rand(10, 2)
val = np.sin(pos[:, 0] + pos[:, 1])
f = idw(pos, val)
with pytest.raises(ValueError):
f([0.5])
def test_positions_3d(self):
with pytest.raises(ValueError):
self.f(np.ones((3, 3, 3)))
class TestInterpolateMaskedData(object):
def test_mask_shape(self):
with pytest.raises(ValueError):
interpolate_masked_data(DATA, WRONG_SHAPE)
def test_error_shape(self):
with pytest.raises(ValueError):
interpolate_masked_data(DATA, MASK, error=WRONG_SHAPE)
def test_background_shape(self):
with pytest.raises(ValueError):
interpolate_masked_data(DATA, MASK, background=WRONG_SHAPE)
def test_interpolation(self):
data2 = DATA.copy()
data2[2, 2] = 100.
error2 = ERROR.copy()
error2[2, 2] = 100.
background2 = BACKGROUND.copy()
background2[2, 2] = 100.
data, error, background = interpolate_masked_data(
data2, MASK, error=error2, background=background2)
assert_allclose(data, DATA)
assert_allclose(error, ERROR)
assert_allclose(background, BACKGROUND)
def test_interpolation_larger_mask(self):
data2 = DATA.copy()
data2[2, 2] = 100.
error2 = ERROR.copy()
error2[2, 2] = 100.
background2 = BACKGROUND.copy()
background2[2, 2] = 100.
mask2 = MASK.copy()
mask2[1:4, 1:4] = True
data, error, background = interpolate_masked_data(
data2, MASK, error=error2, background=background2)
assert_allclose(data, DATA)
assert_allclose(error, ERROR)
assert_allclose(background, BACKGROUND)
class TestMaskToMirroredNum(object):
def test_mask_to_mirrored_num(self):
"""
Test mask_to_mirrored_num.
"""
center = (1.5, 1.5)
data = np.arange(16).reshape(4, 4)
mask = np.zeros_like(data, dtype=bool)
mask[0, 0] = True
mask[1, 1] = True
data_ref = data.copy()
data_ref[0, 0] = data[3, 3]
data_ref[1, 1] = data[2, 2]
mirror_data = mask_to_mirrored_num(data, mask, center)
assert_allclose(mirror_data, data_ref, rtol=0, atol=1.e-6)
def test_mask_to_mirrored_num_range(self):
"""
Test mask_to_mirrored_num when mirrored pixels are outside of the
image.
"""
center = (2.5, 2.5)
data = np.arange(16).reshape(4, 4)
mask = np.zeros_like(data, dtype=bool)
mask[0, 0] = True
mask[1, 1] = True
data_ref = data.copy()
data_ref[0, 0] = 0.
data_ref[1, 1] = 0.
mirror_data = mask_to_mirrored_num(data, mask, center)
assert_allclose(mirror_data, data_ref, rtol=0, atol=1.e-6)
def test_mask_to_mirrored_num_masked(self):
"""
Test mask_to_mirrored_num when mirrored pixels are also masked.
"""
center = (0.5, 0.5)
data = np.arange(16).reshape(4, 4)
data[0, 0] = 100
mask = np.zeros_like(data, dtype=bool)
mask[0, 0] = True
mask[1, 1] = True
data_ref = data.copy()
data_ref[0, 0] = 0.
data_ref[1, 1] = 0.
mirror_data = mask_to_mirrored_num(data, mask, center)
assert_allclose(mirror_data, data_ref, rtol=0, atol=1.e-6)
def test_mask_to_mirrored_num_bbox(self):
"""
Test mask_to_mirrored_num with a bounding box.
"""
center = (1.5, 1.5)
data = np.arange(16).reshape(4, 4)
data[0, 0] = 100
mask = np.zeros_like(data, dtype=bool)
mask[0, 0] = True
mask[1, 1] = True
data_ref = data.copy()
data_ref[1, 1] = data[2, 2]
bbox = (1, 2, 1, 2)
mirror_data = mask_to_mirrored_num(data, mask, center, bbox=bbox)
assert_allclose(mirror_data, data_ref, rtol=0, atol=1.e-6)
|