Spectra Computation Module

Power Spectra Computation Module

This module provides classes for computing polarization power spectra from CMB observations using the pseudo-Cℓ method with NaMaster (pymaster).

The module implements: - Cross-correlation power spectra between different frequency channels - Foreground x CMB cross-spectra for component separation - Foreground x foreground auto-spectra - Mode coupling correction using pre-computed coupling matrices - LAT x SAT cross-spectrum analysis - Parallel computation support

Classes

Spectra

Main class for computing power spectra from LAT or SAT observations. Handles observed x observed, dust x observed, sync x observed, and foreground auto-spectra calculations.

SpectraCross

Class for computing cross-spectra between LAT and SAT observations, used for calibration analysis and cosmic birefringence studies.

Features

  • Automatic mask apodization

  • Pure B-mode estimation support

  • Template bandpass integration

  • CO and point source masking

  • Parallel computation options

  • Efficient caching of intermediate results

  • Memory-efficient streaming computation

Example

from cobi.spectra import Spectra from cobi.simulation import LATsky, Mask

# Initialize LAT sky simulation lat_sky = LATsky(libdir, nside=512)

# Create spectra computation object spec = Spectra(

lat_lib=lat_sky, common_dir=common_libdir, aposcale=2.0, pureB=True, lmax=2000

)

# Compute all spectra for simulation index 0 spec.compute(idx=0, sync=True)

# Retrieve computed spectra spectra_dict = spec.get_spectra(idx=0, sync=True)

Notes

This implementation is optimized for the COBI cosmic birefringence analysis pipeline.

class Spectra(lat_lib, common_dir, aposcale=2.0, template_bandpass=False, pureB=False, CO=True, PS=True, galcut=0, verbose=True, cache=True, parallel=0, dust_model=-1, sync_model=-1, binwidth=1, lmax=0)[source]

Bases: object

Parameters:
__init__(lat_lib, common_dir, aposcale=2.0, template_bandpass=False, pureB=False, CO=True, PS=True, galcut=0, verbose=True, cache=True, parallel=0, dust_model=-1, sync_model=-1, binwidth=1, lmax=0)[source]

Initializes the Spectra class for computing and handling power spectra of observed CMB maps.

Parameters: libdir (str): Directory where the spectra will be stored. lat_lib (LATsky): An instance of the LATsky class containing LAT-related configurations. aposcale (float, optional): Apodisation scale in degrees. Defaults to 2 deg template_bandpass (bool, optional): Apply bandpass integration to the foreground template. Defaults to False. pureB (bool, optional): Apply B-mode purification. Defaults to False CO (bool, optional): Mask the brightest regions of CO emission. Defautls to True. PS (bool, optional): Mask the brightest polarised extragalactic point sources. Defaults to True.

Parameters:
Return type:

None

get_apodised_mask()[source]
Return type:

numpy.ndarray

get_coupling_matrix()[source]

Computes or loads the coupling matrix for power spectrum estimation.

Return type:

None

compute_master(f_a, f_b)[source]

Computes the decoupled power spectrum using the MASTER algorithm.

Parameters: f_a (nmt.NmtField): First NmtField object. f_b (nmt.NmtField): Second NmtField object.

Returns: np.ndarray: Decoupled power spectrum.

Parameters:
  • f_a (pymaster.NmtField)

  • f_b (pymaster.NmtField)

Return type:

numpy.ndarray

__set_dir__(idir, cdir)[source]

Sets up directories for storing power spectra and workspaces.

Parameters: dir (str): Directory for specific spectra. cdir (str): Common directory for spectra and workspaces.

Parameters:
Return type:

None

load_obsQUmaps(idx)[source]

Loads observed Q and U Stokes parameter maps for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

Obs_qu_maps(idx, ii)[source]
Parameters:
Return type:

numpy.ndarray

__get_fg_QUmap__(nu, fg)[source]

Retrieves or generates the Q and U Stokes parameter maps for dust emission for a specific frequency band.

