Skip to content

Commit

Permalink
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 d91a1d3 commit e58f30e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions documentation/changelog.rst
Expand Up @@ -16,6 +16,14 @@ 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
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 e58f30e

Please sign in to comment.