-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdetectAndMeasure.py
More file actions
1358 lines (1226 loc) · 61.3 KB
/
detectAndMeasure.py
File metadata and controls
1358 lines (1226 loc) · 61.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of ip_diffim.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import numpy as np
import requests
import os
import lsst.afw.detection as afwDetection
import lsst.afw.image as afwImage
import lsst.afw.math as afwMath
import lsst.afw.table as afwTable
import lsst.daf.base as dafBase
import lsst.geom
from lsst.ip.diffim.utils import (evaluateMaskFraction, computeDifferenceImageMetrics,
populate_sattle_visit_cache)
from lsst.meas.algorithms import SkyObjectsTask, SourceDetectionTask, SetPrimaryFlagsTask, MaskStreaksTask
from lsst.meas.algorithms import FindGlintTrailsTask
from lsst.meas.base import ForcedMeasurementTask, ApplyApCorrTask, DetectorVisitIdGeneratorConfig
import lsst.meas.deblender
import lsst.meas.extensions.trailedSources # noqa: F401
import lsst.meas.extensions.shapeHSM
import lsst.pex.config as pexConfig
from lsst.pex.exceptions import InvalidParameterError
import lsst.pipe.base as pipeBase
import lsst.utils
from lsst.utils.timer import timeMethod
from . import DipoleFitTask
__all__ = ["DetectAndMeasureConfig", "DetectAndMeasureTask",
"DetectAndMeasureScoreConfig", "DetectAndMeasureScoreTask"]
class BadSubtractionError(pipeBase.AlgorithmError):
"""Raised when the residuals in footprints of stars used to compute the
psf-matching kernel exceeds the configured maximum.
"""
def __init__(self, *, ratio, threshold):
msg = ("The ratio of residual power in source footprints on the"
" difference image to the power in the footprints on the"
f" science image was {ratio}, which exceeds the maximum"
f" threshold of {threshold}")
super().__init__(msg)
self.ratio = ratio
self.threshold = threshold
@property
def metadata(self):
return {"ratio": self.ratio,
"threshold": self.threshold
}
class NoDiaSourcesError(pipeBase.AlgorithmError):
"""Raised when there are no diaSources detected on an image difference.
"""
def __init__(self):
msg = ("No diaSources detected!")
super().__init__(msg)
@property
def metadata(self):
return {}
class DetectAndMeasureConnections(pipeBase.PipelineTaskConnections,
dimensions=("instrument", "visit", "detector"),
defaultTemplates={"coaddName": "deep",
"warpTypeSuffix": "",
"fakesType": ""}):
science = pipeBase.connectionTypes.Input(
doc="Input science exposure.",
dimensions=("instrument", "visit", "detector"),
storageClass="ExposureF",
name="{fakesType}calexp"
)
matchedTemplate = pipeBase.connectionTypes.Input(
doc="Warped and PSF-matched template used to create the difference image.",
dimensions=("instrument", "visit", "detector"),
storageClass="ExposureF",
name="{fakesType}{coaddName}Diff_matchedExp",
)
difference = pipeBase.connectionTypes.Input(
doc="Result of subtracting template from science.",
dimensions=("instrument", "visit", "detector"),
storageClass="ExposureF",
name="{fakesType}{coaddName}Diff_differenceTempExp",
)
kernelSources = pipeBase.connectionTypes.Input(
doc="Final selection of sources used for psf matching.",
dimensions=("instrument", "visit", "detector"),
storageClass="SourceCatalog",
name="{fakesType}{coaddName}Diff_psfMatchSources"
)
outputSchema = pipeBase.connectionTypes.InitOutput(
doc="Schema (as an example catalog) for output DIASource catalog.",
storageClass="SourceCatalog",
name="{fakesType}{coaddName}Diff_diaSrc_schema",
)
diaSources = pipeBase.connectionTypes.Output(
doc="Detected diaSources on the difference image.",
dimensions=("instrument", "visit", "detector"),
storageClass="SourceCatalog",
name="{fakesType}{coaddName}Diff_diaSrc",
)
subtractedMeasuredExposure = pipeBase.connectionTypes.Output(
doc="Difference image with detection mask plane filled in.",
dimensions=("instrument", "visit", "detector"),
storageClass="ExposureF",
name="{fakesType}{coaddName}Diff_differenceExp",
)
differenceBackground = pipeBase.connectionTypes.Output(
doc="Background model that was subtracted from the difference image.",
dimensions=("instrument", "visit", "detector"),
storageClass="Background",
name="difference_background",
)
maskedStreaks = pipeBase.connectionTypes.Output(
doc='Catalog of streak fit parameters for the difference image.',
storageClass="ArrowNumpyDict",
dimensions=("instrument", "visit", "detector"),
name="{fakesType}{coaddName}Diff_streaks",
)
glintTrailInfo = pipeBase.connectionTypes.Output(
doc='Dict of fit parameters for glint trails in the catalog.',
storageClass="ArrowNumpyDict",
dimensions=("instrument", "visit", "detector"),
name="trailed_glints",
)
def __init__(self, *, config):
super().__init__(config=config)
if not (self.config.doCalculateResidualMetics):
self.inputs.remove("kernelSources")
if not (self.config.writeStreakInfo and self.config.doMaskStreaks):
self.outputs.remove("maskedStreaks")
if not (self.config.doSubtractBackground and self.config.doWriteBackground):
self.outputs.remove("differenceBackground")
if not (self.config.writeGlintInfo):
self.outputs.remove("glintTrailInfo")
class DetectAndMeasureConfig(pipeBase.PipelineTaskConfig,
pipelineConnections=DetectAndMeasureConnections):
"""Config for DetectAndMeasureTask
"""
doMerge = pexConfig.Field(
dtype=bool,
default=True,
doc="Merge positive and negative diaSources with grow radius "
"set by growFootprint"
)
doForcedMeasurement = pexConfig.Field(
dtype=bool,
default=True,
doc="Force photometer diaSource locations on PVI?"
)
doAddMetrics = pexConfig.Field(
dtype=bool,
default=False,
doc="Add columns to the source table to hold analysis metrics?"
)
doSubtractBackground = pexConfig.Field(
dtype=bool,
doc="Subtract a background model from the image before detection?",
default=True,
)
doWriteBackground = pexConfig.Field(
dtype=bool,
doc="Persist the fitted background model?",
default=False,
)
doCalculateResidualMetics = pexConfig.Field(
dtype=bool,
doc="Calculate metrics to assess image subtraction quality for the task"
"metadata?",
default=True,
)
subtractInitialBackground = pexConfig.ConfigurableField(
target=lsst.meas.algorithms.SubtractBackgroundTask,
doc="Task to perform intial background subtraction, before first detection pass.",
)
subtractFinalBackground = pexConfig.ConfigurableField(
target=lsst.meas.algorithms.SubtractBackgroundTask,
doc="Task to perform final background subtraction, after first detection pass.",
)
detection = pexConfig.ConfigurableField(
target=SourceDetectionTask,
doc="Final source detection for diaSource measurement",
)
streakDetection = pexConfig.ConfigurableField(
target=SourceDetectionTask,
doc="Separate source detection used only for streak masking",
)
doDeblend = pexConfig.Field(
dtype=bool,
default=False,
doc="Deblend DIASources after detection?"
)
deblend = pexConfig.ConfigurableField(
target=lsst.meas.deblender.SourceDeblendTask,
doc="Task to split blended sources into their components."
)
measurement = pexConfig.ConfigurableField(
target=DipoleFitTask,
doc="Task to measure sources on the difference image.",
)
doApCorr = lsst.pex.config.Field(
dtype=bool,
default=True,
doc="Run subtask to apply aperture corrections"
)
applyApCorr = lsst.pex.config.ConfigurableField(
target=ApplyApCorrTask,
doc="Task to apply aperture corrections"
)
forcedMeasurement = pexConfig.ConfigurableField(
target=ForcedMeasurementTask,
doc="Task to force photometer science image at diaSource locations.",
)
growFootprint = pexConfig.Field(
dtype=int,
default=2,
doc="Grow positive and negative footprints by this many pixels before merging"
)
diaSourceMatchRadius = pexConfig.Field(
dtype=float,
default=0.5,
doc="Match radius (in arcseconds) for DiaSource to Source association"
)
doSkySources = pexConfig.Field(
dtype=bool,
default=False,
doc="Generate sky sources?",
)
skySources = pexConfig.ConfigurableField(
target=SkyObjectsTask,
doc="Generate sky sources",
)
doMaskStreaks = pexConfig.Field(
dtype=bool,
default=True,
doc="Turn on streak masking",
)
maskStreaks = pexConfig.ConfigurableField(
target=MaskStreaksTask,
doc="Subtask for masking streaks. Only used if doMaskStreaks is True. "
"Adds a mask plane to an exposure, with the mask plane name set by streakMaskName.",
)
streakBinFactor = pexConfig.Field(
dtype=int,
default=4,
doc="Bin scale factor to use when rerunning detection for masking streaks. "
"Only used if doMaskStreaks is True.",
)
writeStreakInfo = pexConfig.Field(
dtype=bool,
default=False,
doc="Record the parameters of any detected streaks. For LSST, this should be turned off except for "
"development work."
)
findGlints = pexConfig.ConfigurableField(
target=FindGlintTrailsTask,
doc="Subtask for finding glint trails, usually caused by satellites or debris."
)
writeGlintInfo = pexConfig.Field(
dtype=bool,
default=True,
doc="Record the parameters of any detected glint trails."
)
setPrimaryFlags = pexConfig.ConfigurableField(
target=SetPrimaryFlagsTask,
doc="Task to add isPrimary and deblending-related flags to the catalog."
)
badSourceFlags = lsst.pex.config.ListField(
dtype=str,
doc="Sources with any of these flags set are removed before writing the output catalog.",
default=("base_PixelFlags_flag_offimage",
"base_PixelFlags_flag_edge",
"base_PixelFlags_flag_interpolatedCenterAll",
"base_PixelFlags_flag_badCenter",
"base_PixelFlags_flag_edgeCenter",
"base_PixelFlags_flag_nodataCenter",
"base_PixelFlags_flag_saturatedCenter",
"base_PixelFlags_flag_saturated_templateCenter",
"base_PixelFlags_flag_spikeCenter",
"base_PixelFlags_flag_spike_templateCenter",
),
)
clearMaskPlanes = lsst.pex.config.ListField(
dtype=str,
doc="Mask planes to clear before running detection.",
default=("DETECTED", "DETECTED_NEGATIVE", "NOT_DEBLENDED", "STREAK"),
)
raiseOnBadSubtractionRatio = pexConfig.Field(
dtype=bool,
default=True,
doc="Raise an error if the ratio of power in detected footprints"
" on the difference image to the power in footprints on the science"
" image exceeds ``badSubtractionRatioThreshold``",
)
badSubtractionRatioThreshold = pexConfig.Field(
dtype=float,
default=0.2,
doc="Maximum ratio of power in footprints on the difference image to"
" the same footprints on the science image."
"Only used if ``raiseOnBadSubtractionRatio`` is set",
)
badSubtractionVariationThreshold = pexConfig.Field(
dtype=float,
default=0.4,
doc="Maximum standard deviation of the ratio of power in footprints on"
" the difference image to the same footprints on the science image."
"Only used if ``raiseOnBadSubtractionRatio`` is set",
)
raiseOnNoDiaSources = pexConfig.Field(
dtype=bool,
default=True,
doc="Raise an algorithm error if no diaSources are detected.",
)
run_sattle = pexConfig.Field(
dtype=bool,
default=False,
doc="If true, dia source bounding boxes will be sent for verification"
"to the sattle service."
)
sattle_historical = pexConfig.Field(
dtype=bool,
default=False,
doc="If re-running a pipeline that requires sattle, this should be set "
"to True. This will populate sattle's cache with the historic data "
"closest in time to the exposure."
)
idGenerator = DetectorVisitIdGeneratorConfig.make_field()
def setDefaults(self):
# Background subtraction
# Use a small binsize for the first pass to reduce detections on glints
# and extended structures. Should not affect the detectability of
# faint diaSources
self.subtractInitialBackground.binSize = 8
self.subtractInitialBackground.useApprox = False
self.subtractInitialBackground.statisticsProperty = "MEDIAN"
self.subtractInitialBackground.doFilterSuperPixels = True
self.subtractInitialBackground.ignoredPixelMask = ["BAD",
"EDGE",
"DETECTED",
"DETECTED_NEGATIVE",
"NO_DATA",
]
# Use a larger binsize for the final background subtraction, to reduce
# over-subtraction of bright objects.
self.subtractFinalBackground.binSize = 40
self.subtractFinalBackground.useApprox = False
self.subtractFinalBackground.statisticsProperty = "MEDIAN"
self.subtractFinalBackground.doFilterSuperPixels = True
self.subtractFinalBackground.ignoredPixelMask = ["BAD",
"EDGE",
"DETECTED",
"DETECTED_NEGATIVE",
"NO_DATA",
]
# DiaSource Detection
self.detection.thresholdPolarity = "both"
self.detection.thresholdValue = 5.0
self.detection.reEstimateBackground = False
self.detection.thresholdType = "pixel_stdev"
self.detection.excludeMaskPlanes = []
# Copy configs for binned streak detection from the base detection task
self.streakDetection.thresholdType = self.detection.thresholdType
self.streakDetection.reEstimateBackground = False
self.streakDetection.excludeMaskPlanes = self.detection.excludeMaskPlanes
self.streakDetection.thresholdValue = self.detection.thresholdValue
# Only detect positive streaks
self.streakDetection.thresholdPolarity = "positive"
# Do not grow detected mask for streaks
self.streakDetection.nSigmaToGrow = 0
# Set the streak mask along the entire fit line, not only where the
# detected mask is set.
self.maskStreaks.onlyMaskDetected = False
# Restrict streak masking from growing too large
self.maskStreaks.maxStreakWidth = 100
# Restrict the number of iterations allowed for fitting streaks
# When the fit is good it should solve quickly, and exit a bad fit quickly
self.maskStreaks.maxFitIter = 10
# Only mask to 2 sigma in width
self.maskStreaks.nSigmaMask = 2
# Threshold for including streaks after the Hough Transform.
# A lower value will detect more features that are less linear.
self.maskStreaks.absMinimumKernelHeight = 2
self.measurement.plugins.names |= ["ext_trailedSources_Naive",
"base_LocalPhotoCalib",
"base_LocalWcs",
"ext_shapeHSM_HsmSourceMoments",
"ext_shapeHSM_HsmPsfMoments",
"base_ClassificationSizeExtendedness",
]
self.measurement.slots.psfShape = "ext_shapeHSM_HsmPsfMoments"
self.measurement.slots.shape = "ext_shapeHSM_HsmSourceMoments"
self.measurement.plugins["base_SdssCentroid"].maxDistToPeak = 5.0
self.forcedMeasurement.plugins = ["base_TransformedCentroid", "base_PsfFlux"]
self.forcedMeasurement.copyColumns = {
"id": "objectId", "parent": "parentObjectId", "coord_ra": "coord_ra", "coord_dec": "coord_dec"}
self.forcedMeasurement.slots.centroid = "base_TransformedCentroid"
self.forcedMeasurement.slots.shape = None
# Keep track of which footprints contain streaks
self.measurement.plugins["base_PixelFlags"].masksFpAnywhere = [
"STREAK", "INJECTED", "INJECTED_TEMPLATE", "HIGH_VARIANCE", "SATURATED_TEMPLATE",
"SPIKE", "SPIKE_TEMPLATE"]
self.measurement.plugins["base_PixelFlags"].masksFpCenter = [
"STREAK", "INJECTED", "INJECTED_TEMPLATE", "HIGH_VARIANCE", "SATURATED_TEMPLATE",
"SPIKE", "SPIKE_TEMPLATE"]
self.skySources.avoidMask = ["DETECTED", "DETECTED_NEGATIVE", "BAD", "NO_DATA", "EDGE"]
def validate(self):
super().validate()
if self.run_sattle:
if not os.getenv("SATTLE_URI_BASE"):
raise pexConfig.FieldValidationError(DetectAndMeasureConfig.run_sattle, self,
"Sattle requested but SATTLE_URI_BASE "
"environment variable not set.")
class DetectAndMeasureTask(lsst.pipe.base.PipelineTask):
"""Detect and measure sources on a difference image.
"""
ConfigClass = DetectAndMeasureConfig
_DefaultName = "detectAndMeasure"
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.schema = afwTable.SourceTable.makeMinimalSchema()
self.algMetadata = dafBase.PropertyList()
if self.config.doSubtractBackground:
self.makeSubtask("subtractInitialBackground")
self.makeSubtask("subtractFinalBackground")
self.makeSubtask("detection", schema=self.schema)
if self.config.doDeblend:
self.makeSubtask("deblend", schema=self.schema)
self.makeSubtask("setPrimaryFlags", schema=self.schema, isSingleFrame=True)
self.makeSubtask("measurement", schema=self.schema,
algMetadata=self.algMetadata)
if self.config.doApCorr:
self.makeSubtask("applyApCorr", schema=self.measurement.schema)
if self.config.doForcedMeasurement:
self.schema.addField(
"ip_diffim_forced_PsfFlux_instFlux", "D",
"Forced PSF flux measured on the direct image.",
units="count")
self.schema.addField(
"ip_diffim_forced_PsfFlux_instFluxErr", "D",
"Forced PSF flux error measured on the direct image.",
units="count")
self.schema.addField(
"ip_diffim_forced_PsfFlux_area", "F",
"Forced PSF flux effective area of PSF.",
units="pixel")
self.schema.addField(
"ip_diffim_forced_PsfFlux_flag", "Flag",
"Forced PSF flux general failure flag.")
self.schema.addField(
"ip_diffim_forced_PsfFlux_flag_noGoodPixels", "Flag",
"Forced PSF flux not enough non-rejected pixels in data to attempt the fit.")
self.schema.addField(
"ip_diffim_forced_PsfFlux_flag_edge", "Flag",
"Forced PSF flux object was too close to the edge of the image to use the full PSF model.")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_instFlux", "D",
"Forced PSF flux measured on the template image.",
units="count")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_instFluxErr", "D",
"Forced PSF flux error measured on the template image.",
units="count")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_area", "F",
"Forced template PSF flux effective area of PSF.",
units="pixel")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_flag", "Flag",
"Forced template PSF flux general failure flag.")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_flag_noGoodPixels", "Flag",
"Forced template PSF flux not enough non-rejected pixels in data to attempt the fit.")
self.schema.addField(
"ip_diffim_forced_template_PsfFlux_flag_edge", "Flag",
"""Forced template PSF flux object was too close to the edge of the image """
"""to use the full PSF model.""")
self.makeSubtask("forcedMeasurement", refSchema=self.schema)
self.schema.addField("refMatchId", "L", "unique id of reference catalog match")
self.schema.addField("srcMatchId", "L", "unique id of source match")
# Create the sky source task for use by metrics,
# even if sky sources are not added to the diaSource catalog
self.makeSubtask("skySources", schema=self.schema)
if self.config.doMaskStreaks:
self.makeSubtask("maskStreaks")
self.makeSubtask("streakDetection")
self.makeSubtask("findGlints")
self.schema.addField("glint_trail", "Flag", "DiaSource is part of a glint trail.")
self.schema.addField("reliability", type="F", doc="Reliability score of the DiaSource")
# To get the "merge_*" fields in the schema; have to re-initialize
# this later, once we have a peak schema post-detection.
lsst.afw.detection.FootprintMergeList(self.schema, ["positive", "negative"])
# Check that the schema and config are consistent
for flag in self.config.badSourceFlags:
if flag not in self.schema:
raise pipeBase.InvalidQuantumError("Field %s not in schema" % flag)
# initialize InitOutputs
self.outputSchema = afwTable.SourceCatalog(self.schema)
self.outputSchema.getTable().setMetadata(self.algMetadata)
def runQuantum(self, butlerQC: pipeBase.QuantumContext,
inputRefs: pipeBase.InputQuantizedConnection,
outputRefs: pipeBase.OutputQuantizedConnection):
inputs = butlerQC.get(inputRefs)
idGenerator = self.config.idGenerator.apply(butlerQC.quantum.dataId)
idFactory = idGenerator.make_table_id_factory()
# Specify the fields that `annotate` needs below, to ensure they
# exist, even as None.
measurementResults = pipeBase.Struct(
subtractedMeasuredExposure=None,
diaSources=None,
maskedStreaks=None,
differenceBackground=None,
)
try:
self.run(**inputs, idFactory=idFactory, measurementResults=measurementResults)
except pipeBase.AlgorithmError as e:
error = pipeBase.AnnotatedPartialOutputsError.annotate(
e,
self,
measurementResults.subtractedMeasuredExposure,
measurementResults.diaSources,
measurementResults.maskedStreaks,
log=self.log
)
butlerQC.put(measurementResults, outputRefs)
raise error from e
butlerQC.put(measurementResults, outputRefs)
@timeMethod
def run(self, science, matchedTemplate, difference, kernelSources=None,
idFactory=None, measurementResults=None):
"""Detect and measure sources on a difference image.
The difference image will be convolved with a gaussian approximation of
the PSF to form a maximum likelihood image for detection.
Close positive and negative detections will optionally be merged into
dipole diaSources.
Sky sources, or forced detections in background regions, will optionally
be added, and the configured measurement algorithm will be run on all
detections.
Parameters
----------
science : `lsst.afw.image.ExposureF`
Science exposure that the template was subtracted from.
matchedTemplate : `lsst.afw.image.ExposureF`
Warped and PSF-matched template that was used produce the
difference image.
difference : `lsst.afw.image.ExposureF`
Result of subtracting template from the science image.
kernelSources : `lsst.afw.table.SourceCatalog`, optional
Final selection of sources that was used for psf matching.
idFactory : `lsst.afw.table.IdFactory`, optional
Generator object used to assign ids to detected sources in the
difference image. Ids from this generator are not set until after
deblending and merging positive/negative peaks.
measurementResults : `lsst.pipe.base.Struct`, optional
Result struct that is modified to allow saving of partial outputs
for some failure conditions. If the task completes successfully,
this is also returned.
Returns
-------
measurementResults : `lsst.pipe.base.Struct`
``subtractedMeasuredExposure`` : `lsst.afw.image.ExposureF`
Subtracted exposure with detection mask applied.
``diaSources`` : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
``differenceBackground`` : `lsst.afw.math.BackgroundList`
Background that was subtracted from the difference image.
"""
if measurementResults is None:
measurementResults = pipeBase.Struct()
if idFactory is None:
idFactory = lsst.meas.base.IdGenerator().make_table_id_factory()
if self.config.doSubtractBackground:
# Run background subtraction before clearing the mask planes
detectionExposure = difference.clone()
background = self.subtractInitialBackground.run(detectionExposure).background
else:
detectionExposure = difference
background = afwMath.BackgroundList()
self._prepareInputs(detectionExposure)
# Don't use the idFactory until after deblend+merge, so that we aren't
# generating ids that just get thrown away (footprint merge doesn't
# know about past ids).
table = afwTable.SourceTable.make(self.schema)
results = self.detection.run(
table=table,
exposure=detectionExposure,
doSmooth=True,
background=background
)
if self.config.doSubtractBackground:
# Run background subtraction again after detecting peaks
# but before measurement
# First update the mask using the detection image
difference.setMask(detectionExposure.mask)
background = self.subtractFinalBackground.run(difference).background
# Re-run detection to get final footprints
table = afwTable.SourceTable.make(self.schema)
results = self.detection.run(
table=table,
exposure=difference,
doSmooth=True,
background=background
)
measurementResults.differenceBackground = background
if self.config.doDeblend:
sources, positives, negatives = self._deblend(difference,
results.positive,
results.negative)
else:
positives = afwTable.SourceCatalog(self.schema)
results.positive.makeSources(positives)
negatives = afwTable.SourceCatalog(self.schema)
results.negative.makeSources(negatives)
sources = results.sources
self.processResults(science, matchedTemplate, difference,
sources, idFactory, kernelSources,
positives=positives,
negatives=negatives,
measurementResults=measurementResults)
return measurementResults
def _prepareInputs(self, difference):
"""Ensure that we start with an empty detection and deblended mask.
Parameters
----------
difference : `lsst.afw.image.ExposureF`
The difference image that will be used for detecting diaSources.
The mask plane will be modified in place.
Raises
------
lsst.pipe.base.UpstreamFailureNoWorkFound
If the PSF is not usable for measurement.
"""
# Check that we have a valid PSF now before we do more work
sigma = difference.psf.computeShape(difference.psf.getAveragePosition()).getDeterminantRadius()
if np.isnan(sigma):
raise pipeBase.UpstreamFailureNoWorkFound("Invalid PSF detected! PSF width evaluates to NaN.")
# Ensure that we start with an empty detection and deblended mask.
mask = difference.mask
for mp in self.config.clearMaskPlanes:
if mp not in mask.getMaskPlaneDict():
mask.addMaskPlane(mp)
mask &= ~mask.getPlaneBitMask(self.config.clearMaskPlanes)
def processResults(self, science, matchedTemplate, difference, sources, idFactory,
kernelSources=None, positives=None, negatives=None, measurementResults=None):
"""Measure and process the results of source detection.
Parameters
----------
science : `lsst.afw.image.ExposureF`
Science exposure that the template was subtracted from.
matchedTemplate : `lsst.afw.image.ExposureF`
Warped and PSF-matched template that was used produce the
difference image.
difference : `lsst.afw.image.ExposureF`
Result of subtracting template from the science image.
sources : `lsst.afw.table.SourceCatalog`
Detected sources on the difference exposure.
idFactory : `lsst.afw.table.IdFactory`
Generator object used to assign ids to detected sources in the
difference image.
kernelSources : `lsst.afw.table.SourceCatalog`, optional
Final selection of sources that was used for psf matching.
positives : `lsst.afw.table.SourceCatalog`, optional
Positive polarity footprints.
negatives : `lsst.afw.table.SourceCatalog`, optional
Negative polarity footprints.
measurementResults : `lsst.pipe.base.Struct`, optional
Result struct that is modified to allow saving of partial outputs
for some failure conditions. If the task completes successfully,
this is also returned.
Returns
-------
measurementResults : `lsst.pipe.base.Struct`
``subtractedMeasuredExposure`` : `lsst.afw.image.ExposureF`
Subtracted exposure with detection mask applied.
``diaSources`` : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
"""
if measurementResults is None:
measurementResults = pipeBase.Struct()
self.metadata["nUnmergedDiaSources"] = len(sources)
if self.config.doMerge:
# preserve peak schema, if there are any footprints
if len(positives) > 0:
peakSchema = positives[0].getFootprint().peaks.schema
elif len(negatives) > 0:
peakSchema = negatives[0].getFootprint().peaks.schema
else:
peakSchema = afwDetection.PeakTable.makeMinimalSchema()
mergeList = afwDetection.FootprintMergeList(self.schema,
["positive", "negative"], peakSchema)
initialDiaSources = afwTable.SourceCatalog(self.schema)
# Start with positive, as FootprintMergeList will self-merge the
# subsequent added catalogs, and we want to try to preserve
# deblended positive sources.
mergeList.addCatalog(initialDiaSources.table, positives, "positive", minNewPeakDist=0)
mergeList.addCatalog(initialDiaSources.table, negatives, "negative", minNewPeakDist=0)
mergeList.getFinalSources(initialDiaSources)
# Flag as negative those sources that *only* came from the negative
# footprint set.
initialDiaSources["is_negative"] = initialDiaSources["merge_footprint_negative"] & \
~initialDiaSources["merge_footprint_positive"]
self.log.info("Merging detections into %d sources", len(initialDiaSources))
else:
initialDiaSources = sources
# Assign source ids at the end: deblend/merge mean that we don't keep
# track of parents and children, we only care about the final ids.
for source in initialDiaSources:
source.setId(idFactory())
# Ensure sources added after this get correct ids.
initialDiaSources.getTable().setIdFactory(idFactory)
initialDiaSources.setMetadata(self.algMetadata)
self.metadata["nMergedDiaSources"] = len(initialDiaSources)
if self.config.doMaskStreaks:
streakInfo = self._runStreakMasking(difference)
if self.config.doSkySources:
self.addSkySources(initialDiaSources, difference.mask, difference.info.id)
if not initialDiaSources.isContiguous():
initialDiaSources = initialDiaSources.copy(deep=True)
self.measureDiaSources(initialDiaSources, science, difference, matchedTemplate)
# Remove unphysical diaSources per config.badSourceFlags
diaSources = self._removeBadSources(initialDiaSources)
if self.config.run_sattle:
diaSources = self.filterSatellites(diaSources, science)
# Flag diaSources in glint trails, but do not remove them
diaSources, trail_parameters = self._find_glint_trails(diaSources)
if self.config.writeGlintInfo:
measurementResults.mergeItems(trail_parameters, 'glintTrailInfo')
if self.config.doForcedMeasurement:
self.measureForcedSources(diaSources, science, difference.getWcs())
self.measureForcedSources(diaSources, matchedTemplate, difference.getWcs(),
template=True)
# Clear the image plane for regions with NO_DATA.
# These regions are most often caused by insufficient template coverage.
# Do this for the final difference image after detection and measurement
# since the subtasks should all be configured to handle NO_DATA properly
difference.image.array[difference.mask.array & difference.mask.getPlaneBitMask('NO_DATA') > 0] = 0
measurementResults.subtractedMeasuredExposure = difference
if self.config.doMaskStreaks and self.config.writeStreakInfo:
measurementResults.maskedStreaks = streakInfo.maskedStreaks
if kernelSources is not None:
self.calculateMetrics(science, difference, diaSources, kernelSources)
if np.count_nonzero(~diaSources["sky_source"]) > 0:
measurementResults.diaSources = diaSources
elif self.config.raiseOnNoDiaSources:
raise NoDiaSourcesError()
elif len(diaSources) > 0:
# This option allows returning sky sources,
# even if there are no diaSources
measurementResults.diaSources = diaSources
self.log.info("Measured %d diaSources and %d sky sources",
np.count_nonzero(~diaSources["sky_source"]),
np.count_nonzero(diaSources["sky_source"])
)
return measurementResults
def _deblend(self, difference, positiveFootprints, negativeFootprints):
"""Deblend the positive and negative footprints and return a catalog
containing just the children, and the deblended footprints.
Parameters
----------
difference : `lsst.afw.image.Exposure`
Result of subtracting template from the science image.
positiveFootprints, negativeFootprints : `lsst.afw.detection.FootprintSet`
Positive and negative polarity footprints measured on
``difference`` to be deblended separately.
Returns
-------
sources : `lsst.afw.table.SourceCatalog`
Positive and negative deblended children.
positives, negatives : `lsst.afw.table.SourceCatalog`
Deblended positive and negative polarity sources with footprints
detected on ``difference``.
"""
def deblend(footprints, negative=False):
"""Deblend a positive or negative footprint set,
and return the deblended children.
Parameters
----------
footprints : `lsst.afw.detection.FootprintSet`
negative : `bool`
Set True if the footprints contain negative fluxes
Returns
-------
sources : `lsst.afw.table.SourceCatalog`
"""
sources = afwTable.SourceCatalog(self.schema)
footprints.makeSources(sources)
if negative:
# Invert the image so the deblender can run on positive peaks
difference_inverted = difference.clone()
difference_inverted.image *= -1
self.deblend.run(exposure=difference_inverted, sources=sources)
children = sources[sources["parent"] != 0]
# Set the heavy footprint pixel values back to reality
for child in children:
footprint = child.getFootprint()
array = footprint.getImageArray()
array *= -1
else:
self.deblend.run(exposure=difference, sources=sources)
self.setPrimaryFlags.run(sources)
children = sources["detect_isDeblendedSource"] == 1
sources = sources[children].copy(deep=True)
# Clear parents, so that measurement plugins behave correctly.
sources['parent'] = 0
return sources.copy(deep=True)
positives = deblend(positiveFootprints)
negatives = deblend(negativeFootprints, negative=True)
sources = afwTable.SourceCatalog(self.schema)
sources.reserve(len(positives) + len(negatives))
sources.extend(positives, deep=True)
sources.extend(negatives, deep=True)
if len(negatives) > 0:
sources[-len(negatives):]["is_negative"] = True
return sources, positives, negatives
def _removeBadSources(self, diaSources):
"""Remove unphysical diaSources from the catalog.
Parameters
----------
diaSources : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
Returns
-------
diaSources : `lsst.afw.table.SourceCatalog`
The updated catalog of detected sources, with any source that has a
flag in ``config.badSourceFlags`` set removed.
"""
selector = np.ones(len(diaSources), dtype=bool)
for flag in self.config.badSourceFlags:
flags = diaSources[flag]
nBad = np.count_nonzero(flags)
if nBad > 0:
self.log.debug("Found %d unphysical sources with flag %s.", nBad, flag)
selector &= ~flags
nBadTotal = np.count_nonzero(~selector)
self.metadata["nRemovedBadFlaggedSources"] = nBadTotal
self.log.info("Removed %d unphysical sources.", nBadTotal)
return diaSources[selector].copy(deep=True)
def _find_glint_trails(self, diaSources):
"""Define a new flag column for diaSources that are in a glint trail.
Parameters
----------
diaSources : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
Returns
-------
diaSources : `lsst.afw.table.SourceCatalog`
The updated catalog of detected sources, with a new bool column
called 'glint_trail' added.
trail_parameters : `dict`
Parameters of all the trails that were found.
"""
if self.config.doSkySources:
# Do not include sky sources in glint detection
candidateDiaSources = diaSources[~diaSources["sky_source"]].copy(deep=True)
else:
candidateDiaSources = diaSources
trailed_glints = self.findGlints.run(candidateDiaSources)
glint_mask = [True if id in trailed_glints.trailed_ids else False for id in diaSources['id']]
if np.any(glint_mask):
diaSources['glint_trail'] = np.array(glint_mask)
slopes = np.array([trail.slope for trail in trailed_glints.parameters])
intercepts = np.array([trail.intercept for trail in trailed_glints.parameters])
stderrs = np.array([trail.stderr for trail in trailed_glints.parameters])
lengths = np.array([trail.length for trail in trailed_glints.parameters])
angles = np.array([trail.angle for trail in trailed_glints.parameters])
parameters = {'slopes': slopes, 'intercepts': intercepts, 'stderrs': stderrs, 'lengths': lengths,
'angles': angles}
trail_parameters = pipeBase.Struct(glintTrailInfo=parameters)
return diaSources, trail_parameters
def addSkySources(self, diaSources, mask, seed,
subtask=None):
"""Add sources in empty regions of the difference image
for measuring the background.
Parameters
----------
diaSources : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
mask : `lsst.afw.image.Mask`
Mask plane for determining regions where Sky sources can be added.
seed : `int`
Seed value to initialize the random number generator.
"""
if subtask is None:
subtask = self.skySources
if subtask.config.nSources <= 0:
self.metadata[f"n_{subtask.getName()}"] = 0
return
skySourceFootprints = subtask.run(mask=mask, seed=seed, catalog=diaSources)
self.metadata[f"n_{subtask.getName()}"] = len(skySourceFootprints)
def measureDiaSources(self, diaSources, science, difference, matchedTemplate):
"""Use (matched) template and science image to constrain dipole fitting.
Parameters
----------
diaSources : `lsst.afw.table.SourceCatalog`
The catalog of detected sources.
science : `lsst.afw.image.ExposureF`
Science exposure that the template was subtracted from.
difference : `lsst.afw.image.ExposureF`
Result of subtracting template from the science image.
matchedTemplate : `lsst.afw.image.ExposureF`
Warped and PSF-matched template that was used produce the
difference image.
"""
# Ensure that the required mask planes are present