Skip to content

Commit ed032ca

Browse files
committed
Add unittest for CullFromMaskedRegion selection
1 parent a594b0f commit ed032ca

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

tests/test_sourceSelector.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import astropy.units as u
2727
import warnings
2828

29+
import lsst.afw.image
2930
import lsst.afw.table
3031
import lsst.meas.algorithms
3132
import lsst.meas.base.tests
@@ -56,28 +57,35 @@ def setUp(self):
5657
schema.addField("nChild", np.int32, "Number of children")
5758
schema.addField("detect_isPrimary", "Flag", "Is primary detection?")
5859
schema.addField("sky_source", "Flag", "Empty sky region.")
60+
61+
self.xCol = "centroid_x"
62+
self.yCol = "centroid_y"
63+
schema.addField(self.xCol, float, "Centroid x value.")
64+
schema.addField(self.yCol, float, "Centroid y value.")
65+
5966
self.catalog = lsst.afw.table.SourceCatalog(schema)
6067
self.catalog.reserve(10)
6168
self.config = self.Task.ConfigClass()
69+
self.exposure = None
6270

6371
def tearDown(self):
6472
del self.catalog
6573

6674
def check(self, expected):
6775
task = self.Task(config=self.config)
68-
results = task.run(self.catalog)
76+
results = task.run(self.catalog, exposure=self.exposure)
6977
self.assertListEqual(results.selected.tolist(), expected)
7078
self.assertListEqual([src.getId() for src in results.sourceCat],
7179
[src.getId() for src, ok in zip(self.catalog, expected) if ok])
7280

7381
# Check with pandas.DataFrame version of catalog
74-
results = task.run(self.catalog.asAstropy().to_pandas())
82+
results = task.run(self.catalog.asAstropy().to_pandas(), exposure=self.exposure)
7583
self.assertListEqual(results.selected.tolist(), expected)
7684
self.assertListEqual(list(results.sourceCat['id']),
7785
[src.getId() for src, ok in zip(self.catalog, expected) if ok])
7886

7987
# Check with astropy.table.Table version of catalog
80-
results = task.run(self.catalog.asAstropy())
88+
results = task.run(self.catalog.asAstropy(), exposure=self.exposure)
8189
self.assertListEqual(results.selected.tolist(), expected)
8290
self.assertListEqual(list(results.sourceCat['id']),
8391
[src.getId() for src, ok in zip(self.catalog, expected) if ok])
@@ -369,6 +377,40 @@ def testFiniteRaDec(self):
369377

370378
self.check([False, False, True, True, True])
371379

380+
def testCullFromMaskedRegion(self):
381+
# Test that objects whose centroids land on specified mask(s) are
382+
# culled.
383+
maskNames = ["NO_DATA", "BLAH"]
384+
noDataPoints = [[0, 0], [3, 2]]
385+
self.exposure = lsst.afw.image.ExposureF(5, 5)
386+
mask = self.exposure.mask
387+
for maskName in maskNames:
388+
if maskName not in mask.getMaskPlaneDict():
389+
mask.addMaskPlane(maskName)
390+
num = 5
391+
for _ in range(num):
392+
self.catalog.addNew()
393+
self.catalog[self.xCol][:] = 5.0
394+
self.catalog[self.yCol][:] = 5.0
395+
396+
# Set first two entries in catalog to land maskNames region.
397+
for i, noDataPoint in enumerate(noDataPoints):
398+
# Flip x & y for numpy array convention.
399+
mask.array[noDataPoint[1]][noDataPoint[0]] = mask.getPlaneBitMask(
400+
maskNames[min(i, len(maskNames) - 1)]
401+
)
402+
self.catalog[self.xCol][i] = noDataPoint[0]
403+
self.catalog[self.yCol][i] = noDataPoint[1]
404+
405+
self.config.doCullFromMaskedRegion = True
406+
self.config.cullFromMaskedRegion.xColName = self.xCol
407+
self.config.cullFromMaskedRegion.yColName = self.yCol
408+
self.config.cullFromMaskedRegion.badMaskNames = maskNames
409+
self.check([False, False, True, True, True])
410+
# Reset config back to False and None for other tests.
411+
self.config.doCullFromMaskedRegion = False
412+
self.exposure = None
413+
372414

373415
class TestBaseSourceSelector(lsst.utils.tests.TestCase):
374416
"""Test the API of the Abstract Base Class with a trivial example."""

0 commit comments

Comments
 (0)