Skip to content

Commit

Permalink
better handling of filtering values (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle authored Mar 6, 2025
1 parent bf57e48 commit 6967ebb
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 41 deletions.
11 changes: 6 additions & 5 deletions src/antares/craft/model/binding_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, Union

import pandas as pd

from antares.craft.model.commons import FILTER_VALUES, filtering_option
from antares.craft.service.base_services import BaseBindingConstraintService
from antares.craft.tools.contents_tool import EnumIgnoreCase, transform_name_to_id

Expand Down Expand Up @@ -108,8 +109,8 @@ class BindingConstraintPropertiesUpdate:
time_step: Optional[BindingConstraintFrequency] = None
operator: Optional[BindingConstraintOperator] = None
comments: Optional[str] = None
filter_year_by_year: Optional[str] = None
filter_synthesis: Optional[str] = None
filter_year_by_year: Optional[filtering_option] = None
filter_synthesis: Optional[filtering_option] = None
group: Optional[str] = None


Expand All @@ -119,8 +120,8 @@ class BindingConstraintProperties:
time_step: BindingConstraintFrequency = BindingConstraintFrequency.HOURLY
operator: BindingConstraintOperator = BindingConstraintOperator.LESS
comments: str = ""
filter_year_by_year: str = "hourly"
filter_synthesis: str = "hourly"
filter_year_by_year: filtering_option = field(default_factory=lambda: FILTER_VALUES)
filter_synthesis: filtering_option = field(default_factory=lambda: FILTER_VALUES)
group: str = "default"


Expand Down
6 changes: 4 additions & 2 deletions src/antares/craft/model/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class FilterOption(Enum):
ANNUAL = "annual"


def validate_filters(filter_value: Union[list[FilterOption], str]) -> list[FilterOption]:
def validate_filters(filter_value: Union[list[FilterOption], str, None]) -> list[FilterOption]:
if not filter_value:
return []
if isinstance(filter_value, str):
filter_value = filter_value.strip()
if not filter_value:
Expand All @@ -50,7 +52,7 @@ def join_with_comma(values: Optional[set[FilterOption]] = None) -> str:
return ""


comma_separated_enum_set = Annotated[
filtering_option = Annotated[
set[FilterOption],
BeforeValidator(lambda x: validate_filters(x)),
PlainSerializer(lambda x: join_with_comma(x)),
Expand Down
12 changes: 6 additions & 6 deletions src/antares/craft/model/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# This file is part of the Antares project.
from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, Set
from typing import Optional

import pandas as pd

from antares.craft.model.commons import FILTER_VALUES, FilterOption, comma_separated_enum_set
from antares.craft.model.commons import FILTER_VALUES, filtering_option
from antares.craft.service.base_services import BaseLinkService
from antares.craft.tools.contents_tool import transform_name_to_id

Expand Down Expand Up @@ -50,8 +50,8 @@ class LinkPropertiesUpdate:
asset_type: Optional[AssetType] = None
display_comments: Optional[bool] = None
comments: Optional[str] = None
filter_synthesis: Optional[Set[FilterOption]] = None
filter_year_by_year: Optional[Set[FilterOption]] = None
filter_synthesis: Optional[filtering_option] = None
filter_year_by_year: Optional[filtering_option] = None


@dataclass(frozen=True)
Expand All @@ -63,8 +63,8 @@ class LinkProperties:
asset_type: AssetType = AssetType.AC
display_comments: bool = True
comments: str = ""
filter_synthesis: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_synthesis: filtering_option = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: filtering_option = field(default_factory=lambda: FILTER_VALUES)


@dataclass(frozen=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BindingConstraintProperties,
BindingConstraintPropertiesUpdate,
)
from antares.craft.model.commons import filtering_option
from antares.craft.service.api_services.models.base_model import APIBaseModel
from antares.craft.tools.all_optional_meta import all_optional_model

Expand All @@ -31,8 +32,8 @@ class BindingConstraintPropertiesAPI(APIBaseModel):
time_step: BindingConstraintFrequency
operator: BindingConstraintOperator
comments: str
filter_year_by_year: str
filter_synthesis: str
filter_year_by_year: filtering_option
filter_synthesis: filtering_option
group: str

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions src/antares/craft/service/api_services/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from dataclasses import asdict
from typing import Optional, Union

from antares.craft.model.commons import comma_separated_enum_set
from antares.craft.model.commons import filtering_option
from antares.craft.model.link import (
AssetType,
LinkProperties,
Expand All @@ -39,8 +39,8 @@ class LinkPropertiesAndUiAPI(APIBaseModel):
asset_type: AssetType
display_comments: bool
comments: str
filter_synthesis: comma_separated_enum_set
filter_year_by_year: comma_separated_enum_set
filter_synthesis: filtering_option
filter_year_by_year: filtering_option
link_style: LinkStyle
link_width: float
colorr: int
Expand Down
6 changes: 3 additions & 3 deletions src/antares/craft/service/local_services/models/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Any, Union

from antares.craft.model.area import AdequacyPatchMode, AreaProperties, AreaPropertiesUpdate, AreaUi, AreaUiUpdate
from antares.craft.model.commons import FILTER_VALUES, comma_separated_enum_set
from antares.craft.model.commons import FILTER_VALUES, filtering_option
from antares.craft.service.local_services.models.base_model import LocalBaseModel
from antares.craft.tools.alias_generators import to_kebab
from pydantic import Field
Expand All @@ -31,8 +31,8 @@ class OptimizationPropertiesLocal(LocalBaseModel, alias_generator=to_kebab):


class FilteringPropertiesLocal(LocalBaseModel, alias_generator=to_kebab):
filter_synthesis: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_synthesis: filtering_option = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: filtering_option = field(default_factory=lambda: FILTER_VALUES)


class AdequacyPatchPropertiesLocal(LocalBaseModel, alias_generator=to_kebab):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BindingConstraintProperties,
BindingConstraintPropertiesUpdate,
)
from antares.craft.model.commons import FilterOption, filtering_option
from antares.craft.service.local_services.models.base_model import LocalBaseModel
from pydantic import Field

Expand All @@ -30,8 +31,8 @@ class BindingConstraintPropertiesLocal(LocalBaseModel):
time_step: BindingConstraintFrequency = Field(BindingConstraintFrequency.HOURLY, alias="type")
operator: BindingConstraintOperator = BindingConstraintOperator.LESS
comments: str = ""
filter_year_by_year: str = Field("hourly", alias="filter-year-by-year")
filter_synthesis: str = Field("hourly", alias="filter-synthesis")
filter_year_by_year: filtering_option = Field({FilterOption.HOURLY}, alias="filter-year-by-year")
filter_synthesis: filtering_option = Field({FilterOption.HOURLY}, alias="filter-synthesis")
group: str = "default"

@staticmethod
Expand Down
6 changes: 3 additions & 3 deletions src/antares/craft/service/local_services/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from dataclasses import asdict, field
from typing import Optional, Union

from antares.craft.model.commons import FILTER_VALUES, comma_separated_enum_set
from antares.craft.model.commons import FILTER_VALUES, filtering_option
from antares.craft.model.link import (
AssetType,
LinkProperties,
Expand All @@ -38,8 +38,8 @@ class LinkPropertiesAndUiLocal(LocalBaseModel, alias_generator=to_kebab):
asset_type: AssetType = AssetType.AC
display_comments: bool = True
comments: str = ""
filter_synthesis: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: comma_separated_enum_set = field(default_factory=lambda: FILTER_VALUES)
filter_synthesis: filtering_option = field(default_factory=lambda: FILTER_VALUES)
filter_year_by_year: filtering_option = field(default_factory=lambda: FILTER_VALUES)
link_style: LinkStyle = LinkStyle.PLAIN
link_width: float = 1
colorr: int = 112
Expand Down
3 changes: 2 additions & 1 deletion tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
BindingConstraintProperties,
BindingConstraintPropertiesUpdate,
)
from antares.craft.model.commons import FilterOption
from antares.craft.model.link import Link, LinkProperties, LinkPropertiesUpdate
from antares.craft.model.output import (
Output,
Expand Down Expand Up @@ -224,7 +225,7 @@ def test_create_link_success(self):
def test_create_binding_constraint_success(self):
with requests_mock.Mocker() as mocker:
url = f"https://antares.com/api/v1/studies/{self.study_id}/bindingconstraints"
properties = BindingConstraintProperties(enabled=False, filter_synthesis="annual")
properties = BindingConstraintProperties(enabled=False, filter_synthesis={FilterOption.ANNUAL})
json_response = BindingConstraintPropertiesAPI.from_user_model(properties).model_dump(
mode="json", by_alias=True
)
Expand Down
5 changes: 3 additions & 2 deletions tests/antares/services/local_services/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
ConstraintTerm,
LinkData,
)
from antares.craft.model.commons import FILTER_VALUES
from antares.craft.model.hydro import HydroProperties, HydroPropertiesUpdate
from antares.craft.model.renewable import RenewableClusterGroup, RenewableClusterProperties, TimeSeriesInterpretation
from antares.craft.model.st_storage import STStorageGroup, STStorageProperties
Expand Down Expand Up @@ -281,7 +282,7 @@ def default_constraint_properties() -> BindingConstraintProperties:
time_step=BindingConstraintFrequency.HOURLY,
operator=BindingConstraintOperator.LESS,
comments="",
filter_year_by_year="hourly",
filter_synthesis="hourly",
filter_year_by_year=FILTER_VALUES,
filter_synthesis=FILTER_VALUES,
group="default",
)
20 changes: 10 additions & 10 deletions tests/antares/services/local_services/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ def test_constraints_ini_have_correct_default_content(
type = hourly
operator = less
comments =
filter-year-by-year = hourly
filter-synthesis = hourly
filter-year-by-year = annual, daily, hourly, monthly, weekly
filter-synthesis = annual, daily, hourly, monthly, weekly
group = default
"""
Expand All @@ -1224,7 +1224,7 @@ def test_constraints_and_ini_have_custom_properties(self, local_study_with_const
time_step=BindingConstraintFrequency.WEEKLY,
operator=BindingConstraintOperator.BOTH,
comments="test comment",
filter_year_by_year="yearly",
filter_year_by_year="annual",
filter_synthesis="monthly",
group="test group",
)
Expand All @@ -1235,8 +1235,8 @@ def test_constraints_and_ini_have_custom_properties(self, local_study_with_const
type = hourly
operator = less
comments =
filter-year-by-year = hourly
filter-synthesis = hourly
filter-year-by-year = annual, daily, hourly, monthly, weekly
filter-synthesis = annual, daily, hourly, monthly, weekly
group = default
[1]
Expand All @@ -1246,7 +1246,7 @@ def test_constraints_and_ini_have_custom_properties(self, local_study_with_const
type = weekly
operator = both
comments = test comment
filter-year-by-year = yearly
filter-year-by-year = annual
filter-synthesis = monthly
group = test group
Expand Down Expand Up @@ -1280,8 +1280,8 @@ def test_constraint_term_and_ini_have_correct_defaults(self, local_study_with_co
type = hourly
operator = less
comments =
filter-year-by-year = hourly
filter-synthesis = hourly
filter-year-by-year = annual, daily, hourly, monthly, weekly
filter-synthesis = annual, daily, hourly, monthly, weekly
group = default
at%fr = 1
Expand All @@ -1305,8 +1305,8 @@ def test_constraint_term_with_offset_and_ini_have_correct_values(
type = hourly
operator = less
comments =
filter-year-by-year = hourly
filter-synthesis = hourly
filter-year-by-year = annual, daily, hourly, monthly, weekly
filter-synthesis = annual, daily, hourly, monthly, weekly
group = default
at%fr = 1%1
Expand Down
12 changes: 10 additions & 2 deletions tests/integration/test_web_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop, tmp_path):
assert constraint_2.get_terms() == {link_term_2.id: link_term_2, cluster_term.id: cluster_term}

# test updating constraints
update_bc_props_1 = BindingConstraintPropertiesUpdate(time_step=BindingConstraintFrequency.DAILY, enabled=False)
update_bc_props_1 = BindingConstraintPropertiesUpdate(
time_step=BindingConstraintFrequency.DAILY,
enabled=False,
filter_synthesis={FilterOption.WEEKLY, FilterOption.ANNUAL},
)
update_bc_props_2 = BindingConstraintPropertiesUpdate(
enabled=True, time_step=BindingConstraintFrequency.HOURLY, comments="Bonjour"
)
Expand All @@ -383,6 +387,7 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop, tmp_path):
study.update_multiple_binding_constraints(dict_binding_constraints_update)

assert constraint_1.properties.time_step == BindingConstraintFrequency.DAILY
assert constraint_1.properties.filter_synthesis == {FilterOption.WEEKLY, FilterOption.ANNUAL}
assert not constraint_1.properties.enabled
assert constraint_2.properties.time_step == BindingConstraintFrequency.HOURLY
assert constraint_2.properties.enabled
Expand Down Expand Up @@ -505,7 +510,9 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop, tmp_path):
assert constraint_1.properties.enabled is False # Checks old value wasn't modified

# creating link properties update to modify all the actual links
link_properties_update_1 = LinkPropertiesUpdate(hurdles_cost=True, use_phase_shifter=True)
link_properties_update_1 = LinkPropertiesUpdate(
hurdles_cost=True, use_phase_shifter=True, filter_synthesis={FilterOption.DAILY}
)
link_properties_update_2 = LinkPropertiesUpdate(
transmission_capacities=TransmissionCapacities.ENABLED, asset_type=AssetType.GAZ
)
Expand All @@ -517,6 +524,7 @@ def test_creation_lifecycle(self, antares_web: AntaresWebDesktop, tmp_path):
# Checking given values are well modified
assert link_be_fr.properties.hurdles_cost
assert link_be_fr.properties.use_phase_shifter
assert link_be_fr.properties.filter_synthesis == {FilterOption.DAILY}
assert link_be_fr.properties.display_comments
assert not link_be_fr.properties.loop_flow

Expand Down

0 comments on commit 6967ebb

Please sign in to comment.