tuesday.core.summaries

A sub-package for calculating summaries of simulations, like power spectra.

class tuesday.core.summaries.CylindricalPS(ps: Quantity, kperp: Quantity, kpar: Quantity, redshift: float | None = None, n_modes: ndarray | None = None, variance: Quantity | None = None, is_deltasq: bool = False)[source]

Bases: object

Class to hold the 2D power spectrum data.

Variables:
  • ps (un.Quantity) – Power spectrum data, whose units depend on whether the power spectrum is is ‘dimensionless’ (i.e. the variance per log-k, commonly called delta-squared), and the units of k. A 2D array of shape (n_kperp, n_kpar).

  • kperp (un.Quantity) – Perpendicular wavenumbers, in inverse distance units. These may be bin ‘centers’ (under some definition for ‘center’) or bin edges, in which case the length of the array is one larger than the first dimension of ps.

  • kpar (un.Quantity) – Parallel wavenumbers, in inverse distance units. These may be bin ‘centers’ (under some definition for ‘center’) or bin edges, in which case the length of the array is one larger than the second dimension of ps.

  • redshift (float, optional) – The redshift at which the power spectrum was measured.

  • n_modes (np.ndarray, optional) – The number of k-modes averaged into each ps value. Must be the same shape as ps. This may be used to calculate the variance of the power spectrum.

  • variance (un.Quantity, optional) – The variance of the power spectrum, in the same units as ps (but squared). Covariances are not supported yet.

  • is_deltasq (bool, optional) – Whether the power spectrum is in delta-squared units. If True, the units of ps are expected to be temperature squared or dimensionless. If False, the units of ps are expected to be temperature squared by volume.

is_deltasq: bool = False
kpar: Quantity
property kpar_centers: Quantity

Return the centers of the kpar bins.

kperp: Quantity
property kperp_centers: Quantity

Return the centers of the kperp bins.

n_modes: ndarray | None = None
property nkpar: int

Return the number of kpar bins.

property nkperp: int

Return the number of kperp bins.

ps: Quantity
redshift: float | None = None
variance: Quantity | None = None
class tuesday.core.summaries.SphericalPS(ps: Quantity, k: Quantity, redshift: float | None = None, n_modes: ndarray | None = None, variance: Quantity | None = None, is_deltasq: bool = False)[source]

Bases: object

Class to hold the 1D power spectrum data.

Variables:
  • ps (un.Quantity) – Power spectrum data, whose units depend on whether the power spectrum is is ‘dimensionless’ (i.e. the variance per log-k, commonly called delta-squared), and the units of k. A 1D array of shape (n_k,).

  • k (un.Quantity) – Wavenumber data, in inverse distance units. These may be bin ‘centers’ (under some definition for ‘center’) or bin edges, in which case the length of the array is one larger than ps.

  • redshift (float, optional) – The redshift at which the power spectrum was measured.

  • n_modes (np.ndarray, optional) – The number of k-modes averaged into each ps value. Must be the same shape as ps. This may be used to calculate the variance of the power spectrum.

  • variance (un.Quantity, optional) – The variance of the power spectrum, in the same units as ps (but squared). Covariances are not supported yet.

  • is_deltasq (bool, optional) – Whether the power spectrum is in delta-squared units. If True, the units of ps are expected to be temperature squared or dimensionless. If False, the units of ps are expected to be temperature squared by volume.

is_deltasq: bool = False
k: Quantity
property kcenters: Quantity

Return the centers of the k bins.

n_modes: ndarray | None = None
property nk: int

Return the number of k bins.

ps: Quantity
redshift: float | None = None
variance: Quantity | None = None
tuesday.core.summaries.bin_kpar(bins_kpar: int | Quantity | None = None, log_kpar: bool | None = False, interp_kpar: bool | None = False, crop_kperp: tuple[int, int] | None = None, crop_kpar: tuple[int, int] | None = None)[source]

Bins the kpar axis of a CylindricalPS object.

Parameters:
  • bins_kpar (int, astropy.units.Quantity, or None, optional) – Number of bins (if int), array of bin edges (if array-like), or None to use default binning (half the number of original kpar bins). Default is None.

  • log_kpar (bool or None, optional) – If True, use logarithmic binning for kpar. If False or None, use linear binning. Default is False.

  • interp_kpar (bool or None, optional) – If True, interpolate the power spectrum onto the new kpar bins. If False or None, aggregate using bin means. Default is False.

  • crop_kperp (tuple of int or None, optional) – Tuple specifying the (start, end) indices to crop the kperp axis after binning. If None, no cropping is applied. Default is None.

  • crop_kpar (tuple of int or None, optional) – Tuple specifying the (start, end) indices to crop the kpar axis after binning. If None, no cropping is applied. Default is None.

