statistics — 2D Gaussian fitting and summary stats

statistics provides two utilities used for quick map characterisation.

2D Gaussian fitting

Used to measure the effective beam or PSF of a filtered or beam-convolved map by fitting a 2D Gaussian to a stacked source profile or a correlation function peak. The typical workflow is:

from foregrounds_diffusion.statistics import fitgaussian, fitting_func

# Fit a Gaussian to a 2D thumbnail (e.g. a stacked tSZ cluster profile)
params = fitgaussian(thumbnail)
# params: (height, center_x, center_y, width_x, width_y)

# Or use fitting_func for bounds enforcement and optional residual return
tmap_fit = fitting_func(thumbnail, return_fit=1)   # returns fitted image
residual  = fitting_func(thumbnail, return_fit=0)  # returns data - fit

The initial parameter guess is estimated from image moments via moments(), so no manual initialisation is required.

Summary statistics

stats() reports the mean, std, min, and max of a map, optionally excluding zero-valued (masked) pixels:

from foregrounds_diffusion.statistics import stats

mean, std, vmin, vmax = stats(cib_patch, log=True, mask_zero=True)

API

2D Gaussian fitting and summary statistics for flat-sky maps.

This module provides two distinct capabilities:

2D Gaussian fitting

Used to characterise the angular resolution of beam-convolved or filtered maps by fitting a 2D Gaussian to the stacked profile of point sources or the central peak of a two-point correlation function.

gaussian() — parameterised 2D Gaussian (height, centre, widths, angle). moments() — estimate Gaussian parameters from image moments. fitgaussian() — fit a 2D Gaussian to a 2D array via scipy.optimize. fitting_func() — convenience wrapper that fits and optionally returns the residual map; also enforces parameter bounds.

Summary statistics

stats() — compute mean, standard deviation, minimum, and maximum of a

flat-sky map, optionally masking zero pixels. Used for quick sanity checks on patch statistics before and after normalisation.

foregrounds_diffusion.statistics.gaussian(height, center_x, center_y, width_x, width_y)[source]

Return a 2D Gaussian function with the given parameters.

Parameters:
  • height (float) – Peak amplitude.

  • center_x, center_y (float) – Centre coordinates.

  • width_x, width_y (float) – Standard deviations along x and y.

Returns:

callable – Function f(x, y) evaluating the Gaussian at (x, y).

foregrounds_diffusion.statistics.moments(data)[source]

Estimate 2D Gaussian parameters from image moments.

Parameters:

data (ndarray, shape (ny, nx)) – 2D image.

Returns:

tuple(height, x, y, width_x, width_y)

foregrounds_diffusion.statistics.fitgaussian(data)[source]

Fit a 2D Gaussian to an image using least squares.

Parameters:

data (ndarray, shape (ny, nx)) – 2D image.

Returns:

tuple(height, x, y, width_x, width_y) — best-fit parameters.

foregrounds_diffusion.statistics.fitting_func(p, p0, xgrid, ygrid, tmap, lbounds=None, ubounds=None, fixed=None, return_fit=0)[source]

Evaluate or fit a 2D Gaussian model on a pixel grid.

Used internally by get_mask_using_gaussian_fitting().

Parameters:
  • p (array_like) – Current parameter vector [baseline, amp, x_cen, y_cen, width, ...].

  • p0 (array_like) – Reference parameter vector (used to restore fixed parameters).

  • xgrid, ygrid (ndarray) – Pixel coordinate grids.

  • tmap (ndarray) – Target map (used as fall-back return on bound violations).

  • lbounds, ubounds (array_like, optional) – Lower and upper parameter bounds.

  • fixed (array_like of int, optional) – Indices of parameters to hold fixed at p0.

  • return_fit (int) – If 1, return the model image; if 0, return the residual vector.

Returns:

ndarray – Residual vector (when return_fit=0) or model image (when return_fit=1).

foregrounds_diffusion.statistics.stats(maps)[source]

Return (min, max, mean, std) of an array.

Parameters:

maps (ndarray) – Input array.

Returns:

tuple of float(min, max, mean, std)