Parameters: band (str): The frequency identifier. fg (str): Foreground type, either ‘dust’ or ‘sync’ Returns: Tuple[np.ndarray, np.ndarray]: Q and U maps for dust emission.

Parameters:
Return type:

Tuple[numpy.ndarray, numpy.ndarray]

load_dustQUmaps()[source]

Loads dust Q and U Stokes parameter maps for all frequency bands.

Return type:

None

Dust_qu_maps(ii)[source]
Parameters:

ii (int)

Return type:

numpy.ndarray

load_syncQUmaps()[source]

Loads synchrotron Q and U Stokes parameter maps for all frequency bands.

Return type:

None

Sync_qu_maps(ii)[source]
Parameters:

ii (int)

Return type:

numpy.ndarray

obs_x_obs_check(idx, read_test=False)[source]

Checks if the observed x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

__obs_x_obs_helper_series__(ii, idx, recache=False)[source]

Helper function: Computes or loads the observed x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map.

Returns: np.ndarray: Power spectra for the observed x observed fields.

Parameters:
Return type:

numpy.ndarray

__obs_x_obs_helper_parallel__(ii, idx, recache=False)[source]

Helper function: Computes or loads the observed x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map.

Returns: np.ndarray: Power spectra for the observed x observed fields.

Parameters:
Return type:

numpy.ndarray

obs_x_obs(idx, progress=False)[source]

Computes or loads the observed x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): If 0, runs serially; otherwise, runs with multithreading. Defaults to 1.

Returns: np.ndarray: Combined power spectra for the observed x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

__fg_x_obs_helper_series__(ii, idx, fg, recache=False)[source]

Helper function: Computes or loads the dust x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’ Returns: np.ndarray: Power spectra for the dust x observed fields.

Parameters:
Return type:

numpy.ndarray

__fg_x_obs_helper_parallel__(ii, idx, fg, recache=False)[source]

Helper function: Computes or loads the dust x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’

Returns: np.ndarray: Power spectra for the dust x observed fields.

Parameters:
Return type:

numpy.ndarray

dust_x_obs(idx, progress=False)[source]

Computes or loads the dust x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): If 0, runs serially; otherwise, runs with multithreading. Defaults to 1.

Returns: np.ndarray: Combined power spectra for the dust x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

dust_x_obs_check(idx)[source]

Checks if the dust x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

sync_x_obs_check(idx)[source]

Checks if the synchrotron x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

sync_x_obs(idx, progress=False)[source]

Computes or loads the synchrotron x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): Controls parallelization.

0 = serial, 2 = multithreading. Defaults to using self.parallel.

Returns: np.ndarray: Combined power spectra for the synchrotron x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

__fg_x_fg_helper_series__(ii, fg, recache=False)[source]

Helper function: Computes or loads the synchrotron x synchrotron power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’ Returns: np.ndarray: Power spectra for the synchrotron x synchrotron fields.

Parameters:
Return type:

numpy.ndarray

__fg_x_fg_helper_parallel__(ii, fg, recache=False)[source]

Helper function: Computes or loads the synchrotron x synchrotron power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’

Returns: np.ndarray: Power spectra for the synchrotron x synchrotron fields.

Parameters:
Return type:

numpy.ndarray

sync_x_sync(progress=False)[source]

Computes or loads the synchrotron x synchrotron power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the synchrotron x synchrotron fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

dust_x_dust(progress=False)[source]

Computes or loads the dust x dust power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the dust x dust fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

__sync_x_dust_helper_series__(ii)[source]

Helper function: Computes or loads the synchrotron x dust power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band.

Returns: np.ndarray: Power spectra for the synchrotron x dust fields.

Parameters:

ii (int)

Return type:

numpy.ndarray

__sync_x_dust_helper_parallel__(ii)[source]

Helper function: Computes or loads the synchrotron x dust power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band.

Returns: np.ndarray: Power spectra for the synchrotron x dust fields.

Parameters:

ii (int)

Return type:

numpy.ndarray

