sp_validation.b_modes¶
B-mode analysis functions for weak lensing validation.
This module contains pure E/B mode decomposition, COSEBIs analysis, and semi-analytical covariance calculations extracted from CosmologyValidation.
- find_conservative_scale_cut_key(results, requested_scale_cut)[source]¶
Find scale cut key that conservatively fits within requested range.
- scale_cut_to_bins(gg, min_scale=None, max_scale=None)[source]¶
Convert conservative scale cuts to bin indices.
Conservative approach: excludes ANY bin with partial overlap with forbidden region. For example, with a bin [1,3] arcmin and cuts at 2 arcmin: - Both lower and upper cuts at 2 would exclude this bin
- Parameters:
- Returns:
start_bin (int) – First included bin index
stop_bin (int) – Last included bin index + 1 (for slicing notation)
- correlation_from_covariance(covariance)[source]¶
Convert covariance matrix to correlation matrix.
- Parameters:
covariance (numpy.ndarray) – Covariance matrix
- Returns:
Correlation matrix
- Return type:
- calculate_pure_eb_correlation(gg, gg_int, var_method='jackknife', cov_path_int=None, cosmo_cov=None, n_samples=1000, z_dist=None)[source]¶
Calculate pure E/B modes from correlation function objects.
- Parameters:
gg (treecorr.GGCorrelation) – Correlation function for reporting binning (coarser binning for final results)
gg_int (treecorr.GGCorrelation) – Correlation function for integration binning (fine binning for numerical integration)
var_method (str, optional) – Variance method (“jackknife” or “bootstrap”)
cov_path_int (str, optional) – Path to integration covariance matrix for semi-analytical calculation
cosmo_cov (pyccl.Cosmology, optional) – Cosmology for theoretical predictions in semi-analytical covariance
n_samples (int, optional) – Number of Monte Carlo samples for semi-analytical covariance
z_dist (2D array, optional) – Redshift distribution; z_dist[:, 0] = z, z_dist[:, 1] = n(z)
- Returns:
Dictionary containing pure E/B mode results and covariance
- Return type:
- calculate_cosebis(gg, nmodes=10, scale_cuts=None, cov_path=None)[source]¶
Calculate COSEBIs modes from a correlation function for multiple scale cuts.
- Parameters:
gg (treecorr.GGCorrelation) – Fine-binned correlation function object for COSEBIs calculation
nmodes (int, optional) – Number of COSEBIs modes to compute
scale_cuts (list of tuples, optional) – List of (min_theta, max_theta) scale cuts to apply. If None, uses full range as a single scale cut.
cov_path (str, optional) – Path to theoretical covariance matrix (ξ±) which will be transformed to COSEBIs space
- Returns:
Dictionary with scale cut tuples as keys and results dictionaries as values. Each results dictionary contains ‘En’, ‘Bn’, ‘cov’, ‘chi2_E’, ‘chi2_B’, ‘pte_B’, ‘scale_cut’, and ‘mask’ entries.
- Return type:
- calculate_eb_statistics(results, cov_path_int=None, n_samples=1000)[source]¶
Calculate E/B mode statistics using 2D PTE analysis for all scale cut combinations.
This function processes pure E/B mode results, calculates covariance blocks, and performs comprehensive 2D PTE analysis. The traditional “1D PTE” values are simply extracted from the 2D matrices at the full-range position.
- Parameters:
results (dict) – Dictionary containing pure E/B mode results from calculate_pure_eb_correlation
cov_path_int (str, optional) – Path to integration covariance matrix for semi-analytical calculation
n_samples (int, optional) – Number of Monte Carlo samples used for semi-analytical covariance
min_bins (int, optional) – Minimum number of bins required for valid PTE calculation
- Returns:
Updated results dictionary with PTE matrices and statistics
- Return type:
- plot_integration_vs_reporting(gg, gg_int, output_path, version)[source]¶
Plot integration vs reporting scale comparison.
- _get_pte_from_scale_cut(pte_matrix, gg, scale_cut)[source]¶
Extract PTE value from matrix based on scale cut range using conservative logic.
- Parameters:
pte_matrix (numpy.ndarray) – 2D PTE matrix
gg (treecorr.GGCorrelation) – Correlation function object with bin edges
scale_cut (tuple) – (min_scale, max_scale) angular range for scale cut
- Returns:
PTE value for the given scale cut, or full-range PTE if scale_cut is None
- Return type:
- plot_pure_eb_correlations(results, output_path, version, fiducial_xip_scale_cut=None, fiducial_xim_scale_cut=None)[source]¶
Plot pure E/B mode correlation functions.
- Parameters:
results (dict) – Results dictionary containing E/B mode data and statistics
output_path (str) – Output file path for the plot
version (str) – Version string for plot title
fiducial_xip_scale_cut (tuple, optional) – (min_scale, max_scale) for xi+ fiducial analysis, shown as gray regions
fiducial_xim_scale_cut (tuple, optional) – (min_scale, max_scale) for xi- fiducial analysis, shown as gray regions
- plot_cosebis_scale_cut_heatmap(cosebis_results, gg, version, output_path, fiducial_scale_cut=None)[source]¶
Create 2D heatmaps showing how COSEBIs statistics vary across different scale cuts.
- Parameters:
cosebis_results (dict) – Dictionary with scale cut tuples as keys, containing ‘chi2_E’ and ‘pte_B’ values
gg (treecorr.GGCorrelation) – Correlation function object for bin edges
version (str) – Version string for main title
output_path (str) – Output file path for the plot
fiducial_scale_cut (tuple, optional) – (min_scale, max_scale) for cross-hatching
- plot_pte_2d_heatmaps(results, version, output_path, fiducial_xip_scale_cut=None, fiducial_xim_scale_cut=None)[source]¶
Plot 2D PTE matrix heatmaps for B-mode analysis.
- Parameters:
results (dict) – Results dictionary containing PTE matrices
version (str) – Version string for plot title
output_path (str) – Output file path for the plot
fiducial_xip_scale_cut (tuple, optional) – (min_scale, max_scale) for xi+ fiducial analysis, shown as cross-hatched
fiducial_xim_scale_cut (tuple, optional) – (min_scale, max_scale) for xi- fiducial analysis, shown as cross-hatched
- plot_eb_covariance_matrix(cov_matrix, var_method, output_path, version)[source]¶
Plot E/B mode covariance matrix as correlation matrix.
- Parameters:
cov_matrix (numpy.ndarray) – Covariance matrix from E/B mode analysis
var_method (str) – Variance method used for the analysis
output_path (str) – Output file path for the plot
version (str) – Version string for plot title
- plot_cosebis_modes(results, version, output_path, fiducial_scale_cut=None)[source]¶
Plot COSEBIs E/B mode correlation functions.
- save_pure_eb_results(results, output_path)[source]¶
Save pure E/B mode data vectors and covariance to .npz.
- _cosebis_result_to_dict(r, suffix='')[source]¶
Build save dict entries from a single COSEBIs result.
- save_cosebis_results(results, output_path, fiducial_scale_cut=None)[source]¶
Save COSEBIs data vectors and covariance to .npz.
- Parameters:
results (dict) – COSEBIs results. Either a single-scale-cut dict with keys like ‘En’, ‘Bn’, ‘cov’, or a multi-scale-cut dict with tuple keys.
output_path (str) – Output .npz file path
fiducial_scale_cut (tuple, optional) – If results has multiple scale cuts, save only this one. If None and results has multiple scale cuts, saves all.