simulation module

Contains functions relating to generating and processing simulations.

class simulation.FieldType(value)

Bases: enum.Enum

The three types of fields: position, shear E and shear B.

POS = 1
SHE_B = 3
SHE_E = 2
simulation.combine_sim_bp_batches(input_mask)

Combine batches produced by sim_bps into a single file.

Parameters

input_mask (str) – Path to batches, with batch number replaced by {batch}.

simulation.combine_slics(input_filemask, tomo, save_filemask, lmax)

Combine all SLICS Cls for a particular tomographic bin into a single file, and remove l^2/2π prefactor.

Parameters
  • input_filemask (str) – Path to input SLICS files, with placeholders for {tomo} and {los}.

  • tomo (int) – SLICS tomographic bin index.

  • save_filemask (str) – Path to save combined files to, with placeholder for {tomo}.

  • lmax (int) – Maximum l to save.

simulation.flat_bins_linspaced(leff_min, leff_max, bin_size)

Returns a NaMaster NmtBinFlat object whose effective ells are linearly spaced from leff_min to leff_max (inclusive) with a step of bin_size.

Parameters
  • leff_min (float) – Minimum effective ell.

  • leff_max (float) – Maximum effective ell.

  • bin_size (float) – Difference between effective ells for adjacent bins.

Returns

NaMaster flat binning scheme.

Return type

NmtBinFlat

simulation.gaussian_samples(sim_bp_path, save_path)

Generate Gaussian random samples having the same mean and covariance as simulated bandpowers. Designed to use output from combine_sim_bp_batches.

Parameters
  • sim_bp_path (str) – Path to simulated bandpowers to match mean and covariance of.

  • save_path (str) – Path to save output to.

simulation.get_binning_matrix(n_bandpowers, output_lmin, output_lmax, input_lmin=None, input_lmax=None)

Returns the binning matrix to convert Cls to log-spaced bandpowers, following Eqn. 20 of Hivon et al. 2002.

Input ell range defaults to match output ell range - note this behaviour is not suitable if this matrix is to be used to multiply the raw output from healpy anafast/alm2cl, which returns all ells from l=0. In that case, explicitly set input_lmin=0.

Parameters
  • n_bandpowers (int) – Number of bandpowers required.

  • output_lmin (int) – Minimum l to include in the binning.

  • output_lmax (int) – Maximum l to include in the binning.

  • input_lmin (int, optional) – Minimum l in the input (defaults to output_lmin).

  • input_lmax (int, optional) – Maximum l in the input (defaults to output_lmax).

Returns

Binning matrix P_bl, shape (n_bandpowers, n_input_ell),

with n_input_ell = input_lmax - input_lmin + 1.

Return type

2D numpy array

simulation.idx_to_field_type(idx)

Return whether a row or column index corresponds to position, shear E-mode or shear B-mode, based on the remainder when dividing by 3.

Parameters

idx (int) – Field index.

Returns

FieldType corresponding to field index.

Return type

FieldType

simulation.idx_to_field_type_nob(idx)

Alternative to idx_to_field_type for when shear B modes are not included. Return whether a row or column index corresponds to position or shear E-mode, based on the remainder when dividing by 2.

Parameters

idx (int) – Field index.

Returns

FieldType corresponding to field index.

Return type

FieldType

simulation.idx_to_zbin(idx)

Return the z bin corresponding to a particular row or column index, assuming 3 fields per z bin.

Parameters

idx (int) – Field index.

Returns

z-bin corresponding to field index, where 1 is the first redshift bin.

Return type

int

simulation.idx_to_zbin_nob(idx)

Alternative to idx_to_zbin for when shear B modes are not included. Return the z bin corresponding to a particular row or column index, assuming 2 fields per z bin.

Parameters

idx (int) – Field index.

Returns

z-bin corresponding to field index, where 1 is the first redshift bin.

Return type

int

simulation.is_pd(matrix)

Test whether a matrix is positive definite, using Scipy’s Cholesky decomposition.

Parameters

matrix (2D numpy array) – Matrix to test.

Returns

True if positive definite, False otherwise.

Return type

bool

simulation.leff_obs(n_zbin, pos_pos_dir, she_she_dir, pos_she_dir, ell_filename, pos_nl_path, she_nl_path, noise_ell_path, leff_path, save_dir)

Obtain a pseudo- cut-sky ‘observation’ by sampling from the Wishart distribution with effective l.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • pos_pos_dir (str) – Path to directory containing input position-position power spectra.

  • she_she_dir (str) – Path to directory containing input shear-shear power spectra.

  • pos_she_dir (str) – Path to directory containing input position-shear power spectra.

  • ell_filename (str) – Filename of ells file within each input directory.

  • pos_nl_path (str) – Path to position noise power spectrum.

  • she_nl_path (str) – Path to shear noise power spectrum.

  • noise_ell_path (str) – Path to noise ells.

  • leff_path (str) – Path to leff map file.

  • save_dir (str) – Path to output directory.

