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

ENH add moment weight sums to output #219

Merged
merged 17 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added `fwhm_smooth` keyword to pre-PSF moments routines to allow for extra
smoothing of the profile before the moments are measured.
- Added caching of FFTs in metacal and pre-PSF moment rountines.
- Added moments weight function normalization.


## v2.0.6
Expand Down
5 changes: 4 additions & 1 deletion mdet_tests/test_mdet_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ def test_mdet_regression(fname, write=False):
else:
old_data = fitsio.read(fname)
for col in old_data.dtype.names:
if ('psf_' in col or 'ratio' in col) and '1.3.9' in fname:
if (
('psf_' in col or 'ratio' in col)
and ('1.3.9' in fname or '2.0' in fname)
):
rtol = 1.0e-4
atol = 1.0e-4
else:
Expand Down
6 changes: 2 additions & 4 deletions ngmix/gmix/gmix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,15 +1239,13 @@ def get_weighted_moments_stats(ares):

res = {}
for n in ares.dtype.names:
if n == "sums":
res[n] = ares[n].copy()
elif n == "sums_cov":
if n in ["sums", "sums_cov"]:
res[n] = ares[n].copy()
else:
res[n] = ares[n]

res.update(
moments.make_mom_result(res["sums"], res["sums_cov"])
moments.make_mom_result(res["sums"], res["sums_cov"], res["wsum"])
beckermr marked this conversation as resolved.
Show resolved Hide resolved
)

return res
Expand Down
6 changes: 5 additions & 1 deletion ngmix/moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def g2mom(g1, g2, T):
return Irr, Irc, Icc


def make_mom_result(mom, mom_cov):
def make_mom_result(mom, mom_cov, mom_norm=None):
beckermr marked this conversation as resolved.
Show resolved Hide resolved
"""Make a fitting results dict from a set of moments.

Parameters
Expand All @@ -358,6 +358,9 @@ def make_mom_result(mom, mom_cov):
The array of moments in the order [Mv, Mu, M1, M2, MT, MF].
mom_cov : np.ndarray
The array of moment covariances.
mom_norm : float, optional
The sum of the moment weight function itself. This is added to the output data.
The default of None puts in NaN.

Returns
-------
Expand Down Expand Up @@ -389,6 +392,7 @@ def make_mom_result(mom, mom_cov):
res["flux"] = mom[mf_ind]
res["mom"] = mom
res["mom_cov"] = mom_cov
res["mom_norm"] = mom_norm if mom_norm is not None else np.nan
beckermr marked this conversation as resolved.
Show resolved Hide resolved
res["flux_flags"] = 0
res["flux_flagstr"] = ""
res["T_flags"] = 0
Expand Down
9 changes: 6 additions & 3 deletions ngmix/prepsfmom.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ def _meas(self, obs, psf_obs, return_kernels):
tot_var = np.sum(1.0 / obs.weight[msk])

# run the actual measurements and return
mom, mom_cov = _measure_moments_fft(
mom, mom_cov, mom_norm = _measure_moments_fft(
kim, kpsf_im, tot_var, eff_pad_factor, kernels,
im_row - psf_im_row, im_col - psf_im_col,
)
res = make_mom_result(mom, mom_cov)
res = make_mom_result(mom, mom_cov, mom_norm)
beckermr marked this conversation as resolved.
Show resolved Hide resolved
if res['flags'] != 0:
logger.debug("pre-psf moments failed: %s" % res['flagstr'])

Expand Down Expand Up @@ -291,6 +291,7 @@ def _measure_moments_fft(
fkp = kernels["fkp"]
fkc = kernels["fkc"]

mom_norm = np.sum(kernels["fk00"].real) * df2
mf = np.sum((kim * fkf).real) * df2
mr = np.sum((kim * fkr).real) * df2
mp = np.sum((kim * fkp).real) * df2
Expand Down Expand Up @@ -324,7 +325,7 @@ def _measure_moments_fft(

mom = np.array([np.nan, np.nan, mp, mc, mr, mf])

return mom, m_cov
return mom, m_cov, mom_norm


@njit
Expand Down Expand Up @@ -580,6 +581,7 @@ def _ksigma_kernels(
fkc=fkc,
msk=msk,
nrm=nrm,
fk00=knrm,
)


Expand Down Expand Up @@ -679,6 +681,7 @@ def _gauss_kernels(
fkc=fkc,
msk=msk,
nrm=nrm,
fk00=knrm,
)


Expand Down
8 changes: 4 additions & 4 deletions ngmix/tests/test_prepsfmom.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ def test_moments_make_mom_result_flags():
for i in range(2, 6):
_mom_cov = mom_cov.copy()
_mom_cov[i, i] = -1
res = make_mom_result(mom, _mom_cov)
res = make_mom_result(mom, _mom_cov, 1)
beckermr marked this conversation as resolved.
Show resolved Hide resolved
assert (res["flags"] & ngmix.flags.NONPOS_VAR) != 0
assert ngmix.flags.NAME_MAP[ngmix.flags.NONPOS_VAR] in res["flagstr"]
if i == 5:
Expand All @@ -782,7 +782,7 @@ def test_moments_make_mom_result_flags():
# neg flux
_mom = mom.copy()
_mom[5] = -1
res = make_mom_result(_mom, mom_cov)
res = make_mom_result(_mom, mom_cov, 1)
beckermr marked this conversation as resolved.
Show resolved Hide resolved
assert (res["flags"] & ngmix.flags.NONPOS_FLUX) != 0
assert ngmix.flags.NAME_MAP[ngmix.flags.NONPOS_FLUX] in res["flagstr"]
assert res["flux_flags"] == 0
Expand All @@ -793,7 +793,7 @@ def test_moments_make_mom_result_flags():
# neg T
_mom = mom.copy()
_mom[4] = -1
res = make_mom_result(_mom, mom_cov)
res = make_mom_result(_mom, mom_cov, 1)
beckermr marked this conversation as resolved.
Show resolved Hide resolved
assert (res["flags"] & ngmix.flags.NONPOS_SIZE) != 0
assert ngmix.flags.NAME_MAP[ngmix.flags.NONPOS_SIZE] in res["flagstr"]
assert res["flux_flags"] == 0
Expand All @@ -806,7 +806,7 @@ def test_moments_make_mom_result_flags():
_mom_cov = mom_cov.copy()
_mom_cov[4, i] = np.nan
_mom_cov[i, 4] = np.nan
res = make_mom_result(mom, _mom_cov)
res = make_mom_result(mom, _mom_cov, 1)
beckermr marked this conversation as resolved.
Show resolved Hide resolved
assert (res["flags"] & ngmix.flags.NONPOS_SHAPE_VAR) != 0
assert ngmix.flags.NAME_MAP[ngmix.flags.NONPOS_SHAPE_VAR] in res["flagstr"]
assert res["flux_flags"] == 0
Expand Down