LSSTApplications  16.0-1-gce273f5+17,16.0-1-gf77f410+12,16.0-10-g5a5abec+8,16.0-10-gc1446dd+12,16.0-12-g1dc09ba+6,16.0-12-g569485f,16.0-12-ga22ed6e+1,16.0-13-g4c33ca5+12,16.0-13-gb122224+3,16.0-13-gd9b1b71+12,16.0-14-g22e2ff2,16.0-14-g71e547a+8,16.0-17-g0bdc215+4,16.0-17-g6a7bfb3b+12,16.0-2-g0febb12+14,16.0-2-g839ba83+50,16.0-2-g9d5294e+39,16.0-20-ga7ad2685,16.0-3-g404ea43+9,16.0-3-gbc759ec+10,16.0-3-gcfd6c53+37,16.0-4-g03cf288+28,16.0-4-g13a27c5+14,16.0-4-g5f3a788+13,16.0-4-g8a0f11a+34,16.0-4-ga3eb747+3,16.0-45-g4805a823c,16.0-5-g1991253+12,16.0-5-g1e9226d+1,16.0-5-g865efd9+12,16.0-5-gb3f8a4b+44,16.0-5-gd0f1235+6,16.0-6-gf0acd13+31,16.0-6-gf9cb114+13,16.0-7-g6043bfc,16.0-7-ga8e1655+8,16.0-8-g23bbf3f+3,16.0-8-g4dec96c+25,16.0-8-gfd407c0+2,master-g965b868a3d+1,master-gdc6be1965f+1,w.2018.39
LSSTDataManagementBasePackage
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  "defineFiltersFromPolicy", "CalibNoThrow", "projectImage", "getProjectionIndices"]
25 
26 import numpy as np
27 
28 import lsst.pex.policy as pexPolicy
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 from .calib import Calib
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 
56  """Reset registry of filters and filter properties"""
57  Filter.reset()
58  FilterProperty.reset()
59 
60 
61 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
62  """Define a filter and its properties in the filter registry"""
63  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
64  Filter.define(prop)
65  if isinstance(alias, str):
66  Filter.defineAlias(name, alias)
67  else:
68  for a in alias:
69  Filter.defineAlias(name, a)
70 
71 
72 def defineFiltersFromPolicy(filterPolicy, reset=False):
73  """Process a Policy and define the filters"""
74 
75  if reset:
76  Filter.reset()
77  FilterProperty.reset()
78  #
79  # Process the Policy and define the filters
80  #
81  policyFile = pexPolicy.DefaultPolicyFile(
82  "afw", "FilterDictionary.paf", "policy")
83  defPolicy = pexPolicy.Policy.createPolicy(
84  policyFile, policyFile.getRepositoryPath(), True)
85 
86  filterPolicy.mergeDefaults(defPolicy.getDictionary())
87 
88  for p in filterPolicy.getArray("Filter"):
89  Filter.define(FilterProperty(p.get("name"), p))
90  if p.exists("alias"):
91  for a in p.getArray("alias"):
92  Filter.defineAlias(p.get("name"), a)
93 
94 
96  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
97  instead of raising exceptions (exceptions may be raised for other purposes).
98 
99 E.g.
100  with CalibNoThrow():
101  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
102  """
103 
104  def __enter__(self):
105  self._throwOnNegative = Calib.getThrowOnNegativeFlux()
106  Calib.setThrowOnNegativeFlux(False)
107 
108  def __exit__(self, *args):
109  Calib.setThrowOnNegativeFlux(self._throwOnNegative)
110 
111 
112 def getProjectionIndices(imageBBox, targetBBox):
113  """Get the indices to project an image
114 
115  Given an image and target bounding box,
116  calculate the indices needed to appropriately
117  slice the input image and target image to
118  project the image to the target.
119 
120  Parameters
121  ----------
122  imageBBox: Box2I
123  Bounding box of the input image
124  targetBBox: Box2I
125  Bounding box of the target image
126 
127  Returns
128  -------
129  targetSlices: tuple
130  Slices of the target image in the form (by, bx), (iy, ix).
131  imageIndices: tuple
132  Slices of the input image in the form (by, bx), (iy, ix).
133  """
134  def getMin(dXmin):
135  """Get minimum indices"""
136  if dXmin < 0:
137  bxStart = -dXmin
138  ixStart = 0
139  else:
140  bxStart = 0
141  ixStart = dXmin
142  return bxStart, ixStart
143 
144  def getMax(dXmax):
145  """Get maximum indices"""
146  if dXmax < 0:
147  bxEnd = None
148  ixEnd = dXmax
149  elif dXmax != 0:
150  bxEnd = -dXmax
151  ixEnd = None
152  else:
153  bxEnd = ixEnd = None
154  return bxEnd, ixEnd
155 
156  dXmin = targetBBox.getMinX() - imageBBox.getMinX()
157  dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
158  dYmin = targetBBox.getMinY() - imageBBox.getMinY()
159  dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
160 
161  bxStart, ixStart = getMin(dXmin)
162  byStart, iyStart = getMin(dYmin)
163  bxEnd, ixEnd = getMax(dXmax)
164  byEnd, iyEnd = getMax(dYmax)
165 
166  bx = slice(bxStart, bxEnd)
167  by = slice(byStart, byEnd)
168  ix = slice(ixStart, ixEnd)
169  iy = slice(iyStart, iyEnd)
170  return (by, bx), (iy, ix)
171 
172 
173 def projectImage(image, bbox, fill=0):
174  """Project an image into a bounding box
175 
176  Return a new image whose pixels are equal to those of
177  `image` within `bbox`, and equal to `fill` outside.
178 
179  Parameters
180  ----------
181  image: `afw.Image` or `afw.MaskedImage`
182  The image to project
183  bbox: `Box2I`
184  The bounding box to project onto.
185  fill: number
186  The value to fill the region of the new
187  image outside the bounding box of the original.
188 
189  Returns
190  -------
191  newImage: `afw.Image` or `afw.MaskedImage`
192  The new image with the input image projected
193  into its bounding box.
194  """
195  if image.getBBox() == bbox:
196  return image
197  (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
198 
199  if isinstance(image, MaskedImage):
200  newImage = type(image.image)(bbox)
201  newImage.array[by, bx] = image.image.array[iy, ix]
202  newMask = type(image.mask)(bbox)
203  newMask.array[by, bx] = image.mask.array[iy, ix]
204  newVariance = type(image.image)(bbox)
205  newVariance.array[by, bx] = image.variance.array[iy, ix]
206  newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
207  else:
208  newImage = type(image)(bbox)
209  if fill != 0:
210  newImage.set(fill)
211  newImage.array[by, bx] = image.array[iy, ix]
212  return newImage
def defineFiltersFromPolicy(filterPolicy, reset=False)
Definition: utils.py:72
a representation of a default Policy file that is stored as a file in the installation directory of a...
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
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:1271
def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:61
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
def clipImage(im, minClip, maxClip)
Definition: utils.py:36
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
def projectImage(image, bbox, fill=0)
Definition: utils.py:173
def getProjectionIndices(imageBBox, targetBBox)
Definition: utils.py:112