Coverage for metadetect / mfrac.py: 11%
31 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-07 01:53 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-07 01:53 -0800
1import ngmix
2import numpy as np
4from .defaults import BMASK_EDGE
7def measure_mfrac(
8 *,
9 mfrac,
10 x,
11 y,
12 box_sizes,
13 obs,
14 fwhm,
15):
16 """Measure a Gaussian-weighted average of an image.
18 This function is meant to be used with images that represent the fraction
19 of single-epoch images that are masked in each pixel of a coadd. It
20 computes a Gaussian-weighted average of the image at a list of locations.
22 Parameters
23 ----------
24 mfrac : np.ndarray
25 The input image with which to compute the weighted averages.
26 x : np.ndarray
27 The input x/col values for the positions at which to compute the
28 weighted average.
29 y : np.ndarray
30 The input y/row values for the positions at which to compute the
31 weighted average.
32 box_sizes : np.ndarray
33 The size of the stamp to use to measure the weighted average. Should be
34 big enough to hold 2 * `fwhm`.
35 obs : ngmix.Observation
36 An observation that holds the weight maps, WCS Jacobian, etc
37 corresponding to `mfrac`.
38 fwhm : float or None
39 The FWHM of the Gaussian aperture in arcseconds. If None, a default
40 of 1.2 is used.
42 Returns
43 -------
44 mfracs : np.ndarray
45 The weighted averages at each input location.
46 """
47 from .detect import CatalogMEDSifier
49 if fwhm is None:
50 fwhm = 1.2
52 obs = obs.copy()
53 obs.set_image(mfrac)
55 gauss_wgt = ngmix.GMixModel(
56 [0, 0, 0, 0, ngmix.moments.fwhm_to_T(fwhm), 1],
57 'gauss',
58 )
59 mbobs = ngmix.MultiBandObsList()
60 obslist = ngmix.ObsList()
61 mbobs.append(obslist)
62 obslist.append(obs)
63 m = CatalogMEDSifier(mbobs, x, y, box_sizes).get_meds(0)
64 mfracs = []
65 for i in range(x.shape[0]):
66 try:
67 if box_sizes[i] > 0:
68 obs = m.get_obs(i, 0)
69 wgt = obs.weight.copy()
70 msk = (obs.bmask & BMASK_EDGE) != 0
71 wgt[msk] = 0
72 wgt[~msk] = 1
73 obs.set_weight(wgt)
75 stats = gauss_wgt.get_weighted_sums(
76 obs,
77 fwhm * 2,
78 )
79 # this is the weighted average in the image using the
80 # Gaussian as the weight.
81 mfracs.append(stats["sums"][5] / stats["wsum"])
82 else:
83 mfracs.append(1.0)
84 except ngmix.GMixFatalError:
85 mfracs.append(1.0)
87 return np.array(mfracs)