From b9c6743995828cadb15adbb024810d3ae203173a Mon Sep 17 00:00:00 2001 From: vargastat Date: Wed, 17 Apr 2024 17:06:56 +0200 Subject: [PATCH] TimeScenarioSeriesData modified for dataframe --- src/andromede/study/data.py | 37 --------------------------- tests/unittests/test_data.py | 48 ++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 58 deletions(-) diff --git a/src/andromede/study/data.py b/src/andromede/study/data.py index e93841ff..958ff378 100644 --- a/src/andromede/study/data.py +++ b/src/andromede/study/data.py @@ -104,43 +104,6 @@ def check_requirement(self, time: bool, scenario: bool) -> bool: return scenario -# def load_ts_from_txt(file_ts: str) -> Dict[TimeScenarioIndex, float]: -# time_series = {} -# try: -# if file_ts is not None: -# path = Path(file_ts) -# df = pd.read_csv(path, header=None, sep="\s+") -# num_rows, num_cols = df.shape -# for time in range(num_rows): -# for scenario in range(num_cols): -# index = TimeScenarioIndex(time=time, scenario=scenario) -# cell_value = str(df.iloc[time, scenario]) -# time_series[index] = float(cell_value) -# except FileNotFoundError: -# print(f"Error: File {file_ts} does not exists") -# return time_series -# -# -# @dataclass(frozen=True) -# class TimeScenarioSeriesData(AbstractDataStructure): -# """ -# Container for identifiable timeseries data. -# When a model is instantiated as a component, property values -# can be defined by referencing one of those timeseries by its ID. -# """ -# -# time_scenario_series: Dict[TimeScenarioIndex, float] -# -# def get_value(self, timestep: int, scenario: int) -> float: -# return self.time_scenario_series[TimeScenarioIndex(timestep, scenario)] -# -# def check_requirement(self, time: bool, scenario: bool) -> bool: -# if not isinstance(self, TimeScenarioSeriesData): -# raise ValueError("Invalid data type for TimeScenarioSeriesData") -# -# return time and scenario - - def load_ts_from_txt(file_ts: Optional[str]) -> pd.DataFrame: path = Path(str(file_ts)) try: diff --git a/tests/unittests/test_data.py b/tests/unittests/test_data.py index 11ebd69f..47128697 100644 --- a/tests/unittests/test_data.py +++ b/tests/unittests/test_data.py @@ -16,7 +16,6 @@ import pytest from andromede.expression import param, var -from andromede.expression.expression import port_field from andromede.expression.indexing_structure import IndexingStructure from andromede.libs.standard import ( BALANCE_PORT_TYPE, @@ -44,7 +43,6 @@ ScenarioIndex, ScenarioSeriesData, TimeIndex, - TimeScenarioIndex, TimeScenarioSeriesData, TimeSeriesData, create_component, @@ -125,7 +123,23 @@ def mock_generator_with_scenario_varying_fixed_time_param() -> Model: return scenario_varying_fixed_time_generator -def test_requirements_consistency_demand_model_fix_ok(mock_network: Network) -> None: +@pytest.fixture +def demand_data() -> TimeScenarioSeriesData: + demand_data = pd.DataFrame( + [ + [100], + [50], + ], + index=[0, 1], + columns=[0], + ) + + return TimeScenarioSeriesData(demand_data) + + +def test_requirements_consistency_demand_model_fix_ok( + mock_network: Network, demand_data: TimeScenarioSeriesData +) -> None: # Given # database data for "demand" defined as Time varying # and model "D" DEMAND_MODEL is TIME_AND_SCENARIO_FREE @@ -133,9 +147,6 @@ def test_requirements_consistency_demand_model_fix_ok(mock_network: Network) -> database.add_data("G", "p_max", ConstantData(100)) database.add_data("G", "cost", ConstantData(30)) - demand_data = TimeScenarioSeriesData( - {TimeScenarioIndex(0, 0): 100, TimeScenarioIndex(1, 0): 50} - ) database.add_data("D", "demand", demand_data) # When @@ -157,7 +168,7 @@ def test_requirements_consistency_generator_model_ok(mock_network: Network) -> N def test_consistency_generation_time_free_for_constant_model_raises_exception( - mock_network: Network, + mock_network: Network, demand_data: TimeScenarioSeriesData ) -> None: # Given # database data for "p_max" defined as time varying @@ -167,9 +178,6 @@ def test_consistency_generation_time_free_for_constant_model_raises_exception( database.add_data("G", "cost", ConstantData(30)) - demand_data = TimeScenarioSeriesData( - {TimeScenarioIndex(0, 0): 100, TimeScenarioIndex(1, 0): 50} - ) database.add_data("D", "demand", demand_data) database.add_data("G", "p_max", demand_data) @@ -179,7 +187,7 @@ def test_consistency_generation_time_free_for_constant_model_raises_exception( def test_requirements_consistency_demand_model_time_varying_ok( - mock_network: Network, + mock_network: Network, demand_data: TimeScenarioSeriesData ) -> None: # Given # database data for "demand" defined as constant @@ -187,10 +195,6 @@ def test_requirements_consistency_demand_model_time_varying_ok( database = DataBase() database.add_data("G", "p_max", ConstantData(100)) database.add_data("G", "cost", ConstantData(30)) - - demand_data = TimeScenarioSeriesData( - {TimeScenarioIndex(0, 0): 100, TimeScenarioIndex(1, 0): 50} - ) database.add_data("D", "demand", demand_data) # When @@ -229,12 +233,14 @@ def test_requirements_consistency_time_varying_parameter_with_correct_data_passe (ScenarioSeriesData({ScenarioIndex(0): 100, ScenarioIndex(1): 50})), ( TimeScenarioSeriesData( - { - TimeScenarioIndex(0, 0): 100, - TimeScenarioIndex(0, 1): 50, - TimeScenarioIndex(1, 0): 500, - TimeScenarioIndex(1, 1): 540, - } + pd.DataFrame( + [ + [100, 500], + [500, 540], + ], + index=[0, 1], + columns=[0, 1], + ) ) ), ],