Skip to content

Commit

Permalink
REF remove pixel weighting for now
Browse files Browse the repository at this point in the history
  • Loading branch information
beckermr committed Jun 30, 2022
1 parent d54011c commit 5a25fe8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 66 deletions.
2 changes: 0 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

- Added `fwhm_smooth` keyword to pre-PSF moments routines to allow for extra
smoothing of the profile before the moments are measured.
- Added `use_pix_weight` keyword to pre-PSF moments routines to enable inverse
variance pixel weighting.
- Added caching of FFTs in metacal and pre-PSF moment rountines.


Expand Down
45 changes: 11 additions & 34 deletions ngmix/prepsfmom.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,15 @@ class PrePSFMom(object):
If non-zero, this optional applies additional Gaussian smoothing to the
object before computing the moments. Typically a non-zero value results
in less shape noise.
use_pix_weight : bool, optional
If `True`, this option applies inverse pixel variance weighting in Fourier
space when computing the moments. Typically this optional results in poorer
performance, but for round PSFs, it results in similar performance as
`fwhm_smooth`.
"""
def __init__(
self, fwhm, kernel, pad_factor=4, ap_rad=1.5,
fwhm_smooth=0, use_pix_weight=False
self, fwhm, kernel, pad_factor=4, ap_rad=1.5, fwhm_smooth=0
):
self.fwhm = fwhm
self.pad_factor = pad_factor
self.kernel = kernel
self.ap_rad = ap_rad
self.fwhm_smooth = fwhm_smooth
self.use_pix_weight = use_pix_weight
if self.kernel == "ksigma":
self.kind = "ksigma"
elif self.kernel in ["gauss", "pgauss"]:
Expand Down Expand Up @@ -170,7 +163,6 @@ def _meas(self, obs, psf_obs, return_kernels):
mom, mom_cov = _measure_moments_fft(
kim, kpsf_im, tot_var, eff_pad_factor, kernels,
im_row - psf_im_row, im_col - psf_im_col,
use_pix_weight=self.use_pix_weight,
)
res = make_mom_result(mom, mom_cov)
if res['flags'] != 0:
Expand Down Expand Up @@ -214,18 +206,13 @@ class KSigmaMom(PrePSFMom):
If non-zero, this optional applies additional Gaussian smoothing to the
object before computing the moments. Typically a non-zero value results
in less shape noise.
use_pix_weight : bool, optional
If `True`, this option applies inverse pixel variance weighting in Fourier
space when computing the moments. Typically this optional results in poorer
performance, but for round PSFs, it results in similar performance as
`fwhm_smooth`.
"""
def __init__(
self, fwhm, pad_factor=4, ap_rad=1.5, fwhm_smooth=0, use_pix_weight=False,
self, fwhm, pad_factor=4, ap_rad=1.5, fwhm_smooth=0
):
super().__init__(
fwhm, 'ksigma', pad_factor=pad_factor, ap_rad=ap_rad,
fwhm_smooth=fwhm_smooth, use_pix_weight=use_pix_weight,
fwhm_smooth=fwhm_smooth,
)


