-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path_config.py
More file actions
400 lines (313 loc) · 14.6 KB
/
_config.py
File metadata and controls
400 lines (313 loc) · 14.6 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
"""Resampler configuration."""
from __future__ import annotations
import logging
import statistics
from collections.abc import Sequence
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from typing import Literal, Protocol
from frequenz.core.datetime import UNIX_EPOCH
from ._base_types import SourceProperties
from ._wall_clock_timer import WallClockTimerConfig
_logger = logging.getLogger(__name__)
DEFAULT_BUFFER_LEN_INIT = 16
"""Default initial buffer length.
Buffers will be created initially with this length, but they could grow or
shrink depending on the source properties, like sampling rate, to make
sure all the requested past sampling periods can be stored.
"""
DEFAULT_BUFFER_LEN_MAX = 1024
"""Default maximum allowed buffer length.
If a buffer length would get bigger than this, it will be truncated to this
length.
"""
DEFAULT_BUFFER_LEN_WARN = 128
"""Default minimum buffer length that will produce a warning.
If a buffer length would get bigger than this, a warning will be logged.
"""
class ResamplingFunction(Protocol):
"""Combine multiple samples into a new one.
A resampling function produces a new sample based on a list of pre-existing
samples. It can do "upsampling" when the data rate of the `input_samples`
period is smaller than the `resampling_period`, or "downsampling" if it is
bigger.
In general, a resampling window is the same as the `resampling_period`, and
this function might receive input samples from multiple windows in the past to
enable extrapolation, but no samples from the future (so the timestamp of the
new sample that is going to be produced will always be bigger than the biggest
timestamp in the input data).
"""
def __call__(
self,
input_samples: Sequence[tuple[datetime, float]],
resampler_config: ResamplerConfig,
source_properties: SourceProperties,
/,
) -> float:
"""Call the resampling function.
Args:
input_samples: The sequence of pre-existing samples, where the first item is
the timestamp of the sample, and the second is the value of the sample.
The sequence must be non-empty.
resampler_config: The configuration of the resampler calling this
function.
source_properties: The properties of the source being resampled.
Returns:
The value of new sample produced after the resampling.
"""
... # pylint: disable=unnecessary-ellipsis
@dataclass(frozen=True)
class ResamplerConfig:
"""Resampler configuration."""
resampling_period: timedelta
"""The resampling period.
This is the time it passes between resampled data should be calculated.
It must be a positive time span.
"""
max_data_age_in_periods: float = 3.0
"""The maximum age a sample can have to be considered *relevant* for resampling.
Expressed in number of periods, where period is the `resampling_period`
if we are downsampling (resampling period bigger than the input period) or
the *input sampling period* if we are upsampling (input period bigger than
the resampling period).
It must be bigger than 1.0.
Example:
If `resampling_period` is 3 seconds, the input sampling period is
1 and `max_data_age_in_periods` is 2, then data older than 3*2
= 6 seconds will be discarded when creating a new sample and never
passed to the resampling function.
If `resampling_period` is 3 seconds, the input sampling period is
5 and `max_data_age_in_periods` is 2, then data older than 5*2
= 10 seconds will be discarded when creating a new sample and never
passed to the resampling function.
"""
resampling_function: ResamplingFunction = lambda samples, _, __: statistics.fmean(
s[1] for s in samples
)
"""The resampling function.
This function will be applied to the sequence of relevant samples at
a given time. The result of the function is what is sent as the resampled
value.
"""
closed: Literal["right", "left"] = "right"
"""Indicates which side of the resampling window is closed.
If "right", the resampling window includes samples with timestamps
equal to the end of the window, but not those equal to the start.
If "left", the resampling window includes samples with timestamps
equal to the start of the window, but not those equal to the end.
"""
label: Literal["start", "end"] = "end"
"""Indicates the timestamp label of the resampled data.
If "end", the timestamp of the resampled data corresponds to the end
of the resampling window. If "start", the timestamp corresponds to
the start of the resampling window.
"""
initial_buffer_len: int = DEFAULT_BUFFER_LEN_INIT
"""The initial length of the resampling buffer.
The buffer could grow or shrink depending on the source properties,
like sampling rate, to make sure all the requested past sampling periods
can be stored.
It must be at least 1 and at most `max_buffer_len`.
"""
warn_buffer_len: int = DEFAULT_BUFFER_LEN_WARN
"""The minimum length of the resampling buffer that will emit a warning.
If a buffer grows bigger than this value, it will emit a warning in the
logs, so buffers don't grow too big inadvertently.
It must be at least 1 and at most `max_buffer_len`.
"""
max_buffer_len: int = DEFAULT_BUFFER_LEN_MAX
"""The maximum length of the resampling buffer.
Buffers won't be allowed to grow beyond this point even if it would be
needed to keep all the requested past sampling periods. An error will be
emitted in the logs if the buffer length needs to be truncated to this
value.
It must be at bigger than `warn_buffer_len`.
"""
align_to: datetime | None = UNIX_EPOCH
"""The time to align the resampling period to.
The resampling period will be aligned to this time, so the first resampled
sample will be at the first multiple of `resampling_period` starting from
`align_to`. It must be an aware datetime and can be in the future too.
If `align_to` is `None`, the resampling period will be aligned to the
time the resampler is created.
"""
def __post_init__(self) -> None:
"""Check that config values are valid.
Raises:
ValueError: If any value is out of range.
"""
if self.resampling_period.total_seconds() < 0.0:
raise ValueError(
f"resampling_period ({self.resampling_period}) must be positive"
)
if self.max_data_age_in_periods < 1.0:
raise ValueError(
f"max_data_age_in_periods ({self.max_data_age_in_periods}) should be at least 1.0"
)
if self.warn_buffer_len < 1:
raise ValueError(
f"warn_buffer_len ({self.warn_buffer_len}) should be at least 1"
)
if self.max_buffer_len <= self.warn_buffer_len:
raise ValueError(
f"max_buffer_len ({self.max_buffer_len}) should "
f"be bigger than warn_buffer_len ({self.warn_buffer_len})"
)
if self.initial_buffer_len < 1:
raise ValueError(
f"initial_buffer_len ({self.initial_buffer_len}) should at least 1"
)
if self.initial_buffer_len > self.max_buffer_len:
raise ValueError(
f"initial_buffer_len ({self.initial_buffer_len}) is bigger "
f"than max_buffer_len ({self.max_buffer_len}), use a smaller "
"initial_buffer_len or a bigger max_buffer_len"
)
if self.initial_buffer_len > self.warn_buffer_len:
_logger.warning(
"initial_buffer_len (%s) is bigger than warn_buffer_len (%s)",
self.initial_buffer_len,
self.warn_buffer_len,
)
if self.align_to is not None and self.align_to.tzinfo is None:
raise ValueError(
f"align_to ({self.align_to}) should be a timezone aware datetime"
)
class ResamplingFunction2(Protocol):
"""Combine multiple samples into a new one.
A resampling function produces a new sample based on a list of pre-existing
samples. It can do "upsampling" when the data rate of the `input_samples`
period is smaller than the `resampling_period`, or "downsampling" if it is
bigger.
In general, a resampling window is the same as the `resampling_period`, and
this function might receive input samples from multiple windows in the past to
enable extrapolation, but no samples from the future (so the timestamp of the
new sample that is going to be produced will always be bigger than the biggest
timestamp in the input data).
"""
def __call__(
self,
input_samples: Sequence[tuple[datetime, float]],
resampler_config: ResamplerConfig | ResamplerConfig2,
source_properties: SourceProperties,
/,
) -> float:
"""Call the resampling function.
Args:
input_samples: The sequence of pre-existing samples, where the first item is
the timestamp of the sample, and the second is the value of the sample.
The sequence must be non-empty.
resampler_config: The configuration of the resampler calling this
function.
source_properties: The properties of the source being resampled.
Returns:
The value of new sample produced after the resampling.
"""
... # pylint: disable=unnecessary-ellipsis
@dataclass(frozen=True)
class ResamplerConfig2(ResamplerConfig):
"""Resampler configuration."""
resampling_period: timedelta
"""The resampling period.
This is the time it passes between resampled data should be calculated.
It must be a positive time span.
"""
max_data_age_in_periods: float = 3.0
"""The maximum age a sample can have to be considered *relevant* for resampling.
Expressed in number of periods, where period is the `resampling_period`
if we are downsampling (resampling period bigger than the input period) or
the *input sampling period* if we are upsampling (input period bigger than
the resampling period).
It must be bigger than 1.0.
Example:
If `resampling_period` is 3 seconds, the input sampling period is
1 and `max_data_age_in_periods` is 2, then data older than 3*2
= 6 seconds will be discarded when creating a new sample and never
passed to the resampling function.
If `resampling_period` is 3 seconds, the input sampling period is
5 and `max_data_age_in_periods` is 2, then data older than 5*2
= 10 seconds will be discarded when creating a new sample and never
passed to the resampling function.
"""
resampling_function: ResamplingFunction2 = lambda samples, _, __: statistics.fmean(
s[1] for s in samples
)
"""The resampling function.
This function will be applied to the sequence of relevant samples at
a given time. The result of the function is what is sent as the resampled
value.
"""
initial_buffer_len: int = DEFAULT_BUFFER_LEN_INIT
"""The initial length of the resampling buffer.
The buffer could grow or shrink depending on the source properties,
like sampling rate, to make sure all the requested past sampling periods
can be stored.
It must be at least 1 and at most `max_buffer_len`.
"""
warn_buffer_len: int = DEFAULT_BUFFER_LEN_WARN
"""The minimum length of the resampling buffer that will emit a warning.
If a buffer grows bigger than this value, it will emit a warning in the
logs, so buffers don't grow too big inadvertently.
It must be at least 1 and at most `max_buffer_len`.
"""
max_buffer_len: int = DEFAULT_BUFFER_LEN_MAX
"""The maximum length of the resampling buffer.
Buffers won't be allowed to grow beyond this point even if it would be
needed to keep all the requested past sampling periods. An error will be
emitted in the logs if the buffer length needs to be truncated to this
value.
It must be at bigger than `warn_buffer_len`.
"""
align_to: datetime | None = field(default=None, init=False)
"""Deprecated: Use timer_config.align_to instead."""
timer_config: WallClockTimerConfig | None = None
"""The custom configuration of the wall clock timer used to keep track of time.
If not provided or `None`, a configuration will be created by passing the
[`resampling_period`][frequenz.sdk.timeseries.ResamplerConfig2.resampling_period] to
the [`from_interval()`][frequenz.sdk.timeseries.WallClockTimerConfig.from_interval]
method.
"""
def __post_init__(self) -> None:
"""Check that config values are valid.
Raises:
ValueError: If any value is out of range.
"""
if self.resampling_period.total_seconds() < 0.0:
raise ValueError(
f"resampling_period ({self.resampling_period}) must be positive"
)
if self.max_data_age_in_periods < 1.0:
raise ValueError(
f"max_data_age_in_periods ({self.max_data_age_in_periods}) should be at least 1.0"
)
if self.warn_buffer_len < 1:
raise ValueError(
f"warn_buffer_len ({self.warn_buffer_len}) should be at least 1"
)
if self.max_buffer_len <= self.warn_buffer_len:
raise ValueError(
f"max_buffer_len ({self.max_buffer_len}) should "
f"be bigger than warn_buffer_len ({self.warn_buffer_len})"
)
if self.initial_buffer_len < 1:
raise ValueError(
f"initial_buffer_len ({self.initial_buffer_len}) should at least 1"
)
if self.initial_buffer_len > self.max_buffer_len:
raise ValueError(
f"initial_buffer_len ({self.initial_buffer_len}) is bigger "
f"than max_buffer_len ({self.max_buffer_len}), use a smaller "
"initial_buffer_len or a bigger max_buffer_len"
)
if self.initial_buffer_len > self.warn_buffer_len:
_logger.warning(
"initial_buffer_len (%s) is bigger than warn_buffer_len (%s)",
self.initial_buffer_len,
self.warn_buffer_len,
)
if self.align_to is not None:
raise ValueError(
f"align_to ({self.align_to}) must be specified via timer_config"
)