/
belief_charts.py
198 lines (192 loc) · 7.21 KB
/
belief_charts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import annotations
from datetime import datetime
from flexmeasures.data.models.charts.defaults import FIELD_DEFINITIONS
from flexmeasures.utils.flexmeasures_inflection import capitalize
def bar_chart(
sensor: "Sensor", # noqa F821
event_starts_after: datetime | None = None,
event_ends_before: datetime | None = None,
**override_chart_specs: dict,
):
unit = sensor.unit if sensor.unit else "a.u."
event_value_field_definition = dict(
title=f"{capitalize(sensor.sensor_type)} ({unit})",
format=[".3s", unit],
formatType="quantityWithUnitFormat",
stack=None,
**FIELD_DEFINITIONS["event_value"],
)
event_start_field_definition = FIELD_DEFINITIONS["event_start"]
if event_starts_after and event_ends_before:
event_start_field_definition["scale"] = {
"domain": [
event_starts_after.timestamp() * 10**3,
event_ends_before.timestamp() * 10**3,
]
}
resolution_in_ms = sensor.event_resolution.total_seconds() * 1000
chart_specs = {
"description": "A simple bar chart showing sensor data.",
"title": capitalize(sensor.name) if sensor.name != sensor.sensor_type else None,
"mark": {
"type": "bar",
"clip": True,
},
"encoding": {
"x": event_start_field_definition,
"x2": FIELD_DEFINITIONS["event_end"],
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["source_name"],
"detail": FIELD_DEFINITIONS["source"],
"opacity": {"value": 0.7},
"tooltip": [
FIELD_DEFINITIONS["full_date"],
{
**event_value_field_definition,
**dict(title=f"{capitalize(sensor.sensor_type)}"),
},
FIELD_DEFINITIONS["source_name"],
FIELD_DEFINITIONS["source_model"],
],
},
"transform": [
{
"calculate": f"datum.event_start + {resolution_in_ms}",
"as": "event_end",
},
],
}
for k, v in override_chart_specs.items():
chart_specs[k] = v
return chart_specs
def chart_for_multiple_sensors(
sensors: list["Sensor"], # noqa F821
event_starts_after: datetime | None = None,
event_ends_before: datetime | None = None,
**override_chart_specs: dict,
):
sensors_specs = []
min_resolution_in_ms = (
min(sensor.event_resolution for sensor in sensors).total_seconds() * 1000
)
for sensor in sensors:
unit = sensor.unit if sensor.unit else "a.u."
event_value_field_definition = dict(
title=f"{capitalize(sensor.sensor_type)} ({unit})",
format=[".3~r", unit],
formatType="quantityWithUnitFormat",
stack=None,
**FIELD_DEFINITIONS["event_value"],
)
event_start_field_definition = FIELD_DEFINITIONS["event_start"]
if event_starts_after and event_ends_before:
event_start_field_definition["scale"] = {
"domain": [
event_starts_after.timestamp() * 10**3,
event_ends_before.timestamp() * 10**3,
]
}
shared_tooltip = [
FIELD_DEFINITIONS["full_date"],
{
**event_value_field_definition,
**dict(title=f"{capitalize(sensor.sensor_type)}"),
},
FIELD_DEFINITIONS["source_name"],
FIELD_DEFINITIONS["source_model"],
]
sensor_specs = {
"title": capitalize(sensor.name)
if sensor.name != sensor.sensor_type
else None,
"transform": [{"filter": f"datum.sensor.id == {sensor.id}"}],
"layer": [
{
"mark": {
"type": "line",
"interpolate": "step-after",
"clip": True,
},
"encoding": {
"x": event_start_field_definition,
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["source_name"],
"detail": FIELD_DEFINITIONS["source"],
},
},
{
"mark": {
"type": "rect",
"y2": "height",
"opacity": 0,
},
"encoding": {
"x": event_start_field_definition,
"x2": FIELD_DEFINITIONS["event_end"],
"y": {
"condition": {
"test": "isNaN(datum['event_value'])",
**event_value_field_definition,
},
"value": 0,
},
"detail": FIELD_DEFINITIONS["source"],
"tooltip": shared_tooltip,
},
"transform": [
{
"calculate": f"datum.event_start + {min_resolution_in_ms}",
"as": "event_end",
},
],
},
{
"mark": {
"type": "circle",
"opacity": 1,
"clip": True,
},
"encoding": {
"x": event_start_field_definition,
"y": event_value_field_definition,
"color": FIELD_DEFINITIONS["source_name"],
"detail": FIELD_DEFINITIONS["source"],
"size": {
"condition": {
"value": "200",
"test": {"param": "paintbrush", "empty": False},
},
"value": "0",
},
"tooltip": shared_tooltip,
},
"params": [
{
"name": "paintbrush",
"select": {
"type": "point",
"encodings": ["x"],
"on": "mouseover",
"nearest": False,
},
},
],
},
],
"width": "container",
}
sensors_specs.append(sensor_specs)
chart_specs = dict(
description="A vertically concatenated chart showing sensor data.",
vconcat=[*sensors_specs],
spacing=100,
bounds="flush",
)
chart_specs["config"] = {
"view": {"continuousWidth": 800, "continuousHeight": 150},
"autosize": {"type": "fit-x", "contains": "padding"},
}
chart_specs["resolve"] = {"scale": {"x": "shared"}}
for k, v in override_chart_specs.items():
chart_specs[k] = v
return chart_specs