simulation.load_cls_zerob(n_zbin, pos_pos_dir, she_she_dir, pos_she_dir, lmax, lmin_in=0)

Given the number of redshift bins and relevant directories, load power spectra (position, shear, cross) in the correct order (diagonal / healpy new=True ordering). For lmin_in > 0 they will be padded with zeros, suitable for input to synfast. Assumes zero shear B-mode.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • pos_pos_dir (str) – Path to directory containing position-position power spectra.

  • she_she_dir (str) – Path to directory containing shear-shear power spectra.

  • pos_she_dir (str) – Path to directory containing position-shear power spectra.

  • lmax (int) – Maximum l to load.

  • lmin_in (int, optional) – l corresponding to the first line in each input file, default 0.

Returns

spectra (2D numpy array)

Array of input Cls with shape (n_spectra, lmax + 1), where n_spectra = n * (n + 1) / 2 and n = 3 * n_zbin. Padded with zeros below l = lmin_in.

b_indices (list)

Indices of spectra that involve shear B-modes, i.e. BB, EB, NB.

simulation.noise_cls(gals_per_sq_arcmin, sigma_e, lmin, lmax, save_dir)

Save noise power spectra to file.

Parameters
  • gals_per_sq_arcmin (float) – Number of galaxies per square arcminute per redshift bin.

  • sigma_e (float) – Intrinsic galaxy ellipticity dispersion per component.

  • lmin (int) – Minimum l to save.

  • lmax (int) – Maximum l to save.

  • save_dir (str) – Path to output directory.

simulation.save_bandpowers(bandpowers, n_zbin, save_dir, pbl=None, pos_pos_subdir='galaxy_bp', she_she_subdir='shear_bp', pos_she_subdir='galaxy_shear_bp', verbose=True)

Save bandpowers to disk with one text file per power spectrum. Also saves bandpower binning matrix pbl if provided.

Parameters
  • bandpowers (2D numpy array) – Binned power spectra to save, with shape (n_spectra, n_bandpower).

  • n_zbin (int) – Number of redshift bins.

  • save_dir (str) – Path to base directory to save bandpowers to.

  • pbl (2D numpy array, optional) – Bandpower binning matrix, which will be saved too if provided.

  • pos_pos_subdir (str, optional) – Name of subdirectory for position-position power spectra, default ‘galaxy_bp’.

  • she_she_subdir (str, optional) – Name of subdirectory for shear-shear power spectra, default ‘shear_bp’.

  • pos_she_subdir (str, optional) – Name of subdirectory for position-shear power spectra, default ‘galaxy_shear_bp’.

  • verbose (bool, optional) – Whether to print names of files as they are saved (default True).

simulation.save_cls_nob(spectra, n_zbin, save_dir, pos_pos_subdir='galaxy_cl', she_she_subdir='shear_cl', pos_she_subdir='galaxy_shear_cl', lmin=None, verbose=True)

Takes a diagonal-ordered sequence of tomographic 3x2pt power spectra and saves them each to an individual file matching the structure of CosmoSIS output. Will also save ells if lmin is supplied. Only accounts for shear E-mode.

Parameters
  • spectra (2D numpy array) – Power spectra to save, with shape (n_spectra, n_ell).

  • n_zbin (int) – Number of redshift bins.

  • save_dir (str) – Path to base directory to save Cls to.

  • pos_pos_subdir (str, optional) – Name of subdirectory for position-position power spectra, default ‘galaxy_cl’.

  • she_she_subdir (str, optional) – Name of subdirectory for shear-shear power spectra, default ‘shear_cl’.

  • pos_she_subdir (str, optional) – Name of subdirectory for position-shear power spectra, default ‘galaxy_shear_cl’.

  • lmin (int, optional) – Minimum ell to list in ells file, which is only produced if lmin is supplied.

  • verbose (bool, optional) – Whether to print names of files as they are saved (default True).

simulation.save_maps(maps, save_dir, zbin_no)

Save a set of three maps (galaxy position, shear 1, shear 2) to a single file.

Parameters
  • maps (sequence of 3 healpy maps) – Maps of (galaxy position, shear 1, shear 2).

  • save_dir (str) – Path of directory to save maps to.

  • zbin_no (int) – Redshift bin number, used to label the output file.

simulation.sim_bps(n_zbin, pos_pos_in_dir, she_she_in_dir, pos_she_in_dir, lmax, lmin_in, pos_nl_path, she_nl_path, nside, n_bandpower, lmin_out, mask_path, n_loop, batch_size, save_dir)

