LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
LSST Data Management Base Package
Loading...
Searching...
No Matches
Functions | Variables
lsst.ip.diffim.utils Namespace Reference

Functions

 getKernelCenterDisplacement (kernel, x, y, image=None)
 
 getPsfFwhm (psf, average=True, position=None)
 
float evaluateMeanPsfFwhm (afwImage.Exposure exposure, float fwhmExposureBuffer, int fwhmExposureGrid)
 
afwImage.ImageD computeAveragePsf (afwImage.Exposure exposure, float psfExposureBuffer, int psfExposureGrid)
 
 computeRobustStatistics (image, mask, statsCtrl, statistic=afwMath.MEANCLIP)
 
 computePSFNoiseEquivalentArea (psf)
 
 angleMean (angles)
 
 evaluateMaskFraction (mask, maskPlane)
 

Variables

 _LOG = getLogger(__name__)
 

Detailed Description

Support utilities for Measuring sources

Function Documentation

◆ angleMean()

lsst.ip.diffim.utils.angleMean ( angles)
Calculate the mean of an array of angles.

Parameters
----------
angles : `ndarray`
    An array of angles, in radians

Returns
-------
`lsst.geom.Angle`
    The mean angle

Definition at line 300 of file utils.py.

300def angleMean(angles):
301 """Calculate the mean of an array of angles.
302
303 Parameters
304 ----------
305 angles : `ndarray`
306 An array of angles, in radians
307
308 Returns
309 -------
310 `lsst.geom.Angle`
311 The mean angle
312 """
313 complexArray = [complex(np.cos(angle), np.sin(angle)) for angle in angles]
314 return (geom.Angle(np.angle(np.mean(complexArray))))
315
316
A class representing an angle.
Definition Angle.h:128

◆ computeAveragePsf()

afwImage.ImageD lsst.ip.diffim.utils.computeAveragePsf ( afwImage.Exposure exposure,
float psfExposureBuffer,
int psfExposureGrid )
Get the average PSF by evaluating it on a grid within an exposure.

Parameters
----------
exposure : `~lsst.afw.image.Exposure`
    The exposure for which the average PSF is to be computed.
    The exposure must contain a `psf` attribute.
psfExposureBuffer : `float`
    Fractional buffer margin to be left out of all sides of the image
    during the construction of the grid to compute average PSF in an
    exposure.
psfExposureGrid : `int`
    Grid size to compute the average PSF in an exposure.

Returns
-------
psfImage : `~lsst.afw.image.Image`
    The average PSF across the exposure.

Raises
------
ValueError
    Raised if the PSF cannot be computed at any of the grid points.

See Also
--------
`evaluateMeanPsfFwhm`

Definition at line 195 of file utils.py.

196 psfExposureBuffer: float, psfExposureGrid: int) -> afwImage.ImageD:
197 """Get the average PSF by evaluating it on a grid within an exposure.
198
199 Parameters
200 ----------
201 exposure : `~lsst.afw.image.Exposure`
202 The exposure for which the average PSF is to be computed.
203 The exposure must contain a `psf` attribute.
204 psfExposureBuffer : `float`
205 Fractional buffer margin to be left out of all sides of the image
206 during the construction of the grid to compute average PSF in an
207 exposure.
208 psfExposureGrid : `int`
209 Grid size to compute the average PSF in an exposure.
210
211 Returns
212 -------
213 psfImage : `~lsst.afw.image.Image`
214 The average PSF across the exposure.
215
216 Raises
217 ------
218 ValueError
219 Raised if the PSF cannot be computed at any of the grid points.
220
221 See Also
222 --------
223 `evaluateMeanPsfFwhm`
224 """
225
226 psf = exposure.psf
227
228 bbox = exposure.getBBox()
229 xmax, ymax = bbox.getMax()
230 xmin, ymin = bbox.getMin()
231
232 xbuffer = psfExposureBuffer*(xmax-xmin)
233 ybuffer = psfExposureBuffer*(ymax-ymin)
234
235 nImg = 0
236 psfArray = None
237 for (x, y) in itertools.product(np.linspace(xmin+xbuffer, xmax-xbuffer, psfExposureGrid),
238 np.linspace(ymin+ybuffer, ymax-ybuffer, psfExposureGrid)
239 ):
240 pos = geom.Point2D(x, y)
241 try:
242 singleImage = psf.computeKernelImage(pos)
243 except InvalidParameterError:
244 _LOG.debug("Unable to compute PSF image at position (%f, %f).", x, y)
245 continue
246
247 if psfArray is None:
248 psfArray = singleImage.array
249 else:
250 psfArray += singleImage.array
251 nImg += 1
252
253 if psfArray is None:
254 raise ValueError("Unable to compute PSF image at any position on the exposure.")
255
256 psfImage = afwImage.ImageD(psfArray/nImg)
257 return psfImage
258
259

