Skip to content

Commit

Permalink
refactor(study): remove ini_file from study
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBelthle committed Dec 2, 2024
1 parent b7cf90d commit 8605e33
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,4 @@ def read_thermal_clusters(
def read_hydro(
self,
) -> Hydro:
return self._area_service.read_hydro(self.id)
return self._area_service.read_hydro(self.id)
24 changes: 7 additions & 17 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, Union
from typing import Dict, List, Optional, Union

import pandas as pd

Expand Down Expand Up @@ -130,15 +130,14 @@ def create_study_local(
local_settings_file.write_ini_file()

# Create various .ini files for the study
ini_files = _create_correlation_ini_files(local_settings, study_directory)
_create_correlation_ini_files(local_settings, study_directory)

logging.info(f"Study successfully created: {study_name}")
return Study(
name=study_name,
version=version,
service_factory=ServiceFactory(config=local_config, study_name=study_name),
settings=local_settings,
ini_files=ini_files,
)


Expand Down Expand Up @@ -179,8 +178,6 @@ def __init__(
version: str,
service_factory: ServiceFactory,
settings: Union[StudySettings, StudySettingsLocal, None] = None,
# ini_files: Optional[dict[str, IniFile]] = None,
**kwargs: Any,
):
self.name = name
self.version = version
Expand All @@ -191,9 +188,6 @@ def __init__(
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:
if argument == "ini_files":
self._ini_files: dict[str, IniFile] = kwargs[argument] or dict()

@property
def service(self) -> BaseStudyService:
Expand Down Expand Up @@ -298,7 +292,7 @@ def _create_directory_structure(study_path: Path) -> None:
(study_path / subdirectory).mkdir(parents=True, exist_ok=True)


def _create_correlation_ini_files(local_settings: StudySettingsLocal, study_directory: Path) -> dict[str, IniFile]:
def _create_correlation_ini_files(local_settings: StudySettingsLocal, study_directory: Path) -> None:
fields_to_check = ["hydro", "load", "solar", "wind"]
correlation_inis_to_create = [
(
Expand All @@ -308,17 +302,13 @@ def _create_correlation_ini_files(local_settings: StudySettingsLocal, study_dire
)
for field in fields_to_check
]
ini_files = {
correlation: IniFile(

for correlation, file_type, field in correlation_inis_to_create:
ini_file = IniFile(
study_directory,
file_type,
ini_contents=correlation_defaults(
season_correlation=getattr(local_settings.time_series_parameters, field).season_correlation,
),
)
for (correlation, file_type, field) in correlation_inis_to_create
}

for ini_file in ini_files.keys():
ini_files[ini_file].write_ini_file()
return ini_files
ini_file.write_ini_file()
1 change: 1 addition & 0 deletions src/antares/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def read_areas(self) -> list[Area]:
"""
pass


class BaseLinkService(ABC):
@abstractmethod
def create_link(
Expand Down
15 changes: 10 additions & 5 deletions src/antares/service/local_services/area_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,14 @@ def delete_renewable_clusters(self, area: Area, renewable_clusters: List[Renewab

def delete_st_storages(self, area: Area, storages: List[STStorage]) -> None:
raise NotImplementedError

def _read_timeseries(self, ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, constraint_id: Optional[str] = None) -> pd.DataFrame:

def _read_timeseries(
self,
ts_file_type: TimeSeriesFileType,
study_path: Path,
area_id: Optional[str] = None,
constraint_id: Optional[str] = None,
) -> pd.DataFrame:
file_path = study_path / (
ts_file_type.value
if not (area_id or constraint_id)
Expand All @@ -330,10 +336,10 @@ def _read_timeseries(self, ts_file_type: TimeSeriesFileType, study_path: Path, a
_time_series = pd.DataFrame()

return _time_series

def get_load_matrix(self, area: Area) -> pd.DataFrame:
return self._read_timeseries(TimeSeriesFileType.LOAD, self.config.study_path, area_id=area.id)

def get_solar_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError

Expand Down Expand Up @@ -362,4 +368,3 @@ def read_areas(self) -> List[Area]:
)
)
return areas

14 changes: 8 additions & 6 deletions tests/antares/services/local_services/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,7 @@ def test_k_and_translation_txt_is_empty_by_default(self, local_study, fr_load):
actual_file_path = study_path.joinpath(Path("input") / "load" / "prepro" / "fr" / f"{file}.txt")
assert actual_file_path.read_text() == ""


class TestReadArea:
def test_read_areas_local(self, local_study_w_areas):
study_path = local_study_w_areas.service.config.study_path
Expand All @@ -1128,12 +1129,13 @@ def _write_file(_file_path, _time_series) -> None:

for area in areas:
expected_time_serie = pd.DataFrame(
[
[-9999999980506447872, 0, 9999999980506447872],
[0, area.id, 0],
]
, dtype="object")

[
[-9999999980506447872, 0, 9999999980506447872],
[0, area.id, 0],
],
dtype="object",
)

file_path = study_path / "input" / "load" / "series" / f"load_{area.id}.txt"
_write_file(file_path, expected_time_serie)

Expand Down
139 changes: 9 additions & 130 deletions tests/antares/services/local_services/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,71 +168,7 @@ def test_verify_study_already_exists_error(self, tmp_path):
with pytest.raises(FileExistsError, match=f"Study {study_name} already exists"):
create_study_local(study_name, version, str(tmp_path.absolute()))

def test_solar_correlation_ini_exists(self, local_study_with_hydro):
# Given
expected_ini_path = local_study_with_hydro.service.config.study_path / "input/solar/prepro/correlation.ini"

# Then
assert expected_ini_path.exists()
assert expected_ini_path.is_file()
assert local_study_with_hydro._ini_files["solar_correlation"].ini_path == expected_ini_path

def test_solar_correlation_ini_has_default_values(self, local_study_with_hydro):
# Given
expected_ini_content = """[general]
mode = annual
[annual]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
"""
expected_ini = ConfigParser()
actual_ini = local_study_with_hydro._ini_files["solar_correlation"]

# When
expected_ini.read_string(expected_ini_content)
with actual_ini.ini_path.open("r") as ini_file:
actual_ini_content = ini_file.read()

# Then
assert actual_ini_content == expected_ini_content
assert actual_ini.parsed_ini.sections() == expected_ini.sections()
assert actual_ini.parsed_ini == expected_ini

def test_wind_correlation_ini_exists(self, local_study_with_hydro):
# Given
expected_ini_path = local_study_with_hydro.service.config.study_path / "input/wind/prepro/correlation.ini"

# Then
assert expected_ini_path.exists()
assert expected_ini_path.is_file()
assert local_study_with_hydro._ini_files["wind_correlation"].ini_path == expected_ini_path

def test_wind_correlation_ini_has_default_values(self, local_study_with_hydro):
# Given
def test_all_correlation_ini_files_exists(self, local_study):
expected_ini_content = """[general]
mode = annual
Expand Down Expand Up @@ -263,72 +199,15 @@ def test_wind_correlation_ini_has_default_values(self, local_study_with_hydro):
[11]
"""
expected_ini = ConfigParser()
actual_ini = local_study_with_hydro._ini_files["wind_correlation"]

# When
expected_ini.read_string(expected_ini_content)
with actual_ini.ini_path.open("r") as ini_file:
actual_ini_content = ini_file.read()

# Then
assert actual_ini_content == expected_ini_content
assert actual_ini.parsed_ini.sections() == expected_ini.sections()
assert actual_ini.parsed_ini == expected_ini

def test_load_correlation_ini_exists(self, local_study_with_hydro):
# Given
expected_ini_path = local_study_with_hydro.service.config.study_path / "input/load/prepro/correlation.ini"

# Then
assert expected_ini_path.exists()
assert expected_ini_path.is_file()
assert local_study_with_hydro._ini_files["load_correlation"].ini_path == expected_ini_path

def test_load_correlation_ini_has_default_values(self, local_study_with_hydro):
# Given
expected_ini_content = """[general]
mode = annual
[annual]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
"""
expected_ini = ConfigParser()
actual_ini = local_study_with_hydro._ini_files["load_correlation"]

# When
expected_ini.read_string(expected_ini_content)
with actual_ini.ini_path.open("r") as ini_file:
actual_ini_content = ini_file.read()
local_config = t.cast(LocalConfiguration, local_study.service.config)
study_path = local_config.study_path
for folder in ["hydro", "load", "solar", "wind"]:
ini_path = study_path / "input" / folder / "prepro" / "correlation.ini"
assert ini_path.exists()
assert ini_path.is_file()

# Then
assert actual_ini_content == expected_ini_content
assert actual_ini.parsed_ini.sections() == expected_ini.sections()
assert actual_ini.parsed_ini == expected_ini
ini_content = ini_path.read_text(encoding="utf-8")
assert ini_content == expected_ini_content


class TestStudyProperties:
Expand Down

0 comments on commit 8605e33

Please sign in to comment.