Skip to content

Commit

Permalink
Rename input components to input study (#58)
Browse files Browse the repository at this point in the history
* Rename input components to input study

* Rename StudyConverter to AntaresStudyConverter
  • Loading branch information
tbittar authored Dec 20, 2024
1 parent a087f12 commit 7e87d88
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 37 deletions.
9 changes: 4 additions & 5 deletions src/andromede/input_converter/src/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@
convert_area_to_component_list,
resolve_path,
)
from andromede.study.parsing import InputStudy

from andromede.study.parsing import InputComponents


class StudyConverter:
class AntaresStudyConverter:
def __init__(self, study_path: Optional[Path]):
"""
Initialize processor
"""
self.study_path = resolve_path(study_path) if study_path else None
self.study: Study = read_study_local(self.study_path) if self.study_path else None # type: ignore

def convert_study_to_input_components(self) -> InputComponents:
def convert_study_to_input_study(self) -> InputStudy:
areas = self.study.read_areas()
area_components = convert_area_to_component_list(areas)
return InputComponents(nodes=area_components)
return InputStudy(nodes=area_components)

def validate_with_pydantic(
self, data: dict, model_class: type[BaseModel]
Expand Down
8 changes: 4 additions & 4 deletions src/andromede/main/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def input_database(study_path: Path, timeseries_path: Optional[Path]) -> DataBas
return build_data_base(parse_yaml_components(comp), timeseries_path)


def input_components(study_path: Path, model: Library) -> NetworkComponents:
def input_study(study_path: Path, model: Library) -> NetworkComponents:
with study_path.open() as comp:
return resolve_components_and_cnx(parse_yaml_components(comp), model)

Expand All @@ -64,8 +64,8 @@ def main_cli() -> None:
parsed_args = parse_cli()

models = input_models(parsed_args.models_path)
components = input_components(parsed_args.components_path, models)
consistency_check(components.components, models.models)
study = input_study(parsed_args.components_path, models)
consistency_check(study.components, models.models)

try:
database = input_database(
Expand All @@ -77,7 +77,7 @@ def main_cli() -> None:
f"An error occurred while importing time series."
)

network = build_network(components)
network = build_network(study)

timeblock = TimeBlock(1, list(range(parsed_args.duration)))
scenario = parsed_args.nb_scenarios
Expand Down
8 changes: 4 additions & 4 deletions src/andromede/study/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
from yaml import safe_load


def parse_yaml_components(input_components: typing.TextIO) -> "InputComponents":
tree = safe_load(input_components)
return InputComponents.model_validate(tree["study"])
def parse_yaml_components(input_study: typing.TextIO) -> "InputStudy":
tree = safe_load(input_study)
return InputStudy.model_validate(tree["study"])


def parse_scenario_builder(file: Path) -> pd.DataFrame:
Expand Down Expand Up @@ -66,7 +66,7 @@ class Config:
alias_generator = _to_kebab


class InputComponents(BaseModel):
class InputStudy(BaseModel):
nodes: List[InputComponent] = Field(default_factory=list)
components: List[InputComponent] = Field(default_factory=list)
connections: List[InputPortConnections] = Field(default_factory=list)
Expand Down
18 changes: 6 additions & 12 deletions src/andromede/study/resolve_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
TimeScenarioSeriesData,
load_ts_from_txt,
)
from andromede.study.parsing import (
InputComponent,
InputComponents,
InputPortConnections,
)
from andromede.study.parsing import InputComponent, InputPortConnections, InputStudy


