LSST Applications  21.0.0+75b29a8a7f,21.0.0+e70536a077,21.0.0-1-ga51b5d4+62c747d40b,21.0.0-10-gbfb87ad6+3307648ee3,21.0.0-15-gedb9d5423+47cba9fc36,21.0.0-2-g103fe59+fdf0863a2a,21.0.0-2-g1367e85+d38a93257c,21.0.0-2-g45278ab+e70536a077,21.0.0-2-g5242d73+d38a93257c,21.0.0-2-g7f82c8f+e682ffb718,21.0.0-2-g8dde007+d179fbfa6a,21.0.0-2-g8f08a60+9402881886,21.0.0-2-ga326454+e682ffb718,21.0.0-2-ga63a54e+08647d4b1b,21.0.0-2-gde069b7+26c92b3210,21.0.0-2-gecfae73+0445ed2f95,21.0.0-2-gfc62afb+d38a93257c,21.0.0-27-gbbd0d29+ae871e0f33,21.0.0-28-g5fc5e037+feb0e9397b,21.0.0-3-g21c7a62+f4b9c0ff5c,21.0.0-3-g357aad2+57b0bddf0b,21.0.0-3-g4be5c26+d38a93257c,21.0.0-3-g65f322c+3f454acf5d,21.0.0-3-g7d9da8d+75b29a8a7f,21.0.0-3-gaa929c8+9e4ef6332c,21.0.0-3-ge02ed75+4b120a55c4,21.0.0-4-g3300ddd+e70536a077,21.0.0-4-g591bb35+4b120a55c4,21.0.0-4-gc004bbf+4911b9cd27,21.0.0-4-gccdca77+f94adcd104,21.0.0-4-ge8fba5a+2b3a696ff9,21.0.0-5-gb155db7+2c5429117a,21.0.0-5-gdf36809+637e4641ee,21.0.0-6-g00874e7+c9fd7f7160,21.0.0-6-g4e60332+4b120a55c4,21.0.0-7-gc8ca178+40eb9cf840,21.0.0-8-gfbe0b4b+9e4ef6332c,21.0.0-9-g2fd488a+d83b7cd606,w.2021.05
LSST Data Management Base Package
utils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2017 LSST/AURA.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ["clipImage", "resetFilters", "defineFilter",
24  "projectImage", "getProjectionIndices"]
25 
26 from deprecated.sphinx import deprecated
27 
28 import numpy as np
29 
30 import lsst.afw.detection as afwDetect
31 from .maskedImage import MaskedImage, makeMaskedImage
32 from .image import Mask
33 from .filter import Filter, FilterProperty
34 
35 
36 def clipImage(im, minClip, maxClip):
37  """Clip an image to lie between minClip and maxclip (None to ignore)"""
38  if isinstance(im, MaskedImage):
39  mi = im
40  else:
41  mi = makeMaskedImage(im, Mask(im.getDimensions()))
42 
43  if minClip is not None:
45  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
46  afwDetect.setImageFromFootprintList(
47  mi.getImage(), ds.getFootprints(), minClip)
48 
49  if maxClip is not None:
51  afwDetect.setImageFromFootprintList(
52  mi.getImage(), ds.getFootprints(), maxClip)
53 
54 
55 @deprecated(reason=("Removed with no replacement (FilterLabels do not need to be reset)."
56  " Will be removed after v22."), category=FutureWarning, version="v22")
58  """Reset registry of filters and filter properties"""
59  Filter.reset()
60  FilterProperty.reset()
61 
62 
63 @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."),
64  category=FutureWarning, version="v22")
65 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
66  """Define a filter and its properties in the filter registry"""
67  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
68  Filter.define(prop)
69  if isinstance(alias, str):
70  Filter.defineAlias(name, alias)
71  else:
72  for a in alias:
73  Filter.defineAlias(name, a)
74 
75 
76 def getProjectionIndices(imageBBox, targetBBox):
77  """Get the indices to project an image
78 
79  Given an image and target bounding box,
80  calculate the indices needed to appropriately
81  slice the input image and target image to
82  project the image to the target.
83 
84  Parameters
85  ----------
86  imageBBox: Box2I
87  Bounding box of the input image
88  targetBBox: Box2I
89  Bounding box of the target image
90 
91  Returns
92  -------
93  targetSlices: tuple
94  Slices of the target image in the form (by, bx), (iy, ix).
95  imageIndices: tuple
96  Slices of the input image in the form (by, bx), (iy, ix).
97  """
98  def getMin(dXmin):
99  """Get minimum indices"""
100  if dXmin < 0:
101  bxStart = -dXmin
102  ixStart = 0
103  else:
104  bxStart = 0
105  ixStart = dXmin
106  return bxStart, ixStart
107 
108  def getMax(dXmax):
109  """Get maximum indices"""
110  if dXmax < 0:
111  bxEnd = None
112  ixEnd = dXmax
113  elif dXmax != 0:
114  bxEnd = -dXmax
115  ixEnd = None
116  else:
117  bxEnd = ixEnd = None
118  return bxEnd, ixEnd
119 
120  dXmin = targetBBox.getMinX() - imageBBox.getMinX()
121  dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
122  dYmin = targetBBox.getMinY() - imageBBox.getMinY()
123  dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
124 
125  bxStart, ixStart = getMin(dXmin)
126  byStart, iyStart = getMin(dYmin)
127  bxEnd, ixEnd = getMax(dXmax)
128  byEnd, iyEnd = getMax(dYmax)
129 
130  bx = slice(bxStart, bxEnd)
131  by = slice(byStart, byEnd)
132  ix = slice(ixStart, ixEnd)
133  iy = slice(iyStart, iyEnd)
134  return (by, bx), (iy, ix)
135 
136 
137 def projectImage(image, bbox, fill=0):
138  """Project an image into a bounding box
139 
140  Return a new image whose pixels are equal to those of
141  `image` within `bbox`, and equal to `fill` outside.
142 
143  Parameters
144  ----------
145  image: `afw.Image` or `afw.MaskedImage`
146  The image to project
147  bbox: `Box2I`
148  The bounding box to project onto.
149  fill: number
150  The value to fill the region of the new
151  image outside the bounding box of the original.
152 
153  Returns
154  -------
155  newImage: `afw.Image` or `afw.MaskedImage`
156  The new image with the input image projected
157  into its bounding box.
158  """
159  if image.getBBox() == bbox:
160  return image
161  (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
162 
163  if isinstance(image, MaskedImage):
164  newImage = type(image.image)(bbox)
165  newImage.array[by, bx] = image.image.array[iy, ix]
166  newMask = type(image.mask)(bbox)
167  newMask.array[by, bx] = image.mask.array[iy, ix]
168  newVariance = type(image.image)(bbox)
169  newVariance.array[by, bx] = image.variance.array[iy, ix]
170  newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
171  else:
172  newImage = type(image)(bbox)
173  if fill != 0:
174  newImage.set(fill)
175  newImage.array[by, bx] = image.array[iy, ix]
176  return newImage
177 
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:36
def getProjectionIndices(imageBBox, targetBBox)
Definition: utils.py:77
def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:66
def projectImage(image, bbox, fill=0)
Definition: utils.py:138
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:1268
FilterProperty(FilterProperty const &)=default
table::Key< int > type
Definition: Detector.cc:163