Skip to content

Commit

Permalink
Merge branch 'release/v0.0.3' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MaGering committed Mar 11, 2024
2 parents 0149fe0 + 325ae55 commit 2648e03
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.8, 3.9, "3.10" ]
python-version: [ "3.10" ]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down
8 changes: 6 additions & 2 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
# Required
version: 2

# Optionally set the version of Python and requirements required to build your docs
build:
os: ubuntu-22.04
tools:
python: "3.10"

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# Optionally set the version of Python and requirements required to build your docs
python:
version: "3.7"
install:
- requirements: docs/docs_requirements.txt
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
author = "Reiner Lemoine Institut"

# The full version, including alpha/beta/rc tags
release = "0.0.2"
release = "0.0.3"


# -- General configuration ---------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions docs/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ What's new

.. include:: whats_new/v0-0-1.rst
.. include:: whats_new/v0-0-2.rst
.. include:: whats_new/v0-0-3.rst
10 changes: 10 additions & 0 deletions docs/whats_new/v0-0-3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
v0.0.3
======

* The oemof-tabular version has been increased to v0.0.5 which leads to the oemof.solph version
0.5.2.dev1 (Changes made on dev)
* Postprocessing functions have been moved from oemof-tabular to oemoflex (PR 99)
* With higher pandas version (2.2.1) the concat function in postprocessing.py had to be revised
(PR 100)
* The build of docs has been fixed (PR 102 + commit b3999c40c800454849c0bda537dbe7a8607faa73
and 4a461aa0f0e7e51284d79dec4a1a94d33d66e98c)
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
107 changes: 100 additions & 7 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 Expand Up @@ -720,20 +789,44 @@ def run_postprocessing(es):
summed_marginal_costs,
]

all_scalars = pd.concat(all_scalars, axis=0)
# all_scalars = pd.concat(all_scalars, axis=0)
# This does not work with the update from pandas==2.0.3 to pandas==2.2.1 because
# invested_capacity and invested_storage_capacity both have a TimeStamp as column name which
# results in a mix-up of the levels
# Fixing Approach:
# list(map(lambda series: series.rename('0', inplace=True), all_scalars))
# timestamp_variable = pd.to_datetime("2017-01-01 00:00:00")
# all_scalars = pd.concat(all_scalars, axis=0,
# keys=['source', 'target', 'var_name', '0', 0, 'var_value', timestamp_variable])
# did not work
# Todo: To be further investigated

# Index work-around - issues with concat and Multiindex
all_scalars_reindexed = [
s.rename("var_value").reset_index()
for s in all_scalars
if not isinstance(s, type(None))
]
all_scalars_df_reindexed = pd.concat(
all_scalars_reindexed, ignore_index=True, axis=0
)
all_scalars_df = all_scalars_df_reindexed.set_index(
["source", "target", "var_name"]
)

all_scalars = map_var_names(all_scalars)
# Map var_names
all_scalars_df = map_var_names(all_scalars_df)

all_scalars = add_component_info(all_scalars)
all_scalars_df = add_component_info(all_scalars_df)

# Set index to string
# TODO: Check if this can be done far earlier, also for performance reasons.
# TODO: To do so, the information drawn from the components in add_component_info has
# TODO: to be provided differently.
all_scalars.index = all_scalars.index.map(lambda x: (x[0].label, x[1]))
all_scalars_df.index = all_scalars_df.index.map(lambda x: (x[0].label, x[1]))

all_scalars = pd.concat([all_scalars, total_system_cost], axis=0)
all_scalars_df = pd.concat([all_scalars_df, total_system_cost], axis=0)

all_scalars = sort_scalars(all_scalars)
all_scalars_df = sort_scalars(all_scalars_df)

return all_scalars
return all_scalars_df
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def read(fname):

setup(
name="oemoflex",
version="0.0.2",
version="0.0.3",
description="A flexible model structure for creating and analysing multi-regional"
"sector-integrated energysystem models featuring many flexibility options",
long_description=read("README.md"),
Expand All @@ -32,7 +32,7 @@ def read(fname):
"pyyaml",
"dynaconf",
"pandas",
"oemof.tabular @ git+https://github.com/oemof/[email protected]",
"oemof.tabular==0.0.5",
"plotly",
"frictionless",
"matplotlib",
Expand Down

0 comments on commit 2648e03

Please sign in to comment.