From 32037fedf6089ec4d3918fa2df0ba84ca6dfd169 Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Wed, 31 Jan 2024 22:05:18 +0100 Subject: [PATCH 1/5] Set dependency on oemof.tabular dev --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 08a3df6..a659e2d 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def read(fname): "pyyaml", "dynaconf", "pandas", - "oemof.tabular @ git+https://github.com/oemof/oemof-tabular.git@v0.0.4.dev1", + "oemof.tabular @ git+https://github.com/oemof/oemof-tabular.git@dev", "plotly", "frictionless", "matplotlib", From e530f975af6b30b7d250a1402ddba1ee5d764cdf Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Wed, 31 Jan 2024 22:17:13 +0100 Subject: [PATCH 2/5] Insert component_results and bus_results function --- oemoflex/model/postprocessing.py | 73 +++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/oemoflex/model/postprocessing.py b/oemoflex/model/postprocessing.py index d56a6ea..50f3a26 100644 --- a/oemoflex/model/postprocessing.py +++ b/oemoflex/model/postprocessing.py @@ -4,7 +4,8 @@ import pandas as pd from oemof.solph import Bus, EnergySystem - +from oemof.solph import views +from oemof.tabular import facades def get_sequences(dict): r""" @@ -52,6 +53,76 @@ def get_scalars(dict): return scalars +def component_results(es, results, select="sequences"): + """Aggregated by component type""" + + c = {} + + if not hasattr(es, "typemap"): + setattr(es, "typemap", facades.TYPEMAP) + + for k, v in es.typemap.items(): + if isinstance(k, str): + if select == "sequences": + _seq_by_type = [ + views.node(results, n, multiindex=True).get("sequences") + for n in es.nodes + if isinstance(n, v) and not isinstance(n, Bus) + ] + # check if dataframes / series have been returned + if any( + [ + isinstance(i, (pd.DataFrame, pd.Series)) + for i in _seq_by_type + ] + ): + seq_by_type = pd.concat(_seq_by_type, axis=1) + c[str(k)] = seq_by_type + + if select == "scalars": + _sca_by_type = [ + views.node(results, n, multiindex=True).get("scalars") + for n in es.nodes + if isinstance(n, v) and not isinstance(n, Bus) + ] + + if [x for x in _sca_by_type if x is not None]: + _sca_by_type = pd.concat(_sca_by_type) + c[str(k)] = _sca_by_type + + return c + + +def bus_results(es, results, select="sequences", concat=False): + """Aggregated for every bus of the energy system""" + br = {} + + buses = [b for b in es.nodes if isinstance(b, Bus)] + + for b in buses: + if select == "sequences": + bus_sequences = pd.concat( + [ + views.node(results, b, multiindex=True).get( + "sequences", pd.DataFrame() + ) + ], + axis=1, + ) + br[str(b)] = bus_sequences + if select == "scalars": + br[str(b)] = views.node(results, b, multiindex=True).get("scalars") + + if concat: + if select == "sequences": + axis = 1 + else: + axis = 0 + br = pd.concat([b for b in br.values()], axis=axis) + + return br + + def drop_component_to_component(series): r""" Drops those entries of an oemof_tuple indexed Series From 342c20315fc965e50f7f057f358e18736e6349d2 Mon Sep 17 00:00:00 2001 From: Stefanie Nguyen Date: Wed, 31 Jan 2024 22:19:21 +0100 Subject: [PATCH 3/5] Import results function and adapt code --- oemoflex/model/datapackage.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/oemoflex/model/datapackage.py b/oemoflex/model/datapackage.py index c0fea64..649f384 100644 --- a/oemoflex/model/datapackage.py +++ b/oemoflex/model/datapackage.py @@ -1,14 +1,13 @@ import copy import os -import oemof.tabular.tools.postprocessing as tabular_pp from oemof.tabular.datapackage.building import infer_metadata import pandas as pd from frictionless import Package from oemof.solph.views import convert_to_multiindex from oemoflex.model.model_structure import create_default_data -from oemoflex.model.postprocessing import group_by_element, run_postprocessing +from oemoflex.model.postprocessing import group_by_element, run_postprocessing, component_results, bus_results from oemoflex.tools.helpers import load_yaml from oemoflex.config.config import settings @@ -345,8 +344,8 @@ def drop_empty_dfs(dictionary): return {key: value for key, value in dictionary.items() if not value.empty} methods = { - "bus": tabular_pp.bus_results, - "component": tabular_pp.component_results, + "bus": bus_results, + "component": component_results, "by_variable": self._get_seq_by_var, } From f34b4a96a44cb9a7b7c5cce9da3f95bc5ce6605d Mon Sep 17 00:00:00 2001 From: Marie-Claire Gering <52790556+MaGering@users.noreply.github.com> Date: Thu, 22 Feb 2024 18:48:07 +0100 Subject: [PATCH 4/5] Update setup.py with tabular branch with tag version 0.5.2.dev1 instead of 0.5.2dev1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index a659e2d..0162f44 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ def read(fname): "pyyaml", "dynaconf", "pandas", - "oemof.tabular @ git+https://github.com/oemof/oemof-tabular.git@dev", + "oemof.tabular @ git+https://github.com/oemof/oemof-tabular.git@fix/solph-version-on-dev", "plotly", "frictionless", "matplotlib", From 7bdaeac278d84e1a0288f653ee68b04117ab0571 Mon Sep 17 00:00:00 2001 From: MaGering Date: Mon, 26 Feb 2024 12:23:26 +0100 Subject: [PATCH 5/5] Apply black --- oemoflex/model/datapackage.py | 7 ++++++- oemoflex/model/postprocessing.py | 6 ++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/oemoflex/model/datapackage.py b/oemoflex/model/datapackage.py index 649f384..60203c5 100644 --- a/oemoflex/model/datapackage.py +++ b/oemoflex/model/datapackage.py @@ -7,7 +7,12 @@ from oemof.solph.views import convert_to_multiindex from oemoflex.model.model_structure import create_default_data -from oemoflex.model.postprocessing import group_by_element, run_postprocessing, component_results, bus_results +from oemoflex.model.postprocessing import ( + group_by_element, + run_postprocessing, + component_results, + bus_results, +) from oemoflex.tools.helpers import load_yaml from oemoflex.config.config import settings diff --git a/oemoflex/model/postprocessing.py b/oemoflex/model/postprocessing.py index 50f3a26..984d195 100644 --- a/oemoflex/model/postprocessing.py +++ b/oemoflex/model/postprocessing.py @@ -7,6 +7,7 @@ from oemof.solph import views from oemof.tabular import facades + def get_sequences(dict): r""" Gets sequences from oemof.solph's parameter or results dictionary. @@ -71,10 +72,7 @@ def component_results(es, results, select="sequences"): ] # check if dataframes / series have been returned if any( - [ - isinstance(i, (pd.DataFrame, pd.Series)) - for i in _seq_by_type - ] + [isinstance(i, (pd.DataFrame, pd.Series)) for i in _seq_by_type] ): seq_by_type = pd.concat(_seq_by_type, axis=1) c[str(k)] = seq_by_type