bjet_mcmc.blazar_report module

Name: blazar_report.py

Contains functions for showing results of the MCMC, given the results. It calculates indices and parameters within 1sigma, finds the best parameters, creates a written text report, and creates all necessary plots.

Functions: Function save_plots_and_info (given the configurations, the data, and the minima/maxima for the parameters, and a source of MCMC data) creates a folder with an info.txt file and plots - Corner plot - Plots of chi squared (med, best, all by step) - Plots of the best model with data and models within 1 sigma (random models, extreme models, and a combination of both) - Plot of the chain

Parameters are always listed in the following order: [delta, K, n1, n2, gamma_min, gamma_max, gamma_break, B, R]

All parameters are the logarithm of the true value except for delta, n1, and n2

Parameter

Description

Scale

delta

doppler factor

linear

K

particle density [cm^-3]

log

n1

n_1 (first index)

linear

n2

n_2 (second index)

linear

gamma_min

low-energy cutoff

log

gamma_max

high-energy cutoff

log

gamma_break

energy break

log

B

magnetic field strength [G]

log

R

blob radius (cm)

log

bjet_mcmc.blazar_report.get_best_log_prob_and_params(configs=None, sampler=None, log_probs=None, flat_chain=None, discard=None, fixed_params=None)

This function get_best_log_prob_and_params takes several parameters and returns the best log probability and corresponding parameters.

Parameters:
  • configs (dict or None) – A dictionary containing configuration parameters. (Default: None)

  • sampler (emcee.EnsembleSampler) – An object that implements the get_log_prob and get_chain methods. (Default: None)

  • log_probs (numpy.ndarray or None) – An array of log probabilities. (Default: None)

  • flat_chain (numpy.ndarray or None) – An array of parameter samples. (Default: None)

  • discard (int or None) – The number of discarded samples. (Default: None)

  • fixed_params (numpy.ndarray or None) – An array of fixed parameter values. (Default: None)

Returns:

A tuple containing the maximum log probability and the corresponding parameter values.

Return type:

tuple

Raises:
  • ValueError – If sampler is None and either log_probs or flat_chain is None.

  • ValueError – If sampler is not None and either configs or discard is None.

bjet_mcmc.blazar_report.get_indices_within_1sigma(values, use_log_probs=True, alpha=0.318, eic=False, ndim=1)

This function get_indices_within_1sigma takes in several parameters and returns an array of indices within 1 standard deviation.

Parameters:
  • values (numpy.ndarray or list) – An array of values for which chi-squared values will be calculated.

  • use_log_probs (bool) – A boolean indicating whether to use log probabilities for calculating chi-squared values. Defaults to True.

  • alpha (float) – The significance level used for calculating delta chi-squared. Defaults to 0.318.

  • eic (bool) – A boolean indicating whether to use modelProperties(eic).NUM_DIM for calculating delta chi-squared. Defaults to False.

  • ndim (int) – The number of dimensions used for calculating delta chi-squared. Defaults to 1.

Returns:

An array containing the indices of values that lie within 1 sigma (delta chi-squared) of the minimum chi-squared value.

Return type:

numpy.ndarray

bjet_mcmc.blazar_report.load_from_backend(folder, flat=False)

Loads chains and log probabilities from the backend file.

Parameters:
  • folder (str) – The name of the folder which contains the backend file.

  • flat (bool) – If True, flatten the chain and log probability arrays. Defaults to False.

Returns:

A tuple containing the chain and log probability arrays.

Return type:

tuple

bjet_mcmc.blazar_report.make_text_file(output_file, configs, data_points, backend_file=None, reader=None, sampler=None, use_sampler=False, samples=None, use_samples=False, description=None, time=None, p0_source=None, acceptance_frac=None, eic=False, fixed_params=None, chi_squared_decay=None)
Parameters:
  • output_file (str) – The name of the output file where the text report will be written to.

  • configs (dict) – The configuration parameters for generating the report.

  • data_points (int) – The number of data points used in the analysis.

  • backend_file (str, optional) – The path to the backend file if available. If not provided, the reader parameter is used.

  • reader (obj, optional) – The reader object used to read the data if backend_file is not provided. If neither backend_file nor reader is provided, a ValueError is raised.

  • sampler (obj, optional) – The sampler object to use for sampling the model parameters if use_sampler is set to True. If use_sampler is False or sampler is not provided, the data_source will be determined based on the backend_file or reader parameter. Default is None.

  • use_sampler (bool, optional) – Boolean flag indicating whether to use the sampler object to generate the samples. If set to True, the sampler parameter must be provided.

  • samples (any, optional) – The samples obtained from the sampler or backend file.

  • use_samples (bool, optional) – Boolean flag indicating whether to use the provided samples object. If set to True, the samples parameter must be provided.

  • description (str, optional) – The description for the text report.

  • time (str, optional) – The time for the text report.

  • p0_source (str, optional) – The source of the initial parameters for the text report.

  • acceptance_frac (float, optional) – The acceptance fraction for the samples.

  • eic (bool) – Whether to use EIC for min/max params calculation.

  • fixed_params (dict, optional) – The fixed parameters for the model. Default is None.

