Skip to content

Commit

Permalink
Merge pull request #99 from rl-institut/fix/postprocessing_function
Browse files Browse the repository at this point in the history
Fix/postprocessing function
  • Loading branch information
MaGering authored Feb 26, 2024
2 parents 03cc6af + b88a08c commit 8de4dfe
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
12 changes: 8 additions & 4 deletions oemoflex/model/datapackage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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

Expand Down Expand Up @@ -345,8 +349,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,
}

Expand Down
69 changes: 69 additions & 0 deletions oemoflex/model/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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):
Expand Down Expand Up @@ -52,6 +54,73 @@ 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
Expand Down

0 comments on commit 8de4dfe

Please sign in to comment.