From 132b9b70e260f2e58fbfd27760c1e9cee1953d4b Mon Sep 17 00:00:00 2001 From: "christoph.von.oy" <christoph.von.oy@rwth-aachen.de> Date: Mon, 29 Apr 2024 17:41:06 +0200 Subject: [PATCH] Eliminated input profile processor from examples --- dynamics.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/dynamics.py b/dynamics.py index f4bfbe5..c131aed 100644 --- a/dynamics.py +++ b/dynamics.py @@ -24,6 +24,7 @@ THE SOFTWARE. import abc import numpy as np +import pandas as pd from typing import List, Tuple, Union @@ -1160,6 +1161,36 @@ class Profile: self.values = values self.dynamic = dynamic + @staticmethod + def from_csv( + path, dynamic: Dynamic + ): # path: [path to a file], type hinting dict[str, Profile]] + if isinstance(dynamic, (TrivialDynamic, TreeDynamic)): + df = pd.read_csv(path) + if len(df) != dynamic.number_of_steps(): + raise ValueError( + f"The number of rows in the csv file and the number of steps in the dynamic have to be the same!" + ) + return { + column: Profile(df[column].values, dynamic) for column in df.columns + } + elif isinstance(dynamic, AggregatedDynamic): + df = pd.read_csv(path, index_col=[0, 1]) + profiles = {column: dict() for column in df.columns} + for period_index in df.index.levels[0]: + if ( + len(df.loc[period_index]) + != dynamic.period_dynamics[period_index].number_of_steps() + ): + raise ValueError( + f"The number of rows in the csv file for a period and the number of steps in the period dynamic have to be the same!" + ) + for column in df.columns: + profiles[column][period_index] = df.loc[period_index][column].values + return {column: Profile(profiles[column], dynamic) for column in df.columns} + else: + raise ValueError(f"Invalid dynamic type {type(dynamic)}!") + def test_single_resampling( dynamic: TreeDynamic, -- GitLab