◆ computePSFNoiseEquivalentArea()

lsst.ip.diffim.utils.computePSFNoiseEquivalentArea ( psf)
Compute the noise equivalent area for an image psf

Parameters
----------
psf : `lsst.afw.detection.Psf`

Returns
-------
nea : `float`

Definition at line 284 of file utils.py.

284def computePSFNoiseEquivalentArea(psf):
285 """Compute the noise equivalent area for an image psf
286
287 Parameters
288 ----------
289 psf : `lsst.afw.detection.Psf`
290
291 Returns
292 -------
293 nea : `float`
294 """
295 psfImg = psf.computeImage(psf.getAveragePosition())
296 nea = 1./np.sum(psfImg.array**2)
297 return nea
298
299

◆ computeRobustStatistics()

lsst.ip.diffim.utils.computeRobustStatistics ( image,
mask,
statsCtrl,
statistic = afwMath.MEANCLIP )
Calculate a robust mean of the variance plane of an exposure.

Parameters
----------
image : `lsst.afw.image.Image`
    Image or variance plane of an exposure to evaluate.
mask : `lsst.afw.image.Mask`
    Mask plane to use for excluding pixels.
statsCtrl : `lsst.afw.math.StatisticsControl`
    Statistics control object for configuring the calculation.
statistic : `lsst.afw.math.Property`, optional
    The type of statistic to compute. Typical values are
    ``afwMath.MEANCLIP`` or ``afwMath.STDEVCLIP``.

Returns
-------
value : `float`
    The result of the statistic calculated from the unflagged pixels.

Definition at line 260 of file utils.py.

260def computeRobustStatistics(image, mask, statsCtrl, statistic=afwMath.MEANCLIP):
261 """Calculate a robust mean of the variance plane of an exposure.
262
263 Parameters
264 ----------
265 image : `lsst.afw.image.Image`
266 Image or variance plane of an exposure to evaluate.
267 mask : `lsst.afw.image.Mask`
268 Mask plane to use for excluding pixels.
269 statsCtrl : `lsst.afw.math.StatisticsControl`
270 Statistics control object for configuring the calculation.
271 statistic : `lsst.afw.math.Property`, optional
272 The type of statistic to compute. Typical values are
273 ``afwMath.MEANCLIP`` or ``afwMath.STDEVCLIP``.
274
275 Returns
276 -------
277 value : `float`
278 The result of the statistic calculated from the unflagged pixels.
279 """
280 statObj = afwMath.makeStatistics(image, mask, statistic, statsCtrl)
281 return statObj.getValue(statistic)
282
283
Statistics makeStatistics(lsst::afw::image::Image< Pixel > const &img, lsst::afw::image::Mask< image::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl=StatisticsControl())
Handle a watered-down front-end to the constructor (no variance)
Definition Statistics.h:361

◆ evaluateMaskFraction()

lsst.ip.diffim.utils.evaluateMaskFraction ( mask,
maskPlane )
Evaluate the fraction of masked pixels in a mask plane.

Parameters
----------
mask : `lsst.afw.image.Mask`
    The mask to evaluate the fraction on
maskPlane : `str`
    The particular mask plane to evaluate the fraction

Returns
-------
value : `float`
    The calculated fraction of masked pixels

Definition at line 317 of file utils.py.

317def evaluateMaskFraction(mask, maskPlane):
318 """Evaluate the fraction of masked pixels in a mask plane.
319
320 Parameters
321 ----------
322 mask : `lsst.afw.image.Mask`
323 The mask to evaluate the fraction on
324 maskPlane : `str`
325 The particular mask plane to evaluate the fraction
326
327 Returns
328 -------
329 value : `float`
330 The calculated fraction of masked pixels
331 """
332 nMaskSet = np.count_nonzero((mask.array & mask.getPlaneBitMask(maskPlane)))
333 return nMaskSet/mask.array.size