Returns:

None

Return type:

None

This function generates a text report based on the provided parameters. The report includes information such as the best parameters, chi squared value, 1-sigma parameter ranges, acceptance fraction, autocorrelation time, configurations, description, time taken, and other relevant information. The report is written to the specified output file.

bjet_mcmc.blazar_report.min_max_params_1sigma(flat_data, flat_log_probs, eic=False, ndim=1)

Calculate the minimum and maximum values within the range defined by 1 sigma.

Parameters:
  • flat_data (list[list[float]]) – The flattened data array.

  • flat_log_probs (list[float]) – A list of log probabilities corresponding to each data point in flat_data.

  • eic (bool) – Whether to consider events inside the 1-sigma contour. Default is False.

  • ndim (int) – Number of dimensions of the data points. Default is 1.

Returns:

A tuple containing the minimum values and maximum values within the 1-sigma contour.

Return type:

tuple(array, array)

bjet_mcmc.blazar_report.parse_info_doc(info_doc, info=None)

This method parses the content of an info documentation file and extracts relevant information into a dictionary. The parsed information can include the folder name, report description, time, p0 label, configurations, reduced chi squared, best parameters, minimum 1 sigma parameters, maximum 1 sigma parameters, acceptance fraction, tau average, config file, and previous files.

To use this method, provide the filename of the info documentation file as the info_doc parameter. Optionally, you can provide an existing dictionary to populate or leave it as None to create a new one. The method returns the populated information dictionary.

Example Usage:
>>> info = parse_info_doc("info.txt")
Parameters:
  • info_doc (str) – The filename of the info documentation file.

  • info (dict, optional) – The dictionary object where the parsed information will be stored. If not provided, an empty dictionary will be used.

Returns:

The dictionary object with the parsed information.

Return type:

dict

bjet_mcmc.blazar_report.save(folder, description=None, time=None, p0_source=None, acceptance_frac=None, data_file=None, configs=None, text_file_name='info.txt', text_only=False, theta=None, redshift=None, min_freq=None, max_freq=None, torus_temp=None, torus_luminosity=None, torus_frac=None, data_folder=None, executable=None, command_params_full=None, command_params_1=None, command_params_2=None, verbose=False, eic=False, fixed_params=None)
Parameters:
  • folder (str) – The folder where the data and plots will be saved.

  • description (str) – A description of the data.

  • time (float) – The time of the data.

  • p0_source (str) – The p0 label of the data.

  • acceptance_frac (float) – The acceptance fraction of the data.

  • data_file (str) – The path to the data file.

  • configs (dict) – The configurations for the data.

  • text_file_name (str) – The name of the text file to be generated.

  • text_only (bool) – If True, only a text file will be generated. If False, plots and info will also be saved.

  • theta (float) – The theta value.

  • redshift (float) – The redshift value.

  • min_freq (float) – The minimum frequency.

  • max_freq (float) – The maximum frequency.

  • torus_temp (float) – The torus temperature value.

  • torus_luminosity (float) – The torus luminosity value.

  • torus_frac (float) – The torus fraction value.

  • data_folder (str) – The folder where the data is stored.

  • executable (str) – The path to the executable file.

  • command_params_full (str) – The full command parameters.

  • command_params_1 (str) – The first command parameters.

  • command_params_2 (str) – The second command parameters.

  • verbose (bool) – If True, additional information will be printed during the execution. If False, no additional information will be printed.

  • eic (bool) – If True, additional EIC information will be considered. If False, EIC is disabled.

  • fixed_params (dict) – The fixed parameters.

Returns:

None

Return type:

None

