Skip to content

Commit

Permalink
Binding constraint time series can be created
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigurd-Borge committed Sep 25, 2024
1 parent d163cee commit 1115467
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/antares/service/local_services/binding_constraint_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
ConstraintTerm,
BindingConstraint,
ConstraintMatrixName,
BindingConstraintOperator,
)
from antares.service.base_services import BaseBindingConstraintService
from antares.tools.ini_tool import IniFile, IniFileTypes
from antares.tools.time_series_tool import TimeSeriesFile, TimeSeriesFileType


class BindingConstraintLocalService(BaseBindingConstraintService):
Expand All @@ -32,6 +34,7 @@ def __init__(self, config: LocalConfiguration, study_name: str, **kwargs: Any) -
self.study_name = study_name
self._binding_constraints: dict[str, BindingConstraint] = {}
self.ini_file = IniFile(self.config.study_path, IniFileTypes.BINDING_CONSTRAINTS_INI)
self._time_series: dict[str, TimeSeriesFile] = {}

def create_binding_constraint(
self,
Expand All @@ -49,10 +52,40 @@ def create_binding_constraint(
terms=terms,
)
constraint.properties = constraint.local_properties.yield_binding_constraint_properties()
self._binding_constraints[constraint.id] = constraint

# Add binding constraints
self._binding_constraints[constraint.id] = constraint
self._write_binding_constraint_ini()

# Add constraint time series
if (
constraint.properties.operator in (BindingConstraintOperator.LESS, BindingConstraintOperator.BOTH)
and less_term_matrix is not None
):
self._time_series[f"{name}_lt"] = TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_LESS,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=less_term_matrix,
)
if constraint.properties.operator == BindingConstraintOperator.EQUAL and equal_term_matrix is not None:
self._time_series[f"{name}_eq"] = TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_EQUAL,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=equal_term_matrix,
)
if (
constraint.properties.operator in (BindingConstraintOperator.GREATER, BindingConstraintOperator.BOTH)
and greater_term_matrix is not None
):
self._time_series[f"{name}_gt"] = TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_GREATER,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=greater_term_matrix,
)

return constraint

def _write_binding_constraint_ini(self) -> None:
Expand All @@ -67,6 +100,10 @@ def _write_binding_constraint_ini(self) -> None:
def binding_constraints(self) -> dict[str, BindingConstraint]:
return self._binding_constraints

@property
def time_series(self) -> dict[str, TimeSeriesFile]:
return self._time_series

def add_constraint_terms(self, constraint: BindingConstraint, terms: list[ConstraintTerm]) -> list[ConstraintTerm]:
new_terms = constraint.local_properties.terms | {
term.id: term for term in terms if term.id not in constraint.get_terms()
Expand Down
10 changes: 9 additions & 1 deletion src/antares/tools/time_series_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class TimeSeriesFileType(Enum):
TimeSeriesFileType.SOLAR.value.format(area_id="test_area")
"""

BINDING_CONSTRAINT_EQUAL = "input/bindingconstraints/{constraint_id}_eq.txt"
BINDING_CONSTRAINT_GREATER = "input/bindingconstraints/{constraint_id}_gt.txt"
BINDING_CONSTRAINT_LESS = "input/bindingconstraints/{constraint_id}_lt.txt"
LOAD = "input/load/series/load_{area_id}.txt"
LOAD_CONVERSION = "input/load/prepro/{area_id}/conversion.txt"
Expand Down Expand Up @@ -60,6 +62,7 @@ class TimeSeriesFile:
ts_file_type: Type of time series file using the class TimeSeriesFileType.
study_path: `Path` to the study directory.
area_id: Area ID for file paths that use the area's id in their path
constraint_id: Constraint ID for file paths that use the binding constraint's id in their path
time_series: The actual timeseries as a pandas DataFrame.
Raises:
Expand All @@ -72,13 +75,18 @@ def __init__(
study_path: Path,
*,
area_id: Optional[str] = None,
constraint_id: Optional[str] = None,
time_series: Optional[pd.DataFrame] = None,
) -> None:
if "{area_id}" in ts_file_type.value and area_id is None:
raise ValueError("area_id is required for this file type.")
if "{constraint_id}" in ts_file_type.value and constraint_id is None:
raise ValueError("constraint_id is required for this file type.")

self.file_path = study_path / (
ts_file_type.value if not area_id else ts_file_type.value.format(area_id=area_id)
ts_file_type.value
if not (area_id or constraint_id)
else ts_file_type.value.format(area_id=area_id, constraint_id=constraint_id)
)

if self.file_path.is_file() and time_series is not None:
Expand Down
64 changes: 64 additions & 0 deletions tests/antares/services/local_services/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from configparser import ConfigParser
from pathlib import Path

import numpy as np
import pandas as pd
import pytest

from antares.config.local_configuration import LocalConfiguration
Expand Down Expand Up @@ -1237,3 +1239,65 @@ def test_constraint_term_with_offset_and_ini_have_correct_values(
actual_ini_content = file.read()

assert actual_ini_content == expected_ini_contents

def test_binding_constraint_with_timeseries_stores_ts_file(self, local_study_with_hydro):
# Given
ts_matrix = pd.DataFrame(np.zeros([365 * 24, 2]))

# When
constraints = {
"lesser":
# Less than timeseries
local_study_with_hydro.create_binding_constraint(
name="test constraint - less",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.LESS,
),
less_term_matrix=ts_matrix,
),
"equal":
# Equal timeseries
local_study_with_hydro.create_binding_constraint(
name="test constraint - equal",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.EQUAL,
),
equal_term_matrix=ts_matrix,
),
"greater":
# Greater than timeseries
local_study_with_hydro.create_binding_constraint(
name="test constraint - greater",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.GREATER,
),
greater_term_matrix=ts_matrix,
),
"both":
# Greater than timeseries
local_study_with_hydro.create_binding_constraint(
name="test constraint - both",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.BOTH,
),
less_term_matrix=ts_matrix,
greater_term_matrix=ts_matrix,
),
}

# Then
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['lesser'].id.lower()}_lt"
].file_path.is_file()
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['equal'].id.lower()}_eq"
].file_path.is_file()
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['greater'].id.lower()}_gt"
].file_path.is_file()
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['both'].id.lower()}_lt"
].file_path.is_file()
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['both'].id.lower()}_gt"
].file_path.is_file()

0 comments on commit 1115467

Please sign in to comment.