This page details the classes and methods provided by the py21cmfish module.

Parameters Class

class py21cmfish.Parameter(param, HII_DIM=200, BOX_LEN=400, min_redshift=5.0, n_chunks=24, k_PEAK_order=2.0, output_dir='/home/docs/checkouts/readthedocs.org/user_builds/21cmfish/checkouts/latest/examples/data/', PS_err_dir='/home/docs/checkouts/readthedocs.org/user_builds/21cmfish/checkouts/latest/examples/data/21cmSense_noise/21cmSense_fid_EOS21/', Park19=None, k_HERA=True, cosmology='CDM', clobber=False, new=False, fid_only=False, vb=True)

Class for creating derivatives given 21cm parameters

derivative_global_signal(save=True, plot=True, ax=None)

Calculate global signal derivatives

derivative_power_spectrum(save=True, plot=True, ax=None)

Calculate power spectrum derivatives

  • save (bool, optional) – Save PS derivative to file?

  • plot (bool, optional) – Plot PS derivatives as a function of redshift

  • ax (Union[None, plt.Axes]) – Matplotlib axes to plot on. If None, make a new figure with subplots


Given k centers, find the bin edges and length to give to powerbox

Because 21cmsense interpolates PS onto k grid based on HERA baselines etc, we must generate our PS and Poisson noise on that same k grid in order to get the errors for the Fisher.

(Because if we used e.g. smaller k bins, our Poisson error would be too large)

get_global_signal(save=True, plot=False)

Get global signal and parameters and save to a file


Load lightcones and theta params

get_power_spectra(n_psbins=50, k_min=None, k_max=None, save=True)

Make 21cm power spectra from redshift chunk list (bin edges)

  • n_psbins (int) – Number of k bins

  • k_min (float, optional) – Minimum k value for PS [in 1/Mpc]

  • k_max (float, optional) – Maximum k value for PS [in 1/Mpc]

  • save (bool) – Save PS to file?


Load 21cmsense errors from a given directory and save arrays to self


Load Poisson noise from PS


Make fiducial PS in 21cmsense k bins [Mpc^-1]

Fisher matrix functions

py21cmfish.fishy.Fij(dObs_dtheta_i, dObs_dtheta_j, sigma_obs=1.0, sigma_mod=0.0, sigma_poisson=0.0, axis=None)

Make fisher matrix elements

  • dObs_dtheta_i (array_like) – derivative wrt theta_i

  • dObs_dtheta_j (array_like) – derivative wrt theta_j

  • sigma_obs (array_like) – measurement uncertainties in the observations

  • sigma_mod – modelling uncertainty

  • axis (None or int or tuple of ints, optional) – Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.


F_ij fisher matrix element

Return type


py21cmfish.fishy.fisher_correlations(Fij_matrix, fisher_params, plot=True)

Fisher correlation matrix

  • Fij_matrix (array_like) – Fisher information matrix

  • fisher_params (list) – ordered list of parameters in the fisher matrix

  • plot (bool) – heatmap plot


Return type

R_ij_fisher correlation matrix

py21cmfish.fishy.get_ellipse_params(i: int, j: int, cov: numpy.array)

Extract ellipse parameters from covariance matrix. Based on Coe 2009

  • i (int) – index of parameter 1

  • j (int) – index of parameter 2

  • cov (array_like) – covariance matrix


Return type

ellipse a, b, angle in degrees

py21cmfish.fishy.make_fisher_matrix(params_dict, fisher_params, hpeak=0.0, obs='GS', sigma=None, sigma_mod_frac=0.0, k_min=None, k_max=None, z_min=None, z_max=None, axis_PS=None, cosmo_key='CDM', add_sigma_poisson=False)

Make Fisher matrix and its inverse from global signal or powerspectra

  • params_dict (dict) – Dictionary of parameter objects

  • fisher_params (list) – List of parameter strings to use for Fisher matrix (these strings must be the keys to params_dict)

  • hpeak (float) – TODO

  • obs (str) – ‘GS’ - global signal, ‘PS’ - power spectrum

  • sigma (None,array) – TODO

  • sigma_mod_frac (float) – Fraction of modelling error in PS e.g. 0.2 adds a 20% error on the PS in quadrature to the 21cmsense error

  • k_min (None,float) – Minimum k to use for PS [1/Mpc]

  • k_max (None,float) – Maximum k to use for PS [1/Mpc]

  • z_min (None,float) – Minimum redshift to use for PS

  • z_max (None,float) – Maximum redshift to use for PS

  • axis_PS (None,int) – TODO

  • cosmo_key (None,str) – TODO

  • add_sigma_poisson (bool) – TODO


Return type

Fisher matrix, Finv matrix

py21cmfish.fishy.plot_ellipse(ax, par1, par2, parameters, fiducial, cov, resize_lims=True, positive_definite=[], N_std=[1.0, 2.0, 3.0], plot_rescale=4.0, kwargs=[{'ls': '-'}], color='tab:blue', default_kwargs={'lw': 0})

