Skip to content

Commit

Permalink
Merge pull request #230 from esheldon/bdf-guesser
Browse files Browse the repository at this point in the history
add guesser for BDF with psf flux
  • Loading branch information
esheldon authored Oct 14, 2022
2 parents ad8e8ac + 1a28c5f commit ec09804
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
16 changes: 16 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## v2.2.1

### New Features

- Added new guesser BDFPSFFluxGuesser, makes a flux guess
based on the PSF flux

### Performance
- Added O(N) phase shift computation for pre-psf moments

### Misc

- ignore NumbaExperimentalFeatureWarning being spewed by
numba. We can ignore these, as the features are of order
ten years old from the time of writing

## v2.2.0

### new features
Expand Down
5 changes: 5 additions & 0 deletions ngmix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# flake8: noqa

from numba.core.errors import NumbaExperimentalFeatureWarning
import warnings

warnings.simplefilter('ignore', category=NumbaExperimentalFeatureWarning)

from ._version import __version__

from . import util
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.2.0' # noqa
__version__ = '2.2.1' # noqa
57 changes: 55 additions & 2 deletions ngmix/guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,60 @@ def __call__(self, nrand=1, obs=None):
return guess


class BDFPSFFluxGuesser(TPSFFluxGuesser):
"""
Make BDF guesses from the input T, the psf flux and the prior
parameters
----------
T: float
Center for T guesses
prior:
cen, g drawn from this prior
"""

def __init__(self, T, prior):
self.T = T
self.prior = prior
self._id_last = None
self._psf_fluxes = None
self.rng = self.prior.cen_prior.rng

def __call__(self, obs, nrand=1):
"""
center, shape are just distributed around zero
obs: Observation
The observation(s) used for psf fluxes
nrand: int, optional
Number of samples to draw. Default 1
"""
rng = self.prior.cen_prior.rng

fluxes = self._get_psf_fluxes(obs=obs)

guess = self.prior.sample(nrand)

nband = fluxes.size

r = rng.uniform(low=-0.1, high=0.1, size=nrand)
guess[:, 4] = self.T * (1.0 + r)

# fracdev prior
guess[:, 5] = rng.uniform(low=0.4, high=0.6, size=nrand)

for band in range(nband):
r = rng.uniform(low=-0.1, high=0.1, size=nrand)
guess[:, 6 + band] = fluxes[band] * (1.0 + r)

_fix_guess(guess, self.prior)

if nrand == 1:
guess = guess[0, :]

return guess


class BDFGuesser(object):
"""
Make BDF guesses from the input T, flux and prior
Expand Down Expand Up @@ -368,8 +422,7 @@ def __call__(self, nrand=1, obs=None):
r = rng.uniform(low=-0.1, high=0.1, size=nrand)
guess[:, 6 + band] = fluxes[band] * (1.0 + r)

if self.prior is not None:
_fix_guess(guess, self.prior)
_fix_guess(guess, self.prior)

if nrand == 1:
guess = guess[0, :]
Expand Down
17 changes: 11 additions & 6 deletions ngmix/tests/test_guessers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_noprior_guessers_smoke(guesser_type, nband, with_prior):

@pytest.mark.parametrize(
'guesser_type',
['TFluxAndPrior', 'TPSFFluxAndPrior', 'BD', 'BDF', 'Prior'],
['TFluxAndPrior', 'TPSFFluxAndPrior', 'BD', 'BDF', 'BDFPSFlux', 'Prior'],
)
@pytest.mark.parametrize('nband', [None, 1, 2])
def test_prior_guessers_smoke(guesser_type, nband):
Expand All @@ -113,7 +113,7 @@ def test_prior_guessers_smoke(guesser_type, nband):

# this always gets coverage
assert guesser_type in [
'TFluxAndPrior', 'TPSFFluxAndPrior', 'BD', 'BDF', 'Prior',
'TFluxAndPrior', 'TPSFFluxAndPrior', 'BD', 'BDF', 'BDFPSFlux', 'Prior',
]

T_center = 0.001
Expand Down Expand Up @@ -150,17 +150,22 @@ def test_prior_guessers_smoke(guesser_type, nband):
T=T_center, flux=flux_center, prior=prior,
)
npars = 7 + nband_use
elif guesser_type == 'BDF':
elif guesser_type in ('BDF', 'BDFPSFlux'):
prior = ngmix.joint_prior.PriorBDFSep(
cen_prior=ngmix.priors.CenPrior(0.0, 0.0, 0.1, 0.1, rng=rng),
g_prior=ngmix.priors.GPriorBA(0.3, rng=rng),
fracdev_prior=ngmix.priors.Normal(0.5, 0.1, rng=rng),
T_prior=ngmix.priors.FlatPrior(-1.0, 1.e5, rng=rng),
F_prior=[ngmix.priors.FlatPrior(-1.0, 1.e5, rng=rng)]*nband_use,
)
guesser = guessers.BDFGuesser(
T=T_center, flux=flux_center, prior=prior,
)
if guesser_type == 'BDF':
guesser = guessers.BDFGuesser(
T=T_center, flux=flux_center, prior=prior,
)
else:
guesser = guessers.BDFPSFFluxGuesser(
T=T_center, prior=prior,
)
npars = 6 + nband_use

guess = guesser(obs=data['obs'])
Expand Down

0 comments on commit ec09804

Please sign in to comment.