Expand All @@ -252,18 +239,13 @@ class PGaussMom(PrePSFMom):
If non-zero, this optional applies additional Gaussian smoothing to the
object before computing the moments. Typically a non-zero value results
in less shape noise.
use_pix_weight : bool, optional
If `True`, this option applies inverse pixel variance weighting in Fourier
space when computing the moments. Typically this optional results in poorer
performance, but for round PSFs, it results in similar performance as
`fwhm_smooth`.
"""
def __init__(
self, fwhm, pad_factor=4, ap_rad=1.5, fwhm_smooth=0, use_pix_weight=False,
self, fwhm, pad_factor=4, ap_rad=1.5, fwhm_smooth=0,
):
super().__init__(
fwhm, 'pgauss', pad_factor=pad_factor, ap_rad=ap_rad,
fwhm_smooth=fwhm_smooth, use_pix_weight=use_pix_weight,
fwhm_smooth=fwhm_smooth,
)


Expand All @@ -272,7 +254,7 @@ def __init__(


def _measure_moments_fft(
kim, kpsf_im, tot_var, eff_pad_factor, kernels, drow, dcol, use_pix_weight=False,
kim, kpsf_im, tot_var, eff_pad_factor, kernels, drow, dcol,
):
# we only need to do things where the kernel is non-zero
# this saves a bunch of CPU cycles
Expand Down Expand Up @@ -309,15 +291,10 @@ def _measure_moments_fft(
fkp = kernels["fkp"]
fkc = kernels["fkc"]

if use_pix_weight:
wgt = (kpsf_im * np.conj(kpsf_im)).real
else:
wgt = 1.0

mf = np.sum((kim * fkf * wgt).real) * df2
mr = np.sum((kim * fkr * wgt).real) * df2
mp = np.sum((kim * fkp * wgt).real) * df2
mc = np.sum((kim * fkc * wgt).real) * df2
mf = np.sum((kim * fkf).real) * df2
mr = np.sum((kim * fkr).real) * df2
mp = np.sum((kim * fkp).real) * df2
mc = np.sum((kim * fkc).real) * df2

# build a covariance matrix of the moments
# here we assume each Fourier mode is independent and sum the variances
Expand All @@ -331,7 +308,7 @@ def _measure_moments_fft(
m_cov[1, 1] = 1
tot_var *= eff_pad_factor**2
tot_var_df4 = tot_var * df4
psf_kerns_fac = wgt / kpsf_im
psf_kerns_fac = 1 / kpsf_im
kerns = [
fkp * psf_kerns_fac,
fkc * psf_kerns_fac,
Expand Down
42 changes: 12 additions & 30 deletions ngmix/tests/test_prepsfmom.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,10 @@ def test_prepsfmom_gauss(
@pytest.mark.parametrize('image_size', [53])
@pytest.mark.parametrize('pad_factor', [1.5])
@pytest.mark.parametrize('round', [True, False])
@pytest.mark.parametrize(
'fwhm_smooth,use_pix_weight',
[(0, False), (0, True), (1, False)],
)
@pytest.mark.parametrize('fwhm_smooth', [0, 1])
def test_prepsfmom_mn_cov_psf(
pad_factor, image_size, fwhm, psf_fwhm, pixel_scale, snr, mom_fwhm, cls,
use_pix_weight, fwhm_smooth, round,
fwhm_smooth, round,
):
"""Slower test to make sure means and errors are right
w/ tons of monte carlo samples.
Expand Down Expand Up @@ -461,35 +458,20 @@ def test_prepsfmom_mn_cov_psf(
fitter = cls(
fwhm=mom_fwhm,
pad_factor=pad_factor,
use_pix_weight=use_pix_weight,
fwhm_smooth=fwhm_smooth,
)

# get true flux
if use_pix_weight:
im_true = galsim.Convolve([gal, psf]).drawImage(
nx=image_size,
ny=image_size,
wcs=gs_wcs,
method='no_pixel').array
obs = Observation(
image=im_true,
weight=wgt,
jacobian=jac,
psf=Observation(image=psf_im, jacobian=psf_jac),
)
res = fitter.go(obs=obs)
else:
im_true = gal.drawImage(
nx=image_size,
ny=image_size,
wcs=gs_wcs,
method='no_pixel').array
obs = Observation(
image=im_true,
jacobian=jac,
)
res = fitter.go(obs=obs, no_psf=True)
im_true = gal.drawImage(
nx=image_size,
ny=image_size,
wcs=gs_wcs,
method='no_pixel').array
obs = Observation(
image=im_true,
jacobian=jac,
)
res = fitter.go(obs=obs, no_psf=True)
flux_true = res["flux"]
T_true = res["T"]
g1_true = res["e"][0]
Expand Down

0 comments on commit 5a25fe8

Please sign in to comment.