Returns:

transform_ps (callable) – A function that takes a CylindricalPS object and returns a new CylindricalPS object with the binned kpar axis.

Raises:

ValueError – If bins_kpar is not an int or a valid array of bin edges/centres.

Notes

  • If interp_kpar is True, the power spectrum and its variance (if present) are interpolated onto the new kpar bins.

  • If interp_kpar is False, the power spectrum and its variance are aggregated using the mean within each bin.

  • Cropping is applied after binning/interpolation.

tuesday.core.summaries.calculate_ps(chunk: ~astropy.units.quantity.Quantity, box_length: ~astropy.units.quantity.Quantity, *, chunk_redshift: float | None = None, calc_2d: bool | None = True, kperp_bins: int | ~numpy.ndarray | None = None, k_weights_2d: ~collections.abc.Callable | None = <function ignore_zero_ki>, log_bins: bool | None = True, calc_1d: bool | None = False, k_bins: int | None = None, k_weights_1d: ~collections.abc.Callable | None = <function ignore_zero_ki>, bin_ave: bool | None = True, interp: bool | None = None, prefactor_fnc: ~collections.abc.Callable | None = <function power2delta>, interp_points_generator: ~collections.abc.Callable | None = None, get_variance: bool | None = False) tuple[SphericalPS | None, CylindricalPS | None][source]

Calculate power spectra from a lightcone or coeval box.

Parameters:
  • chunk (un.Quantity) – The 3D chunk whose power spectrum we want to calculate. This can be either a coeval box or a lightcone chunk.

  • box_length (un.Quantity) – The side length of the box. Accepted units are: Mpc and Mpc/h.

  • chunk_redshift (float, optional) – The central redshift of the lightcone chunk or coeval box.

  • calc_2d (bool, optional) – If True, calculate the 2D power spectrum.

  • kperp_bins (int, optional) – The number of bins to use for the kperp axis of the 2D PS.

  • k_weights (callable, optional) – A function that takes a frequency tuple and returns a boolean mask for the k values to ignore. See powerbox.tools.ignore_zero_ki for an example and powerbox.tools.get_power documentation for more details. Default is powerbox.tools.ignore_zero_ki, which excludes the power any k_i = 0 mode. Typically, only the central zero mode |k| = 0 is excluded, in which case use powerbox.tools.ignore_zero_absk.

  • calc_1d (bool, optional) – If True, calculate the 1D power spectrum.

  • k_bins (int, optional) – The number of bins on which to calculate 1D PS.

  • bin_ave (bool, optional) – If True, return the center value of each kperp and kpar bin i.e. len(kperp) = ps_2d.shape[0]. If False, return the left edge of each bin i.e. len(kperp) = ps_2d.shape[0] + 1.

  • interp (str, optional) – If True, use linear interpolation to calculate the PS at the points specified by interp_points_generator. Note that this significantly slows down the calculation.

  • prefactor_fnc (callable, optional) – A function that takes a frequency tuple and returns the prefactor to multiply the PS with. Default is powerbox.tools.power2delta, which converts the power P [mK^2 Mpc^{-3}] to the dimensionless power \(\\delta^2\) [mK^2].

  • interp_points_generator (callable, optional) – A function that generates the points at which to interpolate the PS. See powerbox.tools.get_power documentation for more details.

  • get_variance (bool, optional) – If True, compute the variance of the PS over the modes within each bin. Default is False.

Returns:

  • ps1d (SphericalPS or None) – The 1D power spectrum. None if calc_1d is False.

  • ps2d (CylindricalPS or None) – The 2D power spectrum. None if calc_2d is False.

tuesday.core.summaries.calculate_ps_coeval(box: ~astropy.units.quantity.Quantity, box_length: ~astropy.units.quantity.Quantity, *, box_redshift: float | None = None, calc_2d: bool | None = True, kperp_bins: int | None = None, k_weights_2d: ~collections.abc.Callable | None = <function ignore_zero_ki>, k_weights_1d: ~collections.abc.Callable | None = <function ignore_zero_ki>, log_bins: bool | None = True, calc_1d: bool | None = True, k_bins: int | None = None, mu_min: float | None = None, bin_ave: bool | None = True, interp: bool | None = None, deltasq: bool | None = True, interp_points_generator: ~collections.abc.Callable | None = None, get_variance: bool | None = False, transform_ps1d: ~collections.abc.Callable | None = None, transform_ps2d: ~collections.abc.Callable | None = None) tuple[SphericalPS | None, CylindricalPS | None][source]

