/usr/lib/python3/dist-packages/photutils/centroids/core.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 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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | # Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
Functions for centroiding sources and measuring their morphological
properties.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import warnings
import numpy as np
from astropy.modeling import Fittable2DModel, Parameter
from astropy.modeling.models import (Gaussian1D, Gaussian2D, Const1D,
Const2D, CONSTRAINTS_DOC)
from astropy.modeling.fitting import LevMarLSQFitter
from astropy.utils.exceptions import AstropyUserWarning
from ..morphology import data_properties
__all__ = ['GaussianConst2D', 'centroid_com', 'gaussian1d_moments',
'fit_2dgaussian', 'centroid_1dg', 'centroid_2dg']
class _GaussianConst1D(Const1D + Gaussian1D):
"""A model for a 1D Gaussian plus a constant."""
class GaussianConst2D(Fittable2DModel):
"""
A model for a 2D Gaussian plus a constant.
Parameters
----------
constant : float
Value of the constant.
amplitude : float
Amplitude of the Gaussian.
x_mean : float
Mean of the Gaussian in x.
y_mean : float
Mean of the Gaussian in y.
x_stddev : float
Standard deviation of the Gaussian in x.
``x_stddev`` and ``y_stddev`` must be specified unless a covariance
matrix (``cov_matrix``) is input.
y_stddev : float
Standard deviation of the Gaussian in y.
``x_stddev`` and ``y_stddev`` must be specified unless a covariance
matrix (``cov_matrix``) is input.
theta : float, optional
Rotation angle in radians. The rotation angle increases
counterclockwise.
"""
constant = Parameter(default=1)
amplitude = Parameter(default=1)
x_mean = Parameter(default=0)
y_mean = Parameter(default=0)
x_stddev = Parameter(default=1)
y_stddev = Parameter(default=1)
theta = Parameter(default=0)
@staticmethod
def evaluate(x, y, constant, amplitude, x_mean, y_mean, x_stddev,
y_stddev, theta):
"""Two dimensional Gaussian plus constant function."""
model = Const2D(constant)(x, y) + Gaussian2D(amplitude, x_mean,
y_mean, x_stddev,
y_stddev, theta)(x, y)
return model
GaussianConst2D.__doc__ += CONSTRAINTS_DOC
def centroid_com(data, mask=None):
"""
Calculate the centroid of a 2D array as its "center of mass"
determined from image moments.
Invalid values (e.g. NaNs or infs) in the ``data`` array are
automatically masked.
Parameters
----------
data : array_like
The 2D array of the image.
mask : array_like (bool), optional
A boolean mask, with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
Returns
-------
centroid : `~numpy.ndarray`
The ``x, y`` coordinates of the centroid.
"""
from skimage.measure import moments
data = np.ma.asanyarray(data)
if mask is not None and mask is not np.ma.nomask:
mask = np.asanyarray(mask)
if data.shape != mask.shape:
raise ValueError('data and mask must have the same shape.')
data.mask |= mask
if np.any(~np.isfinite(data)):
data = np.ma.masked_invalid(data)
warnings.warn('Input data contains input values (e.g. NaNs or infs), '
'which were automatically masked.', AstropyUserWarning)
# Convert the data to a float64 (double) `numpy.ndarray`,
# which is required for input to `skimage.measure.moments`.
# Masked values are set to zero.
data = data.astype(np.float)
data.fill_value = 0.
data = data.filled()
m = moments(data, 1)
xcen = m[1, 0] / m[0, 0]
ycen = m[0, 1] / m[0, 0]
return np.array([xcen, ycen])
def gaussian1d_moments(data, mask=None):
"""
Estimate 1D Gaussian parameters from the moments of 1D data.
This function can be useful for providing initial parameter values
when fitting a 1D Gaussian to the ``data``.
Parameters
----------
data : array_like (1D)
The 1D array.
mask : array_like (1D bool), optional
A boolean mask, with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
Returns
-------
amplitude, mean, stddev : float
The estimated parameters of a 1D Gaussian.
"""
if np.any(~np.isfinite(data)):
data = np.ma.masked_invalid(data)
warnings.warn('Input data contains input values (e.g. NaNs or infs), '
'which were automatically masked.', AstropyUserWarning)
else:
data = np.ma.array(data)
if mask is not None and mask is not np.ma.nomask:
mask = np.asanyarray(mask)
if data.shape != mask.shape:
raise ValueError('data and mask must have the same shape.')
data.mask |= mask
data.fill_value = 0.
data = data.filled()
x = np.arange(data.size)
x_mean = np.sum(x * data) / np.sum(data)
x_stddev = np.sqrt(abs(np.sum(data * (x - x_mean)**2) / np.sum(data)))
amplitude = np.ptp(data)
return amplitude, x_mean, x_stddev
def fit_2dgaussian(data, error=None, mask=None):
"""
Fit a 2D Gaussian plus a constant to a 2D image.
Invalid values (e.g. NaNs or infs) in the ``data`` or ``error``
arrays are automatically masked. The mask for invalid values
represents the combination of the invalid-value masks for the
``data`` and ``error`` arrays.
Parameters
----------
data : array_like
The 2D array of the image.
error : array_like, optional
The 2D array of the 1-sigma errors of the input ``data``.
mask : array_like (bool), optional
A boolean mask, with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
Returns
-------
result : A `GaussianConst2D` model instance.
The best-fitting Gaussian 2D model.
"""
data = np.ma.asanyarray(data)
if mask is not None and mask is not np.ma.nomask:
mask = np.asanyarray(mask)
if data.shape != mask.shape:
raise ValueError('data and mask must have the same shape.')
data.mask |= mask
if np.any(~np.isfinite(data)):
data = np.ma.masked_invalid(data)
warnings.warn('Input data contains input values (e.g. NaNs or infs), '
'which were automatically masked.', AstropyUserWarning)
if error is not None:
error = np.ma.masked_invalid(error)
if data.shape != error.shape:
raise ValueError('data and error must have the same shape.')
data.mask |= error.mask
weights = 1.0 / error.clip(min=1.e-30)
else:
weights = np.ones(data.shape)
if np.ma.count(data) < 7:
raise ValueError('Input data must have a least 7 unmasked values to '
'fit a 2D Gaussian plus a constant.')
# assign zero weight to masked pixels
if data.mask is not np.ma.nomask:
weights[data.mask] = 0.
mask = data.mask
data.fill_value = 0.0
data = data.filled()
# Subtract the minimum of the data as a crude background estimate.
# This will also make the data values positive, preventing issues with
# the moment estimation in data_properties (moments from negative data
# values can yield undefined Gaussian parameters, e.g. x/y_stddev).
props = data_properties(data - np.min(data), mask=mask)
init_const = 0. # subtracted data minimum above
init_amplitude = np.ptp(data)
g_init = GaussianConst2D(constant=init_const, amplitude=init_amplitude,
x_mean=props.xcentroid.value,
y_mean=props.ycentroid.value,
x_stddev=props.semimajor_axis_sigma.value,
y_stddev=props.semiminor_axis_sigma.value,
theta=props.orientation.value)
fitter = LevMarLSQFitter()
y, x = np.indices(data.shape)
gfit = fitter(g_init, x, y, data, weights=weights)
return gfit
def centroid_1dg(data, error=None, mask=None):
"""
Calculate the centroid of a 2D array by fitting 1D Gaussians to the
marginal ``x`` and ``y`` distributions of the array.
Invalid values (e.g. NaNs or infs) in the ``data`` or ``error``
arrays are automatically masked. The mask for invalid values
represents the combination of the invalid-value masks for the
``data`` and ``error`` arrays.
Parameters
----------
data : array_like
The 2D data array.
error : array_like, optional
The 2D array of the 1-sigma errors of the input ``data``.
mask : array_like (bool), optional
A boolean mask, with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
Returns
-------
centroid : `~numpy.ndarray`
The ``x, y`` coordinates of the centroid.
"""
data = np.ma.asanyarray(data)
if mask is not None and mask is not np.ma.nomask:
mask = np.asanyarray(mask)
if data.shape != mask.shape:
raise ValueError('data and mask must have the same shape.')
data.mask |= mask
if np.any(~np.isfinite(data)):
data = np.ma.masked_invalid(data)
warnings.warn('Input data contains input values (e.g. NaNs or infs), '
'which were automatically masked.', AstropyUserWarning)
if error is not None:
error = np.ma.masked_invalid(error)
if data.shape != error.shape:
raise ValueError('data and error must have the same shape.')
data.mask |= error.mask
error.mask = data.mask
xy_error = np.array([np.sqrt(np.ma.sum(error**2, axis=i))
for i in [0, 1]])
xy_weights = [(1.0 / xy_error[i].clip(min=1.e-30)) for i in [0, 1]]
else:
xy_weights = [np.ones(data.shape[i]) for i in [1, 0]]
# assign zero weight to masked pixels
if data.mask is not np.ma.nomask:
bad_idx = [np.all(data.mask, axis=i) for i in [0, 1]]
for i in [0, 1]:
xy_weights[i][bad_idx[i]] = 0.
xy_data = np.array([np.ma.sum(data, axis=i) for i in [0, 1]])
constant_init = np.ma.min(data)
centroid = []
for (data_i, weights_i) in zip(xy_data, xy_weights):
params_init = gaussian1d_moments(data_i)
g_init = _GaussianConst1D(constant_init, *params_init)
fitter = LevMarLSQFitter()
x = np.arange(data_i.size)
g_fit = fitter(g_init, x, data_i, weights=weights_i)
centroid.append(g_fit.mean_1.value)
return np.array(centroid)
def centroid_2dg(data, error=None, mask=None):
"""
Calculate the centroid of a 2D array by fitting a 2D Gaussian (plus
a constant) to the array.
Invalid values (e.g. NaNs or infs) in the ``data`` or ``error``
arrays are automatically masked. The mask for invalid values
represents the combination of the invalid-value masks for the
``data`` and ``error`` arrays.
Parameters
----------
data : array_like
The 2D data array.
error : array_like, optional
The 2D array of the 1-sigma errors of the input ``data``.
mask : array_like (bool), optional
A boolean mask, with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
Returns
-------
centroid : `~numpy.ndarray`
The ``x, y`` coordinates of the centroid.
"""
gfit = fit_2dgaussian(data, error=error, mask=mask)
return np.array([gfit.x_mean.value, gfit.y_mean.value])
|