Skip to content

Commit

Permalink
Update and refine unit tests for HSM shape measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
enourbakhsh committed Dec 13, 2023
1 parent 0487ced commit 3aa5e9d
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions tests/test_hsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,39 @@ def runMeasurement(self, algorithmName, imageid, x, y, v):

# perform the shape measurement
msConfig = base.SingleFrameMeasurementConfig()
alg = base.SingleFramePlugin.registry[algorithmName].PluginClass.AlgClass
control = base.SingleFramePlugin.registry[algorithmName].PluginClass.ConfigClass().makeControl()
msConfig.algorithms.names = [algorithmName]
# Note: It is essential to remove the floating point part of the position for the
msConfig.plugins.names |= [algorithmName]
control = msConfig.plugins[algorithmName]
alg = base.SingleFramePlugin.registry[algorithmName].PluginClass
# NOTE: It is essential to remove the floating point part of the position for the
# Algorithm._apply. Otherwise, when the PSF is realised it will have been warped
# to account for the sub-pixel offset and we won't get *exactly* this PSF.
plugin, table = makePluginAndCat(alg, algorithmName, control, centroid="centroid")
plugin, table = makePluginAndCat(alg, algorithmName, control, centroid="centroid", metadata=True)
center = geom.Point2D(int(x), int(y)) + geom.Extent2D(self.offset + geom.Extent2I(self.xy0))
source = table.makeRecord()
source.set("centroid_x", center.getX())
source.set("centroid_y", center.getY())
source.setFootprint(afwDetection.Footprint(afwGeom.SpanSet(exposure.getBBox(afwImage.PARENT))))
plugin.measure(source, exposure)

return source
# Get the trace radius of the PSF and GalSim images to use in the
# EstimateShear call.
bbox = source.getFootprint().getBBox()
bounds = galsim.bounds.BoundsI(bbox.getMinX(), bbox.getMaxX(), bbox.getMinY(), bbox.getMaxY())
image = galsim.Image(exposure.image[bbox].array, bounds=bounds, copy=False)
psf = galsim.Image(psfImg.array, copy=False)

# Retrieve the measurement "type" that Galsim outputs after estimation.
# NOTE: not passing weight, badpix, sky_var, and some guess parameters
# as the objective is solely to deduce the `meas_type` for this setup.
postEstimationMeasType = galsim.hsm.EstimateShear(
gal_image=image,
PSF_image=psf,
shear_est=control.shearType,
guess_centroid=galsim.PositionD(center.getX(), center.getY()),
strict=False,
).meas_type

return source, alg.measTypeSymbol, postEstimationMeasType

def testHsmShape(self):
"""Test that we can instantiate and play with a measureShape"""
Expand All @@ -432,7 +450,16 @@ def testHsmShape(self):
enumerate(file_indices)):
algorithmName = "ext_shapeHSM_HsmShape" + algName[0:1].upper() + algName[1:].lower()

source = self.runMeasurement(algorithmName, imageid, x_centroid[i], y_centroid[i], sky_var[i])
source, preEstimationMeasType, postEstimationMeasType = self.runMeasurement(
algorithmName, imageid, x_centroid[i], y_centroid[i], sky_var[i]
)

# Check consistency with GalSim output
self.assertEqual(
preEstimationMeasType,
postEstimationMeasType,
"The plugin setup is incompatible with GalSim output.",
)

##########################################
# see how we did
Expand Down Expand Up @@ -480,6 +507,16 @@ def testHsmShape(self):

self.assertEqual(nFail, 0, "\n"+msg)

def testValidate(self):
for algName in correction_methods:
with self.assertRaises(pexConfig.FieldValidationError):
algorithmName = "ext_shapeHSM_HsmShape" + algName[0:1].upper() + algName[1:].lower()
msConfig = base.SingleFrameMeasurementConfig()
msConfig.plugins.names |= [algorithmName]
control = msConfig.plugins[algorithmName]
control.shearType = "WRONG"
control.validate()


class PyGaussianPsf(afwDetection.Psf):
# Like afwDetection.GaussianPsf, but handles computeImage exactly instead of
Expand Down

0 comments on commit 3aa5e9d

Please sign in to comment.