sync_x_dust(progress=False)[source]

Computes or loads the synchrotron x dust power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the synchrotron x dust fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

clear_obs_qu_maps()[source]

Clears the loaded observed Q and U maps to free up memory.

Return type:

None

clear_dust_qu_maps()[source]

Clears the loaded dust Q and U maps to free up memory.

Return type:

None

clear_sync_qu_maps()[source]

Clears the loaded synchrotron Q and U maps to free up memory.

Return type:

None

compute(idx, sync=False)[source]

Computes and stores all relevant spectra for a given realization index.

Parameters: idx (int): Index for the realization of the CMB map. sync (bool, optional): If True, calculate also synchrotron power spectra. Defaults to False.

Parameters:
Return type:

None

Compute(idx, sync=False)[source]
Parameters:
Return type:

None

get_spectra(idx, sync=False, avoid_bands=None)[source]

Retrieves all relevant spectra for a given realization index.

Parameters: idx (int): Index for the realization of the CMB map. sync (bool, optional): If True, calculate also synchrotron power spectra. Defaults to False. Returns: Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:

Tuple containing the power spectra (oxo, dxo, dxd, sxd, sxs, sxo).

Parameters:
Return type:

Dict

class SpectraCross(libdir, lat, sat, binwidth=1, galcut=40, aposcale=2, lmax=3000)[source]

Bases: object

Parameters:
__init__(libdir, lat, sat, binwidth=1, galcut=40, aposcale=2, lmax=3000)[source]
Parameters:
get_mask(tel)[source]
Parameters:

tel (str)

Return type:

numpy.ndarray

property satmask: numpy.ndarray
property latmask: numpy.ndarray
property satlatmask: numpy.ndarray
compute_master(tel, f_a, f_b)[source]
Parameters:
  • tel (str)

  • f_a (pymaster.NmtField)

  • f_b (pymaster.NmtField)

Return type:

numpy.ndarray

data_matrix(idx, which='EB', sat_lrange=(None, None), lat_lrange=(None, None), avg_splits=False, common_mask_op=<built-in function and_>)[source]
Parameters:
Return type:

numpy.ndarray

Spectra Class

class Spectra(lat_lib, common_dir, aposcale=2.0, template_bandpass=False, pureB=False, CO=True, PS=True, galcut=0, verbose=True, cache=True, parallel=0, dust_model=-1, sync_model=-1, binwidth=1, lmax=0)[source]
Parameters:
__init__(lat_lib, common_dir, aposcale=2.0, template_bandpass=False, pureB=False, CO=True, PS=True, galcut=0, verbose=True, cache=True, parallel=0, dust_model=-1, sync_model=-1, binwidth=1, lmax=0)[source]

Initializes the Spectra class for computing and handling power spectra of observed CMB maps.

Parameters: libdir (str): Directory where the spectra will be stored. lat_lib (LATsky): An instance of the LATsky class containing LAT-related configurations. aposcale (float, optional): Apodisation scale in degrees. Defaults to 2 deg template_bandpass (bool, optional): Apply bandpass integration to the foreground template. Defaults to False. pureB (bool, optional): Apply B-mode purification. Defaults to False CO (bool, optional): Mask the brightest regions of CO emission. Defautls to True. PS (bool, optional): Mask the brightest polarised extragalactic point sources. Defaults to True.

Parameters:
Return type:

None

get_apodised_mask()[source]
Return type:

numpy.ndarray

get_coupling_matrix()[source]

Computes or loads the coupling matrix for power spectrum estimation.

Return type:

None

compute_master(f_a, f_b)[source]

Computes the decoupled power spectrum using the MASTER algorithm.

Parameters: f_a (nmt.NmtField): First NmtField object. f_b (nmt.NmtField): Second NmtField object.

Returns: np.ndarray: Decoupled power spectrum.

Parameters:
  • f_a (pymaster.NmtField)

  • f_b (pymaster.NmtField)

Return type:

numpy.ndarray

__set_dir__(idir, cdir)[source]

