/usr/lib/python3/dist-packages/photutils/centroids/tests/test_core.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 | # 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
from astropy.modeling.models import Gaussian1D, Gaussian2D
from astropy.tests.helper import pytest
from ..core import (centroid_com, centroid_1dg, centroid_2dg,
gaussian1d_moments, fit_2dgaussian)
try:
import skimage
HAS_SKIMAGE = True
except ImportError:
HAS_SKIMAGE = False
XCS = [25.7]
YCS = [26.2]
XSTDDEVS = [3.2, 4.0]
YSTDDEVS = [5.7, 4.1]
THETAS = np.array([30., 45.]) * np.pi / 180.
DATA = np.zeros((3, 3))
DATA[0:2, 1] = 1.
DATA[1, 0:2] = 1.
DATA[1, 1] = 2.
@pytest.mark.parametrize(
('xc_ref', 'yc_ref', 'x_stddev', 'y_stddev', 'theta'),
list(itertools.product(XCS, YCS, XSTDDEVS, YSTDDEVS, THETAS)))
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_centroids(xc_ref, yc_ref, x_stddev, y_stddev, theta):
model = Gaussian2D(2.4, xc_ref, yc_ref, x_stddev=x_stddev,
y_stddev=y_stddev, theta=theta)
y, x = np.mgrid[0:50, 0:47]
data = model(x, y)
xc, yc = centroid_com(data)
assert_allclose([xc_ref, yc_ref], [xc, yc], rtol=0, atol=1.e-3)
xc2, yc2 = centroid_1dg(data)
assert_allclose([xc_ref, yc_ref], [xc2, yc2], rtol=0, atol=1.e-3)
xc3, yc3 = centroid_2dg(data)
assert_allclose([xc_ref, yc_ref], [xc3, yc3], rtol=0, atol=1.e-3)
@pytest.mark.parametrize(
('xc_ref', 'yc_ref', 'x_stddev', 'y_stddev', 'theta'),
list(itertools.product(XCS, YCS, XSTDDEVS, YSTDDEVS, THETAS)))
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_centroids_witherror(xc_ref, yc_ref, x_stddev, y_stddev, theta):
model = Gaussian2D(2.4, xc_ref, yc_ref, x_stddev=x_stddev,
y_stddev=y_stddev, theta=theta)
y, x = np.mgrid[0:50, 0:50]
data = model(x, y)
error = np.sqrt(data)
xc2, yc2 = centroid_1dg(data, error=error)
assert_allclose([xc_ref, yc_ref], [xc2, yc2], rtol=0, atol=1.e-3)
xc3, yc3 = centroid_2dg(data, error=error)
assert_allclose([xc_ref, yc_ref], [xc3, yc3], rtol=0, atol=1.e-3)
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_centroids_withmask():
xc_ref, yc_ref = 24.7, 25.2
model = Gaussian2D(2.4, xc_ref, yc_ref, x_stddev=5.0, y_stddev=5.0)
y, x = np.mgrid[0:50, 0:50]
data = model(x, y)
mask = np.zeros_like(data, dtype=bool)
data[10, 10] = 1.e5
mask[10, 10] = True
xc, yc = centroid_com(data, mask=mask)
assert_allclose([xc, yc], [xc_ref, yc_ref], rtol=0, atol=1.e-3)
xc2, yc2 = centroid_1dg(data, mask=mask)
assert_allclose([xc2, yc2], [xc_ref, yc_ref], rtol=0, atol=1.e-3)
xc3, yc3 = centroid_2dg(data, mask=mask)
assert_allclose([xc3, yc3], [xc_ref, yc_ref], rtol=0, atol=1.e-3)
@pytest.mark.skipif('not HAS_SKIMAGE')
@pytest.mark.parametrize('use_mask', [True, False])
def test_centroids_nan_withmask(use_mask):
xc_ref, yc_ref = 24.7, 25.2
model = Gaussian2D(2.4, xc_ref, yc_ref, x_stddev=5.0, y_stddev=5.0)
y, x = np.mgrid[0:50, 0:50]
data = model(x, y)
data[20, :] = np.nan
if use_mask:
mask = np.zeros_like(data, dtype=bool)
mask[20, :] = True
else:
mask = None
xc, yc = centroid_com(data, mask=mask)
assert_allclose(xc, xc_ref, rtol=0, atol=1.e-3)
assert yc > yc_ref
xc2, yc2 = centroid_1dg(data, mask=mask)
assert_allclose([xc2, yc2], [xc_ref, yc_ref], rtol=0, atol=1.e-3)
xc3, yc3 = centroid_2dg(data, mask=mask)
assert_allclose([xc3, yc3], [xc_ref, yc_ref], rtol=0, atol=1.e-3)
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_centroid_com_mask():
"""Test centroid_com with and without an image_mask."""
data = np.ones((2, 2)).astype(np.float)
mask = [[False, False], [True, True]]
centroid = centroid_com(data, mask=None)
centroid_mask = centroid_com(data, mask=mask)
assert_allclose([0.5, 0.5], centroid, rtol=0, atol=1.e-6)
assert_allclose([0.5, 0.0], centroid_mask, rtol=0, atol=1.e-6)
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_invalid_mask_shape():
"""
Test if ValueError raises if mask shape doesn't match data
shape.
"""
data = np.zeros((4, 4))
mask = np.zeros((2, 2), dtype=bool)
with pytest.raises(ValueError):
centroid_com(data, mask=mask)
with pytest.raises(ValueError):
centroid_1dg(data, mask=mask)
with pytest.raises(ValueError):
centroid_2dg(data, mask=mask)
with pytest.raises(ValueError):
gaussian1d_moments(data, mask=mask)
@pytest.mark.skipif('not HAS_SKIMAGE')
def test_invalid_error_shape():
"""
Test if ValueError raises if error shape doesn't match data
shape.
"""
error = np.zeros((2, 2), dtype=bool)
with pytest.raises(ValueError):
centroid_1dg(np.zeros((4, 4)), error=error)
with pytest.raises(ValueError):
centroid_2dg(np.zeros((4, 4)), error=error)
def test_gaussian1d_moments():
x = np.arange(100)
desired = (75, 50, 5)
g = Gaussian1D(*desired)
data = g(x)
result = gaussian1d_moments(data)
assert_allclose(result, desired, rtol=0, atol=1.e-6)
data[0] = 1.e5
mask = np.zeros_like(data).astype(bool)
mask[0] = True
result = gaussian1d_moments(data, mask=mask)
assert_allclose(result, desired, rtol=0, atol=1.e-6)
data[0] = np.nan
mask = np.zeros_like(data).astype(bool)
mask[0] = True
result = gaussian1d_moments(data, mask=mask)
assert_allclose(result, desired, rtol=0, atol=1.e-6)
def test_fit2dgaussian_dof():
data = np.ones((2, 2))
with pytest.raises(ValueError):
fit_2dgaussian(data)
|