LSSTApplications  16.0-1-gce273f5+20,16.0-10-g1758552,16.0-10-gc1446dd+21,16.0-12-g569485f+3,16.0-13-g5f20d24+1,16.0-13-g80874fd+2,16.0-13-gb122224+12,16.0-14-g08f9460+4,16.0-14-g8a3b804,16.0-14-ga5060d2,16.0-15-g77ef378+7,16.0-18-gdf247dd+2,16.0-18-ge18fa5b,16.0-18-ge4151178,16.0-2-g0febb12+16,16.0-2-g9d5294e+46,16.0-2-gc6e0ed0+5,16.0-23-ge8a9b866+3,16.0-3-g404ea43+13,16.0-3-gbc759ec+19,16.0-3-gcfd6c53+44,16.0-3-ge00e371,16.0-4-g03cf288+35,16.0-4-g13a27c5+21,16.0-4-g5f3a788+15,16.0-4-ga3eb747+5,16.0-5-g1991253+21,16.0-5-g1e9226d+3,16.0-5-g6a53317,16.0-5-g865efd9+23,16.0-5-gb3f8a4b+53,16.0-5-gd0f1235+10,16.0-52-gad2f36c79,16.0-7-g6043bfc+9,16.0-7-gd2eeba5+3,16.0-7-gde5bd64+3,16.0-8-g0e813a6+1,16.0-9-g52c50f7,16.0-9-g73415e6,master-g5768c874b9+5,w.2018.41
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
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