Skip to content

Commit

Permalink
possibility to defined grid of configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanfacchinetti committed Feb 14, 2024
1 parent 5b0209d commit 4d33076
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 73 deletions.
30 changes: 21 additions & 9 deletions build/lib/py21cmcast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,9 @@ def plot_power_spectrum(self, std = None, figname = None, plot=True, ps_modeling
figname = self._dir_path + "/power_spectra/power_spectrum.pdf"

fig.savefig(figname, bbox_layout='tight')
p21c_tools.close_figure(fig)

return fig
return

@property
def power_spectrum(self):
Expand Down Expand Up @@ -703,6 +704,7 @@ def chi2_UV_luminosity_functions(self, data_set = 'Bouwens21', plot = True):

fig.gca().legend()
fig.savefig(self._dir_path + '/global_quantities/UV_lum_func_FIDUCIAL.pdf', bbox_inches='tight')
p21c_tools.close_figure(fig)

## The chi2 is given by a sum on the elements where z > 6 (as we cannot trust 21cmFAST below)

Expand Down Expand Up @@ -730,21 +732,23 @@ def plot_xH_box(self):
xlabel=r'$z$',
ylabel=r'$x_{\rm H_{I}}$')
fig.savefig(self._dir_path + '/global_quantities/xH_FIDUCIAL.pdf', bbox_inches='tight')
p21c_tools.close_figure(fig)


def plot_global_signal(self):
fig = p21c_tools.plot_func(self.z_glob, self.global_signal, ylim=[-150, 50],
xlabel=r'$z$',
ylabel=r'$\overline{T_{\rm b}}~\rm [mK]$')
fig.savefig(self._dir_path + '/global_quantities/global_signal_FIDUCIAL.pdf', bbox_inches='tight')
p21c_tools.close_figure(fig)





class Parameter:

def __init__(self, fiducial, name, plot = True, verbose = True, **kwargs):
def __init__(self, fiducial, name, plot = True, verbose = True, values = None, **kwargs):

self._fiducial = fiducial
self._name = name
Expand All @@ -761,7 +765,7 @@ def __init__(self, fiducial, name, plot = True, verbose = True, **kwargs):
self._verbose = verbose

# Additional parameters to specify the value of the parameter
self._values = kwargs.get('values', None)
self._values = values
self._add_name = kwargs.get('add_name', '')

if self._add_name != '':
Expand Down Expand Up @@ -890,9 +894,19 @@ def __init__(self, fiducial, name, plot = True, verbose = True, **kwargs):

# Plotting the derivatives
if self._plot is True:
self.plot_ps_derivative()
self.plot_weighted_ps_derivative()
self.plot_power_spectra()

fig_der = self.plot_ps_derivative()
fig_w_der = self.plot_weighted_ps_derivative()
fig_ps = self.plot_power_spectra()

fig_der.savefig(self._dir_path + "/derivatives/derivatives_" + self._name + self._add_name + ".pdf")
fig_w_der.savefig(self._dir_path + "/derivatives/weighted_derivatives_" + self._name + self._add_name + ".pdf")
fig_ps.savefig(self._dir_path + "/power_spectra/power_spectra_" + self._name + self._add_name + ".pdf")

p21c_tools.close_figure(fig_der)
p21c_tools.close_figure(fig_w_der)
p21c_tools.close_figure(fig_ps)


@property
def ps_derivative(self):
Expand Down Expand Up @@ -1039,7 +1053,6 @@ def plot_ps_derivative(self):
fig = p21c_tools.plot_func_vs_z_and_k(self._z_array, self._k_array, der_array, marker='.', markersize=2,
title=r'$\frac{\partial \Delta_{21}^2}{\partial ' + self._tex_name + r'}$',
xlim = [0.1, 1], xlog=self._logk, ylog=False)
fig.savefig(self._dir_path + "/derivatives/derivatives_" + self._name + self._add_name + ".pdf")
return fig


