Skip to content

Commit

Permalink
Add variance_median and improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
erykoff committed Oct 19, 2021
1 parent 61eb88f commit 8de8f5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
15 changes: 12 additions & 3 deletions python/lsst/meas/base/noiseReplacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class NoiseReplacerConfig(lsst.pex.config.Config):
'measure': 'Measure clipped mean and variance from the whole image',
'meta': 'Mean = 0, variance = the "BGMEAN" metadata entry',
'variance': "Mean = 0, variance = the image's variance",
'variance_median': "Mean = 0, variance = median(variance plane)"
},
default='measure', optional=False
)
Expand Down Expand Up @@ -195,9 +196,6 @@ def __init__(self, config, exposure, footprints, noiseImage=None, exposureId=Non
# We'll put the noise footprints in a dict heavyNoise = {id:heavyNoiseFootprint}
self.heavyNoise = {}
noisegen = self.getNoiseGenerator(exposure, noiseImage, noiseMeanVar, exposureId=exposureId)
# The noiseGenMean and Std are used by the unit tests
self.noiseGenMean = noisegen.mean
self.noiseGenStd = noisegen.std
if self.log:
self.log.debug('Using noise generator: %s', str(noisegen))
for id in self.heavies:
Expand Down Expand Up @@ -352,6 +350,17 @@ def getNoiseGenerator(self, exposure, noiseImage, noiseMeanVar, exposureId=None)
var = exposure.getMaskedImage().getVariance()
return VariancePlaneNoiseGenerator(var, mean=offset, rand=rand)

if noiseSource == 'variance_median':
if self.log:
self.log.debug('Will draw noise using the median of the variance plane.')
var = exposure.getMaskedImage().getVariance()
s = afwMath.makeStatistics(var, afwMath.MEDIAN)
varMedian = s.getValue(afwMath.MEDIAN)
if self.log:
self.log.debug("Measured from variance: median variance = %g",
varMedian)
return FixedGaussianNoiseGenerator(offset, math.sqrt(varMedian), rand=rand)

# Compute an image-wide clipped variance.
im = exposure.getMaskedImage().getImage()
s = afwMath.makeStatistics(im, afwMath.MEANCLIP | afwMath.STDEVCLIP)
Expand Down
16 changes: 12 additions & 4 deletions tests/test_NoiseReplacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import lsst.afw.table
import lsst.meas.base.tests
import lsst.utils.tests
from lsst.daf.base import PropertyList
from lsst.utils.tests import methodParameters


@lsst.meas.base.register("test_NoiseReplacer")
Expand Down Expand Up @@ -75,16 +77,22 @@ def setUp(self):
family.addChild(140000.0, lsst.geom.Point2D(72.3, 149.1))
family.addChild(90000.0, lsst.geom.Point2D(68.5, 156.9))

def testSingleFrameMeasurement(self):
"""Test noise replacement in single frame measurement.
@methodParameters(noiseSource=['measure', 'variance', 'meta', 'variance_median'],
variance=[1.0, 1.0, 2.0, 1.0])
def testNoiseReplacer(self, noiseSource, variance):
"""Test noise replacement in SFM with ''measure'' mode.
We compare the instFlux inside and outside source Footprints on an
extremely high S/N image.
"""

# We choose a random seed which causes the test to pass.
task = self.makeSingleFrameMeasurementTask("test_NoiseReplacer")
exposure, catalog = self.dataset.realize(1.0, task.schema, randomSeed=0)
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)
task.run(catalog, exposure)
sumVariance = exposure.getMaskedImage().getVariance().getArray().sum()
for record in catalog:
Expand Down

0 comments on commit 8de8f5d

Please sign in to comment.