Skip to content

Commit

Permalink
Fixed offAxis coeff for Danish. Catch GalsimFFT error.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcrenshaw committed Mar 21, 2024
1 parent e668f09 commit bc3636e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
45 changes: 32 additions & 13 deletions python/lsst/ts/wep/estimation/danish.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import danish
import numpy as np
from galsim import GalSimFFTSizeError
from lsst.ts.wep import Image, ImageMapper, Instrument
from lsst.ts.wep.estimation.wfAlgorithm import WfAlgorithm
from scipy.ndimage import binary_erosion
Expand Down Expand Up @@ -101,6 +102,7 @@ def history(self) -> dict:
- "zkResid" - the Zernike coefficients fit to the donut
- "zkSum" - zkResid + the intrinsic Zernikes
- "model" - the final forward-modeled donut image
- "GalSimFFTSizeError" - whether this was hit during least_squares
Note the units for all Zernikes are in meters, and all Zernikes start
with Noll index 4.
Expand Down Expand Up @@ -150,6 +152,7 @@ def _estimateSingleZk(
*image.fieldAngle,
image.defocalType,
image.bandLabel,
jmaxIntrinsic=jmax,
return4Up=False,
)
size = max(zkStart.size, offAxisCoeff.size)
Expand Down Expand Up @@ -189,28 +192,43 @@ def _estimateSingleZk(
x0 = [0.0, 0.0, 1.0] + [0.0] * (jmax - 3)

# Use scipy to optimize the parameters
result = least_squares(
model.chi,
jac=model.jac,
x0=x0,
args=(img, backgroundStd**2),
**self.lstsqKwargs,
)
try:
result = least_squares(
model.chi,
jac=model.jac,
x0=x0,
args=(img, backgroundStd**2),
**self.lstsqKwargs,
)
result = dict(result)

# Unpack the parameters
dx, dy, fwhm, *zkResid = result["x"]
zkResid = np.array(zkResid)

# Add the starting zernikes back into the result
zkSum = zkResid + zkStart[4:]

# Flag that we didn't hit GalSimFFTSizeError
galSimFFTSizeError = False

# Unpack the parameters
dx, dy, fwhm, *zkResid = result.x
zkResid = np.array(zkResid)
# Sometimes this happens with Danish :(
except GalSimFFTSizeError:
# Fill dummy objects
result = None
zkResid = np.full(jmax - 3, np.nan)
zkSum = np.full(jmax - 3, np.nan)

# Add the starting zernikes back into the result
zkSum = zkResid + zkStart[4:]
# Flag the error
galSimFFTSizeError = True

if saveHistory:
# Save the history
hist = {
"image": img.copy(),
"variance": backgroundStd**2,
"zkStart": zkStart.copy(),
"lstsqResult": dict(result),
"lstsqResult": result,
"zkResid": zkResid.copy(),
"zkSum": zkSum.copy(),
"model": model.model(
Expand All @@ -221,6 +239,7 @@ def _estimateSingleZk(
sky_level=backgroundStd,
flux=img.sum(),
),
"GalSimFFTSizeError": galSimFFTSizeError,
}

else:
Expand Down
2 changes: 1 addition & 1 deletion tests/task/test_calcZernikesDanishTaskCwfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def testEstimateCornerZernikes(self):
# Make sure the total rms error is less than 0.35 microns off
# from the OPD truth as a sanity check
self.assertLess(
np.sqrt(np.sum(np.square(zernCoeffAvgR40 - trueZernCoeffR40))), 0.39
np.sqrt(np.sum(np.square(zernCoeffAvgR40 - trueZernCoeffR40))), 0.35
)

def testGetCombinedZernikes(self):
Expand Down

0 comments on commit bc3636e

Please sign in to comment.