Skip to content

Commit

Permalink
Feat/interpolate instantaneous sensor charts (#851)
Browse files Browse the repository at this point in the history
* fix: sensor chart with instantaneous sensor readings

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

* docs: changelog entry

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

* fix(docs): correct PR number

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

* feat: validate attribute before storing

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

---------

Signed-off-by: F.N. Claessen <felix@seita.nl>
  • Loading branch information
Flix6x committed Sep 15, 2023
1 parent f3b5c4d commit 8204b16
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions documentation/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features

* Introduce new reporter to compute profit/loss due to electricity flows: `ProfitOrLossReporter` [see `PR #808 <https://github.com/FlexMeasures/flexmeasures/pull/808>`_ and `PR #844 <https://github.com/FlexMeasures/flexmeasures/pull/844>`_]
* Charts visible in the UI can be exported to PNG or SVG formats in a more automated fashion, using the new CLI command flexmeasures show chart [see `PR #833 <https://github.com/FlexMeasures/flexmeasures/pull/833>`_]
* Sensor charts showing instantaneous observations can be interpolated by setting the ``interpolate`` sensor attribute to one of the `supported Vega-Lite interpolation methods <https://vega.github.io/vega-lite/docs/area.html#properties>`_ [see `PR #851 <https://github.com/FlexMeasures/flexmeasures/pull/851>`_]

Infrastructure / Support
----------------------
Expand Down
4 changes: 4 additions & 0 deletions flexmeasures/cli/data_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from flexmeasures import Sensor
from flexmeasures.data import db
from flexmeasures.data.schemas.attributes import validate_special_attributes
from flexmeasures.data.schemas.generic_assets import GenericAssetIdField
from flexmeasures.data.schemas.sensors import SensorIdField
from flexmeasures.data.models.generic_assets import GenericAsset
Expand Down Expand Up @@ -129,6 +130,9 @@ def edit_attribute(
attribute_null_value=attribute_null_value,
)

# Some attributes with special in meaning in FlexMeasures must pass validation
validate_special_attributes(attribute_key, attribute_value)

# Set attribute
for asset in assets:
asset.attributes[attribute_key] = attribute_value
Expand Down
10 changes: 8 additions & 2 deletions flexmeasures/data/models/charts/belief_charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,20 @@ def bar_chart(
event_ends_before.timestamp() * 10**3,
]
}
mark_type = "bar"
mark_interpolate = None
if sensor.event_resolution == timedelta(0) and sensor.has_attribute("interpolate"):
mark_type = "area"
mark_interpolate = sensor.get_attribute("interpolate")
chart_specs = {
"description": "A simple bar chart showing sensor data.",
"description": f"A simple {mark_type} chart showing sensor data.",
# the sensor type is already shown as the y-axis title (avoid redundant info)
"title": capitalize(sensor.name) if sensor.name != sensor.sensor_type else None,
"layer": [
{
"mark": {
"type": "bar",
"type": mark_type,
"interpolate": mark_interpolate,
"clip": True,
"width": {"band": 0.999},
},
Expand Down
9 changes: 9 additions & 0 deletions flexmeasures/data/schemas/attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Any

from altair.vegalite.schema import Interpolate


def validate_special_attributes(key: str, value: Any):
"""Validate attributes with a special meaning in FlexMeasures."""
if key == "interpolate":
Interpolate.validate(value)

0 comments on commit 8204b16

Please sign in to comment.