Skip to content

Commit

Permalink
Refactored StudySettings to follow the same pattern as others with a …
Browse files Browse the repository at this point in the history
…default and a local variant.
  • Loading branch information
Sigurd-Borge committed Oct 4, 2024
1 parent d09d1eb commit 76e0e8c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
37 changes: 26 additions & 11 deletions src/antares/model/settings/study_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@
#
# This file is part of the Antares project.

from typing import Dict, Optional
from typing import Dict

from pydantic import BaseModel

from antares.model.settings.adequacy_patch import AdequacyPatchProperties
from antares.model.settings.adequacy_patch import DefaultAdequacyPatchProperties
from antares.model.settings.advanced_parameters import AdvancedProperties
from antares.model.settings.general import GeneralProperties
from antares.model.settings.general import DefaultGeneralProperties
from antares.model.settings.optimization import OptimizationProperties
from antares.model.settings.thematic_trimming import ThematicTrimming
from antares.model.settings.time_series import TimeSeriesProperties
from antares.tools.all_optional_meta import all_optional_model


class PlaylistData(BaseModel):
status: bool
weight: float


class StudySettings(BaseModel):
general_properties: Optional[GeneralProperties] = None
thematic_trimming: Optional[ThematicTrimming] = None
time_series_properties: Optional[TimeSeriesProperties] = None
adequacy_patch_properties: Optional[AdequacyPatchProperties] = None
advanced_properties: Optional[AdvancedProperties] = None
optimization_properties: Optional[OptimizationProperties] = None
playlist: Optional[Dict[str, PlaylistData]] = None
class DefaultStudySettings(BaseModel):
general_properties: DefaultGeneralProperties = DefaultGeneralProperties()
thematic_trimming: ThematicTrimming = ThematicTrimming()
# These parameters are listed under the [variables selection] section in the .ini file.
# They are required if thematic-trimming is set to true.
# https://antares-simulator.readthedocs.io/en/latest/user-guide/solver/04-parameters/#variables-selection-parameters
time_series_properties: TimeSeriesProperties = TimeSeriesProperties()
# These parameters are listed under the [general] section in the .ini file.
# https://antares-simulator.readthedocs.io/en/latest/user-guide/ts-generator/04-parameters/
adequacy_patch_properties: DefaultAdequacyPatchProperties = DefaultAdequacyPatchProperties()
advanced_properties: AdvancedProperties = AdvancedProperties()
optimization_properties: OptimizationProperties = OptimizationProperties()
playlist: Dict[str, PlaylistData] = {}


@all_optional_model
class StudySettings(DefaultStudySettings):
pass


class StudySettingsLocal(DefaultStudySettings):
pass
20 changes: 7 additions & 13 deletions src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pathlib import Path
from types import MappingProxyType
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List, Optional, Union

import pandas as pd

Expand All @@ -27,8 +27,7 @@
from antares.model.area import Area, AreaProperties, AreaUi
from antares.model.binding_constraint import BindingConstraint, BindingConstraintProperties, ConstraintTerm
from antares.model.link import Link, LinkProperties, LinkUi
from antares.model.settings.general import GeneralPropertiesLocal
from antares.model.settings.study_settings import StudySettings
from antares.model.settings.study_settings import DefaultStudySettings, StudySettings, StudySettingsLocal
from antares.service.api_services.study_api import _returns_study_settings
from antares.service.base_services import BaseStudyService
from antares.service.service_factory import ServiceFactory
Expand Down Expand Up @@ -76,7 +75,7 @@ def create_study_api(


def create_study_local(
study_name: str, version: str, local_config: LocalConfiguration, settings: Optional[StudySettings] = None
study_name: str, version: str, local_config: LocalConfiguration, settings: StudySettingsLocal = StudySettingsLocal()
) -> "Study":
"""
Create a directory structure for the study with empty files.
Expand Down Expand Up @@ -120,12 +119,7 @@ def create_study_local(
with open(desktop_ini_path, "w") as desktop_ini_file:
desktop_ini_file.write(desktop_ini_content)

settings = settings if settings is not None else StudySettings()
local_settings = StudySettings(
general_properties=GeneralPropertiesLocal.model_validate(
settings.model_dump(exclude_none=True)
).yield_properties()
)
local_settings = StudySettingsLocal.model_validate(settings)

# Create various .ini files for the study
correlation_inis_to_create = [
Expand Down Expand Up @@ -156,7 +150,7 @@ def __init__(
name: str,
version: str,
service_factory: ServiceFactory,
settings: Optional[StudySettings] = None,
settings: Union[StudySettings, StudySettingsLocal, None] = None,
# ini_files: Optional[dict[str, IniFile]] = None,
**kwargs: Any,
):
Expand All @@ -166,7 +160,7 @@ def __init__(
self._area_service = service_factory.create_area_service()
self._link_service = service_factory.create_link_service()
self._binding_constraints_service = service_factory.create_binding_constraints_service()
self._settings = settings or StudySettings()
self._settings = DefaultStudySettings.model_validate(settings if settings is not None else StudySettings())
self._areas: Dict[str, Area] = dict()
self._links: Dict[str, Link] = dict()
for argument in kwargs:
Expand All @@ -183,7 +177,7 @@ def get_areas(self) -> MappingProxyType[str, Area]:
def get_links(self) -> MappingProxyType[str, Link]:
return MappingProxyType(self._links)

def get_settings(self) -> StudySettings:
def get_settings(self) -> DefaultStudySettings:
return self._settings

def get_binding_constraints(self) -> MappingProxyType[str, BindingConstraint]:
Expand Down

0 comments on commit 76e0e8c

Please sign in to comment.