312 def __init__(self, refObjLoader=None, schema=None, **kwargs):
316 schema = SourceTable.makeMinimalSchema()
318 self.makeSubtask(
"background")
319 self.makeSubtask(
"installSimplePsf")
320 self.makeSubtask(
"repair")
321 self.makeSubtask(
"measurePsf", schema=self.
schema)
323 if self.config.doMeasurePsf
and self.measurePsf.usesMatches:
324 self.makeSubtask(
"ref_match", refObjLoader=refObjLoader)
326 self.makeSubtask(
'detection', schema=self.
schema)
327 if self.config.doDeblend:
328 self.makeSubtask(
"deblend", schema=self.
schema)
329 self.makeSubtask(
'measurement', schema=self.
schema, algMetadata=self.
algMetadata)
330 if self.config.doApCorr:
331 self.makeSubtask(
'measureApCorr', schema=self.
schema)
332 self.makeSubtask(
'applyApCorr', schema=self.
schema)
333 self.makeSubtask(
'catalogCalculation', schema=self.
schema)
336 self.
schema.checkUnits(parse_strict=self.config.checkUnitsParseStrict)
340 inputs = butlerQC.get(inputRefs)
341 if 'idGenerator' not in inputs.keys():
342 inputs[
'idGenerator'] = self.config.idGenerator.apply(butlerQC.quantum.dataId)
343 outputs = self.
run(**inputs)
344 butlerQC.put(outputs, outputRefs)
347 def run(self, exposure, exposureIdInfo=None, background=None, idGenerator=None):
348 """Characterize a science image.
350 Peforms the following operations:
351 - Iterate the following config.psfIterations times, or once if config.doMeasurePsf false:
352 - detect and measure sources and estimate PSF (see detectMeasureAndEstimatePsf for details)
353 - interpolate over cosmic rays
354 - perform final measurement
358 exposure : `lsst.afw.image.ExposureF`
359 Exposure to characterize.
360 exposureIdInfo : `lsst.obs.base.ExposureIdInfo`, optional
361 Exposure ID info. Deprecated in favor of ``idGenerator``, and
362 ignored if that is provided.
363 background : `lsst.afw.math.BackgroundList`, optional
364 Initial model of background already subtracted from exposure.
365 idGenerator : `lsst.meas.base.IdGenerator`, optional
366 Object that generates source IDs and provides RNG seeds.
370 result : `lsst.pipe.base.Struct`
371 Results as a struct with attributes:
374 Characterized exposure (`lsst.afw.image.ExposureF`).
376 Detected sources (`lsst.afw.table.SourceCatalog`).
378 Model of subtracted background (`lsst.afw.math.BackgroundList`).
380 Spatial cells of PSF candidates (`lsst.afw.math.SpatialCellSet`).
382 Another reference to ``exposure`` for compatibility.
384 Another reference to ``background`` for compatibility.
389 Raised if PSF sigma is NaN.
393 if not self.config.doMeasurePsf
and not exposure.hasPsf():
394 self.log.info(
"CharacterizeImageTask initialized with 'simple' PSF.")
395 self.installSimplePsf.run(exposure=exposure)
397 if idGenerator
is None:
398 if exposureIdInfo
is not None:
399 idGenerator = IdGenerator._from_exposure_id_info(exposureIdInfo)
401 idGenerator = IdGenerator()
406 background = self.background.run(exposure).background
408 psfIterations = self.config.psfIterations
if self.config.doMeasurePsf
else 1
409 for i
in range(psfIterations):
412 idGenerator=idGenerator,
413 background=background,
416 psf = dmeRes.exposure.getPsf()
418 psfAvgPos = psf.getAveragePosition()
419 psfSigma = psf.computeShape(psfAvgPos).getDeterminantRadius()
420 psfDimensions = psf.computeImage(psfAvgPos).getDimensions()
421 medBackground = np.median(dmeRes.background.getImage().getArray())
422 self.log.info(
"iter %s; PSF sigma=%0.2f, dimensions=%s; median background=%0.2f",
423 i + 1, psfSigma, psfDimensions, medBackground)
424 if np.isnan(psfSigma):
425 raise RuntimeError(
"PSF sigma is NaN, cannot continue PSF determination.")
427 self.
display(
"psf", exposure=dmeRes.exposure, sourceCat=dmeRes.sourceCat)
430 self.repair.run(exposure=dmeRes.exposure)
431 self.
display(
"repair", exposure=dmeRes.exposure, sourceCat=dmeRes.sourceCat)
435 self.measurement.run(measCat=dmeRes.sourceCat, exposure=dmeRes.exposure,
436 exposureId=idGenerator.catalog_id)
437 if self.config.doApCorr:
439 apCorrMap = self.measureApCorr.run(
440 exposure=dmeRes.exposure,
441 catalog=dmeRes.sourceCat,
443 except MeasureApCorrError:
447 dmeRes.exposure.info.setApCorrMap(
None)
449 dmeRes.exposure.info.setApCorrMap(apCorrMap)
450 self.applyApCorr.run(catalog=dmeRes.sourceCat, apCorrMap=exposure.getInfo().getApCorrMap())
452 self.catalogCalculation.run(dmeRes.sourceCat)
454 self.
display(
"measure", exposure=dmeRes.exposure, sourceCat=dmeRes.sourceCat)
456 return pipeBase.Struct(
457 exposure=dmeRes.exposure,
458 sourceCat=dmeRes.sourceCat,
459 background=dmeRes.background,
460 psfCellSet=dmeRes.psfCellSet,
462 characterized=dmeRes.exposure,
463 backgroundModel=dmeRes.background
468 """Perform one iteration of detect, measure, and estimate PSF.
470 Performs the following operations:
472 - if config.doMeasurePsf or not exposure.hasPsf():
474 - install a simple PSF model (replacing the existing one, if need be)
476 - interpolate over cosmic rays with keepCRs=True
477 - estimate background and subtract it from the exposure
478 - detect, deblend and measure sources, and subtract a refined background model;
479 - if config.doMeasurePsf:
484 exposure : `lsst.afw.image.ExposureF`
485 Exposure to characterize.
486 idGenerator : `lsst.meas.base.IdGenerator`
487 Object that generates source IDs and provides RNG seeds.
488 background : `lsst.afw.math.BackgroundList`, optional
489 Initial model of background already subtracted from exposure.
493 result : `lsst.pipe.base.Struct`
494 Results as a struct with attributes:
497 Characterized exposure (`lsst.afw.image.ExposureF`).
499 Detected sources (`lsst.afw.table.SourceCatalog`).
501 Model of subtracted background (`lsst.afw.math.BackgroundList`).
503 Spatial cells of PSF candidates (`lsst.afw.math.SpatialCellSet`).
508 Raised if there are too many CR pixels.
511 if not exposure.hasPsf()
or (self.config.doMeasurePsf
and self.config.useSimplePsf):
512 self.log.info(
"PSF estimation initialized with 'simple' PSF")
513 self.installSimplePsf.run(exposure=exposure)
516 if self.config.requireCrForPsf:
517 self.repair.run(exposure=exposure, keepCRs=
True)
520 self.repair.run(exposure=exposure, keepCRs=
True)
522 self.log.warning(
"Skipping cosmic ray detection: Too many CR pixels (max %0.f)",
523 self.config.repair.cosmicray.nCrPixelMax)
525 self.
display(
"repair_iter", exposure=exposure)
527 if background
is None:
528 background = BackgroundList()
530 sourceIdFactory = idGenerator.make_table_id_factory()
531 table = SourceTable.make(self.
schema, sourceIdFactory)
534 detRes = self.detection.run(table=table, exposure=exposure, doSmooth=
True)
535 sourceCat = detRes.sources
536 if detRes.background:
537 for bg
in detRes.background:
538 background.append(bg)
540 if self.config.doDeblend:
541 self.deblend.run(exposure=exposure, sources=sourceCat)
543 if not sourceCat.isContiguous():
544 sourceCat = sourceCat.copy(deep=
True)
546 self.measurement.run(measCat=sourceCat, exposure=exposure, exposureId=idGenerator.catalog_id)
548 measPsfRes = pipeBase.Struct(cellSet=
None)
549 if self.config.doMeasurePsf:
551 if self.measurePsf.usesMatches:
552 matches = self.ref_match.loadAndMatch(exposure=exposure, sourceCat=sourceCat).matches
555 measPsfRes = self.measurePsf.run(exposure=exposure, sources=sourceCat, matches=matches,
556 expId=idGenerator.catalog_id)
557 self.
display(
"measure_iter", exposure=exposure, sourceCat=sourceCat)
559 return pipeBase.Struct(
562 background=background,
563 psfCellSet=measPsfRes.cellSet,
566 def display(self, itemName, exposure, sourceCat=None):
567 """Display exposure and sources on next frame (for debugging).
572 Name of item in ``debugInfo``.
573 exposure : `lsst.afw.image.ExposureF`
575 sourceCat : `lsst.afw.table.SourceCatalog`, optional
576 Catalog of sources detected on the exposure.
578 val = getDebugFrame(self._display, itemName)
582 displayAstrometry(exposure=exposure, sourceCat=sourceCat, frame=self.
_frame, pause=
False)