Skip to content

Commit

Permalink
__init__ file adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
cafriedb committed Aug 6, 2024
1 parent a156714 commit cff7320
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 22 deletions.
6 changes: 6 additions & 0 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
174 changes: 174 additions & 0 deletions dev/jupyter_dopo_test1.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "3e1ffe53-e204-4ccf-bd45-f29f2f3f0d45",
"metadata": {},
"outputs": [],
"source": [
"import dopo"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "aa0ef4e3-b2a6-46ef-b0bf-324559920d0f",
"metadata": {},
"outputs": [],
"source": [
"import brightway2 as bw\n",
"import bw2data as bd\n",
"import bw2analyzer as ba"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7516140d-2999-4f80-8c76-a6518d0ce40e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Biosphere database already present!!! No setup is needed\n"
]
}
],
"source": [
"bd.projects.create_project(\"dopo-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": "code",
"execution_count": 28,
"id": "fc59dd39-a878-4ede-9a4c-2e97a5b64bca",
"metadata": {},
"outputs": [],
"source": [
"fp_yaml='cement_concrete.yaml'\n",
"flt_act=dopo.generate_sets_from_filters(fp_yaml, database=ei39)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "ab6e4b20-bb06-44e8-bc99-6bb6601bc1a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Brightway2 SQLiteBackend: ei_cutoff_3.9_image_SSP2-RCP19_2050 2024-06-27"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ei39SSP2"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "b867fd79-4ebb-4656-be93-d09959c8863f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Cement': set(),\n",
" 'Concrete': set(),\n",
" 'Steel': set(),\n",
" 'Electricity production all': set(),\n",
" 'Electricity production fossil': set(),\n",
" 'Electricity production renewables': set(),\n",
" 'Electricity production nuclear': set(),\n",
" 'Electricity production biomass': set(),\n",
" 'Electricity production biomethane': set(),\n",
" 'Electricity production hydro': set(),\n",
" 'Electricity production geothermal': set(),\n",
" 'Electricity production photovoltaic': set(),\n",
" 'Electricity production solar': set(),\n",
" 'Electricity production wind': set(),\n",
" 'Electricity production wood': set(),\n",
" 'Electricity production wave energy': set(),\n",
" 'Electricity production coal': set(),\n",
" 'Electricity production lignite': set(),\n",
" 'Electricity production natural gas': set(),\n",
" 'Electricity production oil': set(),\n",
" 'Electricity production petroleum': set()}"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"flt_act"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6c55f5a8-4cff-4a21-9121-76a0b1222f26",
"metadata": {},
"outputs": [],
"source": [
"small_cement=[x for x in sets['Cement']]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "5225205b-852e-4fe9-9935-c022ad1526d6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"small_cement"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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": 5
}
78 changes: 75 additions & 3 deletions dev/test_function_flow_v4.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,84 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['cement production, CP V RS' (kilogram, BR, None),\n",
" 'cement production, type ICo' (kilogram, PE, None),\n",
" 'cement production, Portland' (kilogram, IN, None),\n",
" 'cement production, CP II-E' (kilogram, BR, None),\n",
" 'cement production, CEM III/A' (kilogram, ZA, None),\n",
" 'cement production, type IS' (kilogram, US, None),\n",
" 'cement production, CEM V/B' (kilogram, RoW, None),\n",
" 'cement production, Portland' (kilogram, CH, None),\n",
" 'cement production, CEM V/A' (kilogram, RoW, None),\n",
" 'cement production, CEM II/A' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CEM II/A-V' (kilogram, ZA, None),\n",
" 'cement production, CEM IV/B' (kilogram, CH, None),\n",
" 'cement production, type I-PM' (kilogram, US, None),\n",
" 'cement production, CEM II/B-V' (kilogram, ZA, None),\n",
" 'cement production, Portland' (kilogram, ZA, None),\n",
" 'cement production, CEM V/A' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CEM III/C' (kilogram, RoW, None),\n",
" 'cement production, CEM III/B' (kilogram, CH, None),\n",
" 'cement production, CEM V/A' (kilogram, CH, None),\n",
" 'cement production, CEM V/B' (kilogram, CH, None),\n",
" 'cement production, Portland' (kilogram, RoW, None),\n",
" 'cement production, CEM II/B' (kilogram, CH, None),\n",
" 'cement production, CEM II/A-S' (kilogram, ZA, None),\n",
" 'cement production, CEM III/A' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, Pozzolana Portland' (kilogram, IN, None),\n",
" 'cement production, CEM II/A' (kilogram, CH, None),\n",
" 'cement production, CEM II/B' (kilogram, RoW, None),\n",
" 'cement production, CEM II/B-S' (kilogram, ZA, None),\n",
" 'cement production, CEM III/C' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, type IP' (kilogram, PE, None),\n",
" 'cement production, CP IV' (kilogram, BR, None),\n",
" 'cement production, CEM IV/B' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, Portland' (kilogram, BR, None),\n",
" 'cement production, CEM III/B' (kilogram, RoW, None),\n",
" 'cement production, type general use' (kilogram, CO, None),\n",
" 'cement production, type IP/P' (kilogram, US, None),\n",
" 'cement production, CEM IV/A' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CEM II/A' (kilogram, CA-QC, None),\n",
" 'cement production, CEM II/A' (kilogram, RoW, None),\n",
" 'cement production, CEM IV/A' (kilogram, CH, None),\n",
" 'cement production, Portland' (kilogram, PE, None),\n",
" 'cement production, CEM II/A-L' (kilogram, ZA, None),\n",
" 'cement production, Portland' (kilogram, US, None),\n",
" 'cement production, type I (SM)' (kilogram, US, None),\n",
" 'cement production, CEM IV/B' (kilogram, RoW, None),\n",
" 'cement production, CEM III/C' (kilogram, CH, None),\n",
" 'cement production, type S' (kilogram, US, None),\n",
" 'cement production, Portland' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CEM II/B' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CEM III/A' (kilogram, RoW, None),\n",
" 'cement production, CEM III/B' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, Portland' (kilogram, CA-QC, None),\n",
" 'cement production, CP II-F' (kilogram, BR, None),\n",
" 'cement production, CP III' (kilogram, BR, None),\n",
" 'cement production, CEM IV/A' (kilogram, RoW, None),\n",
" 'cement production, CEM V/B' (kilogram, Europe without Switzerland, None),\n",
" 'cement production, CP II-Z' (kilogram, BR, None),\n",
" 'cement production, ART' (kilogram, CO, None),\n",
" 'cement production, CEM II/B-L' (kilogram, ZA, None),\n",
" 'cement production, CEM III/A' (kilogram, CH, None),\n",
" 'cement production, Portland Slag' (kilogram, IN, None)]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#List comprehension of yaml filtered sets to create activity list.\n",
"sector_cement=[x for x in set_cement_concrete['Cement']]"
"sector_cement=[x for x in set_cement_concrete['Cement']]\n",
"sector_cement"
]
},
{
Expand Down
14 changes: 12 additions & 2 deletions dopo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
"""dopo."""

__all__ = (
"__version__",
# Add functions and variables you want exposed in `dopo.` namespace here
"__version__",
'generate_sets_from_filters',
'MethodFinder',
'find_and_create_method',
'get_all_methods',
'compare_activities_multiple_methods',
'lvl1_plot',
'lvl21_plot_stacked_absolute',
'lvl22_plot_input_comparison_with_method',
'lvl23_plot_input_comparison_plot_no_method',
'lvl3_plot_relative_changes'
)

__version__ = "0.0.1"

from dopo.activity_filter import generate_sets_from_filters
from dopo.methods import MethodFinder, find_and_create_method, get_all_methods
from dopo.methods import MethodFinder
from dopo.sector_score_dict import compare_activities_multiple_methods
from dopo.plots import (
lvl1_plot,
Expand Down
10 changes: 5 additions & 5 deletions dopo/activity_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Sector filter functions from premise
# ---------------------------------------------------

def act_fltr(
def _act_fltr(
database: list,
fltr = None,
mask = None,
Expand Down Expand Up @@ -78,13 +78,13 @@ def generate_sets_from_filters(yaml_filepath, database=None) -> dict:
technologies from the filter specifications.
:param filtr:
:func:`activity_maps.InventorySet.act_fltr`.
:func:`activity_maps.InventorySet._act_fltr`.
:return: dictionary with the same keys as provided in filter
and a set of activity data set names as values.
:rtype: dict
"""

filtr=get_mapping(yaml_filepath, var='ecoinvent_aliases')
filtr=_get_mapping(yaml_filepath, var='ecoinvent_aliases')

names = []

Expand Down Expand Up @@ -115,7 +115,7 @@ def generate_sets_from_filters(yaml_filepath, database=None) -> dict:


techs = {
tech: act_fltr(subset, fltr.get("fltr"), fltr.get("mask"))
tech: _act_fltr(subset, fltr.get("fltr"), fltr.get("mask"))
for tech, fltr in filtr.items()
}

Expand All @@ -126,7 +126,7 @@ def generate_sets_from_filters(yaml_filepath, database=None) -> dict:

return mapping

def get_mapping(filepath, var):
def _get_mapping(filepath, var):
"""
Loa a YAML file and return a dictionary given a variable.
:param filepath: YAML file path
Expand Down
14 changes: 7 additions & 7 deletions dopo/cpc_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Level 2.3 plot dependency
# ------------------------------------------------------------------------------------------------------------------------------------

def get_cpc_inputs_of_activities(activities_list, input_type='list'):
def _get_cpc_inputs_of_activities(activities_list, input_type='list'):

'''
for param description see function lvl23_plot_input_comparison_plot_no_method
Expand All @@ -26,7 +26,7 @@ def get_cpc_inputs_of_activities(activities_list, input_type='list'):
'''

def activity_list_inputs_cpc(activities_list, input_type):
def _activity_list_inputs_cpc(activities_list, input_type):
all_inputs = []

if input_type == 'list':
Expand Down Expand Up @@ -59,7 +59,7 @@ def activity_list_inputs_cpc(activities_list, input_type):

return combined_inputs

def update_cpc_information(combined_inputs):
def _update_cpc_information(combined_inputs):
for index, row in combined_inputs.iterrows():
# Transform each key to tuple
tuple_key = ast.literal_eval(index)
Expand All @@ -75,7 +75,7 @@ def update_cpc_information(combined_inputs):

return combined_inputs

def transform_dataframe(combined_inputs):
def _transform_dataframe(combined_inputs):
# Set 'identifier' as the new index and drop the 'full_key' index
combined_inputs = combined_inputs.reset_index().set_index('identifier').drop('full_key', axis=1)

Expand Down Expand Up @@ -103,8 +103,8 @@ def transform_dataframe(combined_inputs):
return result

# Execute the workflow
combined_inputs = activity_list_inputs_cpc(activities_list, input_type)
combined_inputs_with_cpc = update_cpc_information(combined_inputs)
final_result = transform_dataframe(combined_inputs_with_cpc)
combined_inputs = _activity_list_inputs_cpc(activities_list, input_type)
combined_inputs_with_cpc = _update_cpc_information(combined_inputs)
final_result = _transform_dataframe(combined_inputs_with_cpc)

return final_result
Loading

0 comments on commit cff7320

Please sign in to comment.