Plot N-sigma ellipses, from Coe 2009.

  • ax (matpotlib axis) – axis upon which the ellipses will be drawn

  • par1 (string) – parameter 1 name

  • par2 (string) – parameter 2 name

  • parameters (list) – list of parameter names

  • fiducial (array_like(ndim,)) – fiducial values of parameters

  • cov (array_like(ndim,ndim,)) – covariance matrix

  • color (string) – color to plot ellipse with

  • positive_definite (list of string) – convenience input, parameter names passed in this list will be cut off at 0 in plots.


list of float

Return type

sigma_x, sigma_y, sigma_xy

py21cmfish.fishy.plot_triangle(params, fiducial, cov, fig=None, ax=None, positive_definite=[], labels=None, resize_lims=True, N_std=[1.0, 2.0], plot_rescale=4.0, ellipse_color='tab:blue', ellipse_kwargs=[{}, {'alpha': 0.5}], title_fontsize=20, xlabel_kwargs={'fontsize': 18, 'labelpad': 5}, ylabel_kwargs={'fontsize': 18, 'labelpad': 5}, fig_kwargs={'figsize': (8, 8)}, plot1D_kwargs={'c': 'black', 'lw': 1})

Make a triangle plot from a covariance matrix

Based on https://github.com/xzackli/fishchips-public/blob/master/fishchips/util.py

  • params (list of strings) – List of parameter strings

  • fiducial (array) – Numpy array consisting of where the centers of ellipses should be

  • cov (numpy array) – Covariance matrix to plot

  • fig (optional, matplotlib figure) – Pass this if you already have a figure

  • ax (array containing matplotlib axes) – Pass this if you already have a set of matplotlib axes

  • positive_definite (list) – List of parameter strings which are positive definite

  • resize_lims (bool) – Resize ellipse limits to scale of the errors [default = True]

  • N_std (list) – List of number of standard deviations to plot

  • labels (list) – List of labels corresponding to each dimension of the covariance matrix

  • ellipse_kwargs (dict) – Keyword arguments for passing to the 1-sigma Matplotlib Ellipse call. You can change this to change the color of your ellipses, for example.

  • xlabel_kwargs (dict) – Keyword arguments which are passed to ax.set_xlabel(). You can change the color and font-size of the x-labels, for example. By default, it includes a little bit of label padding.

  • ylabel_kwargs (dict) – Keyword arguments which are passed to ax.set_ylabel(). You can change the color and font-size of the y-labels, for example. By default, it includes a little bit of label padding.

  • fig_kwargs (dict) – Keyword arguments which are passed to figure. E.g. figsize

  • plot1D_kwargs (dict) – Keyword arguments which are passed to plt.plot() for 1D gauss plot


matplotlib figure and axis array

Return type

fig, ax

py21cmfish.fishy.title_double_ellipses(axes, labels, chain=None, med=None, sigma=None, title_fontsize=18, title_pad=58, vspace=0.0, color='k')

Plot title with parameter constraints from 2 covariance matrixes/chains

  • axes (matpotlib axess) – axes upon which the titles will be added

  • labels (list(ndim,)) – list of parameter names

  • chain (array_like(ndim,), optional) – MCMC chain of parameters

  • med (array_like(ndim,), optional) – list of median values

  • sigma (array_like(ndim,)) – list of sigmas

  • color (string) – color to plot ellipse with


Return type


Power spectra functions

py21cmfish.power_spectra.compute_power(box, length, n_psbins, log_bins=True, k_min=None, k_max=None, ignore_kperp_zero=True, ignore_kpar_zero=False, ignore_k_zero=False)

Calculate power spectrum for a redshift chunk


  • box – lightcone brightness_temp chunk

  • length – TODO

  • n_psbins (int) – number of k bins


  • k (1/Mpc)

  • delta (mK^2)

  • err_delta (mK^2)

py21cmfish.power_spectra.get_k_min_max(lightcone, n_chunks=24)

Get the minimum and maximum k in 1/Mpc to calculate powerspectra for given size of box and number of chunks

py21cmfish.power_spectra.powerspectra(brightness_temp, n_psbins=50, nchunks=10, k_min=0.1, k_max=1.0, logk=True)

Make power spectra for given number of equally spaced chunks


k : 1/Mpc delta : mK^2 err_delta : mK^2

py21cmfish.power_spectra.powerspectra_chunks(lightcone, nchunks=10, chunk_indices=None, n_psbins=50, k_min=0.1, k_max=1.0, logk=True, model_uncertainty=0.15, error_on_model=True, ignore_kperp_zero=True, ignore_kpar_zero=False, ignore_k_zero=False, remove_nans=True, vb=False)

Make power spectra for given number of equally spaced redshift chunks OR list of redshift chunk lightcone indices


k : 1/Mpc delta : mK^2 err_delta : mK^2

TODO this isn’t using k_min, k_max…

py21cmfish.power_spectra.powerspectra_np(brightness_temp, n_psbins=50, nchunks=10, k_min=0.1, k_max=1.0, logk=True)

Make power spectra for given number of equally spaced chunks

JBM: same as powerspectra but for input in numpy format. Also outputs errors in delta