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 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..7981e683 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,32 @@ 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): + """ + A noisy image; with the right seed this triggered + a bug that we fixed + """ + # 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'])