/
diaPipe.py
593 lines (541 loc) · 24.7 KB
/
diaPipe.py
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
#
# LSST Data Management System
# Copyright 2008-2016 AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# 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 LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
"""PipelineTask for associating DiaSources with previous DiaObjects.
Additionally performs forced photometry on the calibrated and difference
images at the updated locations of DiaObjects.
Currently loads directly from the Apdb rather than pre-loading.
"""
import pandas as pd
import lsst.dax.apdb as daxApdb
from lsst.meas.base import DiaObjectCalculationTask
import lsst.pex.config as pexConfig
import lsst.pipe.base as pipeBase
import lsst.pipe.base.connectionTypes as connTypes
from lsst.utils.timer import timeMethod
from lsst.ap.association import (
AssociationTask,
DiaForcedSourceTask,
LoadDiaCatalogsTask,
PackageAlertsTask)
from lsst.ap.association.ssoAssociation import SolarSystemAssociationTask
__all__ = ("DiaPipelineConfig",
"DiaPipelineTask",
"DiaPipelineConnections")
class DiaPipelineConnections(
pipeBase.PipelineTaskConnections,
dimensions=("instrument", "visit", "detector"),
defaultTemplates={"coaddName": "deep", "fakesType": ""}):
"""Butler connections for DiaPipelineTask.
"""
diaSourceTable = connTypes.Input(
doc="Catalog of calibrated DiaSources.",
name="{fakesType}{coaddName}Diff_diaSrcTable",
storageClass="DataFrame",
dimensions=("instrument", "visit", "detector"),
)
solarSystemObjectTable = connTypes.Input(
doc="Catalog of SolarSolarSystem objects expected to be observable in "
"this detectorVisit.",
name="visitSsObjects",
storageClass="DataFrame",
dimensions=("instrument", "visit"),
)
diffIm = connTypes.Input(
doc="Difference image on which the DiaSources were detected.",
name="{fakesType}{coaddName}Diff_differenceExp",
storageClass="ExposureF",
dimensions=("instrument", "visit", "detector"),
)
exposure = connTypes.Input(
doc="Calibrated exposure differenced with a template image during "
"image differencing.",
name="{fakesType}calexp",
storageClass="ExposureF",
dimensions=("instrument", "visit", "detector"),
)
template = connTypes.Input(
doc="Warped template used to create `subtractedExposure`. Not PSF "
"matched.",
dimensions=("instrument", "visit", "detector"),
storageClass="ExposureF",
name="{fakesType}{coaddName}Diff_templateExp",
)
apdbMarker = connTypes.Output(
doc="Marker dataset storing the configuration of the Apdb for each "
"visit/detector. Used to signal the completion of the pipeline.",
name="apdb_marker",
storageClass="Config",
dimensions=("instrument", "visit", "detector"),
)
associatedDiaSources = connTypes.Output(
doc="Optional output storing the DiaSource catalog after matching, "
"calibration, and standardization for insertation into the Apdb.",
name="{fakesType}{coaddName}Diff_assocDiaSrc",
storageClass="DataFrame",
dimensions=("instrument", "visit", "detector"),
)
def __init__(self, *, config=None):
super().__init__(config=config)
if not config.doWriteAssociatedSources:
self.outputs.remove("associatedDiaSources")
if not config.doSolarSystemAssociation:
self.inputs.remove("solarSystemObjectTable")
def adjustQuantum(self, inputs, outputs, label, dataId):
"""Override to make adjustments to `lsst.daf.butler.DatasetRef` objects
in the `lsst.daf.butler.core.Quantum` during the graph generation stage
of the activator.
This implementation checks to make sure that the filters in the dataset
are compatible with AP processing as set by the Apdb/DPDD schema.
Parameters
----------
inputs : `dict`
Dictionary whose keys are an input (regular or prerequisite)
connection name and whose values are a tuple of the connection
instance and a collection of associated `DatasetRef` objects.
The exact type of the nested collections is unspecified; it can be
assumed to be multi-pass iterable and support `len` and ``in``, but
it should not be mutated in place. In contrast, the outer
dictionaries are guaranteed to be temporary copies that are true
`dict` instances, and hence may be modified and even returned; this
is especially useful for delegating to `super` (see notes below).
outputs : `dict`
Dict of output datasets, with the same structure as ``inputs``.
label : `str`
Label for this task in the pipeline (should be used in all
diagnostic messages).
data_id : `lsst.daf.butler.DataCoordinate`
Data ID for this quantum in the pipeline (should be used in all
diagnostic messages).
Returns
-------
adjusted_inputs : `dict`
Dict of the same form as ``inputs`` with updated containers of
input `DatasetRef` objects. Connections that are not changed
should not be returned at all. Datasets may only be removed, not
added. Nested collections may be of any multi-pass iterable type,
and the order of iteration will set the order of iteration within
`PipelineTask.runQuantum`.
adjusted_outputs : `dict`
Dict of updated output datasets, with the same structure and
interpretation as ``adjusted_inputs``.
Raises
------
ScalarError
Raised if any `Input` or `PrerequisiteInput` connection has
``multiple`` set to `False`, but multiple datasets.
NoWorkFound
Raised to indicate that this quantum should not be run; not enough
datasets were found for a regular `Input` connection, and the
quantum should be pruned or skipped.
FileNotFoundError
Raised to cause QuantumGraph generation to fail (with the message
included in this exception); not enough datasets were found for a
`PrerequisiteInput` connection.
"""
_, refs = inputs["diffIm"]
for ref in refs:
if ref.dataId["band"] not in self.config.validBands:
raise ValueError(
f"Requested '{ref.dataId['band']}' not in "
"DiaPipelineConfig.validBands. To process bands not in "
"the standard Rubin set (ugrizy) you must add the band to "
"the validBands list in DiaPipelineConfig and add the "
"appropriate columns to the Apdb schema.")
return super().adjustQuantum(inputs, outputs, label, dataId)
class DiaPipelineConfig(pipeBase.PipelineTaskConfig,
pipelineConnections=DiaPipelineConnections):
"""Config for DiaPipelineTask.
"""
coaddName = pexConfig.Field(
doc="coadd name: typically one of deep, goodSeeing, or dcr",
dtype=str,
default="deep",
)
apdb = daxApdb.ApdbSql.makeField(
doc="Database connection for storing associated DiaSources and "
"DiaObjects. Must already be initialized.",
)
validBands = pexConfig.ListField(
dtype=str,
default=["u", "g", "r", "i", "z", "y"],
doc="List of bands that are valid for AP processing. To process a "
"band not on this list, the appropriate band specific columns "
"must be added to the Apdb schema in dax_apdb.",
)
diaCatalogLoader = pexConfig.ConfigurableField(
target=LoadDiaCatalogsTask,
doc="Task to load DiaObjects and DiaSources from the Apdb.",
)
associator = pexConfig.ConfigurableField(
target=AssociationTask,
doc="Task used to associate DiaSources with DiaObjects.",
)
doSolarSystemAssociation = pexConfig.Field(
dtype=bool,
default=False,
doc="Process SolarSystem objects through the pipeline.",
)
solarSystemAssociator = pexConfig.ConfigurableField(
target=SolarSystemAssociationTask,
doc="Task used to associate DiaSources with SolarSystemObjects.",
)
diaCalculation = pexConfig.ConfigurableField(
target=DiaObjectCalculationTask,
doc="Task to compute summary statistics for DiaObjects.",
)
diaForcedSource = pexConfig.ConfigurableField(
target=DiaForcedSourceTask,
doc="Task used for force photometer DiaObject locations in direct and "
"difference images.",
)
alertPackager = pexConfig.ConfigurableField(
target=PackageAlertsTask,
doc="Subtask for packaging Ap data into alerts.",
)
doPackageAlerts = pexConfig.Field(
dtype=bool,
default=False,
doc="Package Dia-data into serialized alerts for distribution and "
"write them to disk.",
)
doWriteAssociatedSources = pexConfig.Field(
dtype=bool,
default=False,
doc="Write out associated and SDMed DiaSources.",
)
def setDefaults(self):
self.apdb.dia_object_index = "baseline"
self.apdb.dia_object_columns = []
self.diaCalculation.plugins = ["ap_meanPosition",
"ap_nDiaSources",
"ap_diaObjectFlag",
"ap_meanFlux",
"ap_percentileFlux",
"ap_sigmaFlux",
"ap_chi2Flux",
"ap_madFlux",
"ap_skewFlux",
"ap_minMaxFlux",
"ap_maxSlopeFlux",
"ap_meanErrFlux",
"ap_linearFit",
"ap_stetsonJ",
"ap_meanTotFlux",
"ap_sigmaTotFlux"]
class DiaPipelineTask(pipeBase.PipelineTask):
"""Task for loading, associating and storing Difference Image Analysis
(DIA) Objects and Sources.
"""
ConfigClass = DiaPipelineConfig
_DefaultName = "diaPipe"
def __init__(self, initInputs=None, **kwargs):
super().__init__(**kwargs)
self.apdb = self.config.apdb.apply()
self.makeSubtask("diaCatalogLoader")
self.makeSubtask("associator")
self.makeSubtask("diaCalculation")
self.makeSubtask("diaForcedSource")
if self.config.doPackageAlerts:
self.makeSubtask("alertPackager")
if self.config.doSolarSystemAssociation:
self.makeSubtask("solarSystemAssociator")
def runQuantum(self, butlerQC, inputRefs, outputRefs):
inputs = butlerQC.get(inputRefs)
expId, expBits = butlerQC.quantum.dataId.pack("visit_detector",
returnMaxBits=True)
inputs["ccdExposureIdBits"] = expBits
inputs["band"] = butlerQC.quantum.dataId["band"]
if not self.config.doSolarSystemAssociation:
inputs["solarSystemObjectTable"] = None
outputs = self.run(**inputs)
butlerQC.put(outputs, outputRefs)
@timeMethod
def run(self,
diaSourceTable,
solarSystemObjectTable,
diffIm,
exposure,
template,
ccdExposureIdBits,
band):
"""Process DiaSources and DiaObjects.
Load previous DiaObjects and their DiaSource history. Calibrate the
values in the diaSourceCat. Associate new DiaSources with previous
DiaObjects. Run forced photometry at the updated DiaObject locations.
Store the results in the Alert Production Database (Apdb).
Parameters
----------
diaSourceTable : `pandas.DataFrame`
Newly detected DiaSources.
diffIm : `lsst.afw.image.ExposureF`
Difference image exposure in which the sources in ``diaSourceCat``
were detected.
exposure : `lsst.afw.image.ExposureF`
Calibrated exposure differenced with a template to create
``diffIm``.
template : `lsst.afw.image.ExposureF`
Template exposure used to create diffIm.
ccdExposureIdBits : `int`
Number of bits used for a unique ``ccdVisitId``.
band : `str`
The band in which the new DiaSources were detected.
Returns
-------
results : `lsst.pipe.base.Struct`
Results struct with components.
- ``apdbMaker`` : Marker dataset to store in the Butler indicating
that this ccdVisit has completed successfully.
(`lsst.dax.apdb.ApdbConfig`)
- ``associatedDiaSources`` : Catalog of newly associated
DiaSources. (`pandas.DataFrame`)
"""
# Load the DiaObjects and DiaSource history.
loaderResult = self.diaCatalogLoader.run(diffIm, self.apdb)
# Associate new DiaSources with existing DiaObjects.
assocResults = self.associator.run(diaSourceTable,
loaderResult.diaObjects)
if self.config.doSolarSystemAssociation:
ssoAssocResult = self.solarSystemAssociator.run(
assocResults.unAssocDiaSources,
solarSystemObjectTable,
diffIm)
createResults = self.createNewDiaObjects(
ssoAssocResult.unAssocDiaSources)
associatedDiaSources = pd.concat(
[assocResults.matchedDiaSources,
ssoAssocResult.ssoAssocDiaSources,
createResults.diaSources])
nTotalSsObjects = ssoAssocResult.nTotalSsObjects
nAssociatedSsObjects = ssoAssocResult.nAssociatedSsObjects
else:
createResults = self.createNewDiaObjects(
assocResults.unAssocDiaSources)
associatedDiaSources = pd.concat(
[assocResults.matchedDiaSources,
createResults.diaSources])
nTotalSsObjects = 0
nAssociatedSsObjects = 0
# Create new DiaObjects from unassociated diaSources.
self._add_association_meta_data(assocResults.nUpdatedDiaObjects,
assocResults.nUnassociatedDiaObjects,
createResults.nNewDiaObjects,
nTotalSsObjects,
nAssociatedSsObjects)
# Index the DiaSource catalog for this visit after all associations
# have been made.
updatedDiaObjectIds = associatedDiaSources["diaObjectId"][
associatedDiaSources["diaObjectId"] != 0].to_numpy()
associatedDiaSources.set_index(["diaObjectId",
"filterName",
"diaSourceId"],
drop=False,
inplace=True)
# Append new DiaObjects and DiaSources to their previous history.
diaObjects = loaderResult.diaObjects.append(
createResults.newDiaObjects.set_index("diaObjectId", drop=False),
sort=True)
if self.testDataFrameIndex(diaObjects):
raise RuntimeError(
"Duplicate DiaObjects created after association. This is "
"likely due to re-running data with an already populated "
"Apdb. If this was not the case then there was an unexpected "
"failure in Association while matching and creating new "
"DiaObjects and should be reported. Exiting.")
mergedDiaSourceHistory = loaderResult.diaSources.append(
associatedDiaSources,
sort=True)
# Test for DiaSource duplication first. If duplicates are found,
# this likely means this is duplicate data being processed and sent
# to the Apdb.
if self.testDataFrameIndex(mergedDiaSourceHistory):
raise RuntimeError(
"Duplicate DiaSources found after association and merging "
"with history. This is likely due to re-running data with an "
"already populated Apdb. If this was not the case then there "
"was an unexpected failure in Association while matching "
"sources to objects, and should be reported. Exiting.")
# Compute DiaObject Summary statistics from their full DiaSource
# history.
diaCalResult = self.diaCalculation.run(
diaObjects,
mergedDiaSourceHistory,
updatedDiaObjectIds,
[band])
# Test for duplication in the updated DiaObjects.
if self.testDataFrameIndex(diaCalResult.diaObjectCat):
raise RuntimeError(
"Duplicate DiaObjects (loaded + updated) created after "
"DiaCalculation. This is unexpected behavior and should be "
"reported. Existing.")
if self.testDataFrameIndex(diaCalResult.updatedDiaObjects):
raise RuntimeError(
"Duplicate DiaObjects (updated) created after "
"DiaCalculation. This is unexpected behavior and should be "
"reported. Existing.")
# Force photometer on the Difference and Calibrated exposures using
# the new and updated DiaObject locations.
diaForcedSources = self.diaForcedSource.run(
diaCalResult.diaObjectCat,
diaCalResult.updatedDiaObjects.loc[:, "diaObjectId"].to_numpy(),
ccdExposureIdBits,
exposure,
diffIm)
# Store DiaSources, updated DiaObjects, and DiaForcedSources in the
# Apdb.
self.apdb.store(
exposure.getInfo().getVisitInfo().getDate(),
diaCalResult.updatedDiaObjects,
associatedDiaSources,
diaForcedSources)
if self.config.doPackageAlerts:
if len(loaderResult.diaForcedSources) > 1:
diaForcedSources = diaForcedSources.append(
loaderResult.diaForcedSources,
sort=True)
if self.testDataFrameIndex(diaForcedSources):
self.log.warning(
"Duplicate DiaForcedSources created after merge with "
"history and new sources. This may cause downstream "
"problems. Dropping duplicates.")
# Drop duplicates via index and keep the first appearance.
# Reset due to the index shape being slight different than
# expected.
diaForcedSources = diaForcedSources.groupby(
diaForcedSources.index).first()
diaForcedSources.reset_index(drop=True, inplace=True)
diaForcedSources.set_index(
["diaObjectId", "diaForcedSourceId"],
drop=False,
inplace=True)
self.alertPackager.run(associatedDiaSources,
diaCalResult.diaObjectCat,
loaderResult.diaSources,
diaForcedSources,
diffIm,
template,
ccdExposureIdBits)
return pipeBase.Struct(apdbMarker=self.config.apdb.value,
associatedDiaSources=associatedDiaSources,)
def createNewDiaObjects(self, unAssocDiaSources):
"""Loop through the set of DiaSources and create new DiaObjects
for unassociated DiaSources.
Parameters
----------
unAssocDiaSources : `pandas.DataFrame`
Set of DiaSources to create new DiaObjects from.
Returns
-------
results : `lsst.pipe.base.Struct`
Results struct containing:
- ``diaSources`` : DiaSource catalog with updated DiaObject ids.
(`pandas.DataFrame`)
- ``newDiaObjects`` : Newly created DiaObjects from the
unassociated DiaSources. (`pandas.DataFrame`)
- ``nNewDiaObjects`` : Number of newly created diaObjects.(`int`)
"""
if len(unAssocDiaSources) == 0:
tmpObj = self._initialize_dia_object(0)
newDiaObjects = pd.DataFrame(data=[],
columns=tmpObj.keys())
else:
newDiaObjects = unAssocDiaSources["diaSourceId"].apply(
self._initialize_dia_object)
unAssocDiaSources["diaObjectId"] = unAssocDiaSources["diaSourceId"]
return pipeBase.Struct(diaSources=unAssocDiaSources,
newDiaObjects=newDiaObjects,
nNewDiaObjects=len(newDiaObjects))
def _initialize_dia_object(self, objId):
"""Create a new DiaObject with values required to be initialized by the
Ppdb.
Parameters
----------
objid : `int`
``diaObjectId`` value for the of the new DiaObject.
Returns
-------
diaObject : `dict`
Newly created DiaObject with keys:
``diaObjectId``
Unique DiaObjectId (`int`).
``pmParallaxNdata``
Number of data points used for parallax calculation (`int`).
``nearbyObj1``
Id of the a nearbyObject in the Object table (`int`).
``nearbyObj2``
Id of the a nearbyObject in the Object table (`int`).
``nearbyObj3``
Id of the a nearbyObject in the Object table (`int`).
``?PSFluxData``
Number of data points used to calculate point source flux
summary statistics in each bandpass (`int`).
"""
new_dia_object = {"diaObjectId": objId,
"pmParallaxNdata": 0,
"nearbyObj1": 0,
"nearbyObj2": 0,
"nearbyObj3": 0,
"flags": 0}
for f in ["u", "g", "r", "i", "z", "y"]:
new_dia_object["%sPSFluxNdata" % f] = 0
return pd.Series(data=new_dia_object)
def testDataFrameIndex(self, df):
"""Test the sorted DataFrame index for duplicates.
Wrapped as a separate function to allow for mocking of the this task
in unittesting. Default of a mock return for this test is True.
Parameters
----------
df : `pandas.DataFrame`
DataFrame to text.
Returns
-------
`bool`
True if DataFrame contains duplicate rows.
"""
return df.index.has_duplicates
def _add_association_meta_data(self,
nUpdatedDiaObjects,
nUnassociatedDiaObjects,
nNewDiaObjects,
nTotalSsObjects,
nAssociatedSsObjects):
"""Store summaries of the association step in the task metadata.
Parameters
----------
nUpdatedDiaObjects : `int`
Number of previous DiaObjects associated and updated in this
ccdVisit.
nUnassociatedDiaObjects : `int`
Number of previous DiaObjects that were not associated or updated
in this ccdVisit.
nNewDiaObjects : `int`
Number of newly created DiaObjects for this ccdVisit.
nTotalSsObjects : `int`
Number of SolarSystemObjects within the observable detector
area.
nAssociatedSsObjects : `int`
Number of successfully associated SolarSystemObjects.
"""
self.metadata.add('numUpdatedDiaObjects', nUpdatedDiaObjects)
self.metadata.add('numUnassociatedDiaObjects', nUnassociatedDiaObjects)
self.metadata.add('numNewDiaObjects', nNewDiaObjects)
self.metadata.add('numTotalSolarSystemObjects', nTotalSsObjects)
self.metadata.add('numAssociatedSsObjects', nAssociatedSsObjects)