sp_validation.b_modes¶
B-mode analysis functions for weak lensing validation.
Pure E/B modes (Schneider et al. 2022) as a fixed linear operator on the fine ξ± grid with their exact covariance, COSEBIs, and the χ²/PTE and plotting helpers shared by both.
- 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)
- bins_from_edges(left_edges, right_edges, min_scale=None, max_scale=None)[source]¶
scale_cut_to_bins()on bare bin edges.
- bins_from_scale_cut(left_edges, right_edges, scale_cut)[source]¶
Reporting bins inside a pure-E/B scale cut, as
(start_bin, stop_bin).Each end of
scale_cutsnaps to the nearest reporting edge in log θ — the rule that snaps reporting edges onto the fine grid — so a cut placed on a nominal edge selects the same bins whichever fine grid the edges were snapped to.stop_binis exclusive.
- log_bin_edges(min_sep, max_sep, nbins)[source]¶
TreeCorr
Logbin edges — the grid a binning defines.SACC ξ± parts store bin centres, not edges, so a consumer working from a part reconstructs the edges from the binning it was measured on.
- correlation_from_covariance(covariance)[source]¶
Convert covariance matrix to correlation matrix.
- Parameters:
covariance (numpy.ndarray) – Covariance matrix
- Returns:
Correlation matrix
- Return type:
- hartlap_factor(npatch, dof)[source]¶
Hartlap (2007) debiasing of an inverse covariance.
(N - p - 2) / (N - 1)for a jackknife covariance fromN = npatchpatches inverted overp = dofdata points; exactly 1 for an analytic covariance, whichnpatch=Nonedenotes.
- _reporting_binning(weight_int, edges_int, reporting_edges)[source]¶
Reporting bins as unions of fine bins, and their pair-weighted average.
Each requested reporting edge snaps to the nearest fine edge (in log θ), so every reporting bin is a whole number of fine bins. Row
iof the(n_report, n_fine)matrix weights the fine bins of reporting biniby their TreeCorr pair weightΣ w_i w_jand sums to one. TreeCorr’s ξ± andmeanrare averages over pairs under that same weight, so each row reproduces what TreeCorr would measure on the snapped bin.Returns
(binning, edges),edgesbeing the snapped reporting edges.
- _fixed_quadrature_operator(edges_int)[source]¶
The Schneider (2022) transform on the fine grid,
_EB_KEYSorder.The single call into cosmo_numba. Its interpolator places the samples on a regular grid in log θ, so the transform runs on the log-uniform nodes of the fine bins — their geometric centres, TreeCorr’s
rnom— and is evaluated at every node, with[tmin, tmax]at their extent. That makes it a function of the fine grid alone (the zero-padded ξ− window extends from the evaluation grid). Returns the(6, n_fine, 2 * n_fine)stack of the six matrices acting on[xi_+; xi_-]; rows whose integration support is too small near the grid edges are NaN.
- pure_eb_operator(weight_int, edges_int, reporting_edges)[source]¶
The pure-E/B estimator as one matrix on the fine ξ± grid.
The Schneider et al. (2022) transform is evaluated with fixed-quadrature weights at the log-uniform fine-grid nodes (TreeCorr
rnom), integrating over the whole fine grid, and the six pure modes are then averaged into the reporting bins with TreeCorr pair weights. The reporting edges snap to the nearest fine edges, so each reporting bin is a union of fine bins. Both steps are linear and independent of the ξ± values, so the estimator isK = (I_6 ⊗ P) · Mand[xip_E; xim_E; xip_B; xim_B; xip_amb; xim_amb] = K @ [xip_int; xim_int]
with
Kof shape(6 * n_report, 2 * n_fine).- Parameters:
weight_int (array_like) – TreeCorr pair weight
Σ w_i w_jper fine bin (gg.weight), the averaging weights.edges_int (array_like) – The
n_fine + 1log-uniform fine-grid bin edges. The transform’s[tmin, tmax]is the extent of their centres, so they must reach beyond the reporting range on both sides.reporting_edges (array_like) – Requested reporting-bin edges,
n_report + 1of them.
- Returns:
operator (numpy.ndarray) –
K, shape(6 * n_report, 2 * n_fine).binning (numpy.ndarray) –
P, the(n_report, n_fine)pair-weighted average.edges (numpy.ndarray) – The reporting edges actually used, each a fine-grid edge.
- calculate_pure_eb_correlation(theta_int, xip_int, xim_int, weight_int, edges_int, cov_xi, reporting_edges, *, npatch=None)[source]¶
Pure E/B modes and their exact covariance from fine-grid ξ±.
The modes are
pure_eb_operator()applied to[xip_int; xim_int], and the covariance isK C_xi Kᵀ— exact for whatever ξ± covariance is supplied, analytic or jackknife.npatchrecords which: the jackknife patch count behindcov_xi, orNonefor an analytic covariance. It travels in the results and sets the Hartlap factor of every χ² built on them (hartlap_factor()).The reporting bins are unions of fine bins (the requested edges snap to the nearest fine edges), and their
theta,xip/ximand variances are the same pair-weighted average of the fine grid, soxi_± = E ± B + ambholds bin by bin andtheta/xip/ximare what TreeCorr would measure on the snapped bins.- Parameters:
theta_int (array_like) – Fine-grid
meanr, ξ±, and TreeCorr pair weightΣ w_i w_j.meanrenters only the reportedtheta; the transform runs on the log-uniform nodes ofedges_int.xip_int (array_like) – Fine-grid
meanr, ξ±, and TreeCorr pair weightΣ w_i w_j.meanrenters only the reportedtheta; the transform runs on the log-uniform nodes ofedges_int.xim_int (array_like) – Fine-grid
meanr, ξ±, and TreeCorr pair weightΣ w_i w_j.meanrenters only the reportedtheta; the transform runs on the log-uniform nodes ofedges_int.weight_int (array_like) – Fine-grid
meanr, ξ±, and TreeCorr pair weightΣ w_i w_j.meanrenters only the reportedtheta; the transform runs on the log-uniform nodes ofedges_int.edges_int (array_like) – The
n_fine + 1log-uniform fine-grid bin edges.cov_xi (array_like) –
(2 n_fine, 2 n_fine)covariance of[xip_int; xim_int].reporting_edges (array_like) – Requested reporting-bin edges,
n_report + 1of them.npatch (int, optional) – Jackknife patch count behind
cov_xi;Noneif it is analytic.
- Returns:
The six
_EB_KEYSmode arrays,cov(in_EB_KEYSblock order),npatch, the reporting grid (theta, the snappedleft_edges/right_edges,xip,xim,var_xip,var_xim) and the fine-grid inputs (theta_int,xip_int,xim_int,weight_int,edges_int).- 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:
- cosebis_scan_from_xi(theta, xip, xim, cov_xipm, left_edges, right_edges, *, nmodes=10, scale_cuts=None, npatch=None)[source]¶
COSEBIs over a set of scale cuts, from ξ± arrays and their covariance.
The values-and-covariance seam of
calculate_cosebis(), for callers holding a ξ± data vector rather than a TreeCorrGGCorrelation. The COSEBIs covariance is the ξ± covariance carried through the same linear kernel as the modes, so no estimator re-run is involved.npatchis the jackknife realisation count behindcov_xipm, which sets the Hartlap debiasing; leave itNonefor a theory covariance, which needs none.Returns the
{scale_cut: result}mappingcalculate_cosebis()returns.
- calculate_eb_statistics(results)[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) – Pure E/B results: the six mode arrays, the
covblock, the reportingtheta, andnpatch— the jackknife patch count behind the covariance, orNonefor an analytic one — which sets the Hartlap factor (hartlap_factor())- Returns:
Updated results dictionary with PTE matrices and statistics
- Return type:
- plot_integration_vs_reporting(results, output_path, version)[source]¶
Plot integration vs reporting scale comparison.
- Parameters:
- _get_pte_from_scale_cut(pte_matrix, edges, scale_cut)[source]¶
Extract PTE value from matrix at a scale cut (
bins_from_scale_cut()).- Parameters:
pte_matrix (numpy.ndarray) – 2D PTE matrix
edges (tuple of numpy.ndarray) –
(left_edges, right_edges)of the grid the matrix is indexed onscale_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, edges, 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
edges (tuple of numpy.ndarray) –
(left_edges, right_edges)of the grid the scale cuts indexversion (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, label, 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
label (str) – How the covariance was made (
covariance_label())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.