Skip to content

Commit a278c6e

Browse files
authored
Make the resampler use timedelta for periods (#334)
As a step towards [using proper units], now the resampler uses `timedelta` for specifying periods. It also includes a couple of cleanup commits. - Move private method to the end of the class - Remove unused timer - Make the resampling period a `timedelta` - Make source properties' sampling period a `timedelta` [using proper units]: #6
2 parents 5ce3f7d + 98c602a commit a278c6e

File tree

15 files changed

+159
-135
lines changed

15 files changed

+159
-135
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
Notice that the parameter `sampling_period` has been renamed to `input_sampling_period`
1919
to better distinguish it from the sampling period parameter in the resampler.
2020
* The serialization feature for the ringbuffer was made more flexible. The `dump` and `load` methods can now work directly with a ringbuffer instance.
21+
* The `ResamplerConfig` now takes the resampling period as a `timedelta`. The configuration was renamed from `resampling_period_s` to `resampling_period` accordingly.
22+
* The `SourceProperties` of the resampler now uses a `timedelta` for the input sampling period. The attribute was renamed from `sampling_period_s` to `sampling_period` accordingly.
2123

2224
## New Features
2325

benchmarks/power_distribution/power_distributor.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import random
99
import timeit
1010
from dataclasses import dataclass
11+
from datetime import timedelta
1112
from typing import Any, Coroutine, Dict, List, Set # pylint: disable=unused-import
1213

1314
from frequenz.channels import Bidirectional, Broadcast
@@ -154,7 +155,9 @@ async def run() -> None:
154155
"""Create microgrid api and run tests."""
155156
# pylint: disable=protected-access
156157

157-
await microgrid.initialize(HOST, PORT, ResamplerConfig(resampling_period_s=1.0))
158+
await microgrid.initialize(
159+
HOST, PORT, ResamplerConfig(resampling_period=timedelta(seconds=1.0))
160+
)
158161

159162
all_batteries: Set[Component] = connection_manager.get().component_graph.components(
160163
component_category={ComponentCategory.BATTERY}

benchmarks/timeseries/resampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _benchmark_resampling_helper(resamples: int, samples: int) -> None:
2929
helper = _ResamplingHelper(
3030
"benchmark",
3131
ResamplerConfig(
32-
resampling_period_s=1.0,
32+
resampling_period=timedelta(seconds=1.0),
3333
max_data_age_in_periods=3.0,
3434
resampling_function=nop,
3535
initial_buffer_len=samples * 3,

examples/battery_pool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import asyncio
99
import logging
10+
from datetime import timedelta
1011
from typing import Any, Dict
1112

1213
from frequenz.channels import Receiver
@@ -25,7 +26,9 @@ async def main() -> None:
2526
level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s:%(message)s"
2627
)
2728
await microgrid.initialize(
28-
host=HOST, port=PORT, resampler_config=ResamplerConfig(resampling_period_s=1.0)
29+
host=HOST,
30+
port=PORT,
31+
resampler_config=ResamplerConfig(resampling_period=timedelta(seconds=1.0)),
2932
)
3033

3134
battery_pool = microgrid.battery_pool()

examples/battery_status.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import asyncio
1111
import logging
12+
from datetime import timedelta
1213

1314
from frequenz.channels import Broadcast
1415

@@ -31,7 +32,9 @@ async def main() -> None:
3132
logging.basicConfig(
3233
level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s:%(message)s"
3334
)
34-
await microgrid.initialize(HOST, PORT, ResamplerConfig(resampling_period_s=1.0))
35+
await microgrid.initialize(
36+
HOST, PORT, ResamplerConfig(resampling_period=timedelta(seconds=1.0))
37+
)
3538
batteries = {
3639
bat.component_id
3740
for bat in connection_manager.get().component_graph.components(

examples/power_distribution.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import asyncio
1414
import logging
15-
from datetime import datetime, timezone
15+
from datetime import datetime, timedelta, timezone
1616
from queue import Queue
1717
from typing import List, Optional, Set
1818

@@ -159,7 +159,9 @@ async def run() -> None:
159159
logging.basicConfig(
160160
level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s:%(message)s"
161161
)
162-
await microgrid.initialize(HOST, PORT, ResamplerConfig(resampling_period_s=1.0))
162+
await microgrid.initialize(
163+
HOST, PORT, ResamplerConfig(resampling_period=timedelta(seconds=1.0))
164+
)
163165

164166
channel_registry = ChannelRegistry(name="Microgrid Channel Registry")
165167

@@ -180,7 +182,7 @@ async def run() -> None:
180182
channel_registry=channel_registry,
181183
data_sourcing_request_sender=data_source_request_channel.new_sender(),
182184
resampling_request_receiver=resampling_actor_request_channel.new_receiver(),
183-
config=ResamplerConfig(resampling_period_s=1.0),
185+
config=ResamplerConfig(resampling_period=timedelta(seconds=1.0)),
184186
)
185187

186188
logical_meter = LogicalMeter(

examples/resampling.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""Frequenz Python SDK resampling example."""
55

66
import asyncio
7-
from datetime import datetime, timezone
7+
from datetime import datetime, timedelta, timezone
88

99
from frequenz.channels import Broadcast
1010
from frequenz.channels.util import Merge
@@ -43,7 +43,9 @@ async def _print_sample(sample: Sample) -> None:
4343

4444
async def run() -> None: # pylint: disable=too-many-locals
4545
"""Run main functions that initializes and creates everything."""
46-
await microgrid.initialize(HOST, PORT, ResamplerConfig(resampling_period_s=0.2))
46+
await microgrid.initialize(
47+
HOST, PORT, ResamplerConfig(resampling_period=timedelta(seconds=0.2))
48+
)
4749

4850
channel_registry = ChannelRegistry(name="data-registry")
4951

@@ -66,7 +68,7 @@ async def run() -> None: # pylint: disable=too-many-locals
6668
channel_registry=channel_registry,
6769
data_sourcing_request_sender=data_source_request_sender,
6870
resampling_request_receiver=resampling_request_receiver,
69-
config=ResamplerConfig(resampling_period_s=1),
71+
config=ResamplerConfig(resampling_period=timedelta(seconds=1)),
7072
)
7173

7274
components = await connection_manager.get().api_client.components()
@@ -105,7 +107,9 @@ async def run() -> None: # pylint: disable=too-many-locals
105107
# Create a channel to calculate an average for all the data
106108
average_chan = Broadcast[Sample]("average")
107109

108-
second_stage_resampler = Resampler(ResamplerConfig(resampling_period_s=3.0))
110+
second_stage_resampler = Resampler(
111+
ResamplerConfig(resampling_period=timedelta(seconds=3.0))
112+
)
109113
second_stage_resampler.add_timeseries(
110114
"avg", average_chan.new_receiver(), _print_sample
111115
)

src/frequenz/sdk/microgrid/_data_pipeline.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import typing
1414
from collections import abc
1515
from dataclasses import dataclass
16-
from datetime import timedelta
1716

1817
from frequenz.channels import Bidirectional, Broadcast, Sender
1918

@@ -175,9 +174,7 @@ def battery_pool(
175174
batteries_status_receiver=self._battery_status_channel.new_receiver(
176175
maxsize=1
177176
),
178-
min_update_interval=timedelta(
179-
seconds=self._resampler_config.resampling_period_s
180-
),
177+
min_update_interval=self._resampler_config.resampling_period,
181178
batteries_id=battery_ids,
182179
)
183180

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,12 @@ def __init__( # pylint: disable=too-many-arguments
142142
self._resampler_task: asyncio.Task[None] | None = None
143143

144144
if resampler_config:
145-
resampling_period = timedelta(seconds=resampler_config.resampling_period_s)
146145
assert (
147-
resampling_period <= size
146+
resampler_config.resampling_period <= size
148147
), "The resampling period should be equal to or lower than the window size."
149148

150149
self._resampler = Resampler(resampler_config)
151-
sampling = resampling_period
150+
sampling = resampler_config.resampling_period
152151

153152
# Sampling period might not fit perfectly into the window size.
154153
num_samples = math.ceil(size.total_seconds() / sampling.total_seconds())

0 commit comments

Comments
 (0)