From 17c230582b8b40e403c7aa895415e1db7bdf64fb Mon Sep 17 00:00:00 2001 From: aan Date: Fri, 9 Aug 2024 16:12:13 +0200 Subject: [PATCH] feat: Simplified house description bugfix. --- .pre-commit-config.yaml | 2 +- tests/test_schema.py | 30 ++++++++---------------- trano/data_models/conversion.py | 41 +++++++++++++++++++++++++-------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7cbb69f..2c08f85 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: rev: 4.0.1 hooks: - id: flake8 - args: ["--ignore=ANN101,ANN102,W503,INP001,FS003", "--max-line-length=120"] + args: ["--ignore=ANN101,ANN102,W503,INP001,FS003,SIM106", "--max-line-length=120"] exclude: tests/excl/ additional_dependencies: - flake8-annotations==2.9.0 diff --git a/tests/test_schema.py b/tests/test_schema.py index 6ca4c66..ca236b0 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -5,16 +5,10 @@ from typing import Optional import pytest -import yaml from linkml.validator import validate_file from tests.conftest import _read, clean_model, is_success -from trano.data_models.conversion import ( - _parse, - assign_space_id, - convert_model, - convert_network, -) +from trano.data_models.conversion import convert_model, convert_network from trano.simulate.simulate import SimulationOptions, simulate @@ -103,17 +97,11 @@ def test_simulate_model_yaml() -> None: def test_simplified_yaml() -> None: model_path = Path(__file__).parents[1].joinpath("tests", "simplified_house.yaml") - data = yaml.safe_load(model_path.read_text()) - data = assign_space_id(data) - _parse(data) - with tempfile.NamedTemporaryFile(mode="w+", suffix=".yaml") as f: - yaml.safe_dump(data, f) - network = convert_network("simplified_yaml", Path(f.name)) - - with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as project_path: - results = simulate( - Path(project_path), - network, - options=SimulationOptions(end_time=3600), - ) - assert is_success(results) + network = convert_network("simplified_yaml", model_path) + with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as project_path: + results = simulate( + Path(project_path), + network, + options=SimulationOptions(end_time=3600), + ) + assert is_success(results) diff --git a/trano/data_models/conversion.py b/trano/data_models/conversion.py index d5ff43e..26fe0f6 100644 --- a/trano/data_models/conversion.py +++ b/trano/data_models/conversion.py @@ -1,5 +1,6 @@ import copy import json +import tempfile from collections import Counter from pathlib import Path from typing import Any, Dict @@ -43,7 +44,6 @@ class Component(BaseModel): def validate_model(model_path: Path) -> None: - # TODO: why? this is not right, report = validate_file(model_path, DATA_MODEL_PATH, "Building") if report.results: @@ -77,19 +77,40 @@ def _instantiate_component(component_: Dict[str, Any]) -> Component: return Component(name=name, component_instance=component) +class EnrichedModel(BaseModel): + data: Dict[str, Any] + path: Path + + +def load_and_enrich_model(model_path: Path) -> EnrichedModel: + if model_path.suffix == ".yaml": + load_function = yaml.safe_load + dump_function = yaml.safe_dump + elif model_path.suffix == ".json": + load_function = json.loads + dump_function = json.dump + else: + raise Exception("Invalid file format") + data = load_function(model_path.read_text()) + data = assign_space_id(data) + _parse(data) + with tempfile.NamedTemporaryFile( + mode="w+", suffix=model_path.suffix, delete=False + ) as f: + dump_function(data, f) + return EnrichedModel(path=Path(f.name), data=data) + + # TODO: reduce complexity -def convert_network(name: str, model_path: Path) -> Network: # noqa: C901 +def convert_network(name: str, model_path: Path) -> Network: network = Network(name=name) occupancy = None - data = None system_counter: Any = Counter() - validate_model(model_path) - if model_path.suffix == ".yaml": - data = yaml.safe_load(model_path.read_text()) - if model_path.suffix == ".json": - data = json.loads(model_path.read_text()) - if not data: - raise Exception("Invalid file format") + + enriched_model = load_and_enrich_model(model_path) + validate_model(enriched_model.path) + data = enriched_model.data + materials = { material["id"]: Material(**(material | {"name": material["id"]})) for material in data["materials"]