Fitting utilities (ps_eor.fitutil)#

Numerical helpers for classical foreground fitting.

ps_eor.fitutil.inv_pca(X, Y, pca, mode)[source]#

Reconstruct data from a fitted PCA model.

Parameters:
  • X – the original data (kept for interface symmetry; unused).

  • Y – the PCA coefficients.

  • pca – the fitted sklearn PCA object.

  • mode – reconstruct from this single component, or None for all.

Returns:

the reconstruction.

Return type:

ndarray

ps_eor.fitutil.inv_pca_complex(X_real, X_imag, Y_real, Y_imag, pca_real, pca_imag, n_mode)[source]#

Reconstruct complex data from independent real / imaginary PCA models.

Parameters:
  • X_real – the original real / imaginary data.

  • X_imag – the original real / imaginary data.

  • Y_real – their PCA coefficients.

  • Y_imag – their PCA coefficients.

  • pca_real – the fitted PCA objects.

  • pca_imag – the fitted PCA objects.

  • n_mode – reconstruct from this single component, or None for all.

Returns:

the complex reconstruction.

Return type:

ndarray

ps_eor.fitutil.alm_pca_fit(alm, n_cmpt, verbose=True, return_mix=False)[source]#

Fit n_cmpt PCA components to complex frequency-dependent data (real and imaginary parts fitted independently).

Parameters:
  • alm – complex data, shape (n_freqs, n_modes).

  • n_cmpt – number of principal components.

  • verbose (bool) – print the explained-variance ratio.

  • return_mix (bool) – also return the component mixing and the inverse transform.

Returns:

the fitted (smooth) data; or (fit, mix, inv_trans) when return_mix.

Return type:

ndarray

ps_eor.fitutil.gmca_fit(X, n_cmpt, mints=0, do_wave_transform=False, do_poly_fit=0, X_n=None)[source]#

Fit n_cmpt GMCA components to a real-valued array.

Requires the optional pyGMCA and libwise packages.

Parameters:
  • X – real data, shape (n_freqs, n_samples).

  • n_cmpt – number of GMCA components.

  • mints – final threshold, in units of the median absolute deviation.

  • do_wave_transform (bool) – wavelet-transform the frequency axis first.

  • do_poly_fit (int) – if > 0, smooth the reconstruction with a Bernstein polynomial of this degree.

  • X_n – optional noise array (kept for interface symmetry; unused).

Returns:

the GMCA reconstruction, same shape as X.

Return type:

ndarray

ps_eor.fitutil.alm_gmca_fit(alm, n_cmpt, alm_n, mints=0, do_wave_transform=False, do_poly_fit=0)[source]#

GMCA fit applied independently to the real and imaginary parts of alm.

Parameters:
  • alm – complex data, shape (n_freqs, n_modes).

  • n_cmpt – number of GMCA components.

  • alm_n – noise array passed through to gmca_fit().

  • mints – see gmca_fit().

  • do_wave_transform – see gmca_fit().

  • do_poly_fit – see gmca_fit().

Returns:

the complex GMCA reconstruction.

Return type:

ndarray

ps_eor.fitutil.bernstein_poly(i, n, x)[source]#

The i-th Bernstein basis polynomial of degree n, evaluated at x.

Returns:

the basis values at x.

Return type:

ndarray

ps_eor.fitutil.poly_fit(x, y, noiserms, deg, min_deg=0, full_cov=False)[source]#

Noise-weighted least-squares polynomial fit of y against x.

Parameters:
  • x – the data points.

  • y – the data points.

  • noiserms – per-point 1-sigma noise, used as inverse-variance weights.

  • deg – maximum polynomial degree.

  • min_deg – minimum degree (drop lower-order terms).

  • full_cov (bool) – return the full model covariance instead of per-coefficient standard deviations.

Returns:

(coeffs, model, cov) – the fitted coefficients, the model at x, and either the full model covariance (full_cov) or the per-coefficient standard deviations.

Return type:

tuple

ps_eor.fitutil.bernstein_fit(x, y, noiserms, deg, full_cov=False)[source]#

Noise-weighted least-squares fit of a Bernstein-polynomial expansion.

Same arguments and return as poly_fit(), using a Bernstein basis of degree deg (x should lie in [0, 1]).

Returns:

(coeffs, model, cov) – see poly_fit().

Return type:

tuple

ps_eor.fitutil.powerlaw_fit(x, y, noiserms, deg, min_deg=0, bernstein=False, offset=True, output_parameters=False)[source]#

Fit a smooth spectrum as a polynomial in log-log space (a generalised power law).

Parameters:
  • x – the data (e.g. frequency and amplitude).

  • y – the data (e.g. frequency and amplitude).

  • noiserms – per-point 1-sigma noise.

  • deg – polynomial degree in log-log space.

  • min_deg – minimum degree.

  • bernstein (bool) – use a Bernstein basis instead of a plain polynomial.

  • offset (bool) – add an offset so the log is defined for non-positive y.

  • output_parameters (bool) – also return the fitted coefficients.

Returns:

the model evaluated at x; or (model, coeffs) when output_parameters.

Return type:

ndarray

ps_eor.fitutil.powerlaw_fit_bernstein(x, y, noiserms, deg)[source]#

A powerlaw_fit() using a Bernstein basis (bernstein=True).

Returns:

the model evaluated at x.

Return type:

ndarray

ps_eor.fitutil.get_fit_fct(fit_type)[source]#

The fit function named by fit_type.

Parameters:

fit_type (str) – one of 'poly', 'bernstein', 'power_poly', 'power_bernstein'.

Returns:

the matching fit function.

Return type:

callable

ps_eor.fitutil.alm_poly_fit(freqs, alm, noiserms, deg, fit_fct=<function powerlaw_fit>, **kargs)[source]#

Fit a spectral model to every column of alm at once.

Parameters:
  • freqs – the spectral axis (e.g. frequency in MHz).

  • alm – data, shape (n_freqs, n_cols).

  • noiserms – per-channel 1-sigma noise.

  • deg – model degree.

  • fit_fct – the fit function to apply (default powerlaw_fit()).

  • **kargs – forwarded to fit_fct.

Returns:

the fitted model, same shape as alm.

Return type:

ndarray

ps_eor.fitutil.fit_cl_cube_poly(cube, du=10, poly_deg=4, log=True)[source]#

Fit a smooth polynomial to a cube’s angular power spectrum P(k_per).

Parameters:
  • cube – the data cube.

  • du – uv-bin width, in wavelengths.

  • poly_deg – polynomial degree.

  • log (bool) – fit in log-log space.

Returns:

(fit_fct, uv_mean, ps, ps_err) – the fitted function of baseline length, the bin centres, and the measured power and its error.

Return type:

tuple