◆ evaluateMeanPsfFwhm()

float lsst.ip.diffim.utils.evaluateMeanPsfFwhm ( afwImage.Exposure exposure,
float fwhmExposureBuffer,
int fwhmExposureGrid )
Get the mean PSF FWHM by evaluating it on a grid within an exposure.

Parameters
----------
exposure : `~lsst.afw.image.Exposure`
    The exposure for which the mean FWHM of the PSF is to be computed.
    The exposure must contain a `psf` attribute.
fwhmExposureBuffer : `float`
    Fractional buffer margin to be left out of all sides of the image
    during the construction of the grid to compute mean PSF FWHM in an
    exposure.
fwhmExposureGrid : `int`
    Grid size to compute the mean FWHM in an exposure.

Returns
-------
meanFwhm : `float`
    The mean PSF FWHM on the exposure.

Raises
------
ValueError
    Raised if the PSF cannot be computed at any of the grid points.

See Also
--------
`getPsfFwhm`
`computeAveragePsf`

Definition at line 135 of file utils.py.

136 fwhmExposureBuffer: float, fwhmExposureGrid: int) -> float:
137 """Get the mean PSF FWHM by evaluating it on a grid within an exposure.
138
139 Parameters
140 ----------
141 exposure : `~lsst.afw.image.Exposure`
142 The exposure for which the mean FWHM of the PSF is to be computed.
143 The exposure must contain a `psf` attribute.
144 fwhmExposureBuffer : `float`
145 Fractional buffer margin to be left out of all sides of the image
146 during the construction of the grid to compute mean PSF FWHM in an
147 exposure.
148 fwhmExposureGrid : `int`
149 Grid size to compute the mean FWHM in an exposure.
150
151 Returns
152 -------
153 meanFwhm : `float`
154 The mean PSF FWHM on the exposure.
155
156 Raises
157 ------
158 ValueError
159 Raised if the PSF cannot be computed at any of the grid points.
160
161 See Also
162 --------
163 `getPsfFwhm`
164 `computeAveragePsf`
165 """
166
167 psf = exposure.psf
168
169 bbox = exposure.getBBox()
170 xmax, ymax = bbox.getMax()
171 xmin, ymin = bbox.getMin()
172
173 xbuffer = fwhmExposureBuffer*(xmax-xmin)
174 ybuffer = fwhmExposureBuffer*(ymax-ymin)
175
176 width = []
177 for (x, y) in itertools.product(np.linspace(xmin+xbuffer, xmax-xbuffer, fwhmExposureGrid),
178 np.linspace(ymin+ybuffer, ymax-ybuffer, fwhmExposureGrid)
179 ):
180 pos = geom.Point2D(x, y)
181 try:
182 fwhm = getPsfFwhm(psf, average=True, position=pos)
183 except (InvalidParameterError, RangeError):
184 _LOG.debug("Unable to compute PSF FWHM at position (%f, %f).", x, y)
185 continue
186
187 width.append(fwhm)
188
189 if not width:
190 raise ValueError("Unable to compute PSF FWHM at any position on the exposure.")
191
192 return np.nanmean(width)
193
194

◆ getKernelCenterDisplacement()

lsst.ip.diffim.utils.getKernelCenterDisplacement ( kernel,
x,
y,
image = None )
Calculate the PSF matching kernel peak offset from the nominal
position.

Parameters
----------
kernel : `~lsst.afw.math.LinearCombinationKernel`
    The PSF matching kernel to evaluate.
x : `float`
    The x position on the detector to evaluate the kernel
y : `float`
    The y position on the detector to evaluate the kernel
image : `~lsst.afw.image.ImageD`
    The image to use as base for computing kernel pixel values

Returns
-------
kernel_sum : `float`
    The sum of the kernel on the desired location
dx : `float`
    The displacement of the kernel averaged peak, with respect to the
    center of the extraction of the kernel
dy : `float`
    The displacement of the kernel averaged peak, with respect to the
    center of the extraction of the kernel
pos_angle: `float`
    The position angle in detector coordinates of the displacement
