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

Guesser fix #242

Merged
merged 3 commits into from
Sep 13, 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
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'])
Loading