/
time_series.py
388 lines (336 loc) · 14.1 KB
/
time_series.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
from typing import List, Dict, Optional, Union, Tuple
from datetime import datetime as datetime_type, timedelta
import json
from altair.utils.html import spec_to_html
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import Query, Session
import timely_beliefs as tb
import timely_beliefs.utils as tb_utils
from marshmallow import Schema, fields
from flexmeasures.data.config import db
from flexmeasures.data import ma
from flexmeasures.data.queries.utils import (
add_belief_timing_filter,
add_user_source_filter,
add_source_type_filter,
create_beliefs_query,
exclude_source_type_filter,
)
from flexmeasures.data.services.time_series import collect_time_series_data
from flexmeasures.ui.charts import belief_charts_mapping
from flexmeasures.utils.time_utils import server_now
from flexmeasures.utils.flexmeasures_inflection import capitalize
class Sensor(db.Model, tb.SensorDBMixin):
"""A sensor measures events. """
def __init__(self, name: str, **kwargs):
tb.SensorDBMixin.__init__(self, name, **kwargs)
tb_utils.remove_class_init_kwargs(tb.SensorDBMixin, kwargs)
db.Model.__init__(self, **kwargs)
def search_beliefs(
self,
event_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
belief_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
source: Optional[Union[int, List[int], str, List[str]]] = None,
):
"""Search all beliefs about events for this sensor.
:param event_time_window: search only events within this time window
:param belief_time_window: search only beliefs within this time window
:param source: search only beliefs by this source (pass its name or id) or list of sources"""
return TimedBelief.search(
sensor=self,
event_time_window=event_time_window,
belief_time_window=belief_time_window,
source=source,
)
def chart(
self,
events_not_before: Optional[datetime_type] = None,
events_before: Optional[datetime_type] = None,
belief_start: Optional[datetime_type] = None,
belief_end: Optional[datetime_type] = None,
source: Optional[Union[int, List[int], str, List[str]]] = None,
chart_type: str = "bar_chart",
data_only: bool = False,
chart_only: bool = True,
as_html: bool = False,
dataset_name: Optional[str] = None,
**kwargs,
) -> str:
"""
:param data_only: return just the data (in case you have the chart specs already)
:param as_html: return the chart with data as a standalone html
"""
# Set up chart specification
if dataset_name is None:
dataset_name = "sensor_" + str(self.id)
self.sensor_type = (
self.name
) # todo remove this placeholder when sensor types are modelled
chart = belief_charts_mapping[chart_type](
title=capitalize(self.name),
quantity=capitalize(self.sensor_type),
unit=self.unit,
dataset_name=dataset_name,
**kwargs,
)
if chart_only:
return json.dumps(chart)
# Set up data
bdf = self.search_beliefs(
(events_not_before, events_before), (belief_start, belief_end), source
)
df = bdf.reset_index()
df["source"] = df["source"].apply(lambda x: x.name)
data = df.to_json(orient="records")
if data_only:
return data
# Combine chart specs and data
chart["datasets"] = {dataset_name: json.loads(data)}
if as_html:
return spec_to_html(
chart,
"vega-lite",
vega_version="5",
vegaembed_version="6.17.0",
vegalite_version="5.0.0",
)
return json.dumps(chart)
@property
def timerange(self) -> Dict[str, datetime_type]:
"""Timerange for which sensor data exists.
:returns: dictionary with start and end, for example:
{
'start': datetime.datetime(2020, 12, 3, 14, 0, tzinfo=pytz.utc),
'end': datetime.datetime(2020, 12, 3, 14, 30, tzinfo=pytz.utc)
}
"""
least_recent_query = (
TimedBelief.query.filter(TimedBelief.sensor == self)
.order_by(TimedBelief.event_start.asc())
.limit(1)
)
most_recent_query = (
TimedBelief.query.filter(TimedBelief.sensor == self)
.order_by(TimedBelief.event_start.desc())
.limit(1)
)
results = least_recent_query.union_all(most_recent_query).all()
if not results:
# return now in case there is no data for the sensor
now = server_now()
return dict(start=now, end=now)
least_recent, most_recent = results
return dict(start=least_recent.event_start, end=most_recent.event_end)
def __repr__(self) -> str:
return f"<Sensor {self.id}: {self.name}>"
class TimedBelief(db.Model, tb.TimedBeliefDBMixin):
"""A timed belief holds a precisely timed record of a belief about an event.
It also records the source of the belief, and the sensor that the event pertains to.
"""
@declared_attr
def source_id(cls):
return db.Column(db.Integer, db.ForeignKey("data_source.id"), primary_key=True)
sensor = db.relationship("Sensor", backref=db.backref("beliefs", lazy=True))
source = db.relationship("DataSource", backref=db.backref("beliefs", lazy=True))
def __init__(
self,
sensor: tb.DBSensor,
source: tb.DBBeliefSource,
**kwargs,
):
tb.TimedBeliefDBMixin.__init__(self, sensor, source, **kwargs)
tb_utils.remove_class_init_kwargs(tb.TimedBeliefDBMixin, kwargs)
db.Model.__init__(self, **kwargs)
@classmethod
def search(
cls,
sensor: Sensor,
event_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
belief_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
source: Optional[Union[int, List[int], str, List[str]]] = None,
) -> tb.BeliefsDataFrame:
"""Search all beliefs about events for a given sensor.
:param sensor: search only this sensor
:param event_time_window: search only events within this time window
:param belief_time_window: search only beliefs within this time window
:param source: search only beliefs by this source (pass its name or id) or list of sources
"""
return cls.search_session(
session=db.session,
sensor=sensor,
event_before=event_time_window[1],
event_not_before=event_time_window[0],
belief_before=belief_time_window[1],
belief_not_before=belief_time_window[0],
source=source,
)
@classmethod
def add(cls, bdf: tb.BeliefsDataFrame, commit_transaction: bool = True):
"""Add a BeliefsDataFrame as timed beliefs in the database.
:param bdf: the BeliefsDataFrame to be persisted
:param commit_transaction: if True, the session is committed
if False, you can still add other data to the session
and commit it all within an atomic transaction
"""
return cls.add_to_session(
session=db.session,
beliefs_data_frame=bdf,
commit_transaction=commit_transaction,
)
def __repr__(self) -> str:
"""timely-beliefs representation of timed beliefs."""
return tb.TimedBelief.__repr__(self)
class SensorSchemaMixin(Schema):
"""
Base sensor schema.
Here we include all fields which are implemented by timely_beliefs.SensorDBMixin
All classes inheriting from timely beliefs sensor don't need to repeat these.
In a while, this schema can represent our unified Sensor class.
When subclassing, also subclass from `ma.SQLAlchemySchema` and add your own DB model class, e.g.:
class Meta:
model = Asset
"""
name = ma.auto_field(required=True)
unit = ma.auto_field(required=True)
timezone = ma.auto_field()
event_resolution = fields.TimeDelta(required=True, precision="minutes")
class SensorSchema(SensorSchemaMixin, ma.SQLAlchemySchema):
"""
Sensor schema, with validations.
"""
class Meta:
model = Sensor
class TimedValue(object):
"""
A mixin of all tables that store time series data, either forecasts or measurements.
Represents one row.
"""
@declared_attr
def __tablename__(cls): # noqa: B902
return cls.__name__.lower()
"""The time at which the value is supposed to (have) happen(ed)."""
@declared_attr
def datetime(cls): # noqa: B902
return db.Column(db.DateTime(timezone=True), primary_key=True, index=True)
"""The time delta of measuring or forecasting.
This should be a duration in ISO8601, e.g. "PT10M", which you can turn into a timedelta with
isodate.parse_duration, optionally with a minus sign, e.g. "-PT10M".
Positive durations indicate a forecast into the future, negative ones a backward forecast into the past or simply
a measurement after the fact.
"""
@declared_attr
def horizon(cls): # noqa: B902
return db.Column(
db.Interval(), nullable=False, primary_key=True
) # todo: default=timedelta(hours=0)
"""The value."""
@declared_attr
def value(cls): # noqa: B902
return db.Column(db.Float, nullable=False)
"""The data source."""
@declared_attr
def data_source_id(cls): # noqa: B902
return db.Column(db.Integer, db.ForeignKey("data_source.id"), primary_key=True)
@classmethod
def make_query(
cls,
asset_class: db.Model,
asset_names: Tuple[str],
query_window: Tuple[Optional[datetime_type], Optional[datetime_type]],
belief_horizon_window: Tuple[Optional[timedelta], Optional[timedelta]] = (
None,
None,
),
belief_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
belief_time: Optional[datetime_type] = None,
user_source_ids: Optional[Union[int, List[int]]] = None,
source_types: Optional[List[str]] = None,
exclude_source_types: Optional[List[str]] = None,
session: Session = None,
) -> Query:
"""
Can be extended with the make_query function in subclasses.
We identify the assets by their name, which assumes a unique string field can be used.
The query window consists of two optional datetimes (start and end).
The horizon window expects first the shorter horizon (e.g. 6H) and then the longer horizon (e.g. 24H).
The session can be supplied, but if None, the implementation should find a session itself.
:param user_source_ids: Optional list of user source ids to query only specific user sources
:param source_types: Optional list of source type names to query only specific source types *
:param exclude_source_types: Optional list of source type names to exclude specific source types *
* If user_source_ids is specified, the "user" source type is automatically included (and not excluded).
Somewhat redundant, but still allowed is to set both source_types and exclude_source_types.
# todo: add examples
# todo: switch to using timely_beliefs queries, which are more powerful
"""
if session is None:
session = db.session
start, end = query_window
query = create_beliefs_query(cls, session, asset_class, asset_names, start, end)
query = add_belief_timing_filter(
cls, query, asset_class, belief_horizon_window, belief_time_window
)
if user_source_ids:
query = add_user_source_filter(cls, query, user_source_ids)
if source_types:
if user_source_ids and "user" not in source_types:
source_types.append("user")
query = add_source_type_filter(cls, query, source_types)
if exclude_source_types:
if user_source_ids and "user" in exclude_source_types:
exclude_source_types.remove("user")
query = exclude_source_type_filter(cls, query, exclude_source_types)
return query
@classmethod
def collect(
cls,
generic_asset_names: Union[str, List[str]],
query_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
belief_horizon_window: Tuple[Optional[timedelta], Optional[timedelta]] = (
None,
None,
),
belief_time_window: Tuple[Optional[datetime_type], Optional[datetime_type]] = (
None,
None,
),
user_source_ids: Union[
int, List[int]
] = None, # None is interpreted as all sources
source_types: Optional[List[str]] = None,
exclude_source_types: Optional[List[str]] = None,
resolution: Union[str, timedelta] = None,
sum_multiple: bool = True,
) -> Union[tb.BeliefsDataFrame, Dict[str, tb.BeliefsDataFrame]]:
"""Basically a convenience wrapper for services.collect_time_series_data,
where time series data collection is implemented.
"""
return collect_time_series_data(
generic_asset_names=generic_asset_names,
make_query=cls.make_query,
query_window=query_window,
belief_horizon_window=belief_horizon_window,
belief_time_window=belief_time_window,
user_source_ids=user_source_ids,
source_types=source_types,
exclude_source_types=exclude_source_types,
resolution=resolution,
sum_multiple=sum_multiple,
)