Expand All @@ -1063,7 +1076,6 @@ def plot_power_spectra(self, **kwargs):
title=r'$\Delta_{21}^2 ~ {\rm [mK^2]}$',
xlog=self._logk, ylog=True, istd = _order[0], **kwargs)

fig.savefig(self._dir_path + "/power_spectra/power_spectra_" + self._name + self._add_name + ".pdf")
return fig


Expand All @@ -1078,7 +1090,7 @@ def plot_weighted_ps_derivative(self):
fig = p21c_tools.plot_func_vs_z_and_k(self._z_array, self._k_array, der_array, marker='.', markersize=2,
title=r'$\frac{1}{\sigma}\frac{\partial \Delta_{21}^2}{\partial ' + self._tex_name + r'}$',
xlim = [0.1, 1], xlog=self._logk, ylog=False)
fig.savefig(self._dir_path + "/derivatives/weighted_derivatives_" + self._name + self._add_name + ".pdf")

return fig


Expand Down
8 changes: 5 additions & 3 deletions build/lib/py21cmcast/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@



from matplotlib.pyplot import *

from matplotlib.gridspec import GridSpec
from matplotlib.lines import Line2D
from matplotlib.patches import Ellipse
import matplotlib.cm as mplt_cm
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms

import numpy as np
Expand Down Expand Up @@ -560,7 +562,7 @@ def plot_func_vs_z_and_k(z, k, func, func_err = None, std = None, istd : float

if len(func) > 1:

cmap = matplotlib.cm.get_cmap('Spectral')
cmap = mplt_cm.get_cmap('Spectral')
a_lin = (0.99-0.2)/(len(func)-1) if len(func) > 1 else 1
b_lin = 0.2 if len(func) > 1 else 0.5

Expand Down
11 changes: 10 additions & 1 deletion examples/run_lightcone.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import argparse
import py21cmcast as p21c
from astropy import units

parser = argparse.ArgumentParser()
parser.add_argument("config_file", type = str, help="Path to config file")
parser.add_argument("-nomp", "--n_omp", type = int, help="number of OMP threads available")
parser.add_argument("-nruns", "--n_runs", type = int, help="number of times to run the code < 25")
parser.add_argument("-rs", "--random_seed", type = int, help="the first random seed of the runs")
parser.add_argument("-pp", "--pre_process", help = "pre-process the lightcone and only extract part of the data in the cache", action = 'store_false')
args = parser.parse_args()

config_file = args.config_file
n_omp = args.n_omp if (args.n_omp is not None) else 1
n_runs = args.n_runs if (args.n_runs is not None) else 1
random_seed = args.random_seed if (args.random_seed is not None) else None
preprocess = args.pre_process

# Run the lightcone with the given seed
lightcone, run_id, output_dir = p21c.run_lightcone_from_config(config_file, n_omp, random_seed)
lightcone.save(fname = "Lightcone_rs" + str(lightcone.random_seed) + "_" + run_id + ".h5", direc = output_dir)

if preprocess is True:
z_bins, z_centers, k_bins = p21c.define_grid_modes_redshifts(6., 8 * units.MHz, z_max = 22, k_min = 0.1 / units.Mpc, k_max = 1 / units.Mpc)
p21c.Run(output_dir, "Lightcone_rs" + str(lightcone.random_seed) + "_" + run_id + ".h5", z_bins, z_centers, k_bins, False, lightcone = lightcone, verbose = False)
else:
lightcone.save(fname = "Lightcone_rs" + str(lightcone.random_seed) + "_" + run_id + ".h5", direc = output_dir)

1 change: 1 addition & 0 deletions src/py21cmcast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .runs import (
init_runs,
init_grid_runs,
make_config_one_varying_param,
run_lightcone_from_config,
)
Expand Down
Loading

0 comments on commit 4d33076

Please sign in to comment.