LSSTApplications  16.0-10-g4f78f78+16,16.0-10-gc1446dd+42,16.0-11-g09ed895+1,16.0-13-g7649090,16.0-14-g0a28612+1,16.0-14-g6c7ed55+16,16.0-15-ga29f190+1,16.0-16-g89065d4+14,16.0-16-gd8e3590+16,16.0-16-ge6a35c8+6,16.0-17-g7e0e4ff+10,16.0-17-ga3d2e9f,16.0-19-gb830ed4e+16,16.0-2-g0febb12+21,16.0-2-g9d5294e+61,16.0-2-ga8830df+5,16.0-24-gc1c7f52+9,16.0-25-g07af9f2+1,16.0-3-ge00e371+21,16.0-36-g07840cb1,16.0-4-g18f3627+5,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+16,16.0-4-gade8416+9,16.0-4-gb13d127+5,16.0-5-g6a53317+21,16.0-5-gb3f8a4b+74,16.0-5-gef99c9f+12,16.0-6-g9321be7+4,16.0-6-gcbc7b31+22,16.0-6-gf49912c+16,16.0-63-gae20905ba,16.0-7-gd2eeba5+31,16.0-8-g21fd5fe+16,16.0-8-g3a9f023+12,16.0-8-g4734f7a,16.0-9-g85d1a16+16,16.0-9-gf5c1f43,master-g07ce7b41a7,w.2018.48
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  "CalibNoThrow", "projectImage", "getProjectionIndices"]
25 
26 import numpy as np
27 
28 import lsst.afw.detection as afwDetect
29 from .maskedImage import MaskedImage, makeMaskedImage
30 from .image import Mask
31 from .filter import Filter, FilterProperty
32 from .calib import Calib
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 
55  """Reset registry of filters and filter properties"""
56  Filter.reset()
57  FilterProperty.reset()
58 
59 
60 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
61  """Define a filter and its properties in the filter registry"""
62  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
63  Filter.define(prop)
64  if isinstance(alias, str):
65  Filter.defineAlias(name, alias)
66  else:
67  for a in alias:
68  Filter.defineAlias(name, a)
69 
70 
72  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
73  instead of raising exceptions (exceptions may be raised for other purposes).
74 
75 E.g.
76  with CalibNoThrow():
77  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
78  """
79 
80  def __enter__(self):
81  self._throwOnNegative = Calib.getThrowOnNegativeFlux()
82  Calib.setThrowOnNegativeFlux(False)
83 
84  def __exit__(self, *args):
85  Calib.setThrowOnNegativeFlux(self._throwOnNegative)
86 
87 
88 def getProjectionIndices(imageBBox, targetBBox):
89  """Get the indices to project an image
90 
91  Given an image and target bounding box,
92  calculate the indices needed to appropriately
93  slice the input image and target image to
94  project the image to the target.
95 
96  Parameters
97  ----------
98  imageBBox: Box2I
99  Bounding box of the input image
100  targetBBox: Box2I
101  Bounding box of the target image
102 
103  Returns
104  -------
105  targetSlices: tuple
106  Slices of the target image in the form (by, bx), (iy, ix).
107  imageIndices: tuple
108  Slices of the input image in the form (by, bx), (iy, ix).
109  """
110  def getMin(dXmin):
111  """Get minimum indices"""
112  if dXmin < 0:
113  bxStart = -dXmin
114  ixStart = 0
115  else:
116  bxStart = 0
117  ixStart = dXmin
118  return bxStart, ixStart
119 
120  def getMax(dXmax):
121  """Get maximum indices"""
122  if dXmax < 0:
123  bxEnd = None
124  ixEnd = dXmax
125  elif dXmax != 0:
126  bxEnd = -dXmax
127  ixEnd = None
128  else:
129  bxEnd = ixEnd = None
130  return bxEnd, ixEnd
131 
132  dXmin = targetBBox.getMinX() - imageBBox.getMinX()
133  dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
134  dYmin = targetBBox.getMinY() - imageBBox.getMinY()
135  dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
136 
137  bxStart, ixStart = getMin(dXmin)
138  byStart, iyStart = getMin(dYmin)
139  bxEnd, ixEnd = getMax(dXmax)
140  byEnd, iyEnd = getMax(dYmax)
141 
142  bx = slice(bxStart, bxEnd)
143  by = slice(byStart, byEnd)
144  ix = slice(ixStart, ixEnd)
145  iy = slice(iyStart, iyEnd)
146  return (by, bx), (iy, ix)
147 
148 
149 def projectImage(image, bbox, fill=0):
150  """Project an image into a bounding box
151 
152  Return a new image whose pixels are equal to those of
153  `image` within `bbox`, and equal to `fill` outside.
154 
155  Parameters
156  ----------
157  image: `afw.Image` or `afw.MaskedImage`
158  The image to project
159  bbox: `Box2I`
160  The bounding box to project onto.
161  fill: number
162  The value to fill the region of the new
163  image outside the bounding box of the original.
164 
165  Returns
166  -------
167  newImage: `afw.Image` or `afw.MaskedImage`
168  The new image with the input image projected
169  into its bounding box.
170  """
171  if image.getBBox() == bbox:
172  return image
173  (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
174 
175  if isinstance(image, MaskedImage):
176  newImage = type(image.image)(bbox)
177  newImage.array[by, bx] = image.image.array[iy, ix]
178  newMask = type(image.mask)(bbox)
179  newMask.array[by, bx] = image.mask.array[iy, ix]
180  newVariance = type(image.image)(bbox)
181  newVariance.array[by, bx] = image.variance.array[iy, ix]
182  newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
183  else:
184  newImage = type(image)(bbox)
185  if fill != 0:
186  newImage.set(fill)
187  newImage.array[by, bx] = image.array[iy, ix]
188  return newImage
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:1280
table::Key< int > type
Definition: Detector.cc:164
def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:60
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
def clipImage(im, minClip, maxClip)
Definition: utils.py:35
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
def projectImage(image, bbox, fill=0)
Definition: utils.py:149
def getProjectionIndices(imageBBox, targetBBox)
Definition: utils.py:88