Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-32252: Add variance_median and improve test coverage. #197

Merged
merged 1 commit into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly scary comment - is it super sensitive to this? randomSeed=0 doesn't seem like you had to tune it too much, or just got very lucky. Can you make this sound a little less scary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just cut and paste from the original test. I actually don't know the story...

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