lsst.pipe.tasks  21.0.0-107-gd7efe290+5c5fa69342
imageDifference.py
Go to the documentation of this file.
1 # This file is part of pipe_tasks.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 import math
23 import random
24 import numpy
25 
26 import lsst.utils
27 import lsst.pex.config as pexConfig
28 import lsst.pipe.base as pipeBase
29 import lsst.daf.base as dafBase
30 import lsst.geom as geom
31 import lsst.afw.math as afwMath
32 import lsst.afw.table as afwTable
33 from lsst.meas.astrom import AstrometryConfig, AstrometryTask
34 from lsst.meas.base import ForcedMeasurementTask, ApplyApCorrTask
35 from lsst.meas.algorithms import LoadIndexedReferenceObjectsTask
36 from lsst.pipe.tasks.registerImage import RegisterTask
37 from lsst.pipe.tasks.scaleVariance import ScaleVarianceTask
38 from lsst.meas.algorithms import SourceDetectionTask, SingleGaussianPsf, ObjectSizeStarSelectorTask
39 from lsst.ip.diffim import (DipoleAnalysis, SourceFlagChecker, KernelCandidateF, makeKernelBasisList,
40  KernelCandidateQa, DiaCatalogSourceSelectorTask, DiaCatalogSourceSelectorConfig,
41  GetCoaddAsTemplateTask, GetCalexpAsTemplateTask, DipoleFitTask,
42  DecorrelateALKernelSpatialTask, subtractAlgorithmRegistry)
43 import lsst.ip.diffim.diffimTools as diffimTools
44 import lsst.ip.diffim.utils as diUtils
45 import lsst.afw.display as afwDisplay
46 from lsst.skymap import BaseSkyMap
47 from lsst.obs.base import ExposureIdInfo
48 
49 __all__ = ["ImageDifferenceConfig", "ImageDifferenceTask"]
50 FwhmPerSigma = 2*math.sqrt(2*math.log(2))
51 IqrToSigma = 0.741
52 
53 
54 class ImageDifferenceTaskConnections(pipeBase.PipelineTaskConnections,
55  dimensions=("instrument", "visit", "detector", "skymap"),
56  defaultTemplates={"coaddName": "deep",
57  "skyMapName": "deep",
58  "warpTypeSuffix": "",
59  "fakesType": ""}):
60 
61  exposure = pipeBase.connectionTypes.Input(
62  doc="Input science exposure to subtract from.",
63  dimensions=("instrument", "visit", "detector"),
64  storageClass="ExposureF",
65  name="{fakesType}calexp"
66  )
67 
68  # TODO DM-22953
69  # kernelSources = pipeBase.connectionTypes.Input(
70  # doc="Source catalog produced in calibrate task for kernel candidate sources",
71  # name="src",
72  # storageClass="SourceCatalog",
73  # dimensions=("instrument", "visit", "detector"),
74  # )
75 
76  skyMap = pipeBase.connectionTypes.Input(
77  doc="Input definition of geometry/bbox and projection/wcs for template exposures",
78  name=BaseSkyMap.SKYMAP_DATASET_TYPE_NAME,
79  dimensions=("skymap", ),
80  storageClass="SkyMap",
81  )
82  coaddExposures = pipeBase.connectionTypes.Input(
83  doc="Input template to match and subtract from the exposure",
84  dimensions=("tract", "patch", "skymap", "band"),
85  storageClass="ExposureF",
86  name="{fakesType}{coaddName}Coadd{warpTypeSuffix}",
87  multiple=True,
88  deferLoad=True
89  )
90  dcrCoadds = pipeBase.connectionTypes.Input(
91  doc="Input DCR template to match and subtract from the exposure",
92  name="{fakesType}dcrCoadd{warpTypeSuffix}",
93  storageClass="ExposureF",
94  dimensions=("tract", "patch", "skymap", "band", "subfilter"),
95  multiple=True,
96  deferLoad=True
97  )
98  outputSchema = pipeBase.connectionTypes.InitOutput(
99  doc="Schema (as an example catalog) for output DIASource catalog.",
100  storageClass="SourceCatalog",
101  name="{fakesType}{coaddName}Diff_diaSrc_schema",
102  )
103  # TODO DM-29965: Currently the AL likelihood image is not a separate data product
104  subtractedExposure = pipeBase.connectionTypes.Output(
105  doc="Output AL difference or Zogy proper difference image",
106  dimensions=("instrument", "visit", "detector"),
107  storageClass="ExposureF",
108  name="{fakesType}{coaddName}Diff_differenceExp",
109  )
110  scoreExposure = pipeBase.connectionTypes.Output(
111  doc="Output Zogy score (likelihood) image",
112  dimensions=("instrument", "visit", "detector"),
113  storageClass="ExposureF",
114  name="{fakesType}{coaddName}Diff_scoreExp",
115  )
116  warpedExposure = pipeBase.connectionTypes.Output(
117  doc="Warped template used to create `subtractedExposure`.",
118  dimensions=("instrument", "visit", "detector"),
119  storageClass="ExposureF",
120  name="{fakesType}{coaddName}Diff_warpedExp",
121  )
122  matchedExposure = pipeBase.connectionTypes.Output(
123  doc="Warped template used to create `subtractedExposure`.",
124  dimensions=("instrument", "visit", "detector"),
125  storageClass="ExposureF",
126  name="{fakesType}{coaddName}Diff_matchedExp",
127  )
128  diaSources = pipeBase.connectionTypes.Output(
129  doc="Output detected diaSources on the difference image",
130  dimensions=("instrument", "visit", "detector"),
131  storageClass="SourceCatalog",
132  name="{fakesType}{coaddName}Diff_diaSrc",
133  )
134 
135  def __init__(self, *, config=None):
136  super().__init__(config=config)
137  if config.coaddName == 'dcr':
138  self.inputs.remove("coaddExposures")
139  else:
140  self.inputs.remove("dcrCoadds")
141  if not config.doWriteSubtractedExp:
142  self.outputs.remove("subtractedExposure")
143  if not config.doWriteScoreExp:
144  self.outputs.remove("scoreExposure")
145  if not config.doWriteWarpedExp:
146  self.outputs.remove("warpedExposure")
147  if not config.doWriteMatchedExp:
148  self.outputs.remove("matchedExposure")
149  if not config.doWriteSources:
150  self.outputs.remove("diaSources")
151 
152  # TODO DM-22953: Add support for refObjLoader (kernelSourcesFromRef)
153  # Make kernelSources optional
154 
155 
156 class ImageDifferenceConfig(pipeBase.PipelineTaskConfig,
157  pipelineConnections=ImageDifferenceTaskConnections):
158  """Config for ImageDifferenceTask
159  """
160  doAddCalexpBackground = pexConfig.Field(dtype=bool, default=False,
161  doc="Add background to calexp before processing it. "
162  "Useful as ipDiffim does background matching.")
163  doUseRegister = pexConfig.Field(dtype=bool, default=False,
164  doc="Re-compute astrometry on the template. "
165  "Use image-to-image registration to align template with "
166  "science image (AL only).")
167  doDebugRegister = pexConfig.Field(dtype=bool, default=False,
168  doc="Writing debugging data for doUseRegister")
169  doSelectSources = pexConfig.Field(dtype=bool, default=False,
170  doc="Select stars to use for kernel fitting (AL only)")
171  doSelectDcrCatalog = pexConfig.Field(dtype=bool, default=False,
172  doc="Select stars of extreme color as part "
173  "of the control sample (AL only)")
174  doSelectVariableCatalog = pexConfig.Field(dtype=bool, default=False,
175  doc="Select stars that are variable to be part "
176  "of the control sample (AL only)")
177  doSubtract = pexConfig.Field(dtype=bool, default=True, doc="Compute subtracted exposure?")
178  doPreConvolve = pexConfig.Field(dtype=bool, default=False,
179  doc="Convolve science image by its PSF before PSF-matching (AL only)."
180  " The difference image becomes a likelihood image.")
181  useScoreImageDetection = pexConfig.Field(
182  dtype=bool, default=False, doc="Calculate and detect sources on the Zogy score image (Zogy only).")
183  doWriteScoreExp = pexConfig.Field(
184  dtype=bool, default=False, doc="Write score exposure (Zogy only) ?")
185  doScaleTemplateVariance = pexConfig.Field(dtype=bool, default=False,
186  doc="Scale variance of the template before PSF matching")
187  doScaleDiffimVariance = pexConfig.Field(dtype=bool, default=True,
188  doc="Scale variance of the diffim before PSF matching. "
189  "You may do either this or template variance scaling, "
190  "or neither. (Doing both is a waste of CPU.)")
191  useGaussianForPreConvolution = pexConfig.Field(dtype=bool, default=True,
192  doc="Use a simple gaussian PSF model for pre-convolution "
193  "(else use fit PSF)? Ignored if doPreConvolve false.")
194  doDetection = pexConfig.Field(dtype=bool, default=True, doc="Detect sources?")
195  doDecorrelation = pexConfig.Field(dtype=bool, default=True,
196  doc="Perform diffim decorrelation to undo pixel correlation due to A&L "
197  "kernel convolution (AL only)? If True, also update the diffim PSF.")
198  doMerge = pexConfig.Field(dtype=bool, default=True,
199  doc="Merge positive and negative diaSources with grow radius "
200  "set by growFootprint")
201  doMatchSources = pexConfig.Field(dtype=bool, default=False,
202  doc="Match diaSources with input calexp sources and ref catalog sources")
203  doMeasurement = pexConfig.Field(dtype=bool, default=True, doc="Measure diaSources?")
204  doDipoleFitting = pexConfig.Field(dtype=bool, default=True, doc="Measure dipoles using new algorithm?")
205  doForcedMeasurement = pexConfig.Field(
206  dtype=bool,
207  default=True,
208  doc="Force photometer diaSource locations on PVI?")
209  doWriteSubtractedExp = pexConfig.Field(
210  dtype=bool, default=True, doc="Write difference exposure (AL and Zogy) ?")
211  doWriteWarpedExp = pexConfig.Field(
212  dtype=bool, default=False, doc="Write WCS, warped template coadd exposure?")
213  doWriteMatchedExp = pexConfig.Field(dtype=bool, default=False,
214  doc="Write warped and PSF-matched template coadd exposure?")
215  doWriteSources = pexConfig.Field(dtype=bool, default=True, doc="Write sources?")
216  doAddMetrics = pexConfig.Field(dtype=bool, default=False,
217  doc="Add columns to the source table to hold analysis metrics?")
218 
219  coaddName = pexConfig.Field(
220  doc="coadd name: typically one of deep, goodSeeing, or dcr",
221  dtype=str,
222  default="deep",
223  )
224  convolveTemplate = pexConfig.Field(
225  doc="Which image gets convolved (default = template)",
226  dtype=bool,
227  default=True
228  )
229  refObjLoader = pexConfig.ConfigurableField(
230  target=LoadIndexedReferenceObjectsTask,
231  doc="reference object loader",
232  )
233  astrometer = pexConfig.ConfigurableField(
234  target=AstrometryTask,
235  doc="astrometry task; used to match sources to reference objects, but not to fit a WCS",
236  )
237  sourceSelector = pexConfig.ConfigurableField(
238  target=ObjectSizeStarSelectorTask,
239  doc="Source selection algorithm",
240  )
241  subtract = subtractAlgorithmRegistry.makeField("Subtraction Algorithm", default="al")
242  decorrelate = pexConfig.ConfigurableField(
243  target=DecorrelateALKernelSpatialTask,
244  doc="Decorrelate effects of A&L kernel convolution on image difference, only if doSubtract is True. "
245  "If this option is enabled, then detection.thresholdValue should be set to 5.0 (rather than the "
246  "default of 5.5).",
247  )
248  # Old style ImageMapper grid. ZogyTask has its own grid option
249  doSpatiallyVarying = pexConfig.Field(
250  dtype=bool,
251  default=False,
252  doc="Perform A&L decorrelation on a grid across the "
253  "image in order to allow for spatial variations. Zogy does not use this option."
254  )
255  detection = pexConfig.ConfigurableField(
256  target=SourceDetectionTask,
257  doc="Low-threshold detection for final measurement",
258  )
259  measurement = pexConfig.ConfigurableField(
260  target=DipoleFitTask,
261  doc="Enable updated dipole fitting method",
262  )
263  doApCorr = lsst.pex.config.Field(
264  dtype=bool,
265  default=True,
266  doc="Run subtask to apply aperture corrections"
267  )
268  applyApCorr = lsst.pex.config.ConfigurableField(
269  target=ApplyApCorrTask,
270  doc="Subtask to apply aperture corrections"
271  )
272  forcedMeasurement = pexConfig.ConfigurableField(
273  target=ForcedMeasurementTask,
274  doc="Subtask to force photometer PVI at diaSource location.",
275  )
276  getTemplate = pexConfig.ConfigurableField(
277  target=GetCoaddAsTemplateTask,
278  doc="Subtask to retrieve template exposure and sources",
279  )
280  scaleVariance = pexConfig.ConfigurableField(
281  target=ScaleVarianceTask,
282  doc="Subtask to rescale the variance of the template "
283  "to the statistically expected level"
284  )
285  controlStepSize = pexConfig.Field(
286  doc="What step size (every Nth one) to select a control sample from the kernelSources",
287  dtype=int,
288  default=5
289  )
290  controlRandomSeed = pexConfig.Field(
291  doc="Random seed for shuffing the control sample",
292  dtype=int,
293  default=10
294  )
295  register = pexConfig.ConfigurableField(
296  target=RegisterTask,
297  doc="Task to enable image-to-image image registration (warping)",
298  )
299  kernelSourcesFromRef = pexConfig.Field(
300  doc="Select sources to measure kernel from reference catalog if True, template if false",
301  dtype=bool,
302  default=False
303  )
304  templateSipOrder = pexConfig.Field(
305  dtype=int, default=2,
306  doc="Sip Order for fitting the Template Wcs (default is too high, overfitting)"
307  )
308  growFootprint = pexConfig.Field(
309  dtype=int, default=2,
310  doc="Grow positive and negative footprints by this amount before merging"
311  )
312  diaSourceMatchRadius = pexConfig.Field(
313  dtype=float, default=0.5,
314  doc="Match radius (in arcseconds) for DiaSource to Source association"
315  )
316  requiredTemplateFraction = pexConfig.Field(
317  dtype=float, default=0.1,
318  doc="Do not attempt to run task if template covers less than this fraction of pixels."
319  "Setting to 0 will always attempt image subtraction"
320  )
321 
322  def setDefaults(self):
323  # defaults are OK for catalog and diacatalog
324 
325  self.subtract['al'].kernel.name = "AL"
326  self.subtract['al'].kernel.active.fitForBackground = True
327  self.subtract['al'].kernel.active.spatialKernelOrder = 1
328  self.subtract['al'].kernel.active.spatialBgOrder = 2
329 
330  # DiaSource Detection
331  self.detection.thresholdPolarity = "both"
332  self.detection.thresholdValue = 5.0
333  self.detection.reEstimateBackground = False
334  self.detection.thresholdType = "pixel_stdev"
335 
336  # Add filtered flux measurement, the correct measurement for pre-convolved images.
337  # Enable all measurements, regardless of doPreConvolve, as it makes data harvesting easier.
338  # To change that you must modify algorithms.names in the task's applyOverrides method,
339  # after the user has set doPreConvolve.
340  self.measurement.algorithms.names.add('base_PeakLikelihoodFlux')
341  self.measurement.plugins.names |= ['base_LocalPhotoCalib',
342  'base_LocalWcs']
343 
344  self.forcedMeasurement.plugins = ["base_TransformedCentroid", "base_PsfFlux"]
345  self.forcedMeasurement.copyColumns = {
346  "id": "objectId", "parent": "parentObjectId", "coord_ra": "coord_ra", "coord_dec": "coord_dec"}
347  self.forcedMeasurement.slots.centroid = "base_TransformedCentroid"
348  self.forcedMeasurement.slots.shape = None
349 
350  # For shuffling the control sample
351  random.seed(self.controlRandomSeed)
352 
353  def validate(self):
354  pexConfig.Config.validate(self)
355  if not self.doSubtract and not self.doDetection:
356  raise ValueError("Either doSubtract or doDetection must be enabled.")
357  if self.doMeasurement and not self.doDetection:
358  raise ValueError("Cannot run source measurement without source detection.")
359  if self.doMerge and not self.doDetection:
360  raise ValueError("Cannot run source merging without source detection.")
361  if self.doUseRegister and not self.doSelectSources:
362  raise ValueError("doUseRegister=True and doSelectSources=False. "
363  "Cannot run RegisterTask without selecting sources.")
364  if hasattr(self.getTemplate, "coaddName"):
365  if self.getTemplate.coaddName != self.coaddName:
366  raise ValueError("Mis-matched coaddName and getTemplate.coaddName in the config.")
367  if self.doScaleDiffimVariance and self.doScaleTemplateVariance:
368  raise ValueError("Scaling the diffim variance and scaling the template variance "
369  "are both set. Please choose one or the other.")
370  # We cannot allow inconsistencies that would lead to None or not available output products
371  if self.subtract.name == 'zogy':
372  if self.doWriteScoreExp and not self.useScoreImageDetection:
373  raise ValueError("doWriteScoreExp=True and useScoreImageDetection=False "
374  "is not supported. Score image is not calculated.")
375  if self.doWriteMatchedExp:
376  raise ValueError("doWriteMatchedExp=True Matched exposure is not "
377  "calculated in zogy subtraction.")
378  if self.doAddMetrics:
379  raise ValueError("doAddMetrics=True Kernel metrics does not exist in zogy subtraction.")
380  if self.doDecorrelation:
381  raise ValueError(
382  "doDecorrelation=True The decorrelation afterburner does not exist in zogy subtraction.")
383  if self.doPreConvolve:
384  raise ValueError(
385  "doPreConvolve=True Pre-convolution is not a zogy option.")
386  if self.doSelectSources:
387  raise ValueError(
388  "doSelectSources=True Selecting sources for PSF matching is not a zogy option.")
389  else:
390  if self.useScoreImageDetection:
391  raise ValueError("useScoreImageDetection=True Score exposure does not "
392  "exist in AL subtraction.")
393  if self.doWriteScoreExp: # Ensure output is not None
394  raise ValueError("doWriteScoreExp=True Score exposure does not exist in AL subtraction.")
395  if self.doAddMetrics and not self.doSubtract:
396  raise ValueError("Subtraction must be enabled for kernel metrics calculation.")
397  if self.doPreConvolve and self.doDecorrelation:
398  raise NotImplementedError(
399  "doPreConvolve=True and doDecorrelation=True "
400  "The decorrelation afterburner cannot handle pre-convolved exposures.")
401 
402 
403 class ImageDifferenceTaskRunner(pipeBase.ButlerInitializedTaskRunner):
404 
405  @staticmethod
406  def getTargetList(parsedCmd, **kwargs):
407  return pipeBase.TaskRunner.getTargetList(parsedCmd, templateIdList=parsedCmd.templateId.idList,
408  **kwargs)
409 
410 
411 class ImageDifferenceTask(pipeBase.CmdLineTask, pipeBase.PipelineTask):
412  """Subtract an image from a template and measure the result
413  """
414  ConfigClass = ImageDifferenceConfig
415  RunnerClass = ImageDifferenceTaskRunner
416  _DefaultName = "imageDifference"
417 
418  def __init__(self, butler=None, **kwargs):
419  """!Construct an ImageDifference Task
420 
421  @param[in] butler Butler object to use in constructing reference object loaders
422  """
423  super().__init__(**kwargs)
424  self.makeSubtask("getTemplate")
425 
426  self.makeSubtask("subtract")
427 
428  if self.config.subtract.name == 'al' and self.config.doDecorrelation:
429  self.makeSubtask("decorrelate")
430 
431  if self.config.doScaleTemplateVariance or self.config.doScaleDiffimVariance:
432  self.makeSubtask("scaleVariance")
433 
434  if self.config.doUseRegister:
435  self.makeSubtask("register")
436  self.schema = afwTable.SourceTable.makeMinimalSchema()
437 
438  if self.config.doSelectSources:
439  self.makeSubtask("sourceSelector")
440  if self.config.kernelSourcesFromRef:
441  self.makeSubtask('refObjLoader', butler=butler)
442  self.makeSubtask("astrometer", refObjLoader=self.refObjLoader)
443 
444  self.algMetadata = dafBase.PropertyList()
445  if self.config.doDetection:
446  self.makeSubtask("detection", schema=self.schema)
447  if self.config.doMeasurement:
448  self.makeSubtask("measurement", schema=self.schema,
449  algMetadata=self.algMetadata)
450  if self.config.doApCorr:
451  self.makeSubtask("applyApCorr", schema=self.measurement.schema)
452  if self.config.doForcedMeasurement:
453  self.schema.addField(
454  "ip_diffim_forced_PsfFlux_instFlux", "D",
455  "Forced PSF flux measured on the direct image.",
456  units="count")
457  self.schema.addField(
458  "ip_diffim_forced_PsfFlux_instFluxErr", "D",
459  "Forced PSF flux error measured on the direct image.",
460  units="count")
461  self.schema.addField(
462  "ip_diffim_forced_PsfFlux_area", "F",
463  "Forced PSF flux effective area of PSF.",
464  units="pixel")
465  self.schema.addField(
466  "ip_diffim_forced_PsfFlux_flag", "Flag",
467  "Forced PSF flux general failure flag.")
468  self.schema.addField(
469  "ip_diffim_forced_PsfFlux_flag_noGoodPixels", "Flag",
470  "Forced PSF flux not enough non-rejected pixels in data to attempt the fit.")
471  self.schema.addField(
472  "ip_diffim_forced_PsfFlux_flag_edge", "Flag",
473  "Forced PSF flux object was too close to the edge of the image to use the full PSF model.")
474  self.makeSubtask("forcedMeasurement", refSchema=self.schema)
475  if self.config.doMatchSources:
476  self.schema.addField("refMatchId", "L", "unique id of reference catalog match")
477  self.schema.addField("srcMatchId", "L", "unique id of source match")
478 
479  # initialize InitOutputs
480  self.outputSchema = afwTable.SourceCatalog(self.schema)
481  self.outputSchema.getTable().setMetadata(self.algMetadata)
482 
483  @staticmethod
484  def makeIdFactory(expId, expBits):
485  """Create IdFactory instance for unique 64 bit diaSource id-s.
486 
487  Parameters
488  ----------
489  expId : `int`
490  Exposure id.
491 
492  expBits: `int`
493  Number of used bits in ``expId``.
494 
495  Note
496  ----
497  The diasource id-s consists of the ``expId`` stored fixed in the highest value
498  ``expBits`` of the 64-bit integer plus (bitwise or) a generated sequence number in the
499  low value end of the integer.
500 
501  Returns
502  -------
503  idFactory: `lsst.afw.table.IdFactory`
504  """
505  return ExposureIdInfo(expId, expBits).makeSourceIdFactory()
506 
507  @lsst.utils.inheritDoc(pipeBase.PipelineTask)
508  def runQuantum(self, butlerQC: pipeBase.ButlerQuantumContext,
509  inputRefs: pipeBase.InputQuantizedConnection,
510  outputRefs: pipeBase.OutputQuantizedConnection):
511  inputs = butlerQC.get(inputRefs)
512  self.log.info(f"Processing {butlerQC.quantum.dataId}")
513  expId, expBits = butlerQC.quantum.dataId.pack("visit_detector",
514  returnMaxBits=True)
515  idFactory = self.makeIdFactory(expId=expId, expBits=expBits)
516  if self.config.coaddName == 'dcr':
517  templateExposures = inputRefs.dcrCoadds
518  else:
519  templateExposures = inputRefs.coaddExposures
520  templateStruct = self.getTemplate.runQuantum(
521  inputs['exposure'], butlerQC, inputRefs.skyMap, templateExposures
522  )
523 
524  if templateStruct.area/inputs['exposure'].getBBox().getArea() < self.config.requiredTemplateFraction:
525  message = ("Insufficient Template Coverage. (%.1f%% < %.1f%%) Not attempting subtraction. "
526  "To force subtraction, set config requiredTemplateFraction=0." % (
527  100*templateStruct.area/inputs['exposure'].getBBox().getArea(),
528  100*self.config.requiredTemplateFraction))
529  raise pipeBase.NoWorkFound(message)
530  else:
531  outputs = self.run(exposure=inputs['exposure'],
532  templateExposure=templateStruct.exposure,
533  idFactory=idFactory)
534  # Consistency with runDataref gen2 handling
535  if outputs.diaSources is None:
536  del outputs.diaSources
537  butlerQC.put(outputs, outputRefs)
538 
539  @pipeBase.timeMethod
540  def runDataRef(self, sensorRef, templateIdList=None):
541  """Subtract an image from a template coadd and measure the result.
542 
543  Data I/O wrapper around `run` using the butler in Gen2.
544 
545  Parameters
546  ----------
547  sensorRef : `lsst.daf.persistence.ButlerDataRef`
548  Sensor-level butler data reference, used for the following data products:
549 
550  Input only:
551  - calexp
552  - psf
553  - ccdExposureId
554  - ccdExposureId_bits
555  - self.config.coaddName + "Coadd_skyMap"
556  - self.config.coaddName + "Coadd"
557  Input or output, depending on config:
558  - self.config.coaddName + "Diff_subtractedExp"
559  Output, depending on config:
560  - self.config.coaddName + "Diff_matchedExp"
561  - self.config.coaddName + "Diff_src"
562 
563  Returns
564  -------
565  results : `lsst.pipe.base.Struct`
566  Returns the Struct by `run`.
567  """
568  subtractedExposureName = self.config.coaddName + "Diff_differenceExp"
569  subtractedExposure = None
570  selectSources = None
571  calexpBackgroundExposure = None
572  self.log.info("Processing %s" % (sensorRef.dataId))
573 
574  # We make one IdFactory that will be used by both icSrc and src datasets;
575  # I don't know if this is the way we ultimately want to do things, but at least
576  # this ensures the source IDs are fully unique.
577  idFactory = self.makeIdFactory(expId=int(sensorRef.get("ccdExposureId")),
578  expBits=sensorRef.get("ccdExposureId_bits"))
579  if self.config.doAddCalexpBackground:
580  calexpBackgroundExposure = sensorRef.get("calexpBackground")
581 
582  # Retrieve the science image we wish to analyze
583  exposure = sensorRef.get("calexp", immediate=True)
584 
585  # Retrieve the template image
586  template = self.getTemplate.runDataRef(exposure, sensorRef, templateIdList=templateIdList)
587 
588  if sensorRef.datasetExists("src"):
589  self.log.info("Source selection via src product")
590  # Sources already exist; for data release processing
591  selectSources = sensorRef.get("src")
592 
593  if not self.config.doSubtract and self.config.doDetection:
594  # If we don't do subtraction, we need the subtracted exposure from the repo
595  subtractedExposure = sensorRef.get(subtractedExposureName)
596  # Both doSubtract and doDetection cannot be False
597 
598  results = self.run(exposure=exposure,
599  selectSources=selectSources,
600  templateExposure=template.exposure,
601  templateSources=template.sources,
602  idFactory=idFactory,
603  calexpBackgroundExposure=calexpBackgroundExposure,
604  subtractedExposure=subtractedExposure)
605 
606  if self.config.doWriteSources and results.diaSources is not None:
607  sensorRef.put(results.diaSources, self.config.coaddName + "Diff_diaSrc")
608  if self.config.doWriteWarpedExp:
609  sensorRef.put(results.warpedExposure, self.config.coaddName + "Diff_warpedExp")
610  if self.config.doWriteMatchedExp:
611  sensorRef.put(results.matchedExposure, self.config.coaddName + "Diff_matchedExp")
612  if self.config.doAddMetrics and self.config.doSelectSources:
613  sensorRef.put(results.selectSources, self.config.coaddName + "Diff_kernelSrc")
614  if self.config.doWriteSubtractedExp:
615  sensorRef.put(results.subtractedExposure, subtractedExposureName)
616  if self.config.doWriteScoreExp:
617  sensorRef.put(results.scoreExposure, self.config.coaddName + "Diff_scoreExp")
618  return results
619 
620  @pipeBase.timeMethod
621  def run(self, exposure=None, selectSources=None, templateExposure=None, templateSources=None,
622  idFactory=None, calexpBackgroundExposure=None, subtractedExposure=None):
623  """PSF matches, subtract two images and perform detection on the difference image.
624 
625  Parameters
626  ----------
627  exposure : `lsst.afw.image.ExposureF`, optional
628  The science exposure, the minuend in the image subtraction.
629  Can be None only if ``config.doSubtract==False``.
630  selectSources : `lsst.afw.table.SourceCatalog`, optional
631  Identified sources on the science exposure. This catalog is used to
632  select sources in order to perform the AL PSF matching on stamp images
633  around them. The selection steps depend on config options and whether
634  ``templateSources`` and ``matchingSources`` specified.
635  templateExposure : `lsst.afw.image.ExposureF`, optional
636  The template to be subtracted from ``exposure`` in the image subtraction.
637  ``templateExposure`` is modified in place if ``config.doScaleTemplateVariance==True``.
638  The template exposure should cover the same sky area as the science exposure.
639  It is either a stich of patches of a coadd skymap image or a calexp
640  of the same pointing as the science exposure. Can be None only
641  if ``config.doSubtract==False`` and ``subtractedExposure`` is not None.
642  templateSources : `lsst.afw.table.SourceCatalog`, optional
643  Identified sources on the template exposure.
644  idFactory : `lsst.afw.table.IdFactory`
645  Generator object to assign ids to detected sources in the difference image.
646  calexpBackgroundExposure : `lsst.afw.image.ExposureF`, optional
647  Background exposure to be added back to the science exposure
648  if ``config.doAddCalexpBackground==True``
649  subtractedExposure : `lsst.afw.image.ExposureF`, optional
650  If ``config.doSubtract==False`` and ``config.doDetection==True``,
651  performs the post subtraction source detection only on this exposure.
652  Otherwise should be None.
653 
654  Returns
655  -------
656  results : `lsst.pipe.base.Struct`
657  ``subtractedExposure`` : `lsst.afw.image.ExposureF`
658  Difference image.
659  ``scoreExposure`` : `lsst.afw.image.ExposureF` or `None`
660  The zogy score exposure, if calculated.
661  ``matchedExposure`` : `lsst.afw.image.ExposureF`
662  The matched PSF exposure.
663  ``subtractRes`` : `lsst.pipe.base.Struct`
664  The returned result structure of the ImagePsfMatchTask subtask.
665  ``diaSources`` : `lsst.afw.table.SourceCatalog`
666  The catalog of detected sources.
667  ``selectSources`` : `lsst.afw.table.SourceCatalog`
668  The input source catalog with optionally added Qa information.
669 
670  Notes
671  -----
672  The following major steps are included:
673 
674  - warp template coadd to match WCS of image
675  - PSF match image to warped template
676  - subtract image from PSF-matched, warped template
677  - detect sources
678  - measure sources
679 
680  For details about the image subtraction configuration modes
681  see `lsst.ip.diffim`.
682  """
683  subtractRes = None
684  controlSources = None
685  scoreExposure = None
686  diaSources = None
687  kernelSources = None
688  # We'll clone exposure if modified but will still need the original
689  exposureOrig = exposure
690 
691  if self.config.doAddCalexpBackground:
692  mi = exposure.getMaskedImage()
693  mi += calexpBackgroundExposure.getImage()
694 
695  if not exposure.hasPsf():
696  raise pipeBase.TaskError("Exposure has no psf")
697  sciencePsf = exposure.getPsf()
698 
699  if self.config.doSubtract:
700  if self.config.doScaleTemplateVariance:
701  self.log.info("Rescaling template variance")
702  templateVarFactor = self.scaleVariance.run(
703  templateExposure.getMaskedImage())
704  self.log.info("Template variance scaling factor: %.2f" % templateVarFactor)
705  self.metadata.add("scaleTemplateVarianceFactor", templateVarFactor)
706 
707  if self.config.subtract.name == 'zogy':
708  subtractRes = self.subtract.run(exposure, templateExposure, doWarping=True)
709  scoreExposure = subtractRes.scoreExp
710  subtractedExposure = subtractRes.diffExp
711  subtractRes.subtractedExposure = subtractedExposure
712  subtractRes.matchedExposure = None
713 
714  elif self.config.subtract.name == 'al':
715  # compute scienceSigmaOrig: sigma of PSF of science image before pre-convolution
716  scienceSigmaOrig = sciencePsf.computeShape().getDeterminantRadius()
717  templateSigma = templateExposure.getPsf().computeShape().getDeterminantRadius()
718 
719  # if requested, convolve the science exposure with its PSF
720  # (properly, this should be a cross-correlation, but our code does not yet support that)
721  # compute scienceSigmaPost: sigma of science exposure with pre-convolution, if done,
722  # else sigma of original science exposure
723  # TODO: DM-22762 This functional block should be moved into its own method
724  preConvPsf = None
725  if self.config.doPreConvolve:
726  convControl = afwMath.ConvolutionControl()
727  # cannot convolve in place, so need a new image anyway
728  srcMI = exposure.maskedImage
729  exposure = exposure.clone() # New deep copy
730  srcPsf = sciencePsf
731  if self.config.useGaussianForPreConvolution:
732  # convolve with a simplified PSF model: a double Gaussian
733  kWidth, kHeight = sciencePsf.getLocalKernel().getDimensions()
734  preConvPsf = SingleGaussianPsf(kWidth, kHeight, scienceSigmaOrig)
735  else:
736  # convolve with science exposure's PSF model
737  preConvPsf = srcPsf
738  afwMath.convolve(exposure.maskedImage, srcMI, preConvPsf.getLocalKernel(), convControl)
739  scienceSigmaPost = scienceSigmaOrig*math.sqrt(2)
740  else:
741  scienceSigmaPost = scienceSigmaOrig
742 
743  # If requested, find and select sources from the image
744  # else, AL subtraction will do its own source detection
745  # TODO: DM-22762 This functional block should be moved into its own method
746  if self.config.doSelectSources:
747  if selectSources is None:
748  self.log.warn("Src product does not exist; running detection, measurement, selection")
749  # Run own detection and measurement; necessary in nightly processing
750  selectSources = self.subtract.getSelectSources(
751  exposure,
752  sigma=scienceSigmaPost,
753  doSmooth=not self.config.doPreConvolve,
754  idFactory=idFactory,
755  )
756 
757  if self.config.doAddMetrics:
758  # Number of basis functions
759 
760  nparam = len(makeKernelBasisList(self.subtract.config.kernel.active,
761  referenceFwhmPix=scienceSigmaPost*FwhmPerSigma,
762  targetFwhmPix=templateSigma*FwhmPerSigma))
763  # Modify the schema of all Sources
764  # DEPRECATED: This is a data dependent (nparam) output product schema
765  # outside the task constructor.
766  # NOTE: The pre-determination of nparam at this point
767  # may be incorrect as the template psf is warped later in
768  # ImagePsfMatchTask.matchExposures()
769  kcQa = KernelCandidateQa(nparam)
770  selectSources = kcQa.addToSchema(selectSources)
771  if self.config.kernelSourcesFromRef:
772  # match exposure sources to reference catalog
773  astromRet = self.astrometer.loadAndMatch(exposure=exposure, sourceCat=selectSources)
774  matches = astromRet.matches
775  elif templateSources:
776  # match exposure sources to template sources
777  mc = afwTable.MatchControl()
778  mc.findOnlyClosest = False
779  matches = afwTable.matchRaDec(templateSources, selectSources, 1.0*geom.arcseconds,
780  mc)
781  else:
782  raise RuntimeError("doSelectSources=True and kernelSourcesFromRef=False,"
783  "but template sources not available. Cannot match science "
784  "sources with template sources. Run process* on data from "
785  "which templates are built.")
786 
787  kernelSources = self.sourceSelector.run(selectSources, exposure=exposure,
788  matches=matches).sourceCat
789  random.shuffle(kernelSources, random.random)
790  controlSources = kernelSources[::self.config.controlStepSize]
791  kernelSources = [k for i, k in enumerate(kernelSources)
792  if i % self.config.controlStepSize]
793 
794  if self.config.doSelectDcrCatalog:
795  redSelector = DiaCatalogSourceSelectorTask(
796  DiaCatalogSourceSelectorConfig(grMin=self.sourceSelector.config.grMax,
797  grMax=99.999))
798  redSources = redSelector.selectStars(exposure, selectSources, matches=matches).starCat
799  controlSources.extend(redSources)
800 
801  blueSelector = DiaCatalogSourceSelectorTask(
802  DiaCatalogSourceSelectorConfig(grMin=-99.999,
803  grMax=self.sourceSelector.config.grMin))
804  blueSources = blueSelector.selectStars(exposure, selectSources,
805  matches=matches).starCat
806  controlSources.extend(blueSources)
807 
808  if self.config.doSelectVariableCatalog:
809  varSelector = DiaCatalogSourceSelectorTask(
810  DiaCatalogSourceSelectorConfig(includeVariable=True))
811  varSources = varSelector.selectStars(exposure, selectSources, matches=matches).starCat
812  controlSources.extend(varSources)
813 
814  self.log.info("Selected %d / %d sources for Psf matching (%d for control sample)"
815  % (len(kernelSources), len(selectSources), len(controlSources)))
816 
817  allresids = {}
818  # TODO: DM-22762 This functional block should be moved into its own method
819  if self.config.doUseRegister:
820  self.log.info("Registering images")
821 
822  if templateSources is None:
823  # Run detection on the template, which is
824  # temporarily background-subtracted
825  # sigma of PSF of template image before warping
826  templateSigma = templateExposure.getPsf().computeShape().getDeterminantRadius()
827  templateSources = self.subtract.getSelectSources(
828  templateExposure,
829  sigma=templateSigma,
830  doSmooth=True,
831  idFactory=idFactory
832  )
833 
834  # Third step: we need to fit the relative astrometry.
835  #
836  wcsResults = self.fitAstrometry(templateSources, templateExposure, selectSources)
837  warpedExp = self.register.warpExposure(templateExposure, wcsResults.wcs,
838  exposure.getWcs(), exposure.getBBox())
839  templateExposure = warpedExp
840 
841  # Create debugging outputs on the astrometric
842  # residuals as a function of position. Persistence
843  # not yet implemented; expected on (I believe) #2636.
844  if self.config.doDebugRegister:
845  # Grab matches to reference catalog
846  srcToMatch = {x.second.getId(): x.first for x in matches}
847 
848  refCoordKey = wcsResults.matches[0].first.getTable().getCoordKey()
849  inCentroidKey = wcsResults.matches[0].second.getTable().getCentroidSlot().getMeasKey()
850  sids = [m.first.getId() for m in wcsResults.matches]
851  positions = [m.first.get(refCoordKey) for m in wcsResults.matches]
852  residuals = [m.first.get(refCoordKey).getOffsetFrom(wcsResults.wcs.pixelToSky(
853  m.second.get(inCentroidKey))) for m in wcsResults.matches]
854  allresids = dict(zip(sids, zip(positions, residuals)))
855 
856  cresiduals = [m.first.get(refCoordKey).getTangentPlaneOffset(
857  wcsResults.wcs.pixelToSky(
858  m.second.get(inCentroidKey))) for m in wcsResults.matches]
859  colors = numpy.array([-2.5*numpy.log10(srcToMatch[x].get("g"))
860  + 2.5*numpy.log10(srcToMatch[x].get("r"))
861  for x in sids if x in srcToMatch.keys()])
862  dlong = numpy.array([r[0].asArcseconds() for s, r in zip(sids, cresiduals)
863  if s in srcToMatch.keys()])
864  dlat = numpy.array([r[1].asArcseconds() for s, r in zip(sids, cresiduals)
865  if s in srcToMatch.keys()])
866  idx1 = numpy.where(colors < self.sourceSelector.config.grMin)
867  idx2 = numpy.where((colors >= self.sourceSelector.config.grMin)
868  & (colors <= self.sourceSelector.config.grMax))
869  idx3 = numpy.where(colors > self.sourceSelector.config.grMax)
870  rms1Long = IqrToSigma*(
871  (numpy.percentile(dlong[idx1], 75) - numpy.percentile(dlong[idx1], 25)))
872  rms1Lat = IqrToSigma*(numpy.percentile(dlat[idx1], 75)
873  - numpy.percentile(dlat[idx1], 25))
874  rms2Long = IqrToSigma*(
875  (numpy.percentile(dlong[idx2], 75) - numpy.percentile(dlong[idx2], 25)))
876  rms2Lat = IqrToSigma*(numpy.percentile(dlat[idx2], 75)
877  - numpy.percentile(dlat[idx2], 25))
878  rms3Long = IqrToSigma*(
879  (numpy.percentile(dlong[idx3], 75) - numpy.percentile(dlong[idx3], 25)))
880  rms3Lat = IqrToSigma*(numpy.percentile(dlat[idx3], 75)
881  - numpy.percentile(dlat[idx3], 25))
882  self.log.info("Blue star offsets'': %.3f %.3f, %.3f %.3f" %
883  (numpy.median(dlong[idx1]), rms1Long,
884  numpy.median(dlat[idx1]), rms1Lat))
885  self.log.info("Green star offsets'': %.3f %.3f, %.3f %.3f" %
886  (numpy.median(dlong[idx2]), rms2Long,
887  numpy.median(dlat[idx2]), rms2Lat))
888  self.log.info("Red star offsets'': %.3f %.3f, %.3f %.3f" %
889  (numpy.median(dlong[idx3]), rms3Long,
890  numpy.median(dlat[idx3]), rms3Lat))
891 
892  self.metadata.add("RegisterBlueLongOffsetMedian", numpy.median(dlong[idx1]))
893  self.metadata.add("RegisterGreenLongOffsetMedian", numpy.median(dlong[idx2]))
894  self.metadata.add("RegisterRedLongOffsetMedian", numpy.median(dlong[idx3]))
895  self.metadata.add("RegisterBlueLongOffsetStd", rms1Long)
896  self.metadata.add("RegisterGreenLongOffsetStd", rms2Long)
897  self.metadata.add("RegisterRedLongOffsetStd", rms3Long)
898 
899  self.metadata.add("RegisterBlueLatOffsetMedian", numpy.median(dlat[idx1]))
900  self.metadata.add("RegisterGreenLatOffsetMedian", numpy.median(dlat[idx2]))
901  self.metadata.add("RegisterRedLatOffsetMedian", numpy.median(dlat[idx3]))
902  self.metadata.add("RegisterBlueLatOffsetStd", rms1Lat)
903  self.metadata.add("RegisterGreenLatOffsetStd", rms2Lat)
904  self.metadata.add("RegisterRedLatOffsetStd", rms3Lat)
905 
906  # warp template exposure to match exposure,
907  # PSF match template exposure to exposure,
908  # then return the difference
909 
910  # Return warped template... Construct sourceKernelCand list after subtract
911  self.log.info("Subtracting images")
912  subtractRes = self.subtract.subtractExposures(
913  templateExposure=templateExposure,
914  scienceExposure=exposure,
915  candidateList=kernelSources,
916  convolveTemplate=self.config.convolveTemplate,
917  doWarping=not self.config.doUseRegister
918  )
919  subtractedExposure = subtractRes.subtractedExposure
920 
921  if self.config.doDetection:
922  self.log.info("Computing diffim PSF")
923 
924  # Get Psf from the appropriate input image if it doesn't exist
925  if not subtractedExposure.hasPsf():
926  if self.config.convolveTemplate:
927  subtractedExposure.setPsf(exposure.getPsf())
928  else:
929  subtractedExposure.setPsf(templateExposure.getPsf())
930 
931  # If doSubtract is False, then subtractedExposure was fetched from disk (above),
932  # thus it may have already been decorrelated. Thus, we do not decorrelate if
933  # doSubtract is False.
934 
935  # NOTE: At this point doSubtract == True
936  if self.config.doDecorrelation and self.config.doSubtract:
937  preConvKernel = None
938  if preConvPsf is not None:
939  preConvKernel = preConvPsf.getLocalKernel()
940  decorrResult = self.decorrelate.run(exposureOrig, subtractRes.warpedExposure,
941  subtractedExposure,
942  subtractRes.psfMatchingKernel,
943  spatiallyVarying=self.config.doSpatiallyVarying,
944  preConvKernel=preConvKernel,
945  templateMatched=self.config.convolveTemplate)
946  subtractedExposure = decorrResult.correctedExposure
947 
948  # END (if subtractAlgorithm == 'AL')
949  # END (if self.config.doSubtract)
950  if self.config.doDetection:
951  self.log.info("Running diaSource detection")
952 
953  # subtractedExposure - reserved for task return value
954  # in zogy, it is always the proper difference image
955  # in AL, it may be (yet) pre-convolved and/or decorrelated
956  #
957  # detectionExposure - controls which exposure to use for detection
958  # in-place modifications will appear in task return
959  if self.config.useScoreImageDetection:
960  # zogy with score image detection enabled
961  self.log.info("Detection, diffim rescaling and measurements are on Zogy score image.")
962  detectionExposure = scoreExposure
963  detectOnLikelihood = True
964  else:
965  # AL or zogy with no score image detection
966  detectionExposure = subtractedExposure
967  detectOnLikelihood = False
968  if self.config.doPreConvolve:
969  # In AL, the likelihood image is not a separate product, yet
970  self.log.info("Detection, diffim rescaling and measurements are on AL pre-convolved "
971  "difference (likelihood) image.")
972  detectOnLikelihood = True
973  else:
974  self.log.info("Detection, diffim rescaling and measurements are on "
975  "(proper) difference image.")
976 
977  # Rescale difference image variance plane
978  if self.config.doScaleDiffimVariance:
979  self.log.info("Rescaling diffim variance")
980  diffimVarFactor = self.scaleVariance.run(detectionExposure.getMaskedImage())
981  self.log.info("Diffim variance scaling factor: %.2f" % diffimVarFactor)
982  self.metadata.add("scaleDiffimVarianceFactor", diffimVarFactor)
983 
984  # Erase existing detection mask planes
985  mask = detectionExposure.getMaskedImage().getMask()
986  mask &= ~(mask.getPlaneBitMask("DETECTED") | mask.getPlaneBitMask("DETECTED_NEGATIVE"))
987 
988  table = afwTable.SourceTable.make(self.schema, idFactory)
989  table.setMetadata(self.algMetadata)
990  results = self.detection.run(
991  table=table,
992  exposure=detectionExposure,
993  doSmooth=not detectOnLikelihood
994  )
995 
996  if self.config.doMerge:
997  fpSet = results.fpSets.positive
998  fpSet.merge(results.fpSets.negative, self.config.growFootprint,
999  self.config.growFootprint, False)
1000  diaSources = afwTable.SourceCatalog(table)
1001  fpSet.makeSources(diaSources)
1002  self.log.info("Merging detections into %d sources" % (len(diaSources)))
1003  else:
1004  diaSources = results.sources
1005 
1006  if self.config.doMeasurement:
1007  newDipoleFitting = self.config.doDipoleFitting
1008  self.log.info("Running diaSource measurement: newDipoleFitting=%r", newDipoleFitting)
1009  if not newDipoleFitting:
1010  # Just fit dipole in diffim
1011  self.measurement.run(diaSources, detectionExposure)
1012  else:
1013  # Use (matched) template and science image (if avail.) to constrain dipole fitting
1014  if self.config.doSubtract and 'matchedExposure' in subtractRes.getDict():
1015  self.measurement.run(diaSources, detectionExposure, exposure,
1016  subtractRes.matchedExposure)
1017  else:
1018  self.measurement.run(diaSources, detectionExposure, exposure)
1019  if self.config.doApCorr:
1020  self.applyApCorr.run(
1021  catalog=diaSources,
1022  apCorrMap=detectionExposure.getInfo().getApCorrMap()
1023  )
1024 
1025  if self.config.doForcedMeasurement:
1026  # Run forced psf photometry on the PVI at the diaSource locations.
1027  # Copy the measured flux and error into the diaSource.
1028  forcedSources = self.forcedMeasurement.generateMeasCat(
1029  exposure, diaSources, detectionExposure.getWcs())
1030  self.forcedMeasurement.run(forcedSources, exposure, diaSources, detectionExposure.getWcs())
1031  mapper = afwTable.SchemaMapper(forcedSources.schema, diaSources.schema)
1032  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_instFlux")[0],
1033  "ip_diffim_forced_PsfFlux_instFlux", True)
1034  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_instFluxErr")[0],
1035  "ip_diffim_forced_PsfFlux_instFluxErr", True)
1036  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_area")[0],
1037  "ip_diffim_forced_PsfFlux_area", True)
1038  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_flag")[0],
1039  "ip_diffim_forced_PsfFlux_flag", True)
1040  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_flag_noGoodPixels")[0],
1041  "ip_diffim_forced_PsfFlux_flag_noGoodPixels", True)
1042  mapper.addMapping(forcedSources.schema.find("base_PsfFlux_flag_edge")[0],
1043  "ip_diffim_forced_PsfFlux_flag_edge", True)
1044  for diaSource, forcedSource in zip(diaSources, forcedSources):
1045  diaSource.assign(forcedSource, mapper)
1046 
1047  # Match with the calexp sources if possible
1048  if self.config.doMatchSources:
1049  if selectSources is not None:
1050  # Create key,val pair where key=diaSourceId and val=sourceId
1051  matchRadAsec = self.config.diaSourceMatchRadius
1052  matchRadPixel = matchRadAsec/exposure.getWcs().getPixelScale().asArcseconds()
1053 
1054  srcMatches = afwTable.matchXy(selectSources, diaSources, matchRadPixel)
1055  srcMatchDict = dict([(srcMatch.second.getId(), srcMatch.first.getId()) for
1056  srcMatch in srcMatches])
1057  self.log.info("Matched %d / %d diaSources to sources" % (len(srcMatchDict),
1058  len(diaSources)))
1059  else:
1060  self.log.warn("Src product does not exist; cannot match with diaSources")
1061  srcMatchDict = {}
1062 
1063  # Create key,val pair where key=diaSourceId and val=refId
1064  refAstromConfig = AstrometryConfig()
1065  refAstromConfig.matcher.maxMatchDistArcSec = matchRadAsec
1066  refAstrometer = AstrometryTask(refAstromConfig)
1067  astromRet = refAstrometer.run(exposure=exposure, sourceCat=diaSources)
1068  refMatches = astromRet.matches
1069  if refMatches is None:
1070  self.log.warn("No diaSource matches with reference catalog")
1071  refMatchDict = {}
1072  else:
1073  self.log.info("Matched %d / %d diaSources to reference catalog" % (len(refMatches),
1074  len(diaSources)))
1075  refMatchDict = dict([(refMatch.second.getId(), refMatch.first.getId()) for
1076  refMatch in refMatches])
1077 
1078  # Assign source Ids
1079  for diaSource in diaSources:
1080  sid = diaSource.getId()
1081  if sid in srcMatchDict:
1082  diaSource.set("srcMatchId", srcMatchDict[sid])
1083  if sid in refMatchDict:
1084  diaSource.set("refMatchId", refMatchDict[sid])
1085 
1086  if self.config.doAddMetrics and self.config.doSelectSources:
1087  self.log.info("Evaluating metrics and control sample")
1088 
1089  kernelCandList = []
1090  for cell in subtractRes.kernelCellSet.getCellList():
1091  for cand in cell.begin(False): # include bad candidates
1092  kernelCandList.append(cand)
1093 
1094  # Get basis list to build control sample kernels
1095  basisList = kernelCandList[0].getKernel(KernelCandidateF.ORIG).getKernelList()
1096  nparam = len(kernelCandList[0].getKernel(KernelCandidateF.ORIG).getKernelParameters())
1097 
1098  controlCandList = (
1099  diffimTools.sourceTableToCandidateList(controlSources,
1100  subtractRes.warpedExposure, exposure,
1101  self.config.subtract.kernel.active,
1102  self.config.subtract.kernel.active.detectionConfig,
1103  self.log, doBuild=True, basisList=basisList))
1104 
1105  KernelCandidateQa.apply(kernelCandList, subtractRes.psfMatchingKernel,
1106  subtractRes.backgroundModel, dof=nparam)
1107  KernelCandidateQa.apply(controlCandList, subtractRes.psfMatchingKernel,
1108  subtractRes.backgroundModel)
1109 
1110  if self.config.doDetection:
1111  KernelCandidateQa.aggregate(selectSources, self.metadata, allresids, diaSources)
1112  else:
1113  KernelCandidateQa.aggregate(selectSources, self.metadata, allresids)
1114 
1115  self.runDebug(exposure, subtractRes, selectSources, kernelSources, diaSources)
1116  return pipeBase.Struct(
1117  subtractedExposure=subtractedExposure,
1118  scoreExposure=scoreExposure,
1119  warpedExposure=subtractRes.warpedExposure,
1120  matchedExposure=subtractRes.matchedExposure,
1121  subtractRes=subtractRes,
1122  diaSources=diaSources,
1123  selectSources=selectSources
1124  )
1125 
1126  def fitAstrometry(self, templateSources, templateExposure, selectSources):
1127  """Fit the relative astrometry between templateSources and selectSources
1128 
1129  Todo
1130  ----
1131 
1132  Remove this method. It originally fit a new WCS to the template before calling register.run
1133  because our TAN-SIP fitter behaved badly for points far from CRPIX, but that's been fixed.
1134  It remains because a subtask overrides it.
1135  """
1136  results = self.register.run(templateSources, templateExposure.getWcs(),
1137  templateExposure.getBBox(), selectSources)
1138  return results
1139 
1140  def runDebug(self, exposure, subtractRes, selectSources, kernelSources, diaSources):
1141  """Make debug plots and displays.
1142 
1143  Todo
1144  ----
1145  Test and update for current debug display and slot names
1146  """
1147  import lsstDebug
1148  display = lsstDebug.Info(__name__).display
1149  showSubtracted = lsstDebug.Info(__name__).showSubtracted
1150  showPixelResiduals = lsstDebug.Info(__name__).showPixelResiduals
1151  showDiaSources = lsstDebug.Info(__name__).showDiaSources
1152  showDipoles = lsstDebug.Info(__name__).showDipoles
1153  maskTransparency = lsstDebug.Info(__name__).maskTransparency
1154  if display:
1155  disp = afwDisplay.getDisplay(frame=lsstDebug.frame)
1156  if not maskTransparency:
1157  maskTransparency = 0
1158  disp.setMaskTransparency(maskTransparency)
1159 
1160  if display and showSubtracted:
1161  disp.mtv(subtractRes.subtractedExposure, title="Subtracted image")
1162  mi = subtractRes.subtractedExposure.getMaskedImage()
1163  x0, y0 = mi.getX0(), mi.getY0()
1164  with disp.Buffering():
1165  for s in diaSources:
1166  x, y = s.getX() - x0, s.getY() - y0
1167  ctype = "red" if s.get("flags_negative") else "yellow"
1168  if (s.get("base_PixelFlags_flag_interpolatedCenter")
1169  or s.get("base_PixelFlags_flag_saturatedCenter")
1170  or s.get("base_PixelFlags_flag_crCenter")):
1171  ptype = "x"
1172  elif (s.get("base_PixelFlags_flag_interpolated")
1173  or s.get("base_PixelFlags_flag_saturated")
1174  or s.get("base_PixelFlags_flag_cr")):
1175  ptype = "+"
1176  else:
1177  ptype = "o"
1178  disp.dot(ptype, x, y, size=4, ctype=ctype)
1179  lsstDebug.frame += 1
1180 
1181  if display and showPixelResiduals and selectSources:
1182  nonKernelSources = []
1183  for source in selectSources:
1184  if source not in kernelSources:
1185  nonKernelSources.append(source)
1186 
1187  diUtils.plotPixelResiduals(exposure,
1188  subtractRes.warpedExposure,
1189  subtractRes.subtractedExposure,
1190  subtractRes.kernelCellSet,
1191  subtractRes.psfMatchingKernel,
1192  subtractRes.backgroundModel,
1193  nonKernelSources,
1194  self.subtract.config.kernel.active.detectionConfig,
1195  origVariance=False)
1196  diUtils.plotPixelResiduals(exposure,
1197  subtractRes.warpedExposure,
1198  subtractRes.subtractedExposure,
1199  subtractRes.kernelCellSet,
1200  subtractRes.psfMatchingKernel,
1201  subtractRes.backgroundModel,
1202  nonKernelSources,
1203  self.subtract.config.kernel.active.detectionConfig,
1204  origVariance=True)
1205  if display and showDiaSources:
1206  flagChecker = SourceFlagChecker(diaSources)
1207  isFlagged = [flagChecker(x) for x in diaSources]
1208  isDipole = [x.get("ip_diffim_ClassificationDipole_value") for x in diaSources]
1209  diUtils.showDiaSources(diaSources, subtractRes.subtractedExposure, isFlagged, isDipole,
1210  frame=lsstDebug.frame)
1211  lsstDebug.frame += 1
1212 
1213  if display and showDipoles:
1214  DipoleAnalysis().displayDipoles(subtractRes.subtractedExposure, diaSources,
1215  frame=lsstDebug.frame)
1216  lsstDebug.frame += 1
1217 
1218  def _getConfigName(self):
1219  """Return the name of the config dataset
1220  """
1221  return "%sDiff_config" % (self.config.coaddName,)
1222 
1223  def _getMetadataName(self):
1224  """Return the name of the metadata dataset
1225  """
1226  return "%sDiff_metadata" % (self.config.coaddName,)
1227 
1228  def getSchemaCatalogs(self):
1229  """Return a dict of empty catalogs for each catalog dataset produced by this task."""
1230  return {self.config.coaddName + "Diff_diaSrc": self.outputSchema}
1231 
1232  @classmethod
1233  def _makeArgumentParser(cls):
1234  """Create an argument parser
1235  """
1236  parser = pipeBase.ArgumentParser(name=cls._DefaultName)
1237  parser.add_id_argument("--id", "calexp", help="data ID, e.g. --id visit=12345 ccd=1,2")
1238  parser.add_id_argument("--templateId", "calexp", doMakeDataRefList=True,
1239  help="Template data ID in case of calexp template,"
1240  " e.g. --templateId visit=6789")
1241  return parser
1242 
1243 
1244 class Winter2013ImageDifferenceConfig(ImageDifferenceConfig):
1245  winter2013WcsShift = pexConfig.Field(dtype=float, default=0.0,
1246  doc="Shift stars going into RegisterTask by this amount")
1247  winter2013WcsRms = pexConfig.Field(dtype=float, default=0.0,
1248  doc="Perturb stars going into RegisterTask by this amount")
1249 
1250  def setDefaults(self):
1251  ImageDifferenceConfig.setDefaults(self)
1252  self.getTemplate.retarget(GetCalexpAsTemplateTask)
1253 
1254 
1255 class Winter2013ImageDifferenceTask(ImageDifferenceTask):
1256  """!Image difference Task used in the Winter 2013 data challege.
1257  Enables testing the effects of registration shifts and scatter.
1258 
1259  For use with winter 2013 simulated images:
1260  Use --templateId visit=88868666 for sparse data
1261  --templateId visit=22222200 for dense data (g)
1262  --templateId visit=11111100 for dense data (i)
1263  """
1264  ConfigClass = Winter2013ImageDifferenceConfig
1265  _DefaultName = "winter2013ImageDifference"
1266 
1267  def __init__(self, **kwargs):
1268  ImageDifferenceTask.__init__(self, **kwargs)
1269 
1270  def fitAstrometry(self, templateSources, templateExposure, selectSources):
1271  """Fit the relative astrometry between templateSources and selectSources"""
1272  if self.config.winter2013WcsShift > 0.0:
1273  offset = geom.Extent2D(self.config.winter2013WcsShift,
1274  self.config.winter2013WcsShift)
1275  cKey = templateSources[0].getTable().getCentroidSlot().getMeasKey()
1276  for source in templateSources:
1277  centroid = source.get(cKey)
1278  source.set(cKey, centroid + offset)
1279  elif self.config.winter2013WcsRms > 0.0:
1280  cKey = templateSources[0].getTable().getCentroidSlot().getMeasKey()
1281  for source in templateSources:
1282  offset = geom.Extent2D(self.config.winter2013WcsRms*numpy.random.normal(),
1283  self.config.winter2013WcsRms*numpy.random.normal())
1284  centroid = source.get(cKey)
1285  source.set(cKey, centroid + offset)
1286 
1287  results = self.register.run(templateSources, templateExposure.getWcs(),
1288  templateExposure.getBBox(), selectSources)
1289  return results
def run(self, skyInfo, tempExpRefList, imageScalerList, weightList, altMaskList=None, mask=None, supplementaryData=None)