@dataclass(frozen=True)
Expand All @@ -59,7 +55,7 @@ def network_components(


def resolve_components_and_cnx(
input_comp: InputComponents, library: Library
input_comp: InputStudy, library: Library
) -> NetworkComponents:
"""
Resolves:
Expand Down Expand Up @@ -111,14 +107,14 @@ def _get_component_by_id(


def consistency_check(
input_components: Dict[str, Component], input_models: Dict[str, Model]
input_study: Dict[str, Component], input_models: Dict[str, Model]
) -> bool:
"""
Checks if all components in the Components instances have a valid model from the library.
Returns True if all components are consistent, raises ValueError otherwise.
"""
model_ids_set = input_models.keys()
for component_id, component in input_components.items():
for component_id, component in input_study.items():
if component.model.id not in model_ids_set:
raise ValueError(
f"Error: Component {component_id} has invalid model ID: {component.model.id}"
Expand All @@ -141,9 +137,7 @@ def build_network(comp_network: NetworkComponents) -> Network:
return network


def build_data_base(
input_comp: InputComponents, timeseries_dir: Optional[Path]
) -> DataBase:
def build_data_base(input_comp: InputStudy, timeseries_dir: Optional[Path]) -> DataBase:
database = DataBase()

for comp in input_comp.components:
Expand Down Expand Up @@ -188,7 +182,7 @@ def _resolve_scenarization(


def build_scenarized_data_base(
input_comp: InputComponents,
input_comp: InputStudy,
scenario_builder_data: pd.DataFrame,
timeseries_dir: Optional[Path],
) -> DataBase:
Expand Down
12 changes: 6 additions & 6 deletions tests/input_converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
# This file is part of the Antares project.


from andromede.input_converter.src.converter import StudyConverter
from andromede.study.parsing import InputComponent, InputComponents
from andromede.input_converter.src.converter import AntaresStudyConverter
from andromede.study.parsing import InputComponent, InputStudy


class TestConverter:
def test_convert_area_to_input_components(self, local_study_w_areas):
converter = StudyConverter(study_path=None)
def test_convert_area_to_input_study(self, local_study_w_areas):
converter = AntaresStudyConverter(study_path=None)
converter.study = local_study_w_areas
area_components = converter.convert_study_to_input_components()
expected_area_components = InputComponents(
area_components = converter.convert_study_to_input_study()
expected_area_components = InputStudy(
nodes=[
InputComponent(id="fr", model="area", parameters=None),
InputComponent(id="it", model="area", parameters=None),
Expand Down
12 changes: 6 additions & 6 deletions tests/unittests/study/test_components_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from andromede.model.resolve_library import resolve_library
from andromede.simulation import BlockBorderManagement, TimeBlock, build_problem
from andromede.study import TimeScenarioIndex, TimeScenarioSeriesData
from andromede.study.parsing import InputComponents, parse_yaml_components
from andromede.study.parsing import InputStudy, parse_yaml_components
from andromede.study.resolve_components import (
build_data_base,
build_network,
Expand All @@ -19,7 +19,7 @@
@pytest.fixture
def input_component(
data_dir: Path,
) -> InputComponents:
) -> InputStudy:
compo_file = data_dir / "components.yml"

with compo_file.open() as c:
Expand All @@ -37,7 +37,7 @@ def input_library(


def test_parsing_components_ok(
input_component: InputComponents, input_library: InputLibrary
input_component: InputStudy, input_library: InputLibrary
) -> None:
assert len(input_component.components) == 2
assert len(input_component.nodes) == 1
Expand All @@ -51,15 +51,15 @@ def test_parsing_components_ok(


def test_consistency_check_ok(
input_component: InputComponents, input_library: InputLibrary
input_component: InputStudy, input_library: InputLibrary
) -> None:
result_lib = resolve_library([input_library])
result_comp = resolve_components_and_cnx(input_component, result_lib)
consistency_check(result_comp.components, result_lib.models)


def test_consistency_check_ko(
input_component: InputComponents, input_library: InputLibrary
input_component: InputStudy, input_library: InputLibrary
) -> None:
result_lib = resolve_library([input_library])
result_comp = resolve_components_and_cnx(input_component, result_lib)
Expand All @@ -72,7 +72,7 @@ def test_consistency_check_ko(


def test_basic_balance_using_yaml(
input_component: InputComponents, input_library: InputLibrary
input_component: InputStudy, input_library: InputLibrary
) -> None:
result_lib = resolve_library([input_library])
components_input = resolve_components_and_cnx(input_component, result_lib)
Expand Down

0 comments on commit 7e87d88

Please sign in to comment.