dopo.sector_lca_scores
Generates and saves Life Cycle Assessment (LCA) scores plots for various sectors to an Excel file.
The module computes LCA scores for activities and methods, generates plots (dot plots and stacked bar charts), and saves them along with LCA scores tables to an Excel file. Includes helper functions for processing, plotting, and formatting data.
Functions
|
Adds a sector marker to the DataFrame for labeling and identification purposes. |
|
Adds statistical indicators to a DataFrame for plotting purposes. |
Cleans and formats column labels in the DataFrame by removing unnecessary numbers and colons. |
|
|
Adapted birghtway2 analyzer function. It stores additional labels and data per activity. |
|
Compares a list of activities using multiple LCA methods and stores the results in a dictionary |
Identifies the index of the first column in a DataFrame that contains input contribution data. |
|
|
Generates LCA score tables for each sector's activity list, including total scores and CPC |
|
Writes LCA scores to an Excel file, organizing data by sector and method. |
|
Aggregates insignificant input contributions into an 'other' column for each DataFrame in the |
|
Generate plots of Life Cycle Assessment (LCA) scores for different sectors and save them to an |
Module Contents
- dopo.sector_lca_scores._add_sector_marker(df, sector)[source]
Adds a sector marker to the DataFrame for labeling and identification purposes.
This function is used to add a new column to the DataFrame that indicates the sector associated with the data. The sector information is useful for identifying and labeling data in plots and Excel sheets. The column is positioned immediately after the ‘product’ column if it exists, or appended at the end if ‘product’ is not present.
Parameters
- dfpandas.DataFrame
The DataFrame to which the sector marker will be added.
- sectorstr
The name of the sector to be added as a marker.
Returns
- pandas.DataFrame
The DataFrame with an added ‘sector’ column, positioned immediately after the ‘product’ column if present, or at the end otherwise.
- dopo.sector_lca_scores._add_statistics(df, column_name='total')[source]
Adds statistical indicators to a DataFrame for plotting purposes.
This function computes several statistics based on the values in the specified column (column_name). It adds columns for ranking, mean, standard deviation bounds, and interquartile range (IQR). The statistics are added to aid in visual analysis and plotting.
Parameters
- dfpandas.DataFrame
The DataFrame to which statistical indicators will be added.
- column_namestr, optional
The name of the column on which to base the statistics. Default is ‘total’.
Returns
- pandas.DataFrame
The updated DataFrame with added columns for ranking, mean, standard deviation bounds, and IQR.
- dopo.sector_lca_scores._clean_column_labels(df)[source]
Cleans and formats column labels in the DataFrame by removing unnecessary numbers and colons.
This function is used to standardize column headers by removing leading numbers and colons, which can be present in columns used for input contributions or other data. It should be called after _find_first_input_column to ensure column order and identification are correctly handled.
Parameters
- dfpandas.DataFrame
The DataFrame whose column labels are to be cleaned.
Returns
- pandas.DataFrame
The DataFrame with formatted column labels, where unnecessary numbers and colons have been removed.
- dopo.sector_lca_scores._compare_activities_by_grouped_leaves(activities, lcia_method, mode='relative', max_level=4, cutoff=0.0075, output_format='list', str_length=50)[source]
Adapted birghtway2 analyzer function. It stores additional labels and data per activity.
Compare activities by the impact of their different inputs, aggregated by the product classification of those inputs.
- Args:
activities: list of
Activityinstances. lcia_method: tuple. LCIA method to use when traversing supply chain graph. mode: str. If “relative” (default), results are returned as a fraction of total input. Otherwise, results are absolute impact per input exchange. max_level: int. Maximum level in supply chain to examine. cutoff: float. Fraction of total impact to cutoff supply chain graph traversal at. output_format: str. See below. str_length; int. Ifoutput_formatishtml, this controls how many characters each column label can have.- Raises:
ValueError:
activitiesis malformed.- Returns:
Depends on
output_format:list: Tuple of(column labels, data)html: HTML string that will print nicely in Jupyter notebooks.pandas: a pandasDataFrame.
- dopo.sector_lca_scores._compare_activities_multiple_methods(activities_list, methods, identifier, output_format='pandas', mode='absolute')[source]
Compares a list of activities using multiple LCA methods and stores the results in a dictionary of DataFrames.
This function generates comparison results for each method in methods, formats them into DataFrames, and organizes them in a dictionary where the keys are method-specific names derived from the identifier and method details. Each DataFrame contains total scores and input contributions, with columns ordered and indexed appropriately.
Parameters
- activities_listlist
A list of activities to be compared.
- methodsdict
A dictionary where keys are method names and values are dictionaries with the key “object” being a Brightway Method object used for comparisons.
- identifierstr
A string used to construct unique variable names for the comparison results (e.g., sector name).
- output_formatstr, optional
The format for the output DataFrame. Default is “pandas”. Other formats can be specified if supported.
- modestr, optional
The mode of comparison. Options are “absolute” (default) and “relative”.
Returns
- dict
A dictionary where each key is a unique name derived from the identifier and method name, and each value is a DataFrame containing the comparison results.
- dopo.sector_lca_scores._find_first_input_column(df)[source]
Identifies the index of the first column in a DataFrame that contains input contribution data.
This function is used to locate the column in the DataFrame that holds input contribution data, which is essential for dynamically selecting the correct column for plotting. It ensures compatibility with DataFrames that may have different column orders or names, such as those including “direct emissions.”
Parameters
- dfpandas.DataFrame
The DataFrame in which to find the first input contribution column.
Returns
- int or None
The index of the first column containing input data. Returns None if no such column is found.
- dopo.sector_lca_scores._sector_lca_scores(activity_dict, method_dict, cutoff=0.01)[source]
Generates LCA score tables for each sector’s activity list, including total scores and CPC input contributions.
This function calculates LCA scores for activities within each sector using methods specified in the method_dict. Inputs below or equal to the cutoff value are summarized in an “other” column.
Parameters
- activity_dictdict
A dictionary returned by the process_yaml_files function. It should contain sector names as keys, each with an ‘activities’ entry holding the list of activities for that sector.
- method_dictdict
A dictionary created with the MethodFinder class, containing methods for LCA score calculation.
- cutofffloat, optional
A threshold value for summarizing inputs below or equal to this value in an “other” column. Default is 0.02.
Returns
- dict
The updated dictionary (formerly activity_dict) with an additional key ‘lca_scores’ for each sector. This contains the calculated LCA scores by method.
- dopo.sector_lca_scores._sector_lca_scores_to_excel(scores_dict, excel_file_name)[source]
Writes LCA scores to an Excel file, organizing data by sector and method.
For each sector in the scores_dict, this function performs the following: - Creates a DataFrame for each method within that sector. - Shortens column labels by removing CPC codes. - Adds a sector name marker to facilitate tracking in Excel. - Adds statistical columns for plotting purposes. - Creates a dictionary of column index positions used for plotting, making it dynamic and
avoiding hardcoded column indices.
Parameters
- scores_dictdict
A dictionary where each key is a sector name and each value contains LCA scores and other relevant data. The structure should be compatible with the output of the sector_lca_scores function.
- excel_file_namestr
The name of the Excel file to be created, including the file extension (e.g., ‘lca_scores.xlsx’).
Returns
- dict
A dictionary where each key is a “sector_method” string and each value is another dictionary mapping column names to their index positions. This dictionary aids in dynamic plotting.
- dopo.sector_lca_scores._small_inputs_to_other_column(dataframes_dict, cutoff=0.01)[source]
Aggregates insignificant input contributions into an ‘other’ column for each DataFrame in the input dictionary.
Contributions that are less than or equal to the specified cutoff value (relative to the ‘total’ column) are combined into a new ‘other’ column. The original columns with these contributions are set to zero. Columns that end up containing only zeros are removed, and columns named None or “Unnamed” are also combined into the ‘other’ column before removal.
Parameters
- dataframes_dictdict
A dictionary where each key corresponds to a DataFrame. Each DataFrame should contain a ‘total’ column and may include columns to be aggregated into the ‘other’ column based on their contributions.
- cutofffloat, optional
The cutoff value for determining insignificant contributions. Contributions less than or equal to this value (relative to the ‘total’ column) are aggregated into the ‘other’ column. Default is 0.01.
Returns
- dict
A dictionary with the same keys as dataframes_dict, but with each DataFrame updated to include an ‘other’ column and without insignificant columns.
- dopo.sector_lca_scores.sector_lca_scores_plots(activity_dict, method_dict, excel_file_name, cutoff=0.01)[source]
Generate plots of Life Cycle Assessment (LCA) scores for different sectors and save them to an Excel file.
This function calculates LCA scores for a set of activities and methods, then generates plots (dot plots and stacked bar charts) based on these scores. The generated plots are saved to an Excel file.
- Args:
- activity_dict (dict): A dictionary where keys are activity names or IDs and values are
corresponding activity data.
- method_dict (dict): A dictionary where keys are method names or IDs and values are
corresponding method data.
- excel_file_name (str): The name of the Excel file where the LCA scores tables and plots will
be saved.
- cutoff (float, optional): A cutoff value for filtering LCA scores. Any scores below this
value will be excluded. Default is 0.01.
- Returns:
None
The function performs the following steps: 1. Generates LCA scores tables based on the provided activity and method dictionaries and the
cutoff value.
Saves the generated LCA scores tables to the specified Excel file.
Creates dot plots of the LCA scores and saves them in the Excel file.
Creates stacked bar charts of the LCA scores and appends them to the Excel file.
Prints the last row occupied in the Excel charts sheet, which indicates where the plots end.
- Note:
- The dot_plots_xcl and stacked_bars_xcl functions are imported inside this function to
avoid circular imports.
- The function relies on helper functions such as sector_lca_scores and
sector_lca_scores_to_excel to generate and save LCA scores, and dot_plots_xcl and stacked_bars_xcl for generating plots.