Skip to content

Commit

Permalink
Use new numpy rng API
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Jan 29, 2025
1 parent 7c72625 commit b346023
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions tests/test_objectSizeStarSelector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,33 @@
fluxField = "base_GaussianFlux_instFlux"


def addGoodSource(sourceCat, num=0):
"""Insert a likely-good source into the catalog.
Parameters
----------
sourceCat : `lsst.afw.table.SourceCatalog`
The source catalog for which a "good" source is to be added for testing.
num : `float` or `int`
A number that is added to various values to distinguish them in catalogs with multiple objects.
"""
sourceCat.addNew()
sourceCat["coord_ra"][-1] = 1.0 + num
sourceCat["coord_dec"][-1] = 2.0 + num
# Add some variability to the fluxes to form a cluster with non-finite "width" in the mag-size plane
fluxFactor = np.random.uniform(low=0.98, high=1.02)
sourceCat[fluxField][-1] = 100.0*fluxFactor # We set fluxMin = 50 in setUp
sourceCat[fluxField + "Err"][-1] = 1.0
# Add some variability to the shapes to form a cluster with non-finite "width" in the mag-size plane
widthFactor = np.random.uniform(low=0.98, high=1.02)
sourceCat["truth_xx"][-1] = 3.0*widthFactor
sourceCat["truth_yy"][-1] = 3.0*widthFactor
sourceCat["truth_xy"][-1] = 1.0


class TestObjectSizeSourceSelector(lsst.utils.tests.TestCase):

def addGoodSource(self, sourceCat, num=0):
"""Insert a likely-good source into the catalog.
Parameters
----------
sourceCat : `lsst.afw.table.SourceCatalog`
The source catalog for which a "good" source is to be added for testing.
num : `float` or `int`
A number that is added to various values to distinguish them in catalogs with multiple objects.
"""
sourceCat.addNew()
sourceCat["coord_ra"][-1] = 1.0 + num
sourceCat["coord_dec"][-1] = 2.0 + num
# Add some variability to the fluxes to form a cluster with non-finite "width" in the mag-size plane
fluxFactor = self.rng.uniform(low=0.98, high=1.02)
sourceCat[fluxField][-1] = 100.0*fluxFactor # We set fluxMin = 50 in setUp
sourceCat[fluxField + "Err"][-1] = 1.0
# Add some variability to the shapes to form a cluster with non-finite "width" in the mag-size plane
widthFactor = self.rng.uniform(low=0.98, high=1.02)
sourceCat["truth_xx"][-1] = 3.0*widthFactor
sourceCat["truth_yy"][-1] = 3.0*widthFactor
sourceCat["truth_xy"][-1] = 1.0

def setUp(self):
np.random.seed(100)
self.rng = np.random.Generator(np.random.MT19937(1))

self.sourceSelector = sourceSelector.sourceSelectorRegistry["objectSize"]()
self.badFlags = self.sourceSelector.config.badFlags
Expand All @@ -77,14 +76,14 @@ def tearDown(self):

def testSelectSourcesGood(self):
for i in range(5):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)
result = self.sourceSelector.selectSources(self.sourceCat)
for src in self.sourceCat["id"]:
self.assertIn(src, self.sourceCat[result.selected]["id"])

def testSelectSourcesIndividualBadFlags(self):
for i in range(len(self.badFlags)):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)
for i in range(len(self.badFlags)):
for j, flag in enumerate(self.badFlags):
self.sourceCat[j].set(flag, True) if i == j else self.sourceCat[j].set(flag, False)
Expand All @@ -94,14 +93,14 @@ def testSelectSourcesIndividualBadFlags(self):

def testSelectSourcesAllBadFlags(self):
for i, flag in enumerate(self.badFlags):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)
self.sourceCat[i].set(flag, True)
with self.assertRaises(RuntimeError):
self.sourceSelector.selectSources(self.sourceCat)

def testSelectSourcesSignalToNoiseCuts(self):
for i in range(10):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)

self.sourceCat[fluxField + "Err"][0] = self.sourceCat[fluxField][0]/20.0
self.sourceCat[fluxField + "Err"][1] = self.sourceCat[fluxField][1]/500.0
Expand All @@ -115,21 +114,21 @@ def testSelectSourcesSignalToNoiseCuts(self):

def testSelectSourcesNoSignalToNoiseCut(self):
for i in range(5):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)
self.sourceCat[fluxField + "Err"][0] = self.sourceCat[fluxField][0]*1e+9 # S/N ~1e-9
self.sourceSelector.config.signalToNoiseMin = 0
result = self.sourceSelector.run(self.sourceCat)
self.assertIn(self.sourceCat[0]["id"], result.sourceCat["id"])

def testSelectSourcesFluxCuts(self):
for i in range(10):
addGoodSource(self.sourceCat, i)
self.addGoodSource(self.sourceCat, i)

Check failure on line 125 in tests/test_objectSizeStarSelector.py

View workflow job for this annotation

GitHub Actions / call-workflow / lint

E111

indentation is not a multiple of 4

self.sourceSelector.config.doSignalToNoiseLimit = False
self.sourceSelector.config.doFluxLimit = True
self.sourceSelector.config.fluxMin = 98.0 # Just outside of range allowed in addGoodSource()
self.sourceSelector.config.fluxMin = 98.0 # Just outside of range allowed inself.addGoodSource()
self.sourceCat[fluxField][0] = 97.9
self.sourceSelector.config.fluxMax = 101.3 # Just outside of range allowed in addGoodSource()
self.sourceSelector.config.fluxMax = 101.3 # Just outside of range allowed inself.addGoodSource()
self.sourceCat[fluxField][2] = 101.4
result = self.sourceSelector.run(self.sourceCat)
self.assertNotIn(self.sourceCat[0]["id"], result.sourceCat["id"])
Expand Down

0 comments on commit b346023

Please sign in to comment.