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