Skip to content

Commit 1068725

Browse files
committed
Move default resampling function
The default resampling function, `average`, is moved from the actor to the `timeseries.resampling` module so the `Resampler` class can have a default too. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 39f789c commit 1068725

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/frequenz/sdk/actor/_resampling.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,26 @@
66
import asyncio
77
import dataclasses
88
import logging
9-
from typing import Sequence, Set
9+
from typing import Set
1010

1111
from frequenz.channels import Receiver, Sender
1212

1313
from frequenz.sdk.util.asyncio import cancel_and_await
1414

1515
from ..timeseries import Sample
16-
from ..timeseries.resampling import Resampler, ResamplingError, ResamplingFunction
16+
from ..timeseries.resampling import (
17+
Resampler,
18+
ResamplingError,
19+
ResamplingFunction,
20+
average,
21+
)
1722
from ._channel_registry import ChannelRegistry
1823
from ._data_sourcing import ComponentMetricRequest
1924
from ._decorator import actor
2025

2126
logger = logging.Logger(__name__)
2227

2328

24-
# pylint: disable=unused-argument
25-
def average(samples: Sequence[Sample], resampling_period_s: float) -> float:
26-
"""Calculate average of all the provided values.
27-
28-
Args:
29-
samples: The samples to apply the average to. It must be non-empty.
30-
resampling_period_s: The time it passes between resampled data is
31-
produced (in seconds).
32-
33-
Returns:
34-
The average of all `samples` values.
35-
"""
36-
assert len(samples) > 0, "Average cannot be given an empty list of samples"
37-
values = list(sample.value for sample in samples if sample.value is not None)
38-
return sum(values) / len(values)
39-
40-
4129
@actor
4230
class ComponentMetricsResamplingActor:
4331
"""An actor to resample microgrid component metrics."""

src/frequenz/sdk/timeseries/resampling.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242
"""
4343

4444

45+
# pylint: disable=unused-argument
46+
def average(samples: Sequence[Sample], resampling_period_s: float) -> float:
47+
"""Calculate average of all the provided values.
48+
49+
Args:
50+
samples: The samples to apply the average to. It must be non-empty.
51+
resampling_period_s: The time it passes between resampled data is
52+
produced (in seconds).
53+
54+
Returns:
55+
The average of all `samples` values.
56+
"""
57+
assert len(samples) > 0, "Average cannot be given an empty list of samples"
58+
values = list(sample.value for sample in samples if sample.value is not None)
59+
return sum(values) / len(values)
60+
61+
4562
class SourceStoppedError(RuntimeError):
4663
"""A timeseries stopped producing samples."""
4764

@@ -116,8 +133,8 @@ class Resampler:
116133
timeseries to produce `Sample`s at regular periodic intervals.
117134
118135
This class uses
119-
a [`ResamplingFunction`][frequenz.sdk.timeseries.ResamplingFunction] to
120-
produce a new sample from samples received in the past. If there are no
136+
a [`ResamplingFunction`][frequenz.sdk.timeseries.resampling.ResamplingFunction]
137+
to produce a new sample from samples received in the past. If there are no
121138
samples coming to a resampled timeseries for a while, eventually the
122139
`Resampler` will produce `Sample`s with `None` as value, meaning there is
123140
no way to produce meaningful samples with the available data.
@@ -127,7 +144,7 @@ def __init__(
127144
self,
128145
*,
129146
resampling_period_s: float,
130-
resampling_function: ResamplingFunction,
147+
resampling_function: ResamplingFunction = average,
131148
max_data_age_in_periods: float = 3.0,
132149
) -> None:
133150
"""Initialize an instance.

0 commit comments

Comments
 (0)