Skip to content

Commit

Permalink
Default time series with zeroes are created if none is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigurd-Borge committed Sep 26, 2024
1 parent b63d99f commit 7d58483
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/antares/service/local_services/binding_constraint_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from typing import Optional, Any

import numpy as np
import pandas as pd

from antares.config.local_configuration import LocalConfiguration
Expand All @@ -21,6 +22,7 @@
BindingConstraint,
ConstraintMatrixName,
BindingConstraintOperator,
BindingConstraintFrequency,
)
from antares.service.base_services import BaseBindingConstraintService
from antares.tools.ini_tool import IniFile, IniFileTypes
Expand Down Expand Up @@ -58,40 +60,49 @@ def create_binding_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
):
time_series_length = (
(365 * 24 + 24) if constraint.properties.time_step == BindingConstraintFrequency.HOURLY else 366
)

if constraint.properties.operator in (BindingConstraintOperator.LESS, BindingConstraintOperator.BOTH):
time_series = (
less_term_matrix if less_term_matrix is not None else pd.DataFrame(np.zeros([time_series_length, 1]))
)
self._time_series[f"{name}_lt"] = TimeSeries(
less_term_matrix,
time_series,
TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_LESS,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=less_term_matrix,
time_series=time_series,
),
)
if constraint.properties.operator == BindingConstraintOperator.EQUAL and equal_term_matrix is not None:
if constraint.properties.operator == BindingConstraintOperator.EQUAL:
time_series = (
equal_term_matrix if equal_term_matrix is not None else pd.DataFrame(np.zeros([time_series_length, 1]))
)
self._time_series[f"{name}_eq"] = TimeSeries(
equal_term_matrix,
time_series,
TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_EQUAL,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=equal_term_matrix,
time_series=time_series,
),
)
if (
constraint.properties.operator in (BindingConstraintOperator.GREATER, BindingConstraintOperator.BOTH)
and greater_term_matrix is not None
):
if constraint.properties.operator in (BindingConstraintOperator.GREATER, BindingConstraintOperator.BOTH):
time_series = (
greater_term_matrix
if greater_term_matrix is not None
else pd.DataFrame(np.zeros([time_series_length, 1]))
)
self._time_series[f"{name}_gt"] = TimeSeries(
greater_term_matrix,
time_series,
TimeSeriesFile(
TimeSeriesFileType.BINDING_CONSTRAINT_GREATER,
self.config.study_path,
constraint_id=constraint.id.lower(),
time_series=greater_term_matrix,
time_series=time_series,
),
)

Expand Down
65 changes: 65 additions & 0 deletions tests/antares/services/local_services/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from antares.service.local_services.st_storage_local import ShortTermStorageLocalService
from antares.service.local_services.thermal_local import ThermalLocalService
from antares.tools.ini_tool import IniFileTypes
from antares.tools.time_series_tool import TimeSeriesFileType


class TestCreateStudy:
Expand Down Expand Up @@ -1301,3 +1302,67 @@ def test_binding_constraint_with_timeseries_stores_ts_file(self, local_study_wit
assert local_study_with_hydro._binding_constraints_service.time_series[
f"{constraints['both'].id.lower()}_gt"
].local_file.file_path.is_file()

def test_binding_constraints_have_correct_default_time_series(self, test_constraint, local_study_with_constraint):
# Given
expected_time_series_hourly = pd.DataFrame(np.zeros([365 * 24 + 24, 1]))
expected_time_series_daily_weekly = pd.DataFrame(np.zeros([365 + 1, 1]))
local_study_with_constraint.create_binding_constraint(
name="test greater",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.GREATER, time_step=BindingConstraintFrequency.WEEKLY
),
)
local_study_with_constraint.create_binding_constraint(
name="test equal",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.EQUAL, time_step=BindingConstraintFrequency.DAILY
),
)
local_study_with_constraint.create_binding_constraint(
name="test both",
properties=BindingConstraintProperties(
operator=BindingConstraintOperator.BOTH, time_step=BindingConstraintFrequency.HOURLY
),
)
expected_pre_created_ts_file = (
local_study_with_constraint.service.config.study_path
/ TimeSeriesFileType.BINDING_CONSTRAINT_LESS.value.format(constraint_id=test_constraint.id)
)

# When
with local_study_with_constraint._binding_constraints_service.time_series[
f"{test_constraint.id}_lt"
].local_file.file_path.open("r") as pre_created_file:
actual_time_series_pre_created = pd.read_csv(pre_created_file, header=None)
with local_study_with_constraint._binding_constraints_service.time_series[
"test greater_gt"
].local_file.file_path.open("r") as greater_file:
actual_time_series_greater = pd.read_csv(greater_file, header=None)
with local_study_with_constraint._binding_constraints_service.time_series[
"test equal_eq"
].local_file.file_path.open("r") as equal_file:
actual_time_series_equal = pd.read_csv(equal_file, header=None)
with local_study_with_constraint._binding_constraints_service.time_series[
"test both_gt"
].local_file.file_path.open("r") as both_greater_file:
actual_time_series_both_greater = pd.read_csv(both_greater_file, header=None)
with local_study_with_constraint._binding_constraints_service.time_series[
"test both_lt"
].local_file.file_path.open("r") as both_lesser_file:
actual_time_series_both_lesser = pd.read_csv(both_lesser_file, header=None)

# Then
# Verify that file names are created correctly
assert (
local_study_with_constraint._binding_constraints_service.time_series[
f"{test_constraint.id}_lt"
].local_file.file_path
== expected_pre_created_ts_file
)
# Verify that default file contents are the correct and expected
assert actual_time_series_pre_created.equals(expected_time_series_hourly)
assert actual_time_series_greater.equals(expected_time_series_daily_weekly)
assert actual_time_series_equal.equals(expected_time_series_daily_weekly)
assert actual_time_series_both_greater.equals(expected_time_series_hourly)
assert actual_time_series_both_lesser.equals(expected_time_series_hourly)

0 comments on commit 7d58483

Please sign in to comment.