API Reference#

class uncertainty_lca.monte_carlo.MonteCarloLCA(demand, lcia_method_name=None, lcia_methods=None, iterations=None, run_parallel=True, num_cores=None)#
exchange_list_to_excel(filename=None, identifier='name', folder_path=None, foreground_only=False)#

Save technosphere exchanges to an Excel file.

Parameters:
  • filename (str, optional) – Name of the Excel file to save the exchanges.

  • identifier (str, optional) – Identifier to use for the filename. Default is ‘name’, i.e. the name of the demand activity.

  • folder_path (str, optional) – Path to the folder where the Excel file will be saved. Default is None.

execute_monte_carlo(iterations=None)#

Function to perform a Monte Carlo simulation. It decides whether or not to run in parallel based on the ‘run_parallel’ attribute. Then, it calls either ‘execute_serial_monte_carlo()’ or ‘execute_parallel_monte_carlo()’.

Parameters:

iterations (int, optional) – Number of iterations for the Monte Carlo simulation. If None, the number of iterations provided during initialization is used.

execute_parallel_monte_carlo(iterations)#

Function to perform a parallelised Monte Carlo simulation.

Parameters:

iterations (int) – Number of iterations for the Monte Carlo simulation.

execute_serial_monte_carlo(iterations)#

Function to perform a Monte Carlo simulation.

Parameters:

iterations (int) – Number of iterations for the Monte Carlo simulation.

get_exchange_list(foreground_only=False)#

Get a list of all technosphere exchanges present in the LCA.

Returns:

exchange_list – List of technosphere exchanges present in the LCA.

Return type:

list

get_results_dataframe(method=None)#

Return Monte Carlo results as a Pandas DataFrame. Useful for integration with Activity Browser.

Rows correspond to Monte Carlo iterations. Columns correspond to LCIA methods (impact categories).

Returns:

DataFrame of shape (iterations, n_methods)

Return type:

pandas.DataFrame

mc_lci_calculation(slice_index)#

Function to perform the LCI for one Monte Carlo iteration. This includes rebuilding the technosphere and biosphere matrices with random values.

mc_lci_preparation(iterations)#

Function to prepare the LCI for Monte Carlo simulation. This includes loading LCI data, generating random numbers, and building the demand array.

mc_lcia_calculation()#

Function to perform the LCIA for one Monte Carlo iteration. This includes loading LCIA data, generating random numbers for the characterization matrix, rebuilding it, and performing the LCIA calculation.

property mc_results#

Property to access Monte Carlo results after simulation.

monte_carlo_worker(args)#

Worker function for Monte Carlo simulation. Each worker will perform a subset of the iterations.

Parameters:
  • args (tuple) – Tuple containing the following elements:

  • iterations (int) – Number of iterations to perform in this worker.

  • progress_queue (Queue) – Queue for reporting progress.

Returns:

mc_results – Dictionary containing the Monte Carlo results

Return type:

dict

print_uncertainty_info()#

Print summary statistics about uncertainty information in the exchanges of this LCA. Uses the underlying Brightway tech_params data structure for efficient access.

print_uncertainty_info_old(foreground_only=False)#

Print summary statistics about uncertainty information in the exchanges of this LCA. Uses the underlying Brightway exchange data structure for robust access.

Parameters:

foreground_only (bool, optional) – Whether to consider only foreground exchanges. Default is False.

results_to_json(filename=None, identifier='name', folder_path=None)#

Save Monte Carlo results to a JSON file.

Parameters:
  • filename (str, optional) – Name of the JSON file to save the results. If None, a default filename based on the demand activity name is used.

  • identifier (str, optional) – Identifier to use for the filename. Default is ‘name’, i.e. the name of the demand activity.

  • folder_path (str, optional) – Path to the folder where the JSON file will be saved. Default is None.

set_default_uncertainty(foreground_only=False)#

Add default uniform uncertainty (±10%) to technosphere exchanges missing uncertainty.

stats_to_json(filename=None, identifier='name', folder_path=None)#

Save Monte Carlo statistics to a JSON file.

Parameters:
  • identifier (str, optional) – Identifier to use for the filename. Default is ‘name’, i.e. the name of the demand activity.

  • folder_path (str, optional) – Path to the folder where the JSON file will be saved. Default is None.

uncertainty_lca.monte_carlo.calculate_statistics(mc_results, lcia_method_name=None, lcia_methods=None, key_list=None)#

Calculate statistics for Monte Carlo results.

Parameters:
  • mc_results (dict) – Dictionary containing the Monte Carlo results.

  • lcia_methods (list) – List of LCIA methods.

  • key_list (list) – List of keys for the LCIA methods.

Returns:

mc_statistics – Dictionary containing the calculated statistics.

Return type:

dict

uncertainty_lca.monte_carlo.exchange_to_dict(exc)#

Convert a technosphere exchange to a dictionary.

Parameters:

exc (Exchange) – Technosphere exchange to convert.

Returns:

exc_dict – Dictionary representation of the exchange.

Return type:

dict

uncertainty_lca.monte_carlo.get_key_list(lcia_methods)#

Get a list of keys for the provided LCIA methods.

Parameters:

lcia_methods (list) – List of LCIA methods.

Returns:

key_list – List of keys for the LCIA methods.

Return type:

list

uncertainty_lca.monte_carlo.get_lcia_methods(lcia_method_name, get_keys=False)#

Get LCIA methods and their keys based on the provided method name.

Parameters:

lcia_method_name (str) – Name of the LCIA method (e.g., ‘EF v3.1 no LT’).

Returns:

  • lcia_methods (list) – List of LCIA methods.

  • key_list (list) – List of keys for the LCIA methods.

uncertainty_lca.monte_carlo.write_json(filename, dict_to_write, folder_path=None)#

Write a dictionary to a JSON file.

Parameters:
  • filename (str) – Name of the JSON file to write.

  • dict_to_write (dict) – Dictionary to write to the JSON file.

  • folder_path (str, optional) – Path to the folder where the JSON file will be saved. If None, a default “results” folder is used.