Calculate the PS by chunking a lightcone.

Parameters:
  • box (un.Quantity) – The coeval box with units of temperature or dimensionless.

  • box_length (un.Quantity) – The side length of the box, accepted units are length.

  • box_redshift (float, optional) – The redshift value of the coeval box.

  • chunk_redshift (float, optional) – The central redshift of the lightcone chunk or coeval box.

  • calc_2d (bool, optional) – If True, calculate the 2D power spectrum.

  • kperp_bins (int, optional) – The number of bins to use for the kperp axis of the 2D PS.

  • k_weights (callable, optional) – A function that takes a frequency tuple and returns a boolean mask for the k values to ignore. See powerbox.tools.ignore_zero_ki for an example and powerbox.tools.get_power documentation for more details. Default is powerbox.tools.ignore_zero_ki, which excludes the power any k_i = 0 mode. Typically, only the central zero mode |k| = 0 is excluded, in which case use powerbox.tools.ignore_zero_absk.

  • calc_1d (bool, optional) – If True, calculate the 1D power spectrum.

  • k_bins (int, optional) – The number of bins on which to calculate 1D PS.

  • mu_min (float, optional) – The minimum value of \(\\cos(\theta), \theta = \arctan (k_\\perp/k_\\parallel)\) for all calculated PS. If None, all modes are included.

  • bin_ave (bool, optional) – If True, return the center value of each kperp and kpar bin i.e. len(kperp) = ps_2d.shape[0]. If False, return the left edge of each bin i.e. len(kperp) = ps_2d.shape[0] + 1.

  • interp (str, optional) – If True, use linear interpolation to calculate the PS at the points specified by interp_points_generator. Note that this significantly slows down the calculation.

  • delta (bool, optional) – Whether to convert the power P [mK^2 Mpc^{-3}] to the dimensionless power \(\\delta^2\) [mK^2]. Default is True.

  • interp_points_generator (callable, optional) – A function that generates the points at which to interpolate the PS. See powerbox.tools.get_power documentation for more details.

  • get_variance (bool, optional) – Whether to calculate the variance of the PS. Default is False.

  • transform_ps2d (Callable, optional) – A function that takes in a CylindricalPS object and returns a new CylindricalPS object.

  • transform_ps1d (Callable, optional) – A function that takes in a SphericalPS object and returns a new SphericalPS object.

  • get_variance (bool, optional) – Whether to calculate the variance of the PS. Default is False.

  • interp (bool, optional) – If True, use linear interpolation to calculate the PS at the points specified by interp_points_generator. Note that this significantly slows down the calculation.

  • deltasq (bool, optional) – Whether to convert the power P [mK^2 Mpc^{-3}] to the dimensionless power \(\\delta^2\) [mK^2]. Default is True.

  • transform_ps1d (Callable, optional) – A function that takes in a SphericalPS object and returns a new SphericalPS object.

  • transform_ps2d (Callable, optional) – A function that takes in a CylindricalPS object and returns a new CylindricalPS object.

Returns:

  • ps1d (SphericalPS or None) – The 1D power spectrum. None if calc_1d is False.

  • ps2d (CylindricalPS or None) – The 2D power spectrum. None if calc_2d is False.

tuesday.core.summaries.calculate_ps_lc(lc: ~astropy.units.quantity.Quantity, box_length: ~astropy.units.quantity.Quantity, lc_redshifts: ~numpy.ndarray, *, ps_redshifts: float | ~numpy.ndarray | None = None, chunk_indices: list | None = None, chunk_size: int | None = None, chunk_skip: int | None = None, calc_2d: bool = True, kperp_bins: int | None = None, k_weights_2d: ~collections.abc.Callable | None = <function ignore_zero_ki>, k_weights_1d: ~collections.abc.Callable | None = <function ignore_zero_ki>, log_bins: bool = True, calc_1d: bool = True, k_bins: int | None = None, mu_min: float | None = None, bin_ave: bool = True, interp: bool | None = None, deltasq: bool = True, interp_points_generator: ~collections.abc.Callable | None = None, get_variance: bool = False, transform_ps1d: ~collections.abc.Callable | None = None, transform_ps2d: ~collections.abc.Callable | None = None) tuple[list[SphericalPS] | None, list[CylindricalPS] | None][source]

