LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
utils.py
Go to the documentation of this file.
1 # This file is part of afw.
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 __all__ = ["clipImage", "resetFilters", "defineFilter",
23  "projectImage", "getProjectionIndices"]
24 
25 from deprecated.sphinx import deprecated
26 
27 import numpy as np
28 
29 import lsst.afw.detection as afwDetect
30 from .maskedImage import MaskedImage, makeMaskedImage
31 from .image import Mask
32 from ._filter import Filter, FilterProperty
33 
34 
35 def clipImage(im, minClip, maxClip):
36  """Clip an image to lie between minClip and maxclip (None to ignore)"""
37  if isinstance(im, MaskedImage):
38  mi = im
39  else:
40  mi = makeMaskedImage(im, Mask(im.getDimensions()))
41 
42  if minClip is not None:
44  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
45  afwDetect.setImageFromFootprintList(
46  mi.getImage(), ds.getFootprints(), minClip)
47 
48  if maxClip is not None:
50  afwDetect.setImageFromFootprintList(
51  mi.getImage(), ds.getFootprints(), maxClip)
52 
53 
54 @deprecated(reason=("Removed with no replacement (FilterLabels do not need to be reset)."
55  " Will be removed after v22."), category=FutureWarning, version="v22")
57  """Reset registry of filters and filter properties"""
58  Filter.reset()
59  FilterProperty.reset()
60 
61 
62 @deprecated(reason=("Removed with no replacement (but see lsst::afw::image::TransmissionCurve for how to set" "and retrieve filter wavelength information). Will be removed after v22."),
63  category=FutureWarning, version="v22")
64 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
65  """Define a filter and its properties in the filter registry"""
66  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
67  Filter.define(prop)
68  if isinstance(alias, str):
69  Filter.defineAlias(name, alias)
70  else:
71  for a in alias:
72  Filter.defineAlias(name, a)
73 
74 
75 def getProjectionIndices(imageBBox, targetBBox):
76  """Get the indices to project an image
77 
78  Given an image and target bounding box,
79  calculate the indices needed to appropriately
80  slice the input image and target image to
81  project the image to the target.
82 
83  Parameters
84  ----------
85  imageBBox: Box2I
86  Bounding box of the input image
87  targetBBox: Box2I
88  Bounding box of the target image
89 
90  Returns
91  -------
92  targetSlices: tuple
93  Slices of the target image in the form (by, bx), (iy, ix).
94  imageIndices: tuple
95  Slices of the input image in the form (by, bx), (iy, ix).
96  """
97  def getMin(dXmin):
98  """Get minimum indices"""
99  if dXmin < 0:
100  bxStart = -dXmin
101  ixStart = 0
102  else:
103  bxStart = 0
104  ixStart = dXmin
105  return bxStart, ixStart
106 
107  def getMax(dXmax):
108  """Get maximum indices"""
109  if dXmax < 0:
110  bxEnd = None
111  ixEnd = dXmax
112  elif dXmax != 0:
113  bxEnd = -dXmax
114  ixEnd = None
115  else:
116  bxEnd = ixEnd = None
117  return bxEnd, ixEnd
118 
119  dXmin = targetBBox.getMinX() - imageBBox.getMinX()
120  dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
121  dYmin = targetBBox.getMinY() - imageBBox.getMinY()
122  dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
123 
124  bxStart, ixStart = getMin(dXmin)
125  byStart, iyStart = getMin(dYmin)
126  bxEnd, ixEnd = getMax(dXmax)
127  byEnd, iyEnd = getMax(dYmax)
128 
129  bx = slice(bxStart, bxEnd)
130  by = slice(byStart, byEnd)
131  ix = slice(ixStart, ixEnd)
132  iy = slice(iyStart, iyEnd)
133  return (by, bx), (iy, ix)
134 
135 
136 def projectImage(image, bbox, fill=0):
137  """Project an image into a bounding box
138 
139  Return a new image whose pixels are equal to those of
140  `image` within `bbox`, and equal to `fill` outside.
141 
142  Parameters
143  ----------
144  image: `afw.Image` or `afw.MaskedImage`
145  The image to project
146  bbox: `Box2I`
147  The bounding box to project onto.
148  fill: number
149  The value to fill the region of the new
150  image outside the bounding box of the original.
151 
152  Returns
153  -------
154  newImage: `afw.Image` or `afw.MaskedImage`
155  The new image with the input image projected
156  into its bounding box.
157  """
158  if image.getBBox() == bbox:
159  return image
160  (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
161 
162  if isinstance(image, MaskedImage):
163  newImage = type(image.image)(bbox)
164  newImage.array[by, bx] = image.image.array[iy, ix]
165  newMask = type(image.mask)(bbox)
166  newMask.array[by, bx] = image.mask.array[iy, ix]
167  newVariance = type(image.image)(bbox)
168  newVariance.array[by, bx] = image.variance.array[iy, ix]
169  newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
170  else:
171  newImage = type(image)(bbox)
172  if fill != 0:
173  newImage.set(fill)
174  newImage.array[by, bx] = image.array[iy, ix]
175  return newImage
176 
table::Key< int > type
Definition: Detector.cc:163
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
def clipImage(im, minClip, maxClip)
Definition: utils.py:35
def getProjectionIndices(imageBBox, targetBBox)
Definition: utils.py:76
def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:65
def projectImage(image, bbox, fill=0)
Definition: utils.py:137
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
Definition: MaskedImage.h:1240