[1]:
# from bcnexus import constants as bcnexus_const
# from bcnexus.clews import model_structure
[2]:
import pandas as pd
import plotly.express as px
from pathlib import Path
# local packages
from bcnexus import utils
[3]:
storage_algorithm="Kotzur"
nexus_scenario='Base_CNZ'
timeslices=18
solver="gurobi"
[4]:
from bcnexus.clews.datapackage import GetDataPackage
nexus_results_root=Path(f'results/clews/Model_{storage_algorithm}_{nexus_scenario}/{timeslices}ts_csvs_{solver}')
result_pack=GetDataPackage(nexus_results_root)
result_pack.show
└> Loaded 29 CSV files from results/clews/Model_Kotzur_Base_CNZ/18ts_csvs_gurobi
[4]:
['AccumulatedNewCapacity',
'AnnualEmissions',
'AnnualFixedOperatingCost',
'AnnualTechnologyEmission',
'AnnualTechnologyEmissionByMode',
'AnnualVariableOperatingCost',
'CapitalInvestment',
'Demand',
'DiscountedSalvageValue',
'DiscountedTechnologyEmissionsPenalty',
'NewCapacity',
'NewStorageCapacity',
'ProductionByTechnology',
'ProductionByTechnologyAnnual',
'RateOfActivity',
'RateOfProductionByTechnology',
'RateOfProductionByTechnologyByMode',
'RateOfUseByTechnology',
'RateOfUseByTechnologyByMode',
'SalvageValue',
'SalvageValueStorage',
'StorageLevelChronoDayStart',
'StorageLevelDayTypeFinish',
'StorageLevelYearStart',
'TotalAnnualTechnologyActivityByMode',
'TotalCapacityAnnual',
'TotalTechnologyAnnualActivity',
'TotalTechnologyModelPeriodActivity',
'UseByTechnology']
[5]:
nexus_ts_plots={}
plot_categories = ['Climate', 'Land', 'Energy', 'Water', str(timeslices)]
nexus_plots = {category: {} for category in plot_categories}
nexus_plots[f'{timeslices}'] = nexus_ts_plots
plots = {'nexus': nexus_plots}
[6]:
from bcnexus.vis import plot_Climate
nexus_climate_plots={}
nexus_plots['Climate'] = nexus_climate_plots
nexus_climate_plots['emission_total']=plot_Climate.get_total_annual_emission(result_pack.get_dataframe('AnnualEmissions'), nexus_scenario)
nexus_climate_plots['emission_by_source']=plot_Climate.get_emission_from_fuels(result_pack.get_dataframe('AnnualTechnologyEmission'), nexus_scenario)
nexus_climate_plots['emission_by_sector']=plot_Climate.get_emission_from_sector(result_pack.get_dataframe('AnnualTechnologyEmission'), nexus_scenario)
[7]:
from bcnexus.vis import plot_Land
nexus_plots['Land'] = plot_Land.plot_landuse_for_clusters(result_pack.get_dataframe('RateOfProductionByTechnologyByMode'), nexus_scenario)
[8]:
from bcnexus.vis import plot_Energy
nexus_energy_plots={}
nexus_plots['Energy'] = nexus_energy_plots
nexus_energy_plots["Nexus_sectoral_consumption"] , nexus_energy_plots["Nexus_fuel_consumption"] = plot_Energy.plot_combined_stacked_energy_consumption(result_pack.get_dataframe('UseByTechnology'),
'gwh',
nexus_scenario)
nexus_energy_plots["Nexus_generation_from_fuels"]=plot_Energy.get_annual_generation_plot(result_pack.get_dataframe('ProductionByTechnology'),
nexus_scenario)
nexus_energy_plots["Nexus_capacity_investments"]=plot_Energy.get_capacity_plot(result_pack.get_dataframe('NewCapacity'),
nexus_scenario,
investment=True)
nexus_energy_plots["Nexus_capacity_total"]=plot_Energy.get_capacity_plot(result_pack.get_dataframe('TotalCapacityAnnual'),nexus_scenario,investment=False)
nexus_energy_plots["Nexus_power_generation_timeslices"]=plot_Energy.get_generation_timeseries_plot(result_pack.get_dataframe('RateOfUseByTechnology'),24,nexus_scenario)
nexus_energy_plots["Nexus_power_generation_annual"]=plot_Energy.get_annual_power_generation_plot(result_pack.get_dataframe('ProductionByTechnologyAnnual'),nexus_scenario)
nexus_energy_plots["Nexus_capital_investment_power"]=plot_Energy.get_capital_investments(result_pack.get_dataframe('CapitalInvestment'),nexus_scenario)
[9]:
nexus_energy_plots["Nexus_power_generation_annual"]
[10]:
df = utils.add_power_tech_labels(result_pack.get_dataframe('CapitalInvestment'),'capacity')
df.power_techs_labels.unique()
[10]:
array([nan, 'Wind', 'Battery Storage', 'Solar'], dtype=object)
[11]:
nexus_energy_plots["Nexus_capital_investment_power"]
[12]:
result_pack.get_dataframe('CapitalInvestment').TECHNOLOGY.unique()
[12]:
array(['DEMAGRGWTBC1', 'DEMAGRSURBC1', 'DEMCOMBIO', 'DEMCOMELCB02',
'DEMCOMRPP', 'DEMCOMNGS', 'DEMINDBIO', 'DEMINDDSL', 'DEMINDELCB02',
'DEMINDNGS', 'DEMPUBGWTBC1', 'DEMPUBSURBC1', 'DEMPWRBIO',
'DEMPWRHYD', 'DEMPWRSOL', 'DEMPWRWND', 'DEMRESBIO', 'DEMRESELCB02',
'DEMRESRPP', 'DEMRESNGS', 'DEMTRABIO', 'DEMTRADSL', 'DEMTRAELCB02',
'DEMTRAGSL', 'DEMTRAHFO', 'DEMTRAJFL', 'DEMTRALPG', 'DEMTRANGS',
'IMPDSL', 'IMPGSL', 'IMPHFO', 'IMPJFL', 'IMPRPP', 'IMPLPG',
'LNDAGRBC1C01', 'LNDAGRBC1C02', 'LNDAGRBC1C03', 'LNDAGRBC1C04',
'LNDAGRBC1C05', 'LNDAGRBC1C06', 'LNDAGRBC1C07', 'LNDALFHRBC1',
'LNDBARBC1', 'LNDBLTBC1', 'LNDFORBC1', 'LNDGRSBC1', 'LNDMAIHIBC1',
'LNDOATHIBC1', 'LNDOTHHIBC1', 'LNDPEAHIBC1', 'LNDPTWHIBC1',
'LNDRAPHIBC1', 'LNDRYEHIBC1', 'LNDWATBC1', 'LNDWHEHRBC1',
'MINLNDBC1', 'MINNGS', 'MINPRCBC1', 'PWRTRNB01', 'PWRWNDB11',
'RNWBIO', 'RNWHYD', 'RNWSOL', 'RNWWND', 'BATTERY_STORAGE',
'PWRSOLB02', 'PWRWNDB12', 'PWRWNDB13', 'PWRWNDB14', 'HDG02',
'HDG01', 'CCS01', 'PWRSOLB04', 'PWRSOLB07', 'PWRWNDB15',
'PWRWNDB16', 'PWRWNDB17', 'PWRWNDB18', 'PWRWNDB19', 'PWRWNDB21',
'PWRWNDB25', 'PWRWNDB27'], dtype=object)
[13]:
data=result_pack.get_dataframe('CapitalInvestment')
[ ]:
[ ]:
[14]:
data_pwr = data.loc[data['TECHNOLOGY'].str.startswith('PWR')].copy()
data_pwr.loc[:, 'TECHNOLOGY_CODE'] = data_pwr['TECHNOLOGY'].str[:6]
data_pwr.loc[:, 'TECHNOLOGY_LABEL'] = data_pwr['TECHNOLOGY_CODE'].map(bcnexus_const.legend_labels)
data_pwr
data_pwr_grouped_data = data_pwr.groupby(['TECHNOLOGY_LABEL','TECHNOLOGY_CODE', 'YEAR', 'REGION']).sum().reset_index()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[14], line 3
1 data_pwr = data.loc[data['TECHNOLOGY'].str.startswith('PWR')].copy()
2 data_pwr.loc[:, 'TECHNOLOGY_CODE'] = data_pwr['TECHNOLOGY'].str[:6]
----> 3 data_pwr.loc[:, 'TECHNOLOGY_LABEL'] = data_pwr['TECHNOLOGY_CODE'].map(bcnexus_const.legend_labels)
4 data_pwr
5 data_pwr_grouped_data = data_pwr.groupby(['TECHNOLOGY_LABEL','TECHNOLOGY_CODE', 'YEAR', 'REGION']).sum().reset_index()
NameError: name 'bcnexus_const' is not defined
[ ]:
data_pwr_grouped_data
| TECHNOLOGY_LABEL | TECHNOLOGY_CODE | YEAR | REGION | TECHNOLOGY | VALUE | power_techs | power_techs_labels | |
|---|---|---|---|---|---|---|---|---|
| 0 | Solar | PWRSOL | 2030 | REGION1 | PWRSOLB02 | 154.198720 | PWRSOL | Solar |
| 1 | Solar | PWRSOL | 2041 | REGION1 | PWRSOLB04PWRSOLB07 | 196.447620 | PWRSOLPWRSOL | SolarSolar |
| 2 | Solar | PWRSOL | 2042 | REGION1 | PWRSOLB04 | 46.023512 | PWRSOL | Solar |
| 3 | Solar | PWRSOL | 2043 | REGION1 | PWRSOLB04 | 41.921152 | PWRSOL | Solar |
| 4 | Solar | PWRSOL | 2044 | REGION1 | PWRSOLB07 | 180.242341 | PWRSOL | Solar |
| 5 | Solar | PWRSOL | 2045 | REGION1 | PWRSOLB04PWRSOLB07 | 923.025077 | PWRSOLPWRSOL | SolarSolar |
| 6 | Solar | PWRSOL | 2046 | REGION1 | PWRSOLB04 | 714.000511 | PWRSOL | Solar |
| 7 | Solar | PWRSOL | 2050 | REGION1 | PWRSOLB04 | 78.831701 | PWRSOL | Solar |
| 8 | Transmission Grid | PWRTRN | 2021 | REGION1 | PWRTRNB01 | 0.082407 | 0 | 0 |
| 9 | Transmission Grid | PWRTRN | 2022 | REGION1 | PWRTRNB01 | 0.087942 | 0 | 0 |
| 10 | Transmission Grid | PWRTRN | 2023 | REGION1 | PWRTRNB01 | 0.092708 | 0 | 0 |
| 11 | Transmission Grid | PWRTRN | 2024 | REGION1 | PWRTRNB01 | 0.095800 | 0 | 0 |
| 12 | Transmission Grid | PWRTRN | 2025 | REGION1 | PWRTRNB01 | 0.099011 | 0 | 0 |
| 13 | Transmission Grid | PWRTRN | 2026 | REGION1 | PWRTRNB01 | 0.102372 | 0 | 0 |
| 14 | Transmission Grid | PWRTRN | 2027 | REGION1 | PWRTRNB01 | 0.105132 | 0 | 0 |
| 15 | Transmission Grid | PWRTRN | 2028 | REGION1 | PWRTRNB01 | 0.108612 | 0 | 0 |
| 16 | Transmission Grid | PWRTRN | 2029 | REGION1 | PWRTRNB01 | 0.113077 | 0 | 0 |
| 17 | Transmission Grid | PWRTRN | 2030 | REGION1 | PWRTRNB01 | 0.116846 | 0 | 0 |
| 18 | Transmission Grid | PWRTRN | 2031 | REGION1 | PWRTRNB01 | 0.119784 | 0 | 0 |
| 19 | Transmission Grid | PWRTRN | 2032 | REGION1 | PWRTRNB01 | 0.124479 | 0 | 0 |
| 20 | Transmission Grid | PWRTRN | 2033 | REGION1 | PWRTRNB01 | 0.129068 | 0 | 0 |
| 21 | Transmission Grid | PWRTRN | 2034 | REGION1 | PWRTRNB01 | 0.131598 | 0 | 0 |
| 22 | Transmission Grid | PWRTRN | 2035 | REGION1 | PWRTRNB01 | 0.134036 | 0 | 0 |
| 23 | Transmission Grid | PWRTRN | 2036 | REGION1 | PWRTRNB01 | 0.136388 | 0 | 0 |
| 24 | Transmission Grid | PWRTRN | 2037 | REGION1 | PWRTRNB01 | 0.138601 | 0 | 0 |
| 25 | Transmission Grid | PWRTRN | 2038 | REGION1 | PWRTRNB01 | 0.140688 | 0 | 0 |
| 26 | Transmission Grid | PWRTRN | 2039 | REGION1 | PWRTRNB01 | 0.142723 | 0 | 0 |
| 27 | Transmission Grid | PWRTRN | 2040 | REGION1 | PWRTRNB01 | 0.144651 | 0 | 0 |
| 28 | Transmission Grid | PWRTRN | 2041 | REGION1 | PWRTRNB01 | 0.146480 | 0 | 0 |
| 29 | Transmission Grid | PWRTRN | 2042 | REGION1 | PWRTRNB01 | 0.145630 | 0 | 0 |
| 30 | Transmission Grid | PWRTRN | 2043 | REGION1 | PWRTRNB01 | 0.144049 | 0 | 0 |
| 31 | Transmission Grid | PWRTRN | 2044 | REGION1 | PWRTRNB01 | 0.143604 | 0 | 0 |
| 32 | Transmission Grid | PWRTRN | 2045 | REGION1 | PWRTRNB01 | 0.142651 | 0 | 0 |
| 33 | Transmission Grid | PWRTRN | 2046 | REGION1 | PWRTRNB01 | 0.141284 | 0 | 0 |
| 34 | Transmission Grid | PWRTRN | 2047 | REGION1 | PWRTRNB01 | 0.141284 | 0 | 0 |
| 35 | Transmission Grid | PWRTRN | 2048 | REGION1 | PWRTRNB01 | 0.141284 | 0 | 0 |
| 36 | Transmission Grid | PWRTRN | 2049 | REGION1 | PWRTRNB01 | 0.141284 | 0 | 0 |
| 37 | Transmission Grid | PWRTRN | 2050 | REGION1 | PWRTRNB01 | 0.141284 | 0 | 0 |
| 38 | Wind | PWRWND | 2031 | REGION1 | PWRWNDB11PWRWNDB12 | 693.760946 | PWRWNDPWRWND | WindWind |
| 39 | Wind | PWRWND | 2032 | REGION1 | PWRWNDB13PWRWNDB14PWRWNDB15PWRWNDB16PWRWNDB17P... | 1962.937424 | PWRWNDPWRWNDPWRWNDPWRWNDPWRWNDPWRWNDPWRWND | WindWindWindWindWindWindWind |
| 40 | Wind | PWRWND | 2045 | REGION1 | PWRWNDB18PWRWNDB21PWRWNDB25 | 212.799367 | PWRWNDPWRWNDPWRWND | WindWindWind |
| 41 | Wind | PWRWND | 2046 | REGION1 | PWRWNDB27 | 8914.138107 | PWRWND | Wind |
| 42 | Wind | PWRWND | 2047 | REGION1 | PWRWNDB27 | 506.072191 | PWRWND | Wind |
[ ]:
import plotly.express as px
# Create a bar plot with custom legend colors
# custom_colors = bcnexus_const.custom_colors
fig = px.bar(data_pwr_grouped_data, x='YEAR', y='VALUE', color='TECHNOLOGY_LABEL',
labels={'VALUE': 'Mill $'}, title='Bar Plot of VALUE over Years')
fig.show()
[ ]:
end_use_fuels_set
{'BIO',
'COA',
'CRU',
'DSL',
'ELCB02',
'GEO',
'GSL',
'HDG',
'HFO',
'HYD',
'JFL',
'KER',
'LPG',
'NGS',
'RPP',
'SOL',
'URN',
'WND'}
[ ]:
import re
# Create a regex pattern from the set items
pattern = '|'.join(map(re.escape, end_use_fuels_set))
# Filter rows where FUEL contains any of the set items
data2 = data[~data['FUEL'].str.contains(pattern, regex=True)]
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File ~/miniconda3/envs/bc_combined_modelling/lib/python3.12/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key)
3804 try:
-> 3805 return self._engine.get_loc(casted_key)
3806 except KeyError as err:
File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc()
File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc()
File pandas/_libs/hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item()
File pandas/_libs/hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'FUEL'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
Cell In[17], line 7
4 pattern = '|'.join(map(re.escape, end_use_fuels_set))
6 # Filter rows where FUEL contains any of the set items
----> 7 data2 = data[~data['FUEL'].str.contains(pattern, regex=True)]
File ~/miniconda3/envs/bc_combined_modelling/lib/python3.12/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key)
4100 if self.columns.nlevels > 1:
4101 return self._getitem_multilevel(key)
-> 4102 indexer = self.columns.get_loc(key)
4103 if is_integer(indexer):
4104 indexer = [indexer]
File ~/miniconda3/envs/bc_combined_modelling/lib/python3.12/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key)
3807 if isinstance(casted_key, slice) or (
3808 isinstance(casted_key, abc.Iterable)
3809 and any(isinstance(x, slice) for x in casted_key)
3810 ):
3811 raise InvalidIndexError(key)
-> 3812 raise KeyError(key) from err
3813 except TypeError:
3814 # If we have a listlike key, _check_indexing_error will raise
3815 # InvalidIndexError. Otherwise we fall through and re-raise
3816 # the TypeError.
3817 self._check_indexing_error(key)
KeyError: 'FUEL'
[ ]:
data3=data2[data2['TECHNOLOGY'].str.startswith('LND')]
[ ]:
bcnexus_const.name_mapping.get('FUEL')
[ ]:
import plotly.express as px
# Map legend labels using bcnexus_const.name_mapping
data3['FUEL_LABEL'] = data3['FUEL'].map(bcnexus_const.name_mapping.get('FUEL'))
fig = px.bar(data3, x='YEAR', y='VALUE', color='FUEL_LABEL', title='Bar Plot of Fuel vs Year')
fig.show()