bjet_mcmc.blazar_report.save_plots_and_info(configs, data, param_min_vals, param_max_vals, folder=None, parent_folder=None, backend_file=None, reader=None, sampler=None, use_sampler=False, samples=None, use_samples=False, description=None, time=None, p0_source=None, acceptance_frac=None, theta=None, redshift=None, min_freq=None, max_freq=None, executable=None, data_folder=None, command_params_full=None, command_params_1=None, command_params_2=None, torus_temp=None, torus_luminosity=None, torus_frac=None, verbose=False, eic=False, chi_squared_decay=None)
Parameters:
  • configs (dict) – Dictionary containing various configurations for the analysis.

  • data (list) – Data used for the analysis.

  • param_min_vals (list) – Minimum values of the parameters.

  • param_max_vals (list) – Maximum values of the parameters.

  • folder (str) – Folder where the plots and info will be saved. Defaults to None.

  • parent_folder (str) – Parent folder of the output folder. Defaults to None.

  • backend_file (str) – File name of the backend. Defaults to None.

  • reader (emcee.backends.HDFBackend) – HDFBackend reader. Defaults to None.

  • sampler (emcee.EnsembleSampler) – Sampler used for the analysis. Defaults to None.

  • use_sampler (bool) – Flag indicating whether to use the sampler. Defaults to False.

  • samples (tuple) – Samples from the analysis. Defaults to None.

  • use_samples (bool) – Flag indicating whether to use the samples. Defaults to False.

  • description (str) – Description of the analysis. Defaults to None.

  • time (str) – Time of the analysis. Defaults to None.

  • p0_source (str) – Source of the initial parameters. Defaults to None.

  • acceptance_frac (float) – Acceptance fraction of the sampler. Defaults to None.

  • theta (float) – Theta value for the analysis. Defaults to None.

  • redshift (float) – Redshift value for the analysis. Defaults to None.

  • min_freq (float) – Minimum frequency for the analysis. Defaults to None.

  • max_freq (float) – Maximum frequency for the analysis. Defaults to None.

  • executable (str) – Executable for the blazar model. Defaults to None.

  • data_folder (str) – Folder containing the data files. Defaults to None.

  • command_params_full (list) – Full command parameters for the blazar model. Defaults to None.

  • command_params_1 (list) – Command parameters for the first part of the blazar model. Defaults to None.

  • command_params_2 (list) – Command parameters for the second part of the blazar model. Defaults to None.

  • torus_temp (float) – Temperature of the torus for the blazar model. Defaults to None.

  • torus_luminosity (float) – Luminosity of the torus for the blazar model. Defaults to None.

  • torus_frac (float) – Fraction of the torus for the blazar model. Defaults to None.

  • verbose (bool) – Flag indicating whether to print verbose output. Defaults to False.

  • eic (bool) – Flag indicating whether the analysis is using EIC. Defaults to False.

Returns:

None

Return type:

None

bjet_mcmc.blazar_report.show_results(sampler, time, configs=None, discard=None)

Show the results of a Markov Chain Monte Carlo (MCMC) simulation.

Parameters:
  • sampler (object) – The MCMC sampler object used for the simulation.

  • time (float) – The total duration of the sampling process.

  • configs (dict, optional) – Additional configuration options for the simulation.

  • discard (int, optional) – The number of initial samples to discard as burn-in.

Returns:

None

Raises:

ValueError – If neither configs nor discard is provided.

See also

get_best_log_prob_and_params()

This function prints the results of the MCMC simulation, including the best log probability, the best parameter values, the total simulation time, and the mean auto-correlation time. It optionally accepts additional configurations and allows discarding a specified number of initial samples as burn-in.

Example usage:

# Example 1: Minimum usage
show_results(sampler, 10.0)

# Example 2: With additional configurations
configs = {"discard": 100}
show_results(sampler, 20.0, configs=configs)

# Example 3: With discard only
show_results(sampler, 30.0, discard=200)

Note

The actual use of this function may vary based on the user’s specific MCMC simulation and output requirements.

bjet_mcmc.blazar_report.text_report(best, min_1sigma, max_1sigma, min_1sigma_SED, max_1sigma_SED, best_chi_sq, data_points, param_names=None, eic=False, fixed_params=None)

Generate a text report based on the provided input parameters.

Parameters:
  • best (list or array) – The best-fit values for each parameter.

  • min_1sigma (list or array) – The lower bounds of the 1-sigma error range for each parameter.

  • max_1sigma (list or array) – The upper bounds of the 1-sigma error range for each parameter.

  • min_1sigma_SED (list or array) – The lower bounds of the 1-sigma error range for each parameter within the SED contour.

  • max_1sigma_SED (list or array) – The upper bounds of the 1-sigma error range for each parameter within the SED contour.

  • best_chi_sq (float) – The best reduced chi-squared value.

  • data_points (int) – The total number of data points used for the analysis.

  • param_names (list or array) – (Optional) The names of the parameters. If not provided, the default parameter names will be used.

  • eic (bool) – (Optional) Flag indicating whether the model has extra internal constraits (EIC).

  • fixed_params (list or array) – (Optional) The values of fixed parameters. Any frozen parameters will be excluded from the report.

Returns:

The generated text report.

Return type:

str