Skip to content

Commit

Permalink
Merge pull request #275 from lsst/tickets/DM-44246
Browse files Browse the repository at this point in the history
DM-44246: Add functionality to use an external footprint set for NoiseReplacer in SingleFrameMeasurementTask.
  • Loading branch information
erykoff authored May 8, 2024
2 parents 62aa8f1 + 56c0159 commit 197f127
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
18 changes: 18 additions & 0 deletions python/lsst/meas/base/baseMeasurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,21 @@ def doMeasurementN(self, plugin, measCat, *args, **kwds):
self.log.getChild(plugin.name).warning(
"Exception in %s.measureN on records %s-%s: %s",
plugin.name, measCat[0].getId(), measCat[-1].getId(), error)

@staticmethod
def getFootprintsFromCatalog(catalog):
"""Get a set of footprints from a catalog, keyed by id.
Parameters
----------
catalog : `lsst.afw.table.SourceCatalog`
Catalog with `lsst.afw.detection.Footprint`s attached.
Returns
-------
footprints : `dict` [`int`: (`int`, `lsst.afw.detection.Footprint`)]
Dictionary of footprint, keyed by id number, with a tuple of
the parent id and footprint.
"""
return {measRecord.getId(): (measRecord.getParent(), measRecord.getFootprint())
for measRecord in catalog}
18 changes: 15 additions & 3 deletions python/lsst/meas/base/sfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,16 @@ def __init__(self, schema, algMetadata=None, **kwds):
self.doBlendedness = False

@timeMethod
def run(self, measCat, exposure, noiseImage=None, exposureId=None, beginOrder=None, endOrder=None):
def run(
self,
measCat,
exposure,
noiseImage=None,
exposureId=None,
beginOrder=None,
endOrder=None,
footprints=None,
):
r"""Run single frame measurement over an exposure and source catalog.
Parameters
Expand All @@ -233,10 +242,13 @@ def run(self, measCat, exposure, noiseImage=None, exposureId=None, beginOrder=No
Final execution order (exclusive): measurements with
``executionOrder >= endOrder`` are not executed. `None` for no
limit.
footprints : `dict` {`int`: `lsst.afw.detection.Footprint`}, optional
List of footprints to use for noise replacement. If this is not
supplied then the footprints from the measCat are used.
"""
assert measCat.getSchema().contains(self.schema)
footprints = {measRecord.getId(): (measRecord.getParent(), measRecord.getFootprint())
for measRecord in measCat}
if footprints is None:
footprints = self.getFootprintsFromCatalog(measCat)

# noiseReplacer is used to fill the footprints with noise and save
# heavy footprints of the source pixels so that they can be restored
Expand Down
23 changes: 23 additions & 0 deletions tests/test_NoiseReplacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ def testNoiseReplacer(self, noiseSource, variance):
# fail (indeed, 67% should)
self.assertLess(record.get("test_NoiseReplacer_outside"), np.sqrt(sumVariance))

@methodParameters(noiseSource=['measure', 'variance', 'meta', 'variance_median'],
variance=[1.0, 1.0, 2.0, 1.0])
def testNoiseReplacerExternalFootprints(self, noiseSource, variance):
# We choose a random seed which causes the test to pass.
task = self.makeSingleFrameMeasurementTask("test_NoiseReplacer")
task.config.noiseReplacer.noiseSource = noiseSource
exposure, catalog = self.dataset.realize(variance, task.schema, randomSeed=0)
if noiseSource == 'meta':
md = PropertyList()
md['BGMEAN'] = variance
exposure.setMetadata(md)
footprints = task.getFootprintsFromCatalog(catalog)
task.run(catalog, exposure, footprints=footprints)
sumVariance = exposure.variance.array.sum()
for record in catalog:
self.assertFloatsAlmostEqual(record.get("test_NoiseReplacer_inside"),
record.get("truth_instFlux"), rtol=1E-3)

# N.B. Next line checks that a random value is correct to a
# statistical 1-sigma prediction; some RNG seeds may cause it to
# fail (indeed, 67% should)
self.assertLess(record.get("test_NoiseReplacer_outside"), np.sqrt(sumVariance))

def tearDown(self):
del self.bbox
del self.dataset
Expand Down

0 comments on commit 197f127

Please sign in to comment.