Generate simultaneous full-sky and cut-sky repeated bandpower realisations, saved to disk in batches.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • pos_pos_in_dir (str) – Path to directory containing input position-position power spectra.

  • she_she_in_dir (str) – Path to directory containing input shear-shear power spectra.

  • pos_she_in_dir (str) – Path to directory containing input position-shear power spectra.

  • lmax (int) – Maximum l to simulate.

  • lmin_in (int) – l corresponding to first row in input files.

  • pos_nl_path (str) – Path to position noise power spectrum.

  • she_nl_path (str) – Path to shear noise power spectrum.

  • nside (int) – Healpix map resolution to use.

  • n_bandpower (int) – Number of log-spaced bandpowers to use.

  • lmin_out (int) – Minimum l to include in bandpowers.

  • mask_path (str) – Path to mask fits file.

  • n_loop (int) – Total number of realisations to generate.

  • batch_size (int) – Number of realisations per batch, where one file is saved per batch.

  • save_dir (str) – Path to directory to save batches into (must already exist).

simulation.sim_cls_fullsky(n_zbin, pos_pos_in_dir, she_she_in_dir, pos_she_in_dir, lmax, lmin_in, pos_nl_path, she_nl_path, lmin_out, n_loop, batch_size, save_dir)

Generate full-sky repeated power spectrum realisations, saved to disk in batches.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • pos_pos_in_dir (str) – Path to directory containing input position-position power spectra.

  • she_she_in_dir (str) – Path to directory containing input shear-shear power spectra.

  • pos_she_in_dir (str) – Path to directory containing input position-shear power spectra.

  • lmax (int) – Maximum l to simulate.

  • lmin_in (int) – l corresponding to first row in input files.

  • pos_nl_path (str) – Path to position noise power spectrum.

  • she_nl_path (str) – Path to shear noise power spectrum.

  • lmin_out (int) – Minimum l to include in output.

  • n_loop (int) – Total number of realisations to generate.

  • batch_size (int) – Number of realisations per batch, where one file is saved per batch.

  • save_dir (str) – Path to directory to save batches into (must already exist).

simulation.sim_flat(n_zbin, cl_filemask, input_lmin, nx, ny, lx, ly, leff_min, leff_max, bin_size, n_real, save_path)

Generate repeated flat-sky realisations and save measured bandpowers to disk.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • cl_filemask (str) – Path to input Cls, with the two redshift bins replaced with {i} and {j}.

  • input_lmin (int) – l corresponding to first line in input files.

  • nx (int) – Number of pixels along the x axis.

  • ny (int) – Number of pixels along the y axis.

  • lx (float) – Size along the x axis in radians.

  • ly (float) – Size along the y axis in radians.

  • leff_min (float) – Effective l for the lowest bandpower.

  • leff_max (float) – Effective l for the highest bandpower.

  • bin_size (float) – Difference between effective l for adjacent bandpowers.

  • n_real (int) – Number of realisations to generate.

  • save_path (str) – Path to save output to.

simulation.sim_singlespec(input_cl_path, lmax, lmin_in, nl_path, mask_path, nside, n_loop, batch_size, lmin_out, save_dir)

Generate simple single-field full-spectrum simulations.

Parameters
  • input_cl_path (str) – Path to input power spectrum.

  • lmax (int) – Maximum l to simulate.

  • lmin_in (int) – l corresponding to first line in input power spectrum file.

  • nl_path (str) – Path to noise power spectrum.

  • mask_path (str) – Path to mask fits file.

  • nside (int) – Resolution of healpix maps to use.

  • n_loop (int) – Number of realisations to generate in total.

  • batch_size (int) – Number of realisations per batch, where one file is saved for each batch.

  • lmin_out (int) – Minimum l to save.

  • save_dir (str) – Path to output directory.

simulation.single_obs_cls(n_zbin, pos_pos_in_dir, she_she_in_dir, pos_she_in_dir, lmax, lmin_in, pos_nl_path, she_nl_path, nside, lmin_out, mask_path=None, verbose=True)

Generate a single cut-sky realisation of all power spectra.

Parameters
  • n_zbin (int) – Number of redshift bins.

  • pos_pos_in_dir (str) – Path to directory containing input position-position power spectra.

  • she_she_in_dir (str) – Path to directory containing input shear-shear power spectra.

  • pos_she_in_dir (str) – Path to directory containing input position-shear power spectra.

  • lmax (int) – Maximum l to simulate.

  • lmin_in (int) – l corresponding to first row in input files.

  • pos_nl_path (str) – Path to position noise power spectrum.

  • she_nl_path (str) – Path to shear noise power spectrum.

  • nside (int) – Healpix map resolution to use.

  • lmin_out (int) – Minimum l to return.

  • mask_path (str, optional) – Path to mask fits file, or None for full sky (default).

  • verbose (bool, optional) – Whether to output progress (default True).

Returns

Observed Cls in diagonal order, with shape (n_spectra, n_ell).

Return type

2D numpy array