Skip to content

Commit 43b2a7d

Browse files
committed
Make trailedAssociatorTask
Make trailedAssociatorTask which filters out trails whose lengths are above 0.416 arcseconds/second in length. Updated unit test and added test_trailedSourceFilter.py. Refactored doLongTrailFilter to doTrailedSourceFilter. Update comments
1 parent 6758148 commit 43b2a7d

File tree

7 files changed

+298
-15
lines changed

7 files changed

+298
-15
lines changed

python/lsst/ap/association/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2121

2222
from .version import *
23+
from .trailedSourceFilter import *
2324
from .association import *
2425
from .diaForcedSource import *
2526
from .loadDiaCatalogs import *

python/lsst/ap/association/association.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import lsst.pex.config as pexConfig
3333
import lsst.pipe.base as pipeBase
3434
from lsst.utils.timer import timeMethod
35+
from .trailedSourceFilter import TrailedSourceFilterTask
3536

3637
# Enforce an error for unsafe column/array value setting in pandas.
3738
pd.options.mode.chained_assignment = 'raise'
@@ -47,6 +48,18 @@ class AssociationConfig(pexConfig.Config):
4748
default=1.0,
4849
)
4950

51+
trailedSourceFilter = pexConfig.ConfigurableField(
52+
target=TrailedSourceFilterTask,
53+
doc="Subtask to filter artifact candidates based on morphological "
54+
"criteria, i.g. those that appear to be streaks.",
55+
)
56+
57+
doTrailedSourceFilter = pexConfig.Field(
58+
doc="Set flag for trailed source filter subtask to run.",
59+
dtype=bool,
60+
default=True,
61+
)
62+
5063

