Skip to content

Commit

Permalink
Backport PR #412: Prefer conversion to short stock units (#412)
Browse files Browse the repository at this point in the history
Catch another case of converting flow units to sensible stock units: t/h to t (tonne) instead of to Mg.

* Add test case

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Return shortest unit of stock

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Sneak in some type annotation fixes

Signed-off-by: F.N. Claessen <felix@seita.nl>

* Changelog entry

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Apr 5, 2022
1 parent 90680b3 commit cefabed
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
22 changes: 22 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -2,6 +2,28 @@
FlexMeasures Changelog
**********************

v0.10.0 | April XX, 2022
===========================


New features
-----------

Bugfixes
-----------

Infrastructure / Support
----------------------


v0.9.2 | April XX, 2022
===========================

Bugfixes
--------
* Prefer unit conversions to short stock units [see `PR #412 <http://www.github.com/FlexMeasures/flexmeasures/pull/412>`_]


v0.9.1 | March 31, 2022
===========================

Expand Down
5 changes: 5 additions & 0 deletions flexmeasures/data/models/charts/belief_charts.py
@@ -1,9 +1,12 @@
from __future__ import annotations

from flexmeasures.data.models.charts.defaults import FIELD_DEFINITIONS
from flexmeasures.utils.flexmeasures_inflection import capitalize


def bar_chart(
sensor: "Sensor", # noqa F821
unique_source_names: list[str] | None = None,
**override_chart_specs: dict,
):
unit = sensor.unit if sensor.unit else "a.u."
Expand All @@ -13,6 +16,8 @@ def bar_chart(
stack=None,
**FIELD_DEFINITIONS["event_value"],
)
color_definition = FIELD_DEFINITIONS["source"]
color_definition["scale"] = {"domain": unique_source_names}
chart_specs = {
"description": "A simple bar chart.",
"title": capitalize(sensor.name),
Expand Down
6 changes: 6 additions & 0 deletions flexmeasures/data/models/time_series.py
Expand Up @@ -141,6 +141,11 @@ def is_strictly_non_negative(self) -> bool:
"is_consumer", True
)

@property
def unique_sources(self) -> List[DataSource]:
"""List unique sources that have registered data for this sensor, ordered by id."""
return DataSource.query.join(TimedBelief).filter(TimedBelief.sensor_id == self.id).filter(TimedBelief.source_id == DataSource.id).order_by(DataSource.id).all()

def get_attribute(self, attribute: str, default: Any = None) -> Any:
"""Looks for the attribute on the Sensor.
If not found, looks for the attribute on the Sensor's GenericAsset.
Expand Down Expand Up @@ -334,6 +339,7 @@ def chart(
chart_specs = chart_type_to_chart_specs(
chart_type,
sensor=self,
unique_source_names=[source.name for source in self.unique_sources],
dataset_name=dataset_name,
**kwargs,
)
Expand Down
6 changes: 3 additions & 3 deletions flexmeasures/data/queries/annotations.py
Expand Up @@ -12,9 +12,9 @@

def query_asset_annotations(
asset_id: int,
annotation_starts_after: Optional[datetime],
annotation_ends_before: Optional[datetime],
sources: List[DataSource],
annotation_starts_after: Optional[datetime] = None,
annotation_ends_before: Optional[datetime] = None,
sources: Optional[List[DataSource]] = None,
annotation_type: str = None,
) -> Query:
"""Match annotations assigned to the given asset."""
Expand Down
1 change: 1 addition & 0 deletions flexmeasures/utils/tests/test_unit_utils.py
Expand Up @@ -91,6 +91,7 @@ def test_determine_flow_unit(
("kW", None, "kWh"),
("m/s", "s", "m"),
("m/s", "h", "km"),
("t/h", None, "t"),
],
)
def test_determine_stock_unit(
Expand Down
11 changes: 8 additions & 3 deletions flexmeasures/utils/unit_utils.py
Expand Up @@ -120,12 +120,17 @@ def determine_flow_unit(stock_unit: str, time_unit: str = "h"):


def determine_stock_unit(flow_unit: str, time_unit: str = "h"):
"""For example:
"""Determine the shortest unit of stock, given a unit of flow.
For example:
>>> determine_stock_unit("m³/h") # m³
>>> determine_stock_unit("kW") # kWh
"""
stock = to_preferred(ur.Quantity(flow_unit) * ur.Quantity(time_unit))
return "{:~P}".format(stock.units)
stock = ur.Quantity(flow_unit) * ur.Quantity(time_unit)
return min(
["{:~P}".format(stock.units), "{:~P}".format(to_preferred(stock).units)],
key=len,
)


def units_are_convertible(
Expand Down

0 comments on commit cefabed

Please sign in to comment.