From e4b370a1900b556415c061966525a47b19e0d839 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Fri, 13 Sep 2024 09:29:45 -0400 Subject: [PATCH 1/3] fix bug when moms give a bad size for guesser and using a random number generator that checks low/high such as (np.random.default_rng) --- ngmix/guessers.py | 3 +++ ngmix/tests/_sims.py | 30 +++++++++++++++++++++++++++++- ngmix/tests/test_guessers.py | 15 ++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/ngmix/guessers.py b/ngmix/guessers.py index 85ac9652..90d1d980 100644 --- a/ngmix/guessers.py +++ b/ngmix/guessers.py @@ -873,6 +873,9 @@ def _get_T_flux_from_moms(self, obs): # deweight assuming true profile is a gaussian T = 1.0/(1/Tmeas - 1/Tweight) flux = res['flux'] * np.pi * (Tweight + T) / area + if T < 0: + T = res['T'] + flux = res['flux'] return T, flux diff --git a/ngmix/tests/_sims.py b/ngmix/tests/_sims.py index d5871436..efb8e84b 100644 --- a/ngmix/tests/_sims.py +++ b/ngmix/tests/_sims.py @@ -1,6 +1,6 @@ import numpy as np -from ngmix import DiagonalJacobian, GMix, GMixModel +from ngmix import DiagonalJacobian, Jacobian, GMix, GMixModel from ngmix import Observation, ObsList, MultiBandObsList PIXEL_SCALE = 0.263 @@ -226,3 +226,31 @@ def get_psf_obs(*, rng, T=TPSF, model="turb", noise=1.0e-6): 'obs': Observation(im, weight=weight, jacobian=jacob), 'gmix': gm, } + + +def get_noisy_obs(rng): + """ + highlight a single pixel + """ + # import fitsio + dim = 25 + noise = 50.559849582916115 + + im = rng.normal(scale=noise, size=(dim, dim)) + + dims = im.shape + cen = (np.array(dims) - 1.0) / 2.0 + + jacob = Jacobian( + row=cen[0], col=cen[1], + dvdrow=-0.15179598030886227, + dvdcol=0.13007044200963258, + dudrow=-0.13014613410130665, + dudcol=-0.15185634442578344, + ) + + noise = im.std() + weight = im*0 + 1/noise**2 + return { + 'obs': Observation(im, weight=weight, jacobian=jacob), + } diff --git a/ngmix/tests/test_guessers.py b/ngmix/tests/test_guessers.py index 91712a59..09cbfa38 100644 --- a/ngmix/tests/test_guessers.py +++ b/ngmix/tests/test_guessers.py @@ -4,7 +4,7 @@ import ngmix from ngmix import guessers from ngmix.gmix import get_coellip_npars -from ._sims import get_model_obs, get_psf_obs +from ._sims import get_model_obs, get_psf_obs, get_noisy_obs from ._priors import get_prior @@ -245,3 +245,16 @@ def test_guessers_errors(): with pytest.raises(ValueError): ngmix.guessers.CoellipPSFGuesser(rng=rng, ngauss=1000) + + +def test_mom_guesser_badsize(): + """ + tests a case where moms used for guess are bad + so a fallback guess is used + """ + rng = np.random.default_rng(354365) + guesser = ngmix.guessers.GMixPSFGuesser( + rng=rng, ngauss=1, guess_from_moms=True, + ) + data = get_noisy_obs(rng=rng) + guesser(data['obs']) From 3c75541ce228f6c922ad2f88a6b4056c6b6f20f7 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Fri, 13 Sep 2024 09:31:44 -0400 Subject: [PATCH 2/3] change log --- CHANGES.md | 8 ++++++++ ngmix/_version.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index edab49c7..f940cd8d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,11 @@ +## v2.3.2 (not yet released) + +## Bug Fixes + + - Fixed bug when moments are used in guesser and size is bad. + Only affected rng such as np.random.default_rng that checks + range for uniform + ## v2.3.1 ### new features diff --git a/ngmix/_version.py b/ngmix/_version.py index 2531adcc..036fc10b 100644 --- a/ngmix/_version.py +++ b/ngmix/_version.py @@ -1 +1 @@ -__version__ = '2.3.1' # noqa +__version__ = '2.3.2' # noqa From b5f6e9a04ade80c5b5c7a468b7a3fc4f42928ea8 Mon Sep 17 00:00:00 2001 From: Erin Sheldon Date: Fri, 13 Sep 2024 09:50:15 -0400 Subject: [PATCH 3/3] fix docs --- ngmix/tests/_sims.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ngmix/tests/_sims.py b/ngmix/tests/_sims.py index efb8e84b..7981e683 100644 --- a/ngmix/tests/_sims.py +++ b/ngmix/tests/_sims.py @@ -230,7 +230,8 @@ def get_psf_obs(*, rng, T=TPSF, model="turb", noise=1.0e-6): def get_noisy_obs(rng): """ - highlight a single pixel + A noisy image; with the right seed this triggered + a bug that we fixed """ # import fitsio dim = 25