Sets up directories for storing power spectra and workspaces.

Parameters: dir (str): Directory for specific spectra. cdir (str): Common directory for spectra and workspaces.

Parameters:
Return type:

None

load_obsQUmaps(idx)[source]

Loads observed Q and U Stokes parameter maps for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

Obs_qu_maps(idx, ii)[source]
Parameters:
Return type:

numpy.ndarray

__get_fg_QUmap__(nu, fg)[source]

Retrieves or generates the Q and U Stokes parameter maps for dust emission for a specific frequency band.

Parameters: band (str): The frequency identifier. fg (str): Foreground type, either ‘dust’ or ‘sync’ Returns: Tuple[np.ndarray, np.ndarray]: Q and U maps for dust emission.

Parameters:
Return type:

Tuple[numpy.ndarray, numpy.ndarray]

load_dustQUmaps()[source]

Loads dust Q and U Stokes parameter maps for all frequency bands.

Return type:

None

Dust_qu_maps(ii)[source]
Parameters:

ii (int)

Return type:

numpy.ndarray

load_syncQUmaps()[source]

Loads synchrotron Q and U Stokes parameter maps for all frequency bands.

Return type:

None

Sync_qu_maps(ii)[source]
Parameters:

ii (int)

Return type:

numpy.ndarray

obs_x_obs_check(idx, read_test=False)[source]

Checks if the observed x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

__obs_x_obs_helper_series__(ii, idx, recache=False)[source]

Helper function: Computes or loads the observed x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map.

Returns: np.ndarray: Power spectra for the observed x observed fields.

Parameters:
Return type:

numpy.ndarray

__obs_x_obs_helper_parallel__(ii, idx, recache=False)[source]

Helper function: Computes or loads the observed x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map.

Returns: np.ndarray: Power spectra for the observed x observed fields.

Parameters:
Return type:

numpy.ndarray

obs_x_obs(idx, progress=False)[source]

Computes or loads the observed x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): If 0, runs serially; otherwise, runs with multithreading. Defaults to 1.

Returns: np.ndarray: Combined power spectra for the observed x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

__fg_x_obs_helper_series__(ii, idx, fg, recache=False)[source]

Helper function: Computes or loads the dust x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’ Returns: np.ndarray: Power spectra for the dust x observed fields.

Parameters:
Return type:

numpy.ndarray

__fg_x_obs_helper_parallel__(ii, idx, fg, recache=False)[source]

Helper function: Computes or loads the dust x observed power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. idx (int): Index for the realization of the CMB map. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’

Returns: np.ndarray: Power spectra for the dust x observed fields.

Parameters:
Return type:

numpy.ndarray

dust_x_obs(idx, progress=False)[source]

Computes or loads the dust x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): If 0, runs serially; otherwise, runs with multithreading. Defaults to 1.

Returns: np.ndarray: Combined power spectra for the dust x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

dust_x_obs_check(idx)[source]

Checks if the dust x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

sync_x_obs_check(idx)[source]

Checks if the synchrotron x observed power spectra have been computed for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map.

Parameters:

idx (int)

Return type:

None

sync_x_obs(idx, progress=False)[source]

Computes or loads the synchrotron x observed power spectra for all frequency bands.

Parameters: idx (int): Index for the realization of the CMB map. progress (bool, optional): If True, displays a progress bar. Defaults to False. parallel (int, optional): Controls parallelization.

0 = serial, 2 = multithreading. Defaults to using self.parallel.

Returns: np.ndarray: Combined power spectra for the synchrotron x observed fields across all bands.

Parameters:
Return type:

numpy.ndarray

__fg_x_fg_helper_series__(ii, fg, recache=False)[source]

Helper function: Computes or loads the synchrotron x synchrotron power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’ Returns: np.ndarray: Power spectra for the synchrotron x synchrotron fields.

Parameters:
Return type:

numpy.ndarray

__fg_x_fg_helper_parallel__(ii, fg, recache=False)[source]

