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
PCAobject.mode – reconstruct from this single component, or
Nonefor 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
PCAobjects.pca_imag – the fitted
PCAobjects.n_mode – reconstruct from this single component, or
Nonefor 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_cmptPCA components to complex frequency-dependent data (real and imaginary parts fitted independently).- Parameters:
- Returns:
the fitted (smooth) data; or
(fit, mix, inv_trans)whenreturn_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_cmptGMCA components to a real-valued array.Requires the optional
pyGMCAandlibwisepackages.- 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 degreen, evaluated atx.- 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
yagainstx.- 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 atx, and either the full model covariance (full_cov) or the per-coefficient standard deviations.- Return type:
- 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 degreedeg(xshould lie in[0, 1]).- Returns:
(coeffs, model, cov)– seepoly_fit().- Return type:
- 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-positivey.output_parameters (
bool) – also return the fitted coefficients.
- Returns:
the model evaluated at
x; or(model, coeffs)whenoutput_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
almat 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