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

Prefer conversion to short stock units #412

Merged
merged 4 commits into from Apr 5, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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