Skip to content

Commit

Permalink
serialized_terms_data method moved to ConstraintTerm as class method
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat authored and MartinBelthle committed Dec 5, 2024
1 parent e1f4fbd commit 97188b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 39 deletions.
13 changes: 13 additions & 0 deletions src/antares/model/binding_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def generate_id(cls, data: Union[Dict[str, str], LinkData, ClusterData]) -> str:
return "%".join(sorted((data.area1.lower(), data.area2.lower())))
return ".".join((data.area.lower(), data.cluster.lower()))

def serialize_term_data(self) -> Optional[str]:
"""
Serializes the term data to be correctly written in INI.
"""
if isinstance(self.data, LinkData):
if self.offset is not None:
return f"0.000000%{self.offset}"
if self.weight is not None:
return f"{self.weight}"
return "0"

return None


class DefaultBindingConstraintProperties(BaseModel, extra="forbid", populate_by_name=True, alias_generator=to_camel):
"""Default properties for binding constraints
Expand Down
39 changes: 4 additions & 35 deletions src/antares/service/local_services/binding_constraint_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# SPDX-License-Identifier: MPL-2.0
#
# This file is part of the Antares project.
from typing import Any, Optional, Union
from typing import Any, Optional

import numpy as np
import pandas as pd
Expand All @@ -22,34 +22,15 @@
BindingConstraintOperator,
BindingConstraintProperties,
BindingConstraintPropertiesLocal,
ClusterData,
ConstraintMatrixName,
ConstraintTerm,
LinkData,
)
from antares.service.base_services import BaseBindingConstraintService
from antares.tools.ini_tool import IniFile, IniFileTypes
from antares.tools.matrix_tool import df_save
from antares.tools.time_series_tool import TimeSeriesFileType


def serialize_term_data(
data: Union[LinkData, ClusterData], offset: Optional[int], weight: Optional[float]
) -> Union[str, None]:
"""
Serializes the term data to be correctly written in INI.
"""
if isinstance(data, LinkData):
if offset is not None:
return f"0.000000%{offset}"
if weight is not None:
return f"{weight}"
if weight is None:
return "0"
else:
return None


class BindingConstraintLocalService(BaseBindingConstraintService):
def __init__(self, config: LocalConfiguration, study_name: str, **kwargs: Any) -> None:
super().__init__(**kwargs)
Expand Down Expand Up @@ -142,27 +123,15 @@ def _write_binding_constraint_ini(
if existing_section:
existing_terms = current_ini_content[existing_section]

serialized_terms = (
{term.id: serialize_term_data(term.data, term.offset, term.weight) for term in terms} if terms else {}
)
serialized_terms = {term.id: term.serialize_term_data() for term in terms} if terms else {}

existing_terms.update(serialized_terms) # type: ignore
current_ini_content[existing_section] = existing_terms

# Persist the updated INI content
self.ini_file.ini_dict_binding_constraints = current_ini_content
self.ini_file.write_ini_file()
else:
terms_dict = (
{
term.id: ConstraintTerm(data=term.data, offset=term.offset, weight=term.weight)
if isinstance(term.data, (LinkData, ClusterData))
else term
for term in terms
}
if terms
else {}
)
terms_dict = {term.id: term for term in terms} if terms else {}

full_properties = BindingConstraintPropertiesLocal(
constraint_name=constraint_name,
Expand Down Expand Up @@ -205,7 +174,7 @@ def add_constraint_terms(self, constraint: BindingConstraint, terms: list[Constr
terms=terms_values,
)

return list(new_terms.values())
return terms_values

def delete_binding_constraint_term(self, constraint_id: str, term_id: str) -> None:
raise NotImplementedError
Expand Down
5 changes: 1 addition & 4 deletions src/antares/tools/ini_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def ini_dict_binding_constraints(self) -> dict[str, dict[str, str]]:
def ini_dict_binding_constraints(self, new_ini_dict: dict[str, dict[str, str]]) -> None:
"""Set INI file contents for binding constraints."""
self._ini_contents = CustomRawConfigParser()
for index, (section, values) in enumerate(new_ini_dict.items()):
for index, values in enumerate(new_ini_dict.values()):
self._ini_contents.add_section(str(index))
for key, value in values.items():
self._ini_contents.set(str(index), key, value)
Expand Down Expand Up @@ -145,9 +145,6 @@ def update_from_ini_file(self) -> None:

self._ini_contents = parsed_ini

def update_binding_constraints_from_ini_file(self) -> None:
self.update_from_ini_file()

def write_ini_file(
self,
sort_sections: bool = False,
Expand Down

0 comments on commit 97188b5

Please sign in to comment.