Skip to content

Commit

Permalink
TimeScenarioSeriesData modified for dataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Apr 17, 2024
1 parent 40bf223 commit b9c6743
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 58 deletions.
37 changes: 0 additions & 37 deletions src/andromede/study/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 27 additions & 21 deletions tests/unittests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,7 +43,6 @@
ScenarioIndex,
ScenarioSeriesData,
TimeIndex,
TimeScenarioIndex,
TimeScenarioSeriesData,
TimeSeriesData,
create_component,
Expand Down Expand Up @@ -125,17 +123,30 @@ 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
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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -179,18 +187,14 @@ 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
# and model "D" DEMAND_MODEL is TIME_AND_SCENARIO_FREE
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
Expand Down Expand Up @@ -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],
)
)
),
],
Expand Down

0 comments on commit b9c6743

Please sign in to comment.