Skip to content

Commit

Permalink
fix bug when moms give a bad size for guesser and using a random number
Browse files Browse the repository at this point in the history
generator that checks low/high such as (np.random.default_rng)
  • Loading branch information
esheldon committed Sep 13, 2024
1 parent 61bd2c0 commit e4b370a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
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
30 changes: 29 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,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),
}
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 e4b370a

Please sign in to comment.