length : `float`
    The displacement module of the kernel centroid in pixel units

Definition at line 40 of file utils.py.

40def getKernelCenterDisplacement(kernel, x, y, image=None):
41 """Calculate the PSF matching kernel peak offset from the nominal
42 position.
43
44 Parameters
45 ----------
46 kernel : `~lsst.afw.math.LinearCombinationKernel`
47 The PSF matching kernel to evaluate.
48 x : `float`
49 The x position on the detector to evaluate the kernel
50 y : `float`
51 The y position on the detector to evaluate the kernel
52 image : `~lsst.afw.image.ImageD`
53 The image to use as base for computing kernel pixel values
54
55 Returns
56 -------
57 kernel_sum : `float`
58 The sum of the kernel on the desired location
59 dx : `float`
60 The displacement of the kernel averaged peak, with respect to the
61 center of the extraction of the kernel
62 dy : `float`
63 The displacement of the kernel averaged peak, with respect to the
64 center of the extraction of the kernel
65 pos_angle: `float`
66 The position angle in detector coordinates of the displacement
67 length : `float`
68 The displacement module of the kernel centroid in pixel units
69 """
70
71 if image is None:
72 image = afwImage.ImageD(kernel.getDimensions())
73
74 # obtain the kernel image
75 hsize = kernel.getWidth()//2
76 kernel_sum = kernel.computeImage(image, doNormalize=False, x=x, y=y)
77
78 data = image.array
79 h, w = data.shape
80 xx = np.arange(w)
81 yy = np.arange(h)
82
83 # create sum vectors and estimate weighted average
84 vx = data.sum(axis=0)
85 vx /= vx.sum()
86 dx = np.dot(vx, xx) - hsize
87
88 vy = data.sum(axis=1)
89 vy /= vy.sum()
90 dy = np.dot(vy, yy) - hsize
91
92 # obtain position angle and norm of displacement
93 pos_angle = np.arctan2(dy, dx)
94 length = np.sqrt(dx**2 + dy**2)
95
96 return kernel_sum, dx, dy, pos_angle, length
97
98

◆ getPsfFwhm()

lsst.ip.diffim.utils.getPsfFwhm ( psf,
average = True,
position = None )
Directly calculate the horizontal and vertical widths
of a PSF at half its maximum value.

Parameters
----------
psf : `~lsst.afw.detection.Psf`
    Point spread function (PSF) to evaluate.
average : `bool`, optional
    Set to return the average width over Y and X axes.
position : `~lsst.geom.Point2D`, optional
    The position at which to evaluate the PSF. If `None`, then the
    average position is used.

Returns
-------
psfSize : `float` | `tuple` [`float`]
    The FWHM of the PSF computed at its average position.
    Returns the widths along the Y and X axes,
    or the average of the two if `average` is set.

See Also
--------
evaluateMeanPsfFwhm

Definition at line 99 of file utils.py.

99def getPsfFwhm(psf, average=True, position=None):
100 """Directly calculate the horizontal and vertical widths
101 of a PSF at half its maximum value.
102
103 Parameters
104 ----------
105 psf : `~lsst.afw.detection.Psf`
106 Point spread function (PSF) to evaluate.
107 average : `bool`, optional
108 Set to return the average width over Y and X axes.
109 position : `~lsst.geom.Point2D`, optional
110 The position at which to evaluate the PSF. If `None`, then the
111 average position is used.
112
113 Returns
114 -------
115 psfSize : `float` | `tuple` [`float`]
116 The FWHM of the PSF computed at its average position.
117 Returns the widths along the Y and X axes,
118 or the average of the two if `average` is set.
119
120 See Also
121 --------
122 evaluateMeanPsfFwhm
123 """
124 if position is None:
125 position = psf.getAveragePosition()
126 shape = psf.computeShape(position)
127 sigmaToFwhm = 2*np.log(2*np.sqrt(2))
128
129 if average:
130 return sigmaToFwhm*shape.getTraceRadius()
131 else:
132 return [sigmaToFwhm*np.sqrt(shape.getIxx()), sigmaToFwhm*np.sqrt(shape.getIyy())]
133
134

Variable Documentation

◆ _LOG

lsst.ip.diffim.utils._LOG = getLogger(__name__)
protected

Definition at line 37 of file utils.py.