Skip to content

Commit

Permalink
feat(api): adding read_study method, unit testing and integration tes…
Browse files Browse the repository at this point in the history
…ting of the method
  • Loading branch information
mehdiwahada committed Dec 3, 2024
1 parent c4767cf commit 7d35ba7
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def read_areas(self) -> list[Area]:
Returns:
"""
area_list = self._area_service.read_areas()
self._areas = {area.id:area for area in area_list}
self._areas = {area.id: area for area in area_list}
return area_list

def get_areas(self) -> MappingProxyType[str, Area]:
Expand Down
9 changes: 1 addition & 8 deletions src/antares/service/api_services/renewable_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,7 @@ def update_renewable_properties(

def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame:
try:
path = (
PurePosixPath("input")
/ "renewables"
/ "series"
/ f"{area_id}"
/ f"{cluster_id}"
/ "series"
)
path = PurePosixPath("input") / "renewables" / "series" / f"{area_id}" / f"{cluster_id}" / "series"
return get_matrix(f"{self._base_url}/studies/{self.study_id}/raw?path={path}", self._wrapper)
except APIError as e:
raise RenewableMatrixDownloadError(area_id, cluster_id, e.message) from e
Expand Down
4 changes: 1 addition & 3 deletions src/antares/service/base_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,7 @@ def update_renewable_properties(
pass

@abstractmethod
def get_renewable_matrix(
self, cluster_id: str, area_id: str
) -> pd.DataFrame:
def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame:
"""
Args:
cluster_id: renewable cluster id to retrieve matrix
Expand Down
4 changes: 2 additions & 2 deletions src/antares/service/local_services/area_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def get_load_matrix(self, area: Area) -> pd.DataFrame:
return read_timeseries(TimeSeriesFileType.LOAD, self.config.study_path, area_id=area.id)

def get_solar_matrix(self, area: Area) -> pd.DataFrame:
return read_timeseries(TimeSeriesFileType.SOLAR, self.config.study_path, area_id=area.id)
return read_timeseries(TimeSeriesFileType.SOLAR, self.config.study_path, area_id=area.id)

def get_wind_matrix(self, area: Area) -> pd.DataFrame:
return read_timeseries(TimeSeriesFileType.WIND, self.config.study_path, area_id=area.id)
Expand All @@ -330,7 +330,7 @@ def get_reserves_matrix(self, area: Area) -> pd.DataFrame:

def get_misc_gen_matrix(self, area: Area) -> pd.DataFrame:
return read_timeseries(TimeSeriesFileType.MISC_GEN, self.config.study_path, area_id=area.id)

def read_areas(self) -> List[Area]:
local_path = self.config.local_path
areas_path = local_path / self.study_name / "input" / "areas"
Expand Down
22 changes: 12 additions & 10 deletions src/antares/service/local_services/renewable_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ def update_renewable_properties(
self, renewable_cluster: RenewableCluster, properties: RenewableClusterProperties
) -> RenewableClusterProperties:
raise NotImplementedError


def get_renewable_matrix(
self,
cluster_id: str,
area_id: str
) -> pd.DataFrame:
return read_timeseries(TimeSeriesFileType.RENEWABLE_DATA_SERIES, self.config.study_path, area_id=area_id, cluster_id=cluster_id)

def get_renewable_matrix(self, cluster_id: str, area_id: str) -> pd.DataFrame:
return read_timeseries(
TimeSeriesFileType.RENEWABLE_DATA_SERIES, self.config.study_path, area_id=area_id, cluster_id=cluster_id
)

def read_renewables(self, area_id: str) -> List[RenewableCluster]:
renewable_dict = IniFile(self.config.study_path, IniFileTypes.RENEWABLES_LIST_INI, area_name=area_id).ini_dict
Expand All @@ -56,6 +52,12 @@ def read_renewables(self, area_id: str) -> List[RenewableCluster]:
nominal_capacity=renewable_dict[renewable_cluster]["nominalcapacity"],
ts_interpretation=renewable_dict[renewable_cluster]["ts-interpretation"],
)
renewables_clusters.append(RenewableCluster(renewable_service=self, area_id=area_id, name=renewable_dict[renewable_cluster]["name"], properties=renewable_properties.yield_renewable_cluster_properties()))
renewables_clusters.append(
RenewableCluster(
renewable_service=self,
area_id=area_id,
name=renewable_dict[renewable_cluster]["name"],
properties=renewable_properties.yield_renewable_cluster_properties(),
)
)
return renewables_clusters

10 changes: 8 additions & 2 deletions src/antares/tools/matrix_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ def df_read(path: Path) -> pd.DataFrame:
return pd.read_csv(path, sep="\t", header=None)


