sp_validation.pseudo_cl

Pseudo-Cl / harmonic-space estimator primitives for cosmology validation.

Stateless shared primitives for the pseudo-Cl (harmonic-space) estimator, mirroring b_modes.py and rho_tau.py: the orchestrator mixin in sp_validation.cosmo_val.pseudo_cl (and analysis scripts directly) call these free functions. Everything here is pure computation – NaMaster binning, weighted galaxy number-density maps, random-rotation noise debiasing, and the map-/catalog-based pseudo-Cl estimators. Depends on pymaster (NaMaster) and healpy.

The harmonic geometry the estimators use is fixed by nside: lmin = 8, lmax = 2 * nside, b_lmax = lmax - 1. pseudo_cl_geometry returns that triple so the binning and the fields stay in lockstep.

pseudo_cl_geometry(nside)[source]

Return (lmin, lmax, b_lmax) for the pseudo-Cl estimator at nside.

lmax = 2 * nside is the NaMaster band-power ceiling; b_lmax = lmax - 1 is the field/binning lmax. lmin is the fixed low-ell floor.

make_namaster_bin(lmin, lmax, b_lmax, binning, *, ell_step=10, n_ell_bins=32, power=0.5)[source]

Build a NaMaster binning object for one of the supported schemes.

Parameters:
  • lmin (int) – Multipole range.

  • lmax (int) – Multipole range.

  • b_lmax (int) – Maximum multipole for the NmtBin object.

  • binning ({'linear', 'logspace', 'powspace'}) – Binning scheme.

  • ell_step (int, optional) – Bin width in ell for 'linear' binning.

  • n_ell_bins (int, optional) – Number of ell bins for 'logspace' / 'powspace' binning.

  • power (float, optional) – Exponent for 'powspace' binning.

Return type:

nmt.NmtBin

get_pixels(ra, dec, nside)[source]

Get the HEALPix pixel indices for given RA and Dec.

Parameters:
  • ra (np.ndarray) – Right ascension in degrees.

  • dec (np.ndarray) – Declination in degrees.

  • nside (int) – HEALPix nside parameter.

Returns:

  • unique_pix (np.ndarray) – Sorted unique pixel indices.

  • idx (np.ndarray) – First-occurrence indices into the input from np.unique.

  • idx_rep (np.ndarray) – Inverse map: pixel-group index for each input object.

get_n_gal_map(nside, ra, dec, weights=None, unique_pix=None, idx=None, idx_rep=None)[source]

Weighted galaxy number-density HEALPix map plus pixel bookkeeping.

Bins (ra, dec) (degrees) onto an nside HEALPix grid. With weights=None each object counts as 1 (galaxy counts); pass per-object weights for a weight-summed occupancy map.

Returns:

n_gal – Map of summed weights (or counts) per pixel, shape (npix,).

Return type:

np.ndarray

get_shear_map(ra, dec, e1, e2, w, nside, unique_pix=None, idx=None, idx_rep=None, n_gal_map=None)[source]

Weighted shear HEALPix maps plus pixel bookkeeping.

Bins (ra, dec) (degrees) onto an nside HEALPix grid. The shear components (e1, e2) are weighted by w and summed per pixel. If unique_pix, idx, and idx_rep are provided, they are used to avoid recomputing the pixel indices. If n_gal_map is provided, it is used to normalize the shear maps by the galaxy density.

Returns:

  • e1_map (np.ndarray) – Weighted sum of E1 per pixel, shape (npix,).

  • e2_map (np.ndarray) – Weighted sum of E2 per pixel, shape (npix,).

get_variance_map(nside, ra, dec, e1, e2, w, unique_pix=None, idx=None, idx_rep=None)[source]

Compute the variance map of the shear components.

The variance is computed as the weighted variance of the shear components in each pixel.

Returns:

variance_map – Variance map of the shear components, shape (npix,).

Return type:

np.ndarray

get_noise_bias_analytical(ra, dec, e1, e2, w, lmax, nside=1024, unique_pix=None, idx=None, idx_rep=None)[source]

Compute the analytical noise bias for shear power spectrum.

apply_random_rotation(e1, e2, rng=None)[source]

Apply a uniform random rotation to ellipticity components.

Parameters:
  • e1 (np.ndarray) – Ellipticity components.

  • e2 (np.ndarray) – Ellipticity components.

  • rng (np.random.Generator, optional) – Random generator for the rotation angles. Pass a seeded generator (e.g. np.random.default_rng(seed)) for reproducible draws; when None a fresh entropy-seeded generator is used (non-reproducible).

