/usr/lib/python3/dist-packages/photutils/segmentation/tests/test_properties.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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from distutils.version import LooseVersion
import itertools
import numpy as np
from numpy.testing import assert_allclose
from astropy.tests.helper import pytest, assert_quantity_allclose
from astropy.modeling import models
from astropy.table import Table
import astropy.units as u
from astropy.utils.misc import isiterable
import astropy.wcs as WCS
from ..properties import SourceProperties, source_properties, properties_table
try:
import scipy
HAS_SCIPY = True
except ImportError:
HAS_SCIPY = False
try:
import skimage
HAS_SKIMAGE = True
if LooseVersion(skimage.__version__) < LooseVersion('0.11'):
SKIMAGE_LT_0P11 = True
else:
SKIMAGE_LT_0P11 = False
except ImportError:
HAS_SKIMAGE = False
XCEN = 51.
YCEN = 52.7
MAJOR_SIG = 8.
MINOR_SIG = 3.
THETA = np.pi / 6.
g = models.Gaussian2D(111., XCEN, YCEN, MAJOR_SIG, MINOR_SIG, theta=THETA)
y, x = np.mgrid[0:100, 0:100]
IMAGE = g(x, y)
THRESHOLD = 0.1
SEGM = (IMAGE >= THRESHOLD).astype(np.int)
ERR_VALS = [0., 2.5]
BACKGRD_VALS = [None, 0., 1., 3.5]
@pytest.mark.skipif('not HAS_SKIMAGE')
@pytest.mark.skipif('not HAS_SCIPY')
class TestSourceProperties(object):
def test_segment_shape(self):
with pytest.raises(ValueError):
SourceProperties(IMAGE, np.zeros((2, 2)), label=1)
@pytest.mark.parametrize('label', (0, -1))
def test_label_invalid(self, label):
with pytest.raises(ValueError):
SourceProperties(IMAGE, SEGM, label=label)
@pytest.mark.parametrize('label', (0, -1))
def test_label_missing(self, label):
segm = SEGM.copy()
segm[0:2, 0:2] = 3 # skip label 2
with pytest.raises(ValueError):
SourceProperties(IMAGE, segm, label=2)
def test_wcs(self):
mywcs = WCS.WCS(naxis=2)
rho = np.pi / 3.
scale = 0.1 / 3600.
mywcs.wcs.cd = [[scale*np.cos(rho), -scale*np.sin(rho)],
[scale*np.sin(rho), scale*np.cos(rho)]]
mywcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']
props = SourceProperties(IMAGE, SEGM, wcs=mywcs, label=1)
assert props.icrs_centroid is not None
assert props.ra_icrs_centroid is not None
assert props.dec_icrs_centroid is not None
def test_nowcs(self):
props = SourceProperties(IMAGE, SEGM, wcs=None, label=1)
assert props.icrs_centroid is None
assert props.ra_icrs_centroid is None
assert props.dec_icrs_centroid is None
def test_to_table(self):
props = SourceProperties(IMAGE, SEGM, label=1)
t1 = props.to_table()
t2 = properties_table(props)
assert isinstance(t1, Table)
assert isinstance(t2, Table)
assert len(t1) == 1
assert t1 == t2
@pytest.mark.skipif('not HAS_SKIMAGE')
@pytest.mark.skipif('not HAS_SCIPY')
class TestSourcePropertiesFunctionInputs(object):
def test_segment_shape(self):
wrong_shape = np.zeros((2, 2))
with pytest.raises(ValueError):
source_properties(IMAGE, wrong_shape)
def test_error_shape(self):
wrong_shape = np.zeros((2, 2))
with pytest.raises(ValueError):
source_properties(IMAGE, SEGM, error=wrong_shape)
def test_background_shape(self):
wrong_shape = np.zeros((2, 2))
with pytest.raises(ValueError):
source_properties(IMAGE, SEGM, background=wrong_shape)
def test_mask_shape(self):
wrong_shape = np.zeros((2, 2))
with pytest.raises(ValueError):
source_properties(IMAGE, SEGM, mask=wrong_shape)
def test_labels(self):
props = source_properties(IMAGE, SEGM, labels=1)
assert props[0].id == 1
def test_invalidlabels(self):
props = source_properties(IMAGE, SEGM, labels=-1)
assert len(props) == 0
@pytest.mark.skipif('not HAS_SKIMAGE')
@pytest.mark.skipif('not HAS_SCIPY')
class TestSourcePropertiesFunction(object):
def test_properties(self):
props = source_properties(IMAGE, SEGM)
assert props[0].id == 1
assert_quantity_allclose(props[0].xcentroid, XCEN*u.pix,
rtol=1.e-2)
assert_quantity_allclose(props[0].ycentroid, YCEN*u.pix,
rtol=1.e-2)
assert_allclose(props[0].source_sum, IMAGE[IMAGE >= THRESHOLD].sum())
assert_quantity_allclose(props[0].semimajor_axis_sigma,
MAJOR_SIG*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].semiminor_axis_sigma,
MINOR_SIG*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].orientation, THETA*u.rad,
rtol=1.e-3)
assert_allclose(props[0].bbox.value, [35, 25, 70, 77])
assert_quantity_allclose(props[0].area, 1058.0*u.pix**2)
assert_allclose(len(props[0].values), props[0].area.value)
assert_allclose(len(props[0].coords), 2)
assert_allclose(len(props[0].coords[0]), props[0].area.value)
properties = ['background_at_centroid', 'background_mean',
'eccentricity', 'ellipticity', 'elongation',
'equivalent_radius', 'max_value', 'maxval_xpos',
'maxval_ypos', 'min_value', 'minval_xpos',
'minval_ypos', 'perimeter', 'cxx', 'cxy', 'cyy',
'covar_sigx2', 'covar_sigxy', 'covar_sigy2', 'xmax',
'xmin', 'ymax', 'ymin']
for propname in properties:
assert not isiterable(getattr(props[0], propname))
properties = ['centroid', 'covariance_eigvals', 'cutout_centroid',
'maxval_cutout_pos', 'minval_cutout_pos']
shapes = [getattr(props[0], p).shape for p in properties]
for shape in shapes:
assert shape == (2,)
properties = ['covariance', 'inertia_tensor']
shapes = [getattr(props[0], p).shape for p in properties]
for shape in shapes:
assert shape == (2, 2)
properties = ['moments', 'moments_central']
shapes = [getattr(props[0], p).shape for p in properties]
for shape in shapes:
assert shape == (4, 4)
def test_properties_background_notNone(self):
value = 1.
props = source_properties(IMAGE, SEGM, background=value)
assert props[0].background_mean == value
assert_allclose(props[0].background_at_centroid, value)
def test_properties_error_background_None(self):
props = source_properties(IMAGE, SEGM)
assert props[0].background_cutout_ma is None
assert props[0].error_cutout_ma is None
def test_cutout_shapes(self):
error = np.ones_like(IMAGE) * 1.
props = source_properties(IMAGE, SEGM, error=error, background=1.)
bbox = props[0].bbox.value
true_shape = (bbox[2] - bbox[0] + 1, bbox[3] - bbox[1] + 1)
properties = ['background_cutout_ma', 'data_cutout',
'data_cutout_ma', 'error_cutout_ma']
shapes = [getattr(props[0], p).shape for p in properties]
for shape in shapes:
assert shape == true_shape
def test_make_cutout(self):
props = source_properties(IMAGE, SEGM)
data = np.ones((2, 2))
with pytest.raises(ValueError):
props[0].make_cutout(data)
@pytest.mark.parametrize(('error_value', 'background'),
list(itertools.product(ERR_VALS, BACKGRD_VALS)))
def test_segmentation_inputs(self, error_value, background):
error = np.ones_like(IMAGE) * error_value
props = source_properties(IMAGE, SEGM, error=error,
background=background)
assert_quantity_allclose(props[0].xcentroid, XCEN*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].ycentroid, YCEN*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].semimajor_axis_sigma,
MAJOR_SIG*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].semiminor_axis_sigma,
MINOR_SIG*u.pix, rtol=1.e-2)
assert_quantity_allclose(props[0].orientation, THETA*u.rad,
rtol=1.e-3)
assert_allclose(props[0].bbox.value, [35, 25, 70, 77])
area = props[0].area.value
assert_allclose(area, 1058.0)
if background is not None:
assert_allclose(props[0].background_sum, area * background)
true_sum = IMAGE[IMAGE >= THRESHOLD].sum()
assert_allclose(props[0].source_sum, true_sum)
true_error = np.sqrt(props[0].area.value) * error_value
assert_allclose(props[0].source_sum_err, true_error)
def test_data_allzero(self):
props = source_properties(IMAGE*0., SEGM)
proplist = ['xcentroid', 'ycentroid', 'semimajor_axis_sigma',
'semiminor_axis_sigma', 'eccentricity', 'orientation',
'ellipticity', 'elongation', 'cxx', 'cxy', 'cyy']
for prop in proplist:
assert np.isnan(getattr(props[0], prop))
def test_mask(self):
data = np.zeros((3, 3))
data[0, 1] = 1.
data[1, 1] = 1.
mask = np.zeros_like(data, dtype=np.bool)
mask[0, 1] = True
segm = data.astype(np.int)
props = source_properties(data, segm, mask=mask)
assert_allclose(props[0].xcentroid.value, 1)
assert_allclose(props[0].ycentroid.value, 1)
assert_allclose(props[0].source_sum, 1)
assert_allclose(props[0].area.value, 1)
def test_single_pixel_segment(self):
segm = np.zeros_like(SEGM)
segm[50, 50] = 1
props = source_properties(IMAGE, segm)
assert props[0].eccentricity == 0
def test_filtering(self):
from astropy.convolution import Gaussian2DKernel
FWHM2SIGMA = 1.0 / (2.0 * np.sqrt(2.0 * np.log(2.0)))
filter_kernel = Gaussian2DKernel(2.*FWHM2SIGMA, x_size=3, y_size=3)
error = np.sqrt(IMAGE)
props1 = source_properties(IMAGE, SEGM, error=error)
props2 = source_properties(IMAGE, SEGM, error=error,
filter_kernel=filter_kernel.array)
p1, p2 = props1[0], props2[0]
keys = ['source_sum', 'source_sum_err']
for key in keys:
assert p1[key] == p2[key]
keys = ['semimajor_axis_sigma', 'semiminor_axis_sigma']
for key in keys:
assert p1[key] != p2[key]
def test_filtering_kernel(self):
data = np.zeros((3, 3))
data[1, 1] = 1.
from astropy.convolution import Gaussian2DKernel
FWHM2SIGMA = 1.0 / (2.0 * np.sqrt(2.0 * np.log(2.0)))
filter_kernel = Gaussian2DKernel(2.*FWHM2SIGMA, x_size=3, y_size=3)
error = np.sqrt(IMAGE)
props1 = source_properties(IMAGE, SEGM, error=error)
props2 = source_properties(IMAGE, SEGM, error=error,
filter_kernel=filter_kernel)
p1, p2 = props1[0], props2[0]
keys = ['source_sum', 'source_sum_err']
for key in keys:
assert p1[key] == p2[key]
keys = ['semimajor_axis_sigma', 'semiminor_axis_sigma']
for key in keys:
assert p1[key] != p2[key]
@pytest.mark.skipif('not HAS_SKIMAGE')
@pytest.mark.skipif('not HAS_SCIPY')
class TestPropertiesTable(object):
def test_properties_table(self):
props = source_properties(IMAGE, SEGM)
t = properties_table(props)
assert isinstance(t, Table)
assert len(t) == 1
def test_properties_table_include(self):
props = source_properties(IMAGE, SEGM)
columns = ['id', 'xcentroid']
t = properties_table(props, columns=columns)
assert isinstance(t, Table)
assert len(t) == 1
assert t.colnames == columns
def test_properties_table_include_invalidname(self):
props = source_properties(IMAGE, SEGM)
columns = ['idzz', 'xcentroidzz']
with pytest.raises(AttributeError):
properties_table(props, columns=columns)
def test_properties_table_exclude(self):
props = source_properties(IMAGE, SEGM)
exclude = ['id', 'xcentroid']
t = properties_table(props, exclude_columns=exclude)
assert isinstance(t, Table)
assert len(t) == 1
with pytest.raises(KeyError):
t['id']
def test_properties_table_empty_props(self):
props = source_properties(IMAGE, SEGM, labels=-1)
with pytest.raises(ValueError):
properties_table(props)
def test_properties_table_empty_list(self):
with pytest.raises(ValueError):
properties_table([])
def test_properties_table_wcs(self):
mywcs = WCS.WCS(naxis=2)
rho = np.pi / 3.
scale = 0.1 / 3600.
mywcs.wcs.cd = [[scale*np.cos(rho), -scale*np.sin(rho)],
[scale*np.sin(rho), scale*np.cos(rho)]]
mywcs.wcs.ctype = ['RA---TAN', 'DEC--TAN']
props = source_properties(IMAGE, SEGM, wcs=mywcs)
columns = ['icrs_centroid', 'ra_icrs_centroid', 'dec_icrs_centroid']
t = properties_table(props, columns=columns)
assert t[0]['icrs_centroid'] is not None
assert t[0]['ra_icrs_centroid'] is not None
assert t[0]['dec_icrs_centroid'] is not None
|