lsst.ip.diffim  16.0-5-gb866ac2
snapPsfMatch.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2016 LSST Corporation.
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__ = ["SnapPsfMatchConfigDF", "SnapPsfMatchConfigAL", "SnapPsfMatchConfig", "SnapPsfMatchTask"]
24 
25 import lsst.pex.config as pexConfig
26 from .psfMatch import PsfMatchConfigDF, PsfMatchConfigAL
27 from .imagePsfMatch import ImagePsfMatchTask, ImagePsfMatchConfig
28 
29 
31  """Delta-function Psf-matching config optimized for snap subtraction"""
32 
33  def setDefaults(self):
34  PsfMatchConfigDF.setDefaults(self)
35 
36  # No regularization
37  self.useRegularization = False
38 
39  # Pca
41  self.subtractMeanForPca = True
43 
44 
46  """Sum-of-Gaussian (Alard-Lupton) Psf-matching config optimized for snap subtraction"""
47 
48  def setDefaults(self):
49  PsfMatchConfigAL.setDefaults(self)
50 
51  # Simple basis set
52  self.alardNGauss = 2
53  self.alardDegGauss = (4, 2)
54  self.alardSigGauss = (1.0, 2.5)
55 
56 
58  kernel = pexConfig.ConfigChoiceField(
59  doc="kernel type",
60  typemap=dict(
61  AL=SnapPsfMatchConfigAL,
62  DF=SnapPsfMatchConfigDF
63  ),
64  default="AL",
65  )
66 
67  doWarping = pexConfig.Field(
68  dtype=bool,
69  doc="Warp the snaps?",
70  default=False
71  )
72 
73  def setDefaults(self):
74  ImagePsfMatchConfig.setDefaults(self)
75 
76  # No spatial variation in model
77  self.kernel.active.spatialKernelOrder = 0
78 
79  # Don't fit for differential background
80  self.kernel.active.fitForBackground = False
81 
82  # Small kernel size
83  self.kernel.active.kernelSize = 7
84 
85  # With zero spatial order don't worry about spatial clipping
86  self.kernel.active.spatialKernelClipping = False
87 
88 
94 
95 
97  """!
98 @anchor SnapPsfMatchTask_
99 
100 @brief Image-based Psf-matching of two subsequent snaps from the same visit
101 
102 @section ip_diffim_snappsfmatch_Contents Contents
103 
104  - @ref ip_diffim_snappsfmatch_Purpose
105  - @ref ip_diffim_snappsfmatch_Initialize
106  - @ref ip_diffim_snappsfmatch_IO
107  - @ref ip_diffim_snappsfmatch_Config
108  - @ref ip_diffim_snappsfmatch_Metadata
109  - @ref ip_diffim_snappsfmatch_Debug
110  - @ref ip_diffim_snappsfmatch_Example
111 
112 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
113 
114 @section ip_diffim_snappsfmatch_Purpose Description
115 
116 @copybrief SnapPsfMatchTask
117 
118 This Task differs from ImagePsfMatchTask in that it matches two Exposures assuming that the images have
119 been acquired very closely in time. Under this assumption, the astrometric misalignments and/or
120 relative distortions should be within a pixel, and the Psf-shapes should be very similar. As a
121 consequence, the default configurations for this class assume a very simple solution.
122 
123  . The spatial variation in the kernel (SnapPsfMatchConfig.spatialKernelOrder) is assumed to be zero
124 
125  . With no spatial variation, we turn of the spatial clipping loops (SnapPsfMatchConfig.spatialKernelClipping)
126 
127  . The differential background is _not_ fit for (SnapPsfMatchConfig.fitForBackground)
128 
129  . The kernel is expected to be appx. a delta function, and has a small size (SnapPsfMatchConfig.kernelSize)
130 
131 The sub-configurations for the Alard-Lupton (SnapPsfMatchConfigAL) and delta-function (SnapPsfMatchConfigDF)
132 bases also are designed to generate a small, simple kernel.
133 
134 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
135 
136 @section ip_diffim_snappsfmatch_Initialize Task initialization
137 
138 Initialization is the same as base class ImagePsfMatch.__init__, with the difference being that the Task's
139 ConfigClass is SnapPsfMatchConfig.
140 
141 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
142 
143 @section ip_diffim_snappsfmatch_IO Invoking the Task
144 
145 The Task is only configured to have a subtractExposures method, which in turn calls
146 ImagePsfMatchTask.subtractExposures.
147 
148 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
149 
150 @section ip_diffim_snappsfmatch_Config Configuration parameters
151 
152 See @ref SnapPsfMatchConfig, which uses either @ref SnapPsfMatchConfigDF and @ref SnapPsfMatchConfigAL
153 as its active configuration.
154 
155 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
156 
157 @section ip_diffim_snappsfmatch_Metadata Quantities set in Metadata
158 
159 See @ref ip_diffim_psfmatch_Metadata "PsfMatchTask"
160 
161 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
162 
163 @section ip_diffim_snappsfmatch_Debug Debug variables
164 
165 The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a
166 flag @c -d/--debug to import @b debug.py from your @c PYTHONPATH. The relevant contents of debug.py
167 for this Task include:
168 
169 @code{.py}
170  import sys
171  import lsstDebug
172  def DebugInfo(name):
173  di = lsstDebug.getInfo(name)
174  if name == "lsst.ip.diffim.psfMatch":
175  di.display = True # enable debug output
176  di.maskTransparency = 80 # ds9 mask transparency
177  di.displayCandidates = True # show all the candidates and residuals
178  di.displayKernelBasis = False # show kernel basis functions
179  di.displayKernelMosaic = True # show kernel realized across the image
180  di.plotKernelSpatialModel = False # show coefficients of spatial model
181  di.showBadCandidates = True # show the bad candidates (red) along with good (green)
182  elif name == "lsst.ip.diffim.imagePsfMatch":
183  di.display = True # enable debug output
184  di.maskTransparency = 30 # ds9 mask transparency
185  di.displayTemplate = True # show full (remapped) template
186  di.displaySciIm = True # show science image to match to
187  di.displaySpatialCells = True # show spatial cells
188  di.displayDiffIm = True # show difference image
189  di.showBadCandidates = True # show the bad candidates (red) along with good (green)
190  elif name == "lsst.ip.diffim.diaCatalogSourceSelector":
191  di.display = False # enable debug output
192  di.maskTransparency = 30 # ds9 mask transparency
193  di.displayExposure = True # show exposure with candidates indicated
194  di.pauseAtEnd = False # pause when done
195  return di
196  lsstDebug.Info = DebugInfo
197  lsstDebug.frame = 1
198 @endcode
199 
200 Note that if you want addional logging info, you may add to your scripts:
201 @code{.py}
202 import lsst.log.utils as logUtils
203 logUtils.traceSetAt("ip.diffim", 4)
204 @endcode
205 
206 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
207 
208 @section ip_diffim_snappsfmatch_Example A complete example of using SnapPsfMatchTask
209 
210 This code is snapPsfMatchTask.py in the examples directory, and can be run as @em e.g.
211 @code
212 examples/snapPsfMatchTask.py
213 examples/snapPsfMatchTask.py --debug
214 examples/snapPsfMatchTask.py --debug --template /path/to/templateExp.fits --science /path/to/scienceExp.fits
215 @endcode
216 
217 @dontinclude snapPsfMatchTask.py
218 First, create a subclass of SnapPsfMatchTask that accepts two exposures. Ideally these exposures would have
219 been taken back-to-back, such that the pointing/background/Psf does not vary substantially between the two:
220 @skip MySnapPsfMatchTask
221 @until return
222 
223 And allow the user the freedom to either run the script in default mode, or point to their own images on disk.
224 Note that these images must be readable as an lsst.afw.image.Exposure:
225 @skip main
226 @until parse_args
227 
228 We have enabled some minor display debugging in this script via the --debug option. However, if you
229 have an lsstDebug debug.py in your PYTHONPATH you will get additional debugging displays. The following
230 block checks for this script:
231 @skip args.debug
232 @until sys.stderr
233 
234 
235 @dontinclude snapPsfMatchTask.py
236 Finally, we call a run method that we define below. First set up a Config and choose the basis set to use:
237 @skip run(args)
238 @until AL
239 
240 Make sure the images (if any) that were sent to the script exist on disk and are readable. If no images
241 are sent, make some fake data up for the sake of this example script (have a look at the code if you want
242 more details on generateFakeImages; as a detail of how the fake images were made, you do have to fit for a
243 differential background):
244 @skip requested
245 @until sizeCellY
246 
247 Display the two images if --debug:
248 @skip args.debug
249 @until Science
250 
251 Create and run the Task:
252 @skip Create
253 @until result
254 
255 And finally provide optional debugging display of the Psf-matched (via the Psf models) science image:
256 @skip args.debug
257 @until result.subtractedExposure
258 
259 #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
260  """
261  ConfigClass = SnapPsfMatchConfig
262 
263  # Override ImagePsfMatchTask.subtractExposures to set doWarping on config.doWarping
264  def subtractExposures(self, templateExposure, scienceExposure,
265  templateFwhmPix=None, scienceFwhmPix=None,
266  candidateList=None):
267  return ImagePsfMatchTask.subtractExposures(self,
268  templateExposure=templateExposure,
269  scienceExposure=scienceExposure,
270  templateFwhmPix=templateFwhmPix,
271  scienceFwhmPix=scienceFwhmPix,
272  candidateList=candidateList,
273  doWarping=self.config.doWarping,
274  )
The parameters specific to the "Alard-Lupton" (sum-of-Gaussian) Psf-matching basis.
Definition: psfMatch.py:368
The parameters specific to the delta-function (one basis per-pixel) Psf-matching basis.
Definition: psfMatch.py:425
Configuration for image-to-image Psf matching.
Psf-match two MaskedImages or Exposures using the sources in the images.
def subtractExposures(self, templateExposure, scienceExposure, templateFwhmPix=None, scienceFwhmPix=None, candidateList=None)
Image-based Psf-matching of two subsequent snaps from the same visit.
Definition: snapPsfMatch.py:96