ler.utils
Submodules
Package Contents
Classes
Class for storing a numpy.ndarray or any nested-list composition as JSON file. This is required for dealing np.nan and np.inf. |
Functions
|
Load a json file. |
|
Append and update a json file with a dictionary. |
|
Adds the values of two dictionaries together. |
|
Function to get the parameters from json file. |
|
Helper function for rejection sampling from a pdf with maximum and minimum arguments. |
|
Helper function for rejection sampling from a 2D pdf with maximum and minimum arguments. |
|
Adds two dictionaries with the same keys together. |
|
Filters an event dictionary to only contain the size. |
|
Function to create a interpolated function, inverse function or inverse cdf from the input x and y. |
|
pdf_func is the function to calculate the pdf of x given y |
|
Function to create a spline interpolated function from the input x and y. |
|
Function to create a spline interpolated inverse function from the input x and y. |
|
Function to create a spline interpolated normalized pdf from the input x and y. |
|
Function to create a spline interpolated inverse cdf from the input x and y. |
|
Function to create a conditioned pdf from the input x and y. |
|
Function to create a conditioned inv_cdf from the input x and y. |
|
Function to decide which interpolator to use. |
|
Function to create the interpolator pickle file path. |
|
Function to find the pdf interpolator coefficients from the conditioned y. |
|
Function to find sampler interpolator coefficients from the conditioned y. |
|
Function to interpolate using cubic spline. |
|
Function to sample from the inverse transform method. |
|
Function to run the sampling in batches. |
|
Function to get the parameters from json file. |
|
Function to plot the distribution of the GW source parameters. |
|
Function to generate relative magnification vs time delay difference for unlensed samples. |
|
Function to classify the lensed images wrt to the morse phase difference. |
|
Function to generate 2D KDE and plot the relative magnification vs time delay difference for lensed samples. |
- class ler.utils.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]
Bases:
json.JSONEncoder
Class for storing a numpy.ndarray or any nested-list composition as JSON file. This is required for dealing np.nan and np.inf.
- Parameters:
- json.JSONEncoderclass
class for encoding JSON file
- Returns:
- json.JSONEncoder.defaultfunction
function for encoding JSON file
- ler.utils.load_json(file_name)[source]
Load a json file.
- Parameters:
- file_namestr
json file name for storing the parameters.
- Returns:
- paramdict
- ler.utils.append_json(file_name, new_dictionary, old_dictionary=None, replace=False)[source]
Append and update a json file with a dictionary.
- Parameters:
- file_namestr
json file name for storing the parameters.
- new_dictionarydict
dictionary to be appended to the json file.
- replacebool, optional
If True, replace the json file with the dictionary. Default is False.
- ler.utils.add_dict_values(dict1, dict2)[source]
Adds the values of two dictionaries together.
- Parameters:
- dict1dict
dictionary to be added.
- dict2dict
dictionary to be added.
- Returns:
- dict1dict
dictionary with added values.
- ler.utils.get_param_from_json(json_file)[source]
Function to get the parameters from json file.
- Parameters:
- json_filestr
json file name for storing the parameters.
- Returns:
- paramdict
- ler.utils.rejection_sample(pdf, xmin, xmax, size=100, chunk_size=10000)[source]
Helper function for rejection sampling from a pdf with maximum and minimum arguments.
- Parameters:
- pdffunction
pdf function.
- xminfloat
minimum value of the pdf.
- xmaxfloat
maximum value of the pdf.
- sizeint, optional
number of samples. Default is 100.
- chunk_sizeint, optional
chunk size for sampling. Default is 10000.
- Returns:
- x_samplenumpy.ndarray
samples from the pdf.
- ler.utils.rejection_sample2d(pdf, xmin, xmax, ymin, ymax, size=100, chunk_size=10000)[source]
Helper function for rejection sampling from a 2D pdf with maximum and minimum arguments.
- Parameters:
- pdffunction
2D pdf function.
- xminfloat
minimum value of the pdf in the x-axis.
- xmaxfloat
maximum value of the pdf in the x-axis.
- yminfloat
minimum value of the pdf in the y-axis.
- ymaxfloat
maximum value of the pdf in the y-axis.
- sizeint, optional
number of samples. Default is 100.
- chunk_sizeint, optional
chunk size for sampling. Default is 10000.
- Returns:
- x_samplenumpy.ndarray
samples from the pdf in the x-axis.
- ler.utils.add_dictionaries_together(dictionary1, dictionary2)[source]
Adds two dictionaries with the same keys together.
- Parameters:
- dictionary1dict
dictionary to be added.
- dictionary2dict
dictionary to be added.
- Returns:
- dictionarydict
dictionary with added values.
- ler.utils.trim_dictionary(dictionary, size)[source]
Filters an event dictionary to only contain the size.
- Parameters:
- dictionarydict
dictionary to be trimmed.
- sizeint
size to trim the dictionary to.
- Returns:
- dictionarydict
trimmed dictionary.
- ler.utils.create_func_pdf_invcdf(x, y, category='function')[source]
Function to create a interpolated function, inverse function or inverse cdf from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- ynumpy.ndarray
y values.
- categorystr, optional
category of the function. Default is “function”. Other options are “function_inverse”, “pdf” and “inv_cdf”.
- Returns:
- pdfpdf function
interpolated pdf function.
- inv_pdffunction inverse
interpolated inverse pdf function.
- inv_cdffunction
interpolated inverse cdf.
- ler.utils.create_conditioned_pdf_invcdf(x, conditioned_y, pdf_func, category)[source]
pdf_func is the function to calculate the pdf of x given y x is an array and the output of pdf_func is an array y is the condition we consider parameter plane of x and y
- Parameters:
- xnumpy.ndarray
x values.
- conditioned_ynumpy.ndarray
conditioned y values.
- pdf_funcfunction
function to calculate the pdf of x given y.
- categorystr, optional
category of the function. Default is “function”. Other options are “function_inverse”, “pdf” and “inv_cdf”.
- ler.utils.create_func(x, y)[source]
Function to create a spline interpolated function from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- ynumpy.ndarray
y values.
- Returns:
- cnumpy.ndarray
spline coefficients.
- ler.utils.create_func_inv(x, y)[source]
Function to create a spline interpolated inverse function from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- ynumpy.ndarray
y values.
- Returns:
- cnumpy.ndarray
spline coefficients.
- ler.utils.create_pdf(x, y)[source]
Function to create a spline interpolated normalized pdf from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- ynumpy.ndarray
y values.
- Returns:
- cnumpy.ndarray
spline coefficients.
- ler.utils.create_inv_cdf_array(x, y)[source]
Function to create a spline interpolated inverse cdf from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- ynumpy.ndarray
y values.
- Returns:
- cnumpy.ndarray
spline coefficients.
- ler.utils.create_conditioned_pdf(x, conditioned_y, pdf_func)[source]
Function to create a conditioned pdf from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- conditioned_ynumpy.ndarray
conditioned y values.
- pdf_funcfunction
function to calculate the pdf of x given y.
- Returns:
- list_list
list of pdfs.
- ler.utils.create_conditioned_inv_cdf_array(x, conditioned_y, pdf_func)[source]
Function to create a conditioned inv_cdf from the input x and y.
- Parameters:
- xnumpy.ndarray
x values.
- conditioned_ynumpy.ndarray
conditioned y values.
- pdf_funcfunction
function to calculate the pdf of x given y.
- Returns:
- list_list
list of inv_cdfs.
- ler.utils.interpolator_from_pickle(param_dict_given, directory, sub_directory, name, x, pdf_func=None, y=None, conditioned_y=None, dimension=1, category='pdf', create_new=False)[source]
Function to decide which interpolator to use.
- Parameters:
- param_dict_givendict
dictionary of parameters.
- directorystr
directory to store the interpolator.
- sub_directorystr
sub-directory to store the interpolator.
- namestr
name of the interpolator.
- xnumpy.ndarray
x values.
- pdf_funcfunction
function to calculate the pdf of x given y.
- ynumpy.ndarray
y values.
- conditioned_ynumpy.ndarray
conditioned y values.
- dimensionint
dimension of the interpolator. Default is 1.
- categorystr
category of the function. Default is “pdf”.
- create_newbool
if True, create a new interpolator. Default is False.
- Returns:
- interpolatorfunction
interpolator function.
- ler.utils.interpolator_pickle_path(param_dict_given, directory, sub_directory, interpolator_name)[source]
Function to create the interpolator pickle file path.
- Parameters:
- param_dict_givendict
dictionary of parameters.
- directorystr
directory to store the interpolator.
- sub_directorystr
sub-directory to store the interpolator.
- interpolator_namestr
name of the interpolator.
- Returns:
- path_inv_cdfstr
path of the interpolator pickle file.
- it_existbool
if True, the interpolator exists.
- ler.utils.interpolator_pdf_conditioned(x, conditioned_y, y_array, interpolator_list)[source]
Function to find the pdf interpolator coefficients from the conditioned y.
- Parameters:
- xnumpy.ndarray
x values.
- conditioned_yfloat
conditioned y value.
- y_arraynumpy.ndarray
y values.
- interpolator_listlist
list of interpolators.
- Returns:
- interpolator_list[idx](x)numpy.ndarray
samples from the interpolator.
- ler.utils.interpolator_sampler_conditioned(conditioned_y, y_array, interpolator_list, size=1000)[source]
Function to find sampler interpolator coefficients from the conditioned y.
- Parameters:
- conditioned_yfloat
conditioned y value.
- y_arraynumpy.ndarray
y values.
- interpolator_listlist
list of interpolators.
- sizeint
number of samples.
- Returns:
- ler.utils.cubic_spline_interpolator(xnew, coefficients, x)[source]
Function to interpolate using cubic spline.
- Parameters:
- xnewnumpy.ndarray
new x values.
- coefficientsnumpy.ndarray
coefficients of the cubic spline.
- xnumpy.ndarray
x values.
- Returns:
- resultnumpy.ndarray
interpolated values.
- ler.utils.inverse_transform_sampler(size, cdf, x)[source]
Function to sample from the inverse transform method.
- Parameters:
- sizeint
number of samples.
- cdfnumpy.ndarray
cdf values.
- xnumpy.ndarray
x values.
- Returns:
- samplesnumpy.ndarray
samples from the cdf.
- ler.utils.batch_handler(size, batch_size, sampling_routine, output_jsonfile, save_batch=True, resume=False)[source]
Function to run the sampling in batches.
- Parameters:
- sizeint
number of samples.
- batch_sizeint
batch size.
- sampling_routinefunction
function to sample the parameters. e.g. unlensed_sampling_routine() or lensed_sampling_routine()
- output_jsonfilestr
name of the json file to store the parameters.
- resumebool
if True, it will resume the sampling from the last batch. default resume = False.
- ler.utils.get_param_from_json(json_file)[source]
Function to get the parameters from json file.
- Parameters:
- json_filestr
json file name for storing the parameters.
- Returns:
- paramdict
- ler.utils.param_plot(param_name='zs', param_dict='./gw_params.json', plot_label='zs', param_min=None, param_max=None, kde=True, kde_bandwidth=0.2, histogram=True, histogram_bins=30)[source]
Function to plot the distribution of the GW source parameters.
- Parameters:
- param_namestr
name of the parameter to plot. default param_name = ‘zs’.
- param_dictdict or str
dictionary of GW source parameters or json file name. default param_dict = ‘./gw_params.json’.
- param_xlabelstr
x-axis label. default param_xlabel = ‘source redshift’.
- param_ylabelstr
y-axis label. default param_ylabel = ‘probability density’.
- param_minfloat
minimum value of the parameter. default param_min = None.
- param_maxfloat
maximum value of the parameter. default param_max = None.
- figsizetuple
figure size. default figsize = (4, 4).
- kdebool
if True, kde will be plotted. default kde = True.
- kde_bandwidthfloat
bandwidth for kde. default kde_bandwidth = 0.2.
- histogrambool
if True, histogram will be plotted. default histogram = True.
- histogram_binsint
number of bins for histogram. default histogram_bins = 30.
Examples
>>> import matplotlib.pyplot as plt >>> from ler.rates import LeR >>> ler = LeR() >>> param = ler.unlensed_cbc_statistics(); >>> rate, param_detectable = ler.unlensed_rate() >>> plt.figure(figsize=(6, 4)) >>> ler.param_plot(param_name='zs', param_dict=param, plot_label='all events') >>> ler.param_plot(param_name='zs', param_dict=param_detectable, plot_label='detectable events') >>> plt.xlabel('source redshift') >>> plt.ylabel('probability density') >>> plt.title('source redshift distribution') >>> plt.grid(alpha=0.5) >>> plt.savefig('source_redshift_distribution.png')
- ler.utils.relative_mu_dt_unlensed(param, size=100)[source]
Function to generate relative magnification vs time delay difference for unlensed samples.
- Parameters:
- paramdict
dictionary of unlensed GW source parameters. unlensed_param.keys() = [‘m1’, ‘m2’, ‘z’, ‘snr’, ‘theta_jn’, ‘ra’, ‘dec’, ‘psi’, ‘phase’, ‘geocent_time’]
- Returns:
- dmufloat.array
relative magnification.
- dtfloat.array
relative time delay.
- ler.utils.relative_mu_dt_lensed(lensed_param, snr_threshold=[8.0, 8.0])[source]
Function to classify the lensed images wrt to the morse phase difference.
- Parameters:
- lensed_paramdict
dictionary of lensed GW source parameters, lens galaxy parameters and image paramters. lensed_param.keys() = [‘zl’, ‘zs’, ‘sigma’, ‘q’, ‘e1’, ‘e2’, ‘gamma1’, ‘gamma2’, ‘Dl’, ‘Ds’, ‘Dls’, ‘theta_E’, ‘gamma’, ‘mass_1’, ‘mass_2’, ‘mass_1_source’, ‘mass_2_source’, ‘luminosity_distance’, ‘theta_jn’, ‘psi’, ‘phase’, ‘geocent_time’, ‘ra’, ‘dec’, ‘n_images’, ‘x0_image_positions’, ‘x1_image_positions’, ‘magnifications’, ‘time_delays’, ‘traces’, ‘determinants’, ‘image_type’, ‘weights’, ‘optimal_snr_net’, ‘L1’, ‘H1’, ‘V1’]
- snr_thresholdfloat
threshold for detection signal to noise ratio. e.g. snr_threshold = [8.,8.] or [8.,6.] for subthreshold
- Returns:
- mu_rel0float.array
relative magnification for 0 degree phase difference.
- dt_rel0float.array
relative time delay for 0 degree phase difference.
- mu_rel90float.array
relative magnification for 90 degree phase difference.
- dt_rel90float.array
relative time delay for 90 degree phase difference.
- ler.utils.mu_vs_dt_plot(x_array, y_array, savefig=False, ax=None, colors='blue', linestyles='-', origin='upper', alpha=0.6, extent=[0.01, 500.0, 0.01, 100.0], contour_levels=[0.1, 0.4, 0.68, 0.95])[source]
Function to generate 2D KDE and plot the relative magnification vs time delay difference for lensed samples.
- Parameters:
- x_arrayfloat.array
x array.
- y_arrayfloat.array
y array.
- xlabelstr
x label.
- ylabelstr
y label.
- titlestr
title.
- savefigbool
if True, it will save the figure. default savefig = False.
- axmatplotlib.axes
matplotlib axes. default ax = None.
- colorsstr
color of the plot. default colors = ‘blue’.
- linestylesstr
linestyle of the plot. default linestyles = ‘-‘.
- originstr
origin of the plot. default origin = ‘upper’.
- alphafloat
alpha of the plot. default alpha = 0.6.
- extentlist
extent of the plot. default extent = [1e-2,5e2,1e-2,1e2].
- contour_levelslist
contour levels of the plot. default contour_levels = [0.10,0.40,0.68,0.95] which corresponds to 1,2,3,4 sigma.
- Returns:
- None