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

Fix SingularMatrix problem in LM algorithm #1319

Merged
merged 2 commits into from
Dec 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion galsim/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,11 @@ def least_squares(fun, x0, args=(), kwargs={}, max_iter=1000, tol=1e-9, lambda_i

# Levenberg-Marquardt adjustment
A = JTJ + lambda_ * np.eye(len(JTJ))
delta_params = np.linalg.solve(A, JTr)
try:
delta_params = np.linalg.solve(A, JTr)
except np.linalg.LinAlgError:
lambda_ *= 2
continue

new_params = params - delta_params
new_residuals = fun(new_params, *args, **kwargs)
Expand Down
Binary file added tests/input/singular.npz
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2915,6 +2915,28 @@ def test_fittedsipwcs(run_slow):
fittedWCS.writeToFitsHeader(header, galsim.BoundsI(0, 192, 0, 192))
assert header['CTYPE1'] == 'RA---ARC-SIP'

@timer
def test_fittedsipwcs_singular():
# This test is in response to a case in imsim where the FittedSIPWCS could hit a
# singular matrix error in the solver.

data = np.load('input/singular.npz')
x = data['x']
y = data['y']
ra = data['ra']
dec = data['dec']

wcs = galsim.FittedSIPWCS(x, y, ra, dec, order=3)

# Mostly the test is that that doesn't throw a SingularMatrix exceptions,
# but let's test for accuracy.
print('wcs = ',wcs)

test_ra, test_dec = wcs.xyToradec(x, y, units='radians')

np.testing.assert_allclose(test_ra, ra, rtol=1.e-6)
np.testing.assert_allclose(test_dec, dec, rtol=1.e-6)


@timer
def test_scamp():
Expand Down
Loading