lsst.meas.modelfit  15.0-4-g535e784+3
regularizedMomentsContinued.py
Go to the documentation of this file.
1 # This file is part of meas modelfit.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 import numpy as np
23 
24 
25 def makeGaussian(x, y, scale, muX, muY, varX, varXY, varY):
26  """Create an elliptical Gaussian.
27 
28  Parameters
29  ----------
30  x : 2D numpy array
31  An array containing the x coordinates the Gaussian will be evaluated on.
32  most likely the result of a numpy.indices call
33  y : 2D numpy array
34  An array containing the y coordinates the Gaussian will be evaluated on.
35  most likely the result of a numpy.indices call
36  scale : `float`
37  The value the resulting Gaussian will have when summed over all pixels.
38  muX : `float`
39  The central position of the Gaussian in the x direction
40  muY : `float`
41  The central position of the Gaussian in the y direction
42  varX : `float`
43  The variance of the Gaussian about the muX position
44  varXY : `float`
45  The covariance of the Gaussian in x and y
46  varY : `float`
47  The variance of the Gaussian about the muY position
48 
49  Returns
50  -------
51  Gaussian : 2D numpy array
52  The Gaussian array generated from the input values
53  """
54 
55  rho = varXY/(varX**0.5*varY**0.5)
56 
57  psf = np.exp(-1/(2*(1-rho**2)) *
58  ((x-muX)**2/varX+(y - muY)**2/varY -
59  2*rho*(x-muX)*(y-muY)/(varX**0.5*varY**0.5)))
60 
61  psf /= psf.sum()
62  return scale*psf
63 
64 
65 def buildUncertanty(imShape, W, uncertanty):
66  """ Propagate pixel uncertainties to uncertainties in weighted moments
67 
68  Parameters
69  ----------
70  imShape : tuple(float, float)
71  The shape of image for which weighted moments have been calculated
72  W : iterable
73  An iterable object with six elements corresponding to the moments used
74  in the weighted moment calculation, scale, mean in x, mean in y, variance
75  in x, covariance between x and y, and variance in y.
76  uncertanty : `float`
77  Uncertainty in the pixel value. This is a single value, as this routine
78  assumes errors are background dominated, and uncorrelated
79 
80  Returns
81  -------
82  covarianceMatrix : 2D 6x6 numpy array of floats
83  This is the covariance matrix on the measured moments with uncertainties
84  propagated from pixel uncertainties
85  """
86  yInd, xInd = np.indices(imShape)
87  weightImage = makeGaussian(xInd, yInd, *W)
88  sigmaImage = np.eye(weightImage.size)*uncertanty
89  MomentWeightMatrix = np.zeros((6, weightImage.size))
90 
91  weightImageFlat = weightImage.flatten()
92  xIndFlat = xInd.flatten()
93  yIndFlat = yInd.flatten()
94 
95  MomentWeightMatrix[0] = weightImageFlat
96  MomentWeightMatrix[1] = weightImageFlat*xIndFlat
97  MomentWeightMatrix[2] = weightImageFlat*yIndFlat
98  MomentWeightMatrix[3] = weightImageFlat*xIndFlat**2
99  MomentWeightMatrix[4] = weightImageFlat*xIndFlat*yIndFlat
100  MomentWeightMatrix[5] = weightImageFlat*yIndFlat**2
101 
102  return np.dot(MomentWeightMatrix, np.dot(sigmaImage, np.transpose(MomentWeightMatrix)))
103 
104 
105 def measureMoments(image, W):
106  """ Calculate weighted moments of the input image with the given weight array
107 
108  Parameters
109  ----------
110  image : 2D numpy array of floats
111  This is the input postage stamp of a source for which the weighted moments are
112  to be measured
113  W : 2D numpy array of floats
114  Array of floats that are used as weights when calculating moments on the input
115  image. Array must be the same shape image
116 
117  Returns
118  -------
119  moments : 6 element numpy array
120  These are the weighted moments as measured from the input image in the order of
121  0th, 1st X, 1st Y, 2nd X, 2nd XY, 2nd Y
122 
123  Raises
124  ------
125  AssertionError: Raises if the input arrays are not the same shape
126  """
127  assert image.shape == W.shape, "Input image and weight array must be the same shape"
128 
129  yInd, xInd = np.indices(image.shape)
130  weightImage = makeGaussian(xInd, yInd, *W)
131 
132  zero = np.sum(image*weightImage)
133  oneX = np.sum(image*weightImage*xInd)
134  oneY = np.sum(image*weightImage*yInd)
135  twoX = np.sum(image*weightImage*xInd**2)
136  twoXY = np.sum(image*weightImage*xInd*yInd)
137  twoY = np.sum(image*weightImage*yInd**2)
138 
139  return np.array((zero, oneX, oneY, twoX, twoXY, twoY)), weightImage