Skip to content

Commit

Permalink
feat: Simplified house description bugfix.
Browse files Browse the repository at this point in the history
  • Loading branch information
andoludo committed Aug 9, 2024
1 parent d39f85b commit 17c2305
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 9 additions & 21 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
41 changes: 31 additions & 10 deletions trano/data_models/conversion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import json
import tempfile
from collections import Counter
from pathlib import Path
from typing import Any, Dict
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 17c2305

Please sign in to comment.