Skip to content

Commit

Permalink
Add read load part w test.
Browse files Browse the repository at this point in the history
  • Loading branch information
killian-scalian committed Nov 28, 2024
1 parent 8140313 commit d0264e8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
1 change: 1 addition & 0 deletions scripts/license_checker_and_adder.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re

from pathlib import Path
from typing import List

Expand Down
3 changes: 3 additions & 0 deletions src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,6 @@ def create_hydro(
hydro = self._area_service.create_hydro(self.id, properties, matrices)
self._hydro = hydro
return hydro

def read_load(self) -> Load:
return self._area_service.read_load(self._id)
2 changes: 1 addition & 1 deletion src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def __init__(
def service(self) -> BaseStudyService:
return self._study_service

def read_areas(self) -> List[Area]:
def read_areas(self) -> list[Area]:
return self._area_service.read_areas()

def get_areas(self) -> MappingProxyType[str, Area]:
Expand Down
13 changes: 6 additions & 7 deletions src/antares/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ def create_thermal_cluster(
"""
pass

@abstractmethod
def read_areas(self) -> List[Area]:
"""
Returns: Returns a list of areas
"""
pass

@abstractmethod
def create_thermal_cluster_with_matrices(
self,
Expand Down Expand Up @@ -285,6 +278,12 @@ def get_load_matrix(self, area: Area) -> pd.DataFrame:
# Once it will, there will be no change to do in the code on our side.
pass

@abstractmethod
def read_areas(self) -> list[Area]:
"""
Returns: Returns a list of areas
"""
pass

class BaseLinkService(ABC):
@abstractmethod
Expand Down
33 changes: 33 additions & 0 deletions src/antares/service/local_services/area_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os

from configparser import ConfigParser
from pathlib import Path
from typing import Any, Dict, List, Optional

import pandas as pd
Expand Down Expand Up @@ -334,3 +335,35 @@ def read_areas(self) -> List[Area]:
)
)
return areas

def _read_timeseries(self, ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, group_id: Optional[str] = None,constraint_id: Optional[str] = None, time_series: Optional[pd.DataFrame] = None,) -> pd.DataFrame:
def _write_file(_file_path: Path, _time_series: pd.DataFrame) -> None:
_file_path.parent.mkdir(parents=True, exist_ok=True)
_time_series.to_csv(_file_path, sep="\t", header=False, index=False, encoding="utf-8")

file_path = study_path / (
ts_file_type.value
if not (area_id or constraint_id or group_id)
else ts_file_type.value.format(area_id=area_id, constraint_id=constraint_id, group_id=group_id)
)
if file_path.is_file() and time_series is not None:
raise ValueError(f"File {file_path} already exists and a time series was provided.")
elif file_path.is_file() and time_series is None:
if os.path.getsize(file_path) != 0:
_time_series = pd.read_csv(
file_path,
sep="\t",
header=None,
index_col=None,
encoding="utf-8",
)
else:
_time_series = pd.DataFrame()
else:
_time_series = time_series if time_series is not None else pd.DataFrame([])
_write_file(file_path, _time_series)
return _time_series

def read_load(self, id: str) -> Load:
timeseries = self._read_timeseries(TimeSeriesFileType.LOAD, self.config.study_path, area_id=id)
return Load(timeseries)
24 changes: 24 additions & 0 deletions tests/antares/services/local_services/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,27 @@ def test_read_areas_local(self, local_study_w_areas):
expected_areas = ["at", "it", "fr"]
for area in actual_areas:
assert area.id in expected_areas

class TestReadLoad:
def test_read_load_local(self, local_study_w_areas):
study_path = local_study_w_areas.service.config.study_path
local_study_object = read_study_local(study_path)
areas = local_study_object.read_areas()

def _write_file(_file_path, _time_series) -> None:
_file_path.parent.mkdir(parents=True, exist_ok=True)
_time_series.to_csv(file_path, sep="\t", header=False, index=False, encoding="utf-8")

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

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

load_object = area.read_load()
pd.testing.assert_frame_equal(load_object.time_series.astype(str), expected_conversion.astype(str), check_dtype=False)

0 comments on commit d0264e8

Please sign in to comment.