Skip to content

Commit

Permalink
Merge pull request #242 from esheldon/guesser-fix
Browse files Browse the repository at this point in the history
Guesser fix
  • Loading branch information
esheldon authored Sep 13, 2024
2 parents 61bd2c0 + b5f6e9a commit 180a27e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion ngmix/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.3.1' # noqa
__version__ = '2.3.2' # noqa
3 changes: 3 additions & 0 deletions ngmix/guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 30 additions & 1 deletion ngmix/tests/_sims.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
}
15 changes: 14 additions & 1 deletion ngmix/tests/test_guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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'])

0 comments on commit 180a27e

Please sign in to comment.