5164
class AssociationTask(pipeBase.Task):
5265
"""Associate DIAOSources into existing DIAObjects.
@@ -60,10 +73,16 @@ class AssociationTask(pipeBase.Task):
6073
ConfigClass = AssociationConfig
6174
_DefaultName = "association"
6275

76+
def __init__(self, *args, **kwargs):
77+
super().__init__(*args, **kwargs)
78+
if self.config.doTrailedSourceFilter:
79+
self.makeSubtask("trailedSourceFilter")
80+
6381
@timeMethod
6482
def run(self,
6583
diaSources,
66-
diaObjects):
84+
diaObjects,
85+
exposure=None):
6786
"""Associate the new DiaSources with existing DiaObjects.
6887
6988
Parameters
@@ -72,6 +91,9 @@ def run(self,
7291
New DIASources to be associated with existing DIAObjects.
7392
diaObjects : `pandas.DataFrame`
7493
Existing diaObjects from the Apdb.
94+
exposure : `pandas.DataFrame` optional
95+
Calibrated exposure differenced with a template image during
96+
image differencing.
7597
7698
Returns
7799
-------
@@ -98,7 +120,13 @@ def run(self,
98120
nUpdatedDiaObjects=0,
99121
nUnassociatedDiaObjects=0)
100122

101-
matchResult = self.associate_sources(diaObjects, diaSources)
123+
if self.config.doTrailedSourceFilter:
124+
diaTrailedResult = self.trailedSourceFilter.run(diaSources, exposure)
125+
126+
matchResult = self.associate_sources(diaObjects, diaTrailedResult.diaSources)
127+
128+
else:
129+
matchResult = self.associate_sources(diaObjects, diaSources)
102130

103131
mask = matchResult.diaSources["diaObjectId"] != 0
104132

python/lsst/ap/association/diaPipe.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
Currently loads directly from the Apdb rather than pre-loading.
2929
"""
3030

31+
__all__ = ("DiaPipelineConfig",
32+
"DiaPipelineTask",
33+
"DiaPipelineConnections")
34+
3135
import pandas as pd
3236

3337
import lsst.dax.apdb as daxApdb
@@ -41,13 +45,10 @@
4145
AssociationTask,
4246
DiaForcedSourceTask,
4347
LoadDiaCatalogsTask,
44-
PackageAlertsTask)
48+
PackageAlertsTask,
49+
TrailedSourceFilterTask)
4550
from lsst.ap.association.ssoAssociation import SolarSystemAssociationTask
4651

47-
__all__ = ("DiaPipelineConfig",
48-
"DiaPipelineTask",
49-
"DiaPipelineConnections")
50-
5152

5253
class DiaPipelineConnections(
5354
pipeBase.PipelineTaskConnections,
@@ -221,6 +222,10 @@ class DiaPipelineConfig(pipeBase.PipelineTaskConfig,
221222
target=AssociationTask,
222223
doc="Task used to associate DiaSources with DiaObjects.",
223224
)
225+
trailedFilter = pexConfig.ConfigurableField(
226+
target=TrailedSourceFilterTask,
227+
doc="Task used to find trailed DiaSources.",
228+
)
224229
doSolarSystemAssociation = pexConfig.Field(
225230
dtype=bool,
226231
default=False,
@@ -368,7 +373,7 @@ def run(self,
368373

369374
# Associate new DiaSources with existing DiaObjects.
370375
assocResults = self.associator.run(diaSourceTable,
371-
loaderResult.diaObjects)
376+
loaderResult.diaObjects, exposure=exposure)
372377
if self.config.doSolarSystemAssociation:
373378
ssoAssocResult = self.solarSystemAssociator.run(
374379
assocResults.unAssocDiaSources,
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# This file is part of ap_association.
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+
"""A simple implementation of trailed source filtering for the association
23+
task.
24+
"""
25+
26+
__all__ = ["TrailedSourceFilterTask", "TrailedSourceFilterConfig"]
27+
28+
import lsst.pex.config as pexConfig
29+
import lsst.pipe.base as pipeBase
30+
from lsst.utils.timer import timeMethod
31+
32+
# import numpy as np
33+
import pandas as pd
34+
35+
# Enforce an error for unsafe column/array value setting in pandas.
36+
pd.options.mode.chained_assignment = 'raise'
37+
38+
39+
class TrailedSourceFilterConfig(pexConfig.Config):
40+
"""Config class for TrailedSourceFilterTask.
41+
"""
42+
maxTrailLength = pexConfig.Field(
43+
dtype=float,
44+
doc='Maximum trail length permitted is less than 10 degrees/day. '
45+
'This is a rate of 0.416 arcseconds per second. As trail length'
46+
' is measured in arcseconds, it is dependant on the length of '
47+
'the exposure.',
48+
default=0.416,
49+
)
50+
51+
52+
class TrailedSourceFilterTask(pipeBase.Task):
53+
"""Find trailed sources in DIAObjects.
54+
55+
"""
56+
57+
ConfigClass = TrailedSourceFilterConfig
58+
_DefaultName = "trailedAssociation"
59+
60+
@timeMethod
61+
def run(self,
62+
dia_sources, exposure):
63+
"""Find trailed sources which have not been filtered out and will
64+
not be included in the diaSource catalog.
65+
66+
Parameters
67+
----------
68+
dia_sources : `pandas.DataFrame`
69+
New DIASources to be checked for trailed sources.
70+
exposure : `pandas.DataFrame`
71+
Calibrated exposure differenced with a template image during
72+
image differencing.
73+
74+
Returns
75+
-------
76+
result : `lsst.pipe.base.Struct`
77+
Results struct with components.
78+
79+
- ``"dia_sources"`` : DiaSource table that is free from unwanted
80+
trailed sources (`pandas.DataFrame`)
81+
82+
- ``"trailed_dia_sources"`` : DiaSources that have trailed more
83+
than 0.416 arcseconds/second*exposure_time(`pandas.DataFrame`)
84+
"""
85+
trail_mask = self.check_dia_source_trail(dia_sources, exposure)
86+
87+
return pipeBase.Struct(
88+
diaSources=dia_sources[~trail_mask].reset_index(drop=True),
89+
trailedDiaSources=dia_sources[trail_mask].reset_index(drop=True))
90+
91+
def check_dia_source_trail(self, dia_sources, exposure):
92+
"""Check that all DiaSources have trails.
93+
94+
Creates a mask for sources with lengths greater than 0.416
95+
arcseconds/second times the exposure time.
96+
97+
Parameters
98+
----------
99+
dia_sources : `pandas.DataFrame`
100+
Input DiaSources to check for trail lengths.
101+
exposure : `pandas.DataFrame`
102+
Calibrated exposure differenced with a template image during
103+
image differencing.
104+
105+
Returns
106+
-------
107+
trail_mask : `pandas.DataFrame`
108+
Boolean mask for dia_sources which are greater than the
109+
cuttoff length.
110+
"""
111+
exposure_time = exposure.getInfo().getVisitInfo().getExposureTime()
112+
trail_mask = (dia_sources.loc[:, "trailLength"].values[:] >= self.config.maxTrailLength*exposure_time)
113+
114+
return trail_mask

tests/test_association_task.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
import numpy as np
2323
import pandas as pd
2424
import unittest
25-
25+
import lsst.afw.image as afwImage
26+
from lsst.afw.coord import Weather
2627
import lsst.geom as geom
2728
import lsst.utils.tests
2829

@@ -46,20 +47,38 @@ def setUp(self):
4647
self.diaSources = pd.DataFrame(data=[
4748
{"ra": 0.04*idx + scatter*rng.uniform(-1, 1),
4849
"dec": 0.04*idx + scatter*rng.uniform(-1, 1),
49-
"diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0}
50+
"diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0, "trailLength": 5.5*idx}
5051
for idx in range(self.nSources)])
5152
self.diaSourceZeroScatter = pd.DataFrame(data=[
5253
{"ra": 0.04*idx,
5354
"dec": 0.04*idx,
54-
"diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0}
55+
"diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0, "trailLength": 5.5*idx}
5556
for idx in range(self.nSources)])
57+
exposureId = 5
58+
exposureTime = 30
59+
boresightRotAngle = 45.6 * lsst.geom.degrees
60+
weather = Weather(1.1, 2.2, 0.3)
61+
visitInfo = afwImage.VisitInfo(
62+
exposureId=exposureId,
63+
exposureTime=exposureTime,
64+
boresightRotAngle=boresightRotAngle,
65+
weather=weather,
66+
)
67+
exposureInfo = afwImage.ExposureInfo()
68+
exposureInfo.setVisitInfo(visitInfo)
69+
maskedImage = afwImage.MaskedImageF(lsst.geom.Extent2I(64, 64))
70+
self.exposure = afwImage.ExposureF(maskedImage, exposureInfo)
5671

5772
def test_run(self):
5873
"""Test the full task by associating a set of diaSources to
5974
existing diaObjects.
6075
"""
61-
assocTask = AssociationTask()
62-
results = assocTask.run(self.diaSources, self.diaObjects)
76+
77+
config = AssociationTask.ConfigClass()
78+
config.doTrailedSourceFilter = False
79+
assocTask = AssociationTask(config=config)
80+
81+
results = assocTask.run(self.diaSources, self.diaObjects, self.exposure)
6382

6483
self.assertEqual(results.nUpdatedDiaObjects, len(self.diaObjects) - 1)
6584
self.assertEqual(results.nUnassociatedDiaObjects, 1)
@@ -75,13 +94,36 @@ def test_run(self):
7594
[0]):
7695
self.assertEqual(test_obj_id, expected_obj_id)
7796

97+
def test_run_trailed_sources(self):
98+
"""Test the full task by associating a set of diaSources to
99+
existing diaObjects when trailed sources are filtered.
100+
"""
101+
102+
assocTask = AssociationTask()
103+
104+
results = assocTask.run(self.diaSources, self.diaObjects, self.exposure)
105+
106+
self.assertEqual(results.nUpdatedDiaObjects, len(self.diaObjects) - 3)
107+
self.assertEqual(results.nUnassociatedDiaObjects, 3)
108+
self.assertEqual(len(results.matchedDiaSources),
109+
len(self.diaObjects) - 3)
110+
self.assertEqual(len(results.unAssocDiaSources), 1)
111+
for test_obj_id, expected_obj_id in zip(
112+
results.matchedDiaSources["diaObjectId"].to_numpy(),
113+
[1, 2, 3, 4]):
114+
self.assertEqual(test_obj_id, expected_obj_id)
115+
for test_obj_id, expected_obj_id in zip(
116+
results.unAssocDiaSources["diaObjectId"].to_numpy(),
117+
[0]):
118+
self.assertEqual(test_obj_id, expected_obj_id)
119+
78120
def test_run_no_existing_objects(self):
79121
"""Test the run method with a completely empty database.
80122
"""
81123
assocTask = AssociationTask()
82124
results = assocTask.run(
83125
self.diaSources,
84-
pd.DataFrame(columns=["ra", "dec", "diaObjectId"]))
126+
pd.DataFrame(columns=["ra", "dec", "diaObjectId", "trailLength"]), self.exposure)
85127
self.assertEqual(results.nUpdatedDiaObjects, 0)
86128
self.assertEqual(results.nUnassociatedDiaObjects, 0)
87129
self.assertEqual(len(results.matchedDiaSources), 0)

tests/test_diaPipe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def solarSystemAssociator_run(self, unAssocDiaSources, solarSystemObjectTable, d
121121
unAssocDiaSources=MagicMock(spec=pd.DataFrame()))
122122

123123
@lsst.utils.timer.timeMethod
124-
def associator_run(self, table, diaObjects):
124+
def associator_run(self, table, diaObjects, exposure):
125125
return lsst.pipe.base.Struct(nUpdatedDiaObjects=2, nUnassociatedDiaObjects=3,
126126
matchedDiaSources=MagicMock(spec=pd.DataFrame()),
127127
unAssocDiaSources=MagicMock(spec=pd.DataFrame()))

0 commit comments

Comments
 (0)