def read_timeseries(ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, constraint_id: Optional[str] = None, cluster_id: Optional[str] = None,) -> pd.DataFrame:
def read_timeseries(
ts_file_type: TimeSeriesFileType,
study_path: Path,
area_id: Optional[str] = None,
constraint_id: Optional[str] = None,
cluster_id: Optional[str] = None,
) -> pd.DataFrame:
file_path = study_path / (
ts_file_type.value
if not (area_id or constraint_id or cluster_id)
Expand All @@ -53,4 +59,4 @@ def read_timeseries(ts_file_type: TimeSeriesFileType, study_path: Path, area_id:
else:
_time_series = pd.DataFrame()

return _time_series
return _time_series
49 changes: 18 additions & 31 deletions tests/antares/services/api_services/test_study_api.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.


import re

import pytest
import requests_mock

import re

from antares.api_conf.api_conf import APIconf
from antares.exceptions.exceptions import (
AreaCreationError,
Expand Down Expand Up @@ -201,20 +201,19 @@ def test_create_binding_constraint_fails(self):
self.study.create_binding_constraint(name=constraint_name)

def test_read_study_api(self):

json_study = {
"id": "22c52f44-4c2a-407b-862b-490887f93dd8",
"name": "test_read_areas",
"version": "880",
}

json_area = {
"zone": {
"ui": {"x": 0,"y": 0,"color_r": 230,"color_g": 108, "color_b": 44,"layers": "0"},
"layerX": {"0": 0},
"layerY": { "0": 0},
"layerColor": {"0": "230, 108, 44"}
}
"zone": {
"ui": {"x": 0, "y": 0, "color_r": 230, "color_g": 108, "color_b": 44, "layers": "0"},
"layerX": {"0": 0},
"layerY": {"0": 0},
"layerColor": {"0": "230, 108, 44"},
}
}

json_thermal = [
Expand Down Expand Up @@ -256,26 +255,14 @@ def test_read_study_api(self):
]

json_properties = {
"energyCostUnsupplied": 0,
"energyCostSpilled": 0,
"nonDispatchPower": "true",
"dispatchHydroPower": "true",
"otherDispatchPower": "true",
"filterSynthesis": [
"weekly",
"daily",
"hourly",
"monthly",
"annual"
],
"filterByYear": [
"weekly",
"daily",
"hourly",
"monthly",
"annual"
],
"adequacyPatchMode": "outside"
"energyCostUnsupplied": 0,
"energyCostSpilled": 0,
"nonDispatchPower": "true",
"dispatchHydroPower": "true",
"otherDispatchPower": "true",
"filterSynthesis": ["weekly", "daily", "hourly", "monthly", "annual"],
"filterByYear": ["weekly", "daily", "hourly", "monthly", "annual"],
"adequacyPatchMode": "outside",
}

config_urls = re.compile(f"https://antares.com/api/v1/studies/{self.study_id}/config/.*")
Expand Down Expand Up @@ -306,9 +293,9 @@ def test_read_study_api(self):
expected_study_name,
expected_study_version,
ServiceFactory(self.api, expected_study_id, expected_study_name),
None
None,
)

assert actual_study.name == expected_study.name
assert actual_study.version == expected_study.version
assert actual_study.service.study_id == expected_study.service.study_id
assert actual_study.service.study_id == expected_study.service.study_id
80 changes: 41 additions & 39 deletions tests/antares/services/local_services/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,13 +1122,13 @@ 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")


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()


for area in areas:
expected_time_serie = pd.DataFrame(
[
Expand Down Expand Up @@ -1158,15 +1158,14 @@ def test_read_renewable_local(self, local_study_with_renewable):
local_study_object = read_study_local(study_path)
areas = local_study_object.read_areas()



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",
)
renewable_list = area.read_renewables(area.id)

if renewable_list:
Expand All @@ -1183,9 +1182,9 @@ def test_read_renewable_local(self, local_study_with_renewable):
assert renewable.properties.group.value == "Other RES 1"

# Create folder and file for timeserie.
cluster_path = study_path / "input" / "renewables" / "series"/ Path(area.id) / Path(renewable.id)
cluster_path = study_path / "input" / "renewables" / "series" / Path(area.id) / Path(renewable.id)
os.makedirs(cluster_path, exist_ok=True)
series_path = cluster_path / 'series.txt'
series_path = cluster_path / "series.txt"
_write_file(series_path, expected_time_serie)

# Check matrix
Expand All @@ -1199,14 +1198,14 @@ def test_read_solar_local(self, local_study_w_areas):
local_study_object = read_study_local(study_path)
areas = local_study_object.read_areas()


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" / "solar" / "series" / f"solar_{area.id}.txt"
_write_file(file_path, expected_time_serie)
Expand All @@ -1221,49 +1220,51 @@ def test_read_solar_local(self, local_study_w_areas):
matrix = area.get_solar_matrix()
pd.testing.assert_frame_equal(matrix, expected_time_serie)


class TestReadReserves:
def test_read_reserve_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()


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" / "reserves" / f"{area.id}.txt"
file_path = study_path / "input" / "reserves" / f"{area.id}.txt"
_write_file(file_path, expected_time_serie)

matrix = area.get_reserves_matrix()
pd.testing.assert_frame_equal(matrix.astype(str), expected_time_serie.astype(str), check_dtype=False)

expected_time_serie = pd.DataFrame([])
for area in areas:
file_path = study_path / "input" / "reserves" / f"{area.id}.txt"
file_path = study_path / "input" / "reserves" / f"{area.id}.txt"
_write_file(file_path, expected_time_serie)
matrix = area.get_reserves_matrix()
pd.testing.assert_frame_equal(matrix, expected_time_serie)


class TestReadWind:
def test_read_wind_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()


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" / "wind" / "series" / f"wind_{area.id}.txt"
_write_file(file_path, expected_time_serie)

Expand All @@ -1277,21 +1278,22 @@ def test_read_wind_local(self, local_study_w_areas):
matrix = area.get_wind_matrix()
pd.testing.assert_frame_equal(matrix, expected_time_serie)


class TestReadmisc_gen:
def test_read_misc_gen_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()


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" / "misc-gen" / f"miscgen-{area.id}.txt"
_write_file(file_path, expected_time_serie)

Expand All @@ -1303,4 +1305,4 @@ def test_read_misc_gen_local(self, local_study_w_areas):
file_path = study_path / "input" / "misc-gen" / f"miscgen-{area.id}.txt"
_write_file(file_path, expected_time_serie)
matrix = area.get_misc_gen_matrix()
pd.testing.assert_frame_equal(matrix, expected_time_serie)
pd.testing.assert_frame_equal(matrix, expected_time_serie)

0 comments on commit 7d35ba7

Please sign in to comment.