Helper function: Computes or loads the synchrotron x synchrotron power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band. fg (str): Type of foregrounds, either ‘dust’ or ‘sync’

Returns: np.ndarray: Power spectra for the synchrotron x synchrotron fields.

Parameters:
Return type:

numpy.ndarray

sync_x_sync(progress=False)[source]

Computes or loads the synchrotron x synchrotron power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the synchrotron x synchrotron fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

dust_x_dust(progress=False)[source]

Computes or loads the dust x dust power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the dust x dust fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

__sync_x_dust_helper_series__(ii)[source]

Helper function: Computes or loads the synchrotron x dust power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band.

Returns: np.ndarray: Power spectra for the synchrotron x dust fields.

Parameters:

ii (int)

Return type:

numpy.ndarray

__sync_x_dust_helper_parallel__(ii)[source]

Helper function: Computes or loads the synchrotron x dust power spectra for a specific frequency band.

Parameters: ii (int): Index for the current frequency band.

Returns: np.ndarray: Power spectra for the synchrotron x dust fields.

Parameters:

ii (int)

Return type:

numpy.ndarray

sync_x_dust(progress=False)[source]

Computes or loads the synchrotron x dust power spectra for all frequency bands.

Parameters: progress (bool, optional): If True, displays a progress bar. Defaults to False.

Returns: np.ndarray: Combined power spectra for the synchrotron x dust fields across all bands.

Parameters:

progress (bool)

Return type:

numpy.ndarray

clear_obs_qu_maps()[source]

Clears the loaded observed Q and U maps to free up memory.

Return type:

None

clear_dust_qu_maps()[source]

Clears the loaded dust Q and U maps to free up memory.

Return type:

None

clear_sync_qu_maps()[source]

Clears the loaded synchrotron Q and U maps to free up memory.

Return type:

None

compute(idx, sync=False)[source]

Computes and stores all relevant spectra for a given realization index.

Parameters: idx (int): Index for the realization of the CMB map. sync (bool, optional): If True, calculate also synchrotron power spectra. Defaults to False.

Parameters:
Return type:

None

Compute(idx, sync=False)[source]
Parameters:
Return type:

None

_compute_keep_idx_bands(avoid_bands)[source]

Indices for axes of length Nbands (e.g., 12), based on self.bands.

_compute_keep_idx_freq(avoid_bands)[source]

Indices for axes of length Nbands//2 (e.g., 6), based on self.freq.

_filter_bands_and_freq_axes(arr, keep_idx_bands, keep_idx_freq)[source]

Sequentially index any axis sized Nbands with keep_idx_bands and any axis sized Nbands//2 with keep_idx_freq. Logs shapes before/after.

get_spectra(idx, sync=False, avoid_bands=None)[source]

Retrieves all relevant spectra for a given realization index.

Parameters: idx (int): Index for the realization of the CMB map. sync (bool, optional): If True, calculate also synchrotron power spectra. Defaults to False. Returns: Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:

Tuple containing the power spectra (oxo, dxo, dxd, sxd, sxs, sxo).

Parameters:
Return type:

Dict

Cross-Spectra Class

class SpectraCross(libdir, lat, sat, binwidth=1, galcut=40, aposcale=2, lmax=3000)[source]
Parameters:
__init__(libdir, lat, sat, binwidth=1, galcut=40, aposcale=2, lmax=3000)[source]
Parameters:
get_mask(tel)[source]
Parameters:

tel (str)

Return type:

numpy.ndarray

property satmask: numpy.ndarray
property latmask: numpy.ndarray
property satlatmask: numpy.ndarray
compute_master(tel, f_a, f_b)[source]
Parameters:
  • tel (str)

  • f_a (pymaster.NmtField)

  • f_b (pymaster.NmtField)

Return type:

numpy.ndarray

data_matrix(idx, which='EB', sat_lrange=(None, None), lat_lrange=(None, None), avg_splits=False, common_mask_op=<built-in function and_>)[source]
Parameters:
Return type:

numpy.ndarray