Calculate the PS by chunking a lightcone.

Parameters:
  • lc (un.Quantity) – The lightcone with units of temperature or dimensionless.

  • box_length (un.Quantity) – The side length of the box, accepted units are length.

  • lc_redshifts (np.ndarray) – The redshift of each lightcone slice. Array has same length as lc.shape[-1].

  • chunk_redshift (float, optional) – The central redshift of the lightcone chunk or coeval box.

  • calc_2d (bool, optional) – If True, calculate the 2D power spectrum.

  • kperp_bins (int, optional) – The number of bins to use for the kperp axis of the 2D PS.

  • k_weights (callable, optional) – A function that takes a frequency tuple and returns a boolean mask for the k values to ignore. See powerbox.tools.ignore_zero_ki for an example and powerbox.tools.get_power documentation for more details. Default is powerbox.tools.ignore_zero_ki, which excludes the power any k_i = 0 mode. Typically, only the central zero mode |k| = 0 is excluded, in which case use powerbox.tools.ignore_zero_absk.

  • calc_1d (bool, optional) – If True, calculate the 1D power spectrum.

  • k_bins (int, optional) – The number of bins on which to calculate 1D PS.

  • mu_min (float, optional) – The minimum value of \(\\cos(\theta), \theta = \arctan (k_\\perp/k_\\parallel)\) for all calculated PS. If None, all modes are included.

  • bin_ave (bool, optional) – If True, return the center value of each kperp and kpar bin i.e. len(kperp) = ps_2d.shape[0]. If False, return the left edge of each bin i.e. len(kperp) = ps_2d.shape[0] + 1.

  • interp (str, optional) – If True, use linear interpolation to calculate the PS at the points specified by interp_points_generator. Note that this significantly slows down the calculation.

  • delta (bool, optional) – Whether to convert the power P [mK^2 Mpc^{-3}] to the dimensionless power \(\\delta^2\) [mK^2]. Default is True.

  • interp_points_generator (callable, optional) – A function that generates the points at which to interpolate the PS. See powerbox.tools.get_power documentation for more details.

  • transform_ps2d (Callable, optional) – A function that takes in a CylindricalPS object and returns a new CylindricalPS object.

  • transform_ps1d (Callable, optional) – A function that takes in a SphericalPS object and returns a new SphericalPS object.

  • get_variance (bool, optional) – Whether to calculate the variance of the PS. Default is False.

  • chunk_indices (list, optional) – A list of tuples specifying the start and end indices of the lightcone chunks for which power spectra are calculated.

Returns:

  • ps1d (list of SphericalPS or None) – The 1D power spectrum for each chunk. None if calc_1d is False.

  • ps2d (list of CylindricalPS or None) – The 2D power spectrum for each chunk. None if calc_2d is False.

tuesday.core.summaries.cylindrical_to_spherical(ps, kperp, kpar, nbins=16, weights=1, interp=False, mu_min=None, generator=None, bin_ave=True)[source]

Angularly average 2D PS to 1D PS.

Parameters:
  • ps (np.ndarray) – The 2D power spectrum of shape [len(kperp), len(kpar)].

  • kperp (np.ndarray) – Values of kperp.

  • kpar (np.ndarray) – Values of kpar.

  • nbins (int, optional) – The number of bins on which to calculate 1D PS. Default is 16

  • weights (np.ndarray, optional) – Weights to apply to the PS before averaging. Note that to obtain a 1D PS from the 2D PS that is consistent with the 1D PS obtained directly from the 3D PS, the weights should be the number of modes in each bin of the 2D PS (Nmodes).

  • interp (bool, optional) – If True, use linear interpolation to calculate the 1D PS.

  • mu_min (float, optional) – The minimum value of \(\\cos(\theta), \theta = \arctan (k_\\perp/k_\\parallel)\) for all calculated PS. If None, all modes are included.

  • generator (callable, optional) – A function that generates the points at which to interpolate the PS. See powerbox.tools.get_power documentation for more details.

  • bin_ave (bool, optional) – If True, return the center value of each k bin i.e. len(k) = ps_1d.shape[0]. If False, return the left edge of each bin i.e. len(k) = ps_1d.shape[0] + 1.