Skip to content

Commit

Permalink
added excel functions to dopo __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
cafriedb committed Aug 16, 2024
1 parent 7527544 commit 652b5a0
Show file tree
Hide file tree
Showing 5 changed files with 848 additions and 4 deletions.
10 changes: 6 additions & 4 deletions dev/dopo_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import re
import pandas as pd

from dopo import generate_sets_from_filters
from dopo import compare_activities_multiple_methods
from dopo import small_inputs_to_other_column
import openpyxl
from openpyxl import load_workbook
from openpyxl.chart import ScatterChart, Reference, Series
Expand Down Expand Up @@ -177,7 +179,7 @@ def clean_label(label):
# Apply the cleaning function to all column names
df.columns = [clean_label(col) for col in df.columns]

# Regular expression pattern to match "Number: Name" or "Number Name"
# Regular expression pattern to match "Number: Name"
pattern = r'^\d+:\s*'

for idx, column in enumerate(df.columns):
Expand Down Expand Up @@ -207,7 +209,7 @@ def clean_label(label):
return df

def add_sector_marker(df, sector):
'''
'''
It is called in the function sector_lca_scores_to_excel_and_column_positions.
It adds information about the sector for titel and labeling in plotting.
Expand Down Expand Up @@ -427,7 +429,7 @@ def dot_plots(filepath_workbook, worksheet_dict, index_positions):
from openpyxl import load_workbook
from openpyxl.chart import BarChart, Reference

def stacked_bars(filepath_workbook, worksheet_dict, current_row_dot_plot):
def stacked_bars(filepath_workbook, worksheet_dict, index_positions, current_row_dot_plot):
# Load the workbook
wb = load_workbook(filepath_workbook)

Expand Down
Binary file added dev/test_charts_v1.xlsx
Binary file not shown.
297 changes: 297 additions & 0 deletions dev/test_excel_flow.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"0. import dopo"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# from functions_v2 import*\n",
"# from methods import MethodFinder\n",
"import dopo\n",
"\n",
"import brightway2 as bw\n",
"import bw2data as bd\n",
"import bw2analyzer as ba"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Define a bw project, bw setup, get the databases"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Biosphere database already present!!! No setup is needed\n"
]
}
],
"source": [
"bd.projects.set_current(\"premise-validation-try1\")\n",
"bw.bw2setup()\n",
"\n",
"bio3=bw.Database('biosphere3')\n",
"ei39=bw.Database('ecoinvent 3.9.1 cutoff')\n",
"ei39SSP2=bw.Database('ei_cutoff_3.9_image_SSP2-RCP19_2050 2024-06-27')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Define filter files"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Cement': {'yaml': 'yamls\\\\cement_small.yaml', 'yaml identifier': 'Cement'},\n",
" 'Electricity': {'yaml': 'yamls\\\\electricity_small.yaml',\n",
" 'yaml identifier': 'Electricity'}}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"files_dict={}\n",
"files_dict['Cement']={'yaml': 'yamls\\cement_small.yaml',\n",
" 'yaml identifier': 'Cement'}\n",
"files_dict['Electricity']= {'yaml':'yamls\\electricity_small.yaml',\n",
" 'yaml identifier': 'Electricity'} #yaml identifier is the name of the filter in the yaml file, in the first line.\n",
"files_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Get activity lists from filters and store them in a dictionary by sector"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"sectors_dict = dopo.dopo_excel.process_yaml_files(files_dict, ei39SSP2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Initiate MethodFinder()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"finder=dopo.methods.MethodFinder()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Define methods and store them in a dictionary"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'method_1': {'object': Brightway2 Method: IPCC 2013: climate change: global warming potential (GWP100),\n",
" 'method name': ('IPCC 2013',\n",
" 'climate change',\n",
" 'global warming potential (GWP100)'),\n",
" 'short name': 'global warming potential (GWP100)',\n",
" 'unit': 'kg CO2-Eq'},\n",
" 'method_2': {'object': Brightway2 Method: EN15804: inventory indicators ISO21930: Cumulative Energy Demand - non-renewable energy resources,\n",
" 'method name': ('EN15804',\n",
" 'inventory indicators ISO21930',\n",
" 'Cumulative Energy Demand - non-renewable energy resources'),\n",
" 'short name': 'Cumulative Energy Demand - non-renewable energy resources',\n",
" 'unit': 'megajoule'},\n",
" 'method_3': {'object': Brightway2 Method: selected LCI results: resource: land occupation,\n",
" 'method name': ('selected LCI results', 'resource', 'land occupation'),\n",
" 'short name': 'land occupation',\n",
" 'unit': 'square meter-year'},\n",
" 'method_4': {'object': Brightway2 Method: EN15804: inventory indicators ISO21930: use of net fresh water,\n",
" 'method name': ('EN15804',\n",
" 'inventory indicators ISO21930',\n",
" 'use of net fresh water'),\n",
" 'short name': 'use of net fresh water',\n",
" 'unit': 'cubic meter'}}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"finder.find_and_create_method(criteria=['IPCC', '2013', 'GWP100'], exclude=['no LT'])\n",
"finder.find_and_create_method(criteria=['EN15804','Cumulative', 'non-renewable' ])\n",
"finder.find_and_create_method(criteria=['land occupation','selected'])\n",
"finder.find_and_create_method(criteria=['EN15804','fresh water'])\n",
"method_dict=finder.get_all_methods()\n",
"method_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. get LCA score tables and store them in scores_dict"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Omitting activity name common prefix: 'cement production, '\n",
"Omitting activity name common prefix: 'cement production, '\n",
"Omitting activity name common prefix: 'cement production, '\n",
"Omitting activity name common prefix: 'cement production, '\n",
"Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n",
"Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n",
"Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n",
"Omitting activity name common prefix: 'electricity production, at biomass-fired IGCC power '\n"
]
}
],
"source": [
"scores_dict = dopo.dopo_excel.sector_lca_scores(sectors_dict, method_dict) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. trasnfer lca tables to excel, add some statistics, get positions of columns."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"index_positions = dopo.dopo_excel.sector_lca_scores_to_excel_and_column_positions(scores_dict, 'test_charts_v1.xlsx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"6. group sheets by sector"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"worksheet_dict=dopo.dopo_excel.categorize_sheets_by_sector('test_charts_v1.xlsx')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"7. generate dot plot and store row position of last chart in variable for stacked bar inital position"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"current_row_dots=dopo.dopo_excel.dot_plots('test_charts_v1.xlsx', worksheet_dict, index_positions=index_positions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"8. get stacked bars on the same sheet as the dot plots"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"dopo.dopo_excel.stacked_bars('test_charts_v1.xlsx', worksheet_dict, index_positions, current_row_dots)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Et voila"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "premise",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
8 changes: 8 additions & 0 deletions dopo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"__version__",
'generate_sets_from_filters',
'MethodFinder',
''
'compare_activities_multiple_methods',
'scores_across_activities',
'inputs_contributions',
Expand All @@ -26,3 +27,10 @@
input_contribution_across_activities,
activities_across_databases
)
from dopo.dopo_excel import (
process_yaml_files,
sector_lca_scores,
sector_lca_scores_to_excel_and_column_positions,
dot_plots,
stacked_bars
)
Loading

0 comments on commit 652b5a0

Please sign in to comment.