From 24c27bb0d3f9ee5570dc76e6d6cf3a45bc006e27 Mon Sep 17 00:00:00 2001 From: Jostein Solaas <33114722+jsolaas@users.noreply.github.com> Date: Thu, 21 Sep 2023 13:32:24 +0200 Subject: [PATCH] fix: forbid extra attributes in TimeSeries (#195) --- src/libecalc/common/utils/rates.py | 3 ++- .../core/consumers/legacy_consumer/component.py | 3 --- src/libecalc/core/graph_result.py | 12 ++++++++---- src/tests/libecalc/core/result/test_results.py | 6 +++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/libecalc/common/utils/rates.py b/src/libecalc/common/utils/rates.py index 97047ba735..219868c1f2 100644 --- a/src/libecalc/common/utils/rates.py +++ b/src/libecalc/common/utils/rates.py @@ -29,7 +29,7 @@ from libecalc.common.units import Unit from libecalc.dto.types import RateType from numpy.typing import NDArray -from pydantic import validator +from pydantic import Extra, validator from pydantic.fields import ModelField from pydantic.generics import GenericModel from typing_extensions import Self @@ -148,6 +148,7 @@ class Config: use_enum_values = True alias_generator = to_camel_case allow_population_by_field_name = True + extra = Extra.forbid @validator("values", each_item=True, pre=True) def convert_none_to_nan(cls, v: float, field: ModelField) -> TimeSeriesValue: diff --git a/src/libecalc/core/consumers/legacy_consumer/component.py b/src/libecalc/core/consumers/legacy_consumer/component.py index dcbeda1c28..d96f442eb2 100644 --- a/src/libecalc/core/consumers/legacy_consumer/component.py +++ b/src/libecalc/core/consumers/legacy_consumer/component.py @@ -200,21 +200,18 @@ def evaluate( timesteps=consumer_function_result.time_vector.tolist(), values=list(consumer_function_result.energy_function_result.suction_pressure), unit=Unit.BARA, - regularity=regularity, ).reindex(new_time_vector=variables_map.time_vector) outlet_pressure_time_series = TimeSeriesFloat( timesteps=consumer_function_result.time_vector.tolist(), values=list(consumer_function_result.energy_function_result.discharge_pressure), unit=Unit.BARA, - regularity=regularity, ).reindex(new_time_vector=variables_map.time_vector) operational_head_time_series = TimeSeriesFloat( timesteps=consumer_function_result.time_vector.tolist(), values=list(consumer_function_result.energy_function_result.operational_head), unit=Unit.POLYTROPIC_HEAD_JOULE_PER_KG, - regularity=regularity, ).reindex(new_time_vector=variables_map.time_vector) consumer_result = PumpResult( diff --git a/src/libecalc/core/graph_result.py b/src/libecalc/core/graph_result.py index 64eada8b50..dd98ce766b 100644 --- a/src/libecalc/core/graph_result.py +++ b/src/libecalc/core/graph_result.py @@ -69,10 +69,14 @@ def _compute_intensity( EmissionIntensityResult( name=emissions[key].name, timesteps=emissions[key].timesteps, - intensity_sm3=intensity_sm3, - intensity_boe=intensity_sm3.to_unit(Unit.KG_BOE), - intensity_yearly_sm3=intensity_yearly_sm3, - intensity_yearly_boe=intensity_yearly_sm3.to_unit(Unit.KG_BOE), + intensity_sm3=intensity_sm3.dict(exclude={"rate_type": True, "regularity": True}), + intensity_boe=intensity_sm3.to_unit(Unit.KG_BOE).dict( + exclude={"rate_type": True, "regularity": True} + ), + intensity_yearly_sm3=intensity_yearly_sm3.dict(exclude={"rate_type": True, "regularity": True}), + intensity_yearly_boe=intensity_yearly_sm3.to_unit(Unit.KG_BOE).dict( + exclude={"rate_type": True, "regularity": True} + ), ) ) return emission_intensities diff --git a/src/tests/libecalc/core/result/test_results.py b/src/tests/libecalc/core/result/test_results.py index b18c5ef172..82f04c3e38 100644 --- a/src/tests/libecalc/core/result/test_results.py +++ b/src/tests/libecalc/core/result/test_results.py @@ -24,14 +24,14 @@ def test_merge_compressor_result(self): timesteps=timesteps, values=[1, 2, 3, 4], unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - typ=RateType.STREAM_DAY, + rate_type=RateType.STREAM_DAY, regularity=[1, 1, 1, 1], ), power=TimeSeriesRate( timesteps=timesteps, values=[1, 2, 3, 4], unit=Unit.MEGA_WATT, - typ=RateType.STREAM_DAY, + rate_type=RateType.STREAM_DAY, regularity=[1, 1, 1, 1], ), is_valid=TimeSeriesBoolean( @@ -43,7 +43,7 @@ def test_merge_compressor_result(self): timesteps=timesteps, values=[0.1, 0.2, 0.3, 0.4], unit=Unit.MEGA_WATT, - typ=RateType.STREAM_DAY, + rate_type=RateType.STREAM_DAY, regularity=[1, 1, 1, 1], ), rate_exceeds_maximum=TimeSeriesBoolean(