|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import logging |
9 | | -from collections.abc import Callable, Sequence |
| 9 | +from collections.abc import Sequence |
10 | 10 | from dataclasses import dataclass |
11 | 11 | from datetime import datetime, timedelta |
| 12 | +from typing import Protocol |
12 | 13 |
|
13 | 14 | from frequenz.quantities import Quantity |
14 | 15 |
|
|
42 | 43 | """ |
43 | 44 |
|
44 | 45 |
|
45 | | -ResamplingFunction = Callable[ |
46 | | - [Sequence[Sample[Quantity]], "ResamplerConfig", "SourceProperties"], float |
47 | | -] |
48 | | -"""Resampling function type. |
| 46 | +class ResamplingFunction(Protocol): |
| 47 | + """Combine multiple samples into a new one. |
49 | 48 |
|
50 | | -A resampling function produces a new sample based on a list of pre-existing |
51 | | -samples. It can do "upsampling" when the data rate of the `input_samples` |
52 | | -period is smaller than the `resampling_period`, or "downsampling" if it is |
53 | | -bigger. |
| 49 | + A resampling function produces a new sample based on a list of pre-existing |
| 50 | + samples. It can do "upsampling" when the data rate of the `input_samples` |
| 51 | + period is smaller than the `resampling_period`, or "downsampling" if it is |
| 52 | + bigger. |
54 | 53 |
|
55 | | -In general a resampling window is the same as the `resampling_period`, and |
56 | | -this function might receive input samples from multiple windows in the past to |
57 | | -enable extrapolation, but no samples from the future (so the timestamp of the |
58 | | -new sample that is going to be produced will always be bigger than the biggest |
59 | | -timestamp in the input data). |
60 | | -
|
61 | | -Args: |
62 | | - input_samples (Sequence[Sample]): The sequence of pre-existing samples. |
63 | | - resampler_config (ResamplerConfig): The configuration of the resampling |
64 | | - calling this function. |
65 | | - source_properties (SourceProperties): The properties of the source being |
66 | | - resampled. |
| 54 | + In general, a resampling window is the same as the `resampling_period`, and |
| 55 | + this function might receive input samples from multiple windows in the past to |
| 56 | + enable extrapolation, but no samples from the future (so the timestamp of the |
| 57 | + new sample that is going to be produced will always be bigger than the biggest |
| 58 | + timestamp in the input data). |
| 59 | + """ |
67 | 60 |
|
68 | | -Returns: |
69 | | - new_sample (float): The value of new sample produced after the resampling. |
70 | | -""" |
| 61 | + def __call__( |
| 62 | + self, |
| 63 | + input_samples: Sequence[Sample[Quantity]], |
| 64 | + resampler_config: ResamplerConfig, |
| 65 | + source_properties: SourceProperties, |
| 66 | + /, |
| 67 | + ) -> float: |
| 68 | + """Call the resampling function. |
| 69 | +
|
| 70 | + Args: |
| 71 | + input_samples: The sequence of pre-existing samples. It must be |
| 72 | + non-empty. |
| 73 | + resampler_config: The configuration of the resampler calling this |
| 74 | + function. |
| 75 | + source_properties: The properties of the source being resampled. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + The value of new sample produced after the resampling. |
| 79 | + """ |
| 80 | + ... # pylint: disable=unnecessary-ellipsis |
71 | 81 |
|
72 | 82 |
|
73 | 83 | # pylint: disable=unused-argument |
|
0 commit comments