Returns:

e1_out, e2_out – Rotated ellipticity components.

Return type:

np.ndarray

get_noise_realisation(ra, dec, e1, e2, w, nside, unique_pix=None, idx=None, idx_rep=None, n_gal_map=None, rng=None)[source]

Generate a random noise realisation of the shear maps by applying a random rotation to the ellipticity components.

Parameters:
  • ra (np.ndarray) – Right ascension and declination of the sources.

  • dec (np.ndarray) – Right ascension and declination of the sources.

  • e1 (np.ndarray) – Ellipticity components.

  • e2 (np.ndarray) – Ellipticity components.

  • w (np.ndarray) – Weights of the sources.

  • nside (int) – HEALPix resolution.

  • unique_pix (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • idx (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • idx_rep (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • n_gal_map (np.ndarray, optional) – Galaxy number density map. If not provided, it will be computed.

Returns:

noise_map_e1, noise_map_e2 – Noise map for ellipticity components.

Return type:

np.ndarray

get_noise_bias_from_gaussian_real(ra, dec, e1, e2, w, nside, nrandom_cell, binning, ell_step=10, n_ell_bins=32, power=0.5, unique_pix=None, idx=None, idx_rep=None, n_gal_map=None, wsp=None, seed=42)[source]

Compute the power spectrum of the noise bias from random realisations.

Parameters:
  • ra (np.ndarray) – Right ascension and declination of the sources.

  • dec (np.ndarray) – Right ascension and declination of the sources.

  • e1 (np.ndarray) – Ellipticity components.

  • e2 (np.ndarray) – Ellipticity components.

  • w (np.ndarray) – Weights of the sources.

  • nside (int) – HEALPix resolution.

  • nrandom_cell (int) – Number of random cells to use for the noise estimation.

  • binning (str, int, int, float) – Binning scheme and parameters.

  • ell_step (str, int, int, float) – Binning scheme and parameters.

  • n_ell_bins (str, int, int, float) – Binning scheme and parameters.

  • power (str, int, int, float) – Binning scheme and parameters.

  • unique_pix (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • idx (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • idx_rep (np.ndarray, optional) – Pixel indices and bookkeeping arrays. If not provided, they will be computed.

  • n_gal_map (np.ndarray, optional) – Galaxy number density map. If not provided, it will be computed.

Returns:

noise_bias_cl – Power spectrum of the noise bias.

Return type:

np.ndarray

get_noise_bias(ra, dec, e1, e2, w, nside, noise_bias_method, binning, *, ell_step=10, n_ell_bins=32, power=0.5, nrandom_cell=100, seed=42)[source]

Compute the noise bias from object positions and ellipticities.

Parameters:
  • ra (np.ndarray) – Right ascension and declination of the sources.

  • dec (np.ndarray) – Right ascension and declination of the sources.

  • e1 (np.ndarray) – Ellipticity components.

  • e2 (np.ndarray) – Ellipticity components.

  • w (np.ndarray) – Weights of the sources.

  • nside (int) – HEALPix resolution.

  • noise_bias_method ({'randoms', 'analytic'}) – Method used to estimate the noise bias.

  • binning ({'linear', 'logspace', 'powspace'}) – Binning scheme.

  • ell_step (int, optional) – Bin width in ell for 'linear' binning.

  • n_ell_bins (int, optional) – Number of ell bins for 'logspace' / 'powspace' binning.

  • power (float, optional) – Exponent for 'powspace' binning.

  • nrandom_cell (int, optional) – Number of random cells to use for the noise estimation. (only for the randoms method)

  • seed (int, optional) – Random seed for reproducibility. (only for the randoms method)

Returns:

noise_bias_cl – Power spectrum of the noise bias.

Return type:

np.ndarray

get_field_and_workspace_from_map(b, mask_a, e1_map_a=None, e2_map_a=None, mask_b=None, e1_map_b=None, e2_map_b=None, pol_factor=-1, return_wsp=True)[source]

Compute a NaMaster field and workspace object from the input maps.

If the shear maps are None, returns field objects but only the workspace objects is relevant and contains the mixing matrix. If the second mask and shear maps (indexed b) are provided, the mixing matrix is computed between the two fields.

Parameters:
  • b (nmt.NmtBin) – NaMaster binning object.

  • mask_a (np.ndarray) – Field mask for the first map.

  • e1_map_a (np.ndarray, optional) – E1 map for the first field.

  • e2_map_a (np.ndarray, optional) – E2 map for the first field.

  • mask_b (np.ndarray, optional) – Field mask for the second map.

  • e1_map_b (np.ndarray, optional) – E1 map for the second field.

  • e2_map_b (np.ndarray, optional) – E2 map for the second field.

  • pol_factor (float, optional) – Polarization factor to apply to the E2 map.

  • return_wsp (bool, optional) – If True, return the NaMaster workspace object containing the mixing matrix.

Returns:

  • field_a (nmt.NmtField) – NaMaster field object for the first map.

  • field_b (nmt.NmtField) – NaMaster field object for the second map (if provided, same than the first map otherwise).

  • wsp (nmt.NmtWorkspace) – NaMaster workspace object containing the mixing matrix.

get_field_and_workspace_from_catalog(b, ra_a, dec_a, e1_a, e2_a, w_a, ra_b=None, dec_b=None, e1_b=None, e2_b=None, w_b=None, pol_factor=-1, return_wsp=True, same_bin=False)[source]

Create a NaMaster field and workspace from the input catalog.

If the second catalog is provided, the mixing matrix is computed between the two fields.

Parameters:
  • b (nmt.NmtBin) – NaMaster binning object.

  • ra_a (np.ndarray) – Right ascension of sources in the first catalog.

  • dec_a (np.ndarray) – Declination of sources in the first catalog.

  • e1_a (np.ndarray) – E1 shear component of sources in the first catalog.

  • e2_a (np.ndarray) – E2 shear component of sources in the first catalog.

  • w_a (np.ndarray) – Weights of sources in the first catalog.

  • ra_b (np.ndarray, optional) – Right ascension of sources in the second catalog.

  • dec_b (np.ndarray, optional) – Declination of sources in the second catalog.

  • e1_b (np.ndarray, optional) – E1 shear component of sources in the second catalog.

  • e2_b (np.ndarray, optional) – E2 shear component of sources in the second catalog.

  • w_b (np.ndarray, optional) – Weights of sources in the second catalog.

  • pol_factor (float, optional) – Polarization factor to apply to the E2 component.

  • return_wsp (bool, optional) – If True, return the NaMaster workspace object containing the mixing matrix.

Returns:

  • field_a (nmt.NmtFieldCatalog) – NaMaster field object for the first catalog.

  • field_b (nmt.NmtFieldCatalog) – NaMaster field object for the second catalog (if provided, same as the first catalog otherwise).

  • wsp (nmt.NmtWorkspace) – NaMaster workspace object containing the mixing matrix.

compute_cl_from_field_and_workspace(field_a, field_b, wsp, b)[source]

Compute the angular power spectrum from the input NaMaster field and workspace

Parameters:
  • field_a (nmt.NmtField) – NaMaster field object for the first catalog.

  • field_b (nmt.NmtField) – NaMaster field object for the second catalog.

  • wsp (nmt.NmtWorkspace) – NaMaster workspace object containing the mixing matrix.

  • b (nmt.NmtBin) – NaMaster binning object.

Returns:

  • cl_coupled (np.ndarray) – Coupled angular power spectrum.

  • cl_decoupled (np.ndarray) – Decoupled angular power spectrum.

get_pseudo_cls_map(shear_map_a, mask_a, nside, binning, *, shear_map_b=None, mask_b=None, pol_factor=-1, wsp=None, ell_step=10, n_ell_bins=32, power=0.5)[source]

Map-based pseudo-Cl for a complex shear map.

Parameters:
  • shear_map_a (np.ndarray) – Complex shear map (e1 + 1j * e2).

  • mask_a (np.ndarray) – Field mask (the galaxy number-density map).

  • nside (int) – HEALPix resolution; fixes the harmonic geometry.

  • binning (str) – Binning scheme passed to make_namaster_bin().

  • shear_map_b (np.ndarray, optional) – Complex shear map for the second field (e1 + 1j * e2).

  • mask_b (np.ndarray, optional) – Field mask for the second field (the galaxy number-density map).

  • pol_factor (float, optional) – Polarization factor to apply to the E2 component.

  • wsp (nmt.NmtWorkspace, optional) – Reuse a coupling workspace; built from the field if None.

  • ell_step (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

  • n_ell_bins (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

  • power (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

Returns:

  • ell_eff (np.ndarray) – Effective multipoles of the bandpowers.

  • cl_all (np.ndarray) – Decoupled EE/EB/BE/BB spectra, shape (4, n_bands).

  • wsp (nmt.NmtWorkspace) – The coupling workspace (newly built or the one passed in).

get_pseudo_cls_catalog(catalog, params, nside, binning, *, tomo_bin_a='all', tomo_bin_b='all', pol_factor=-1, wsp=None, ell_step=10, n_ell_bins=32, power=0.5)[source]

Catalog-based pseudo-Cl via NaMaster’s NmtFieldCatalog.

Parameters:
  • catalog (np.ndarray) – Structured catalog array with the columns named in params.

  • params (dict) – Column-name mapping (ra_col, dec_col, w_col, e1_col, e2_col).

  • nside (int) – HEALPix resolution; fixes the harmonic geometry.

  • binning (str) – Binning scheme passed to make_namaster_bin().

  • tomo_bin_a (str or int or None, optional) – Tomographic bin IDs for the two fields.

  • tomo_bin_b (str or int or None, optional) – Tomographic bin IDs for the two fields.

  • pol_factor (int, optional) – Polarization factor to apply to the E2 component.

  • wsp (nmt.NmtWorkspace, optional) – Reuse a coupling workspace; built from the field if None.

  • ell_step (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

  • n_ell_bins (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

  • power (optional) – Binning-scheme parameters forwarded to make_namaster_bin().

Returns:

  • ell_eff (np.ndarray) – Effective multipoles of the bandpowers.

  • cl_all (np.ndarray) – Decoupled EE/EB/BE/BB spectra, shape (4, n_bands).

  • wsp (nmt.NmtWorkspace) – The coupling workspace (newly built or the one passed in).

get_fiducial_cl(z, dndz, lmax, cosmo, backend='camb')[source]

Get the fiducial Cl’s using the redshift distribution. Cosmology is determined by the input cosmo object.

get_pseudo_cl_iNKA_covariance(input_cl_a1_b1, input_cl_a1_b2, input_cl_a2_b1, input_cl_a2_b2, field_a1, field_a2, field_b1, field_b2, wsp_a, wsp_b, b)[source]

Compute the iNKA covariance for pseudo-Cl.

Parameters:
  • input_cl_a1_b1 (np.ndarray) – Input Cl for field a1 and b1.

  • input_cl_a1_b2 (np.ndarray) – Input Cl for field a1 and b2.

  • input_cl_a2_b1 (np.ndarray) – Input Cl for field a2 and b1.

  • input_cl_a2_b2 (np.ndarray) – Input Cl for field a2 and b2.

  • field_a1 (nmt.NmtField) – NaMaster field object for the first catalog (a1).

  • field_a2 (nmt.NmtField) – NaMaster field object for the second catalog (a2).

  • field_b1 (nmt.NmtField) – NaMaster field object for the first catalog (b1).

  • field_b2 (nmt.NmtField) – NaMaster field object for the second catalog (b2).

  • wsp_a (nmt.NmtWorkspace) – NaMaster workspace object containing the mixing matrix for fields a.

  • wsp_b (nmt.NmtWorkspace) – NaMaster workspace object containing the mixing matrix for fields b.

  • b (nmt.NmtBin) – NaMaster binning object.

Returns:

cov_matrix – Covariance matrix of the pseudo-Cl, shape (n_bins, n_bins).

Return type:

np.ndarray

bandpower_window_from_workspace(wsp, nside=None)[source]

Extract the bandpower window matrix W for a spin-2×spin-2 workspace.

For HEALPix-map spectra, supply nside to include the pixel window pw²(ℓ) in the measured spectrum. Catalogue-based spectra have no pixel window and must leave nside unset.

NaMaster’s get_bandpower_windows() returns a four-index array (n_cl_out, n_bpw, n_cl_in, n_ell) describing how each output bandpower is built from the input multipoles across the EE/EB/BE/BB spectra. SACC’s BandpowerWindow model (one window per bandpower, shared across the stored spectra) needs the per-spectrum decoupling window, i.e. the diagonal EE←EE block (equal to BB←BB and EB←EB, verified identical).

Returns:

  • window_ells (np.ndarray) – Multipoles the window spans, arange(n_ell) — the ell axis of compute_coupled_cell.

  • window_weights (np.ndarray) – W of shape (n_ell, n_bpw) — one column per bandpower, the layout sp_validation.sacc_io.add_pseudo_cl() expects. If nside is given, this includes pw²(ℓ) on the window’s multipole grid.