From 7e87d88fc96e6a9276a0c62b822cb67f8cce2e9a Mon Sep 17 00:00:00 2001 From: tbittar Date: Fri, 20 Dec 2024 15:02:45 +0100 Subject: [PATCH] Rename input components to input study (#58) * Rename input components to input study * Rename StudyConverter to AntaresStudyConverter --- src/andromede/input_converter/src/converter.py | 9 ++++----- src/andromede/main/main.py | 8 ++++---- src/andromede/study/parsing.py | 8 ++++---- src/andromede/study/resolve_components.py | 18 ++++++------------ tests/input_converter/test_converter.py | 12 ++++++------ .../unittests/study/test_components_parsing.py | 12 ++++++------ 6 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/andromede/input_converter/src/converter.py b/src/andromede/input_converter/src/converter.py index 50de649..50cbf6b 100644 --- a/src/andromede/input_converter/src/converter.py +++ b/src/andromede/input_converter/src/converter.py @@ -20,11 +20,10 @@ 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 @@ -32,10 +31,10 @@ def __init__(self, study_path: Optional[Path]): 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] diff --git a/src/andromede/main/main.py b/src/andromede/main/main.py index 33d5784..8707f1f 100644 --- a/src/andromede/main/main.py +++ b/src/andromede/main/main.py @@ -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) @@ -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( @@ -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 diff --git a/src/andromede/study/parsing.py b/src/andromede/study/parsing.py index f9e46d6..1e57d2a 100644 --- a/src/andromede/study/parsing.py +++ b/src/andromede/study/parsing.py @@ -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: @@ -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) diff --git a/src/andromede/study/resolve_components.py b/src/andromede/study/resolve_components.py index a2f0d07..e4dca41 100644 --- a/src/andromede/study/resolve_components.py +++ b/src/andromede/study/resolve_components.py @@ -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) @@ -59,7 +55,7 @@ def network_components( def resolve_components_and_cnx( - input_comp: InputComponents, library: Library + input_comp: InputStudy, library: Library ) -> NetworkComponents: """ Resolves: @@ -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}" @@ -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: @@ -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: diff --git a/tests/input_converter/test_converter.py b/tests/input_converter/test_converter.py index e362ecb..c16f9ae 100644 --- a/tests/input_converter/test_converter.py +++ b/tests/input_converter/test_converter.py @@ -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), diff --git a/tests/unittests/study/test_components_parsing.py b/tests/unittests/study/test_components_parsing.py index 74832d1..e8ce313 100644 --- a/tests/unittests/study/test_components_parsing.py +++ b/tests/unittests/study/test_components_parsing.py @@ -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, @@ -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: @@ -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 @@ -51,7 +51,7 @@ 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) @@ -59,7 +59,7 @@ def test_consistency_check_ok( 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) @@ -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)