Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't raise error when missing time series on token collection #730

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions src/libecalc/presentation/yaml/domain/time_series_collections.py
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird diff. I moved init logic to create, with an additional raise_on_error flag.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from typing import Self

from libecalc.presentation.yaml.domain.time_series import TimeSeries
from libecalc.presentation.yaml.domain.time_series_collection import TimeSeriesCollection
Expand All @@ -17,7 +18,41 @@ class TimeSeriesCollections(TimeSeriesProvider):
steps in all collections.
"""

def __init__(self, time_series: list[YamlTimeSeriesCollection], resources: dict[str, Resource]):
def __init__(self, time_series_collections: dict[str, TimeSeriesCollection]):
self._time_series_collections = time_series_collections

def get_time_series_references(self) -> list[str]:
time_series_references = []
for collection in self._time_series_collections.values():
for time_series_reference in collection.get_time_series_references():
time_series_references.append(f"{collection.name};{time_series_reference}")
return time_series_references

def get_time_series(self, time_series_id: str) -> TimeSeries:
reference_id_parts = time_series_id.split(";")
if len(reference_id_parts) != 2:
raise TimeSeriesNotFound(time_series_id)
[collection_id, time_series_id] = reference_id_parts

if collection_id not in self._time_series_collections:
raise TimeSeriesNotFound(time_series_id)

return self._time_series_collections[collection_id].get_time_series(time_series_id)

def get_time_vector(self) -> set[datetime]:
time_vector: set[datetime] = set()
for time_series_collection in self._time_series_collections.values():
if time_series_collection.should_influence_time_vector():
time_vector = time_vector.union(time_series_collection.get_time_vector())
return time_vector

@classmethod
def create(
cls,
time_series: list[YamlTimeSeriesCollection],
resources: dict[str, Resource],
raise_on_error: bool,
) -> tuple[Self, list[ModelValidationError]]:
time_series_collections: dict[str, TimeSeriesCollection] = {}
errors: list[ModelValidationError] = []
for time_series_collection in time_series:
Expand Down Expand Up @@ -52,32 +87,7 @@ def __init__(self, time_series: list[YamlTimeSeriesCollection], resources: dict[
for error in e.errors()
]
)
if len(errors) != 0:
if raise_on_error and len(errors) != 0:
raise ModelValidationException(errors=errors)

self._time_series_collections = time_series_collections

def get_time_series_references(self) -> list[str]:
time_series_references = []
for collection in self._time_series_collections.values():
for time_series_reference in collection.get_time_series_references():
time_series_references.append(f"{collection.name};{time_series_reference}")
return time_series_references

def get_time_series(self, time_series_id: str) -> TimeSeries:
reference_id_parts = time_series_id.split(";")
if len(reference_id_parts) != 2:
raise TimeSeriesNotFound(time_series_id)
[collection_id, time_series_id] = reference_id_parts

if collection_id not in self._time_series_collections:
raise TimeSeriesNotFound(time_series_id)

return self._time_series_collections[collection_id].get_time_series(time_series_id)

def get_time_vector(self) -> set[datetime]:
time_vector: set[datetime] = set()
for time_series_collection in self._time_series_collections.values():
if time_series_collection.should_influence_time_vector():
time_vector = time_vector.union(time_series_collection.get_time_vector())
return time_vector
return cls(time_series_collections), errors
14 changes: 12 additions & 2 deletions src/libecalc/presentation/yaml/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ def end(self) -> Optional[datetime]:
return self._configuration.end

def _get_time_series_collections(self) -> TimeSeriesCollections:
return TimeSeriesCollections(time_series=self._configuration.time_series, resources=self.resources)
time_series_collections, err = TimeSeriesCollections.create(
time_series=self._configuration.time_series,
resources=self.resources,
raise_on_error=True,
)
assert len(err) == 0
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise_on_error should make this empty

return time_series_collections

def _get_time_vector(self):
return get_global_time_vector(
Expand Down Expand Up @@ -158,7 +164,11 @@ def get_graph(self) -> ComponentGraph:
return self._graph

def _get_token_references(self, yaml_model: YamlValidator) -> list[str]:
token_references = self._get_time_series_collections().get_time_series_references()
# Only get references for valid time series collections
time_series_collections, _ = TimeSeriesCollections.create(
time_series=self._configuration.time_series, resources=self.resources, raise_on_error=False
)
token_references = time_series_collections.get_time_series_references()

for reference in yaml_model.variables:
token_references.append(f"$var.{reference}")
Expand Down
Loading