Skip to content

Commit ef3906f

Browse files
authored
Make moving window examples easier to run (frequenz-floss#418)
2 parents bed6720 + 17ca925 commit ef3906f

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ docs-lint = [
5858
]
5959
format = ["black == 23.3.0", "isort == 5.12.0"]
6060
nox = ["nox == 2023.4.22", "toml == 0.10.2"]
61+
examples = [ "polars == 0.18.0" ]
6162
pytest = [
6263
"pytest == 7.3.1",
6364
"pytest-cov == 4.1.0",
@@ -68,6 +69,7 @@ pytest = [
6869
# For checking docstring code examples
6970
"sybil == 5.0.2",
7071
"pylint == 2.17.4",
72+
"frequenz-sdk[examples]",
7173
]
7274
mypy = [
7375
"mypy == 1.3.0",

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,50 +58,74 @@ class MovingWindow:
5858
5959
```python
6060
from datetime import datetime, timedelta, timezone
61-
resampled_data_recv = Broadcast[Sample]("sample-data").new_receiver()
6261
63-
window = MovingWindow(
64-
size=timedelta(minutes=5),
65-
resampled_data_recv=resampled_data_recv,
66-
input_sampling_period=timedelta(seconds=1),
67-
)
62+
async def send_mock_data(sender: Sender[Sample]) -> None:
63+
while True:
64+
await sender.send(Sample(datetime.now(tz=timezone.utc), 10.0))
65+
await asyncio.sleep(1.0)
66+
67+
async def run() -> None:
68+
resampled_data_channel = Broadcast[Sample]("sample-data")
69+
resampled_data_receiver = resampled_data_channel.new_receiver()
70+
resampled_data_sender = resampled_data_channel.new_sender()
71+
72+
send_task = asyncio.create_task(send_mock_data(resampled_data_sender))
73+
74+
window = MovingWindow(
75+
size=timedelta(minutes=5),
76+
resampled_data_recv=resampled_data_receiver,
77+
input_sampling_period=timedelta(seconds=1),
78+
)
79+
80+
time_start = datetime.now(tz=timezone.utc)
81+
time_end = time_start + timedelta(minutes=5)
6882
69-
time_start = datetime.now(tz=timezone.utc)
70-
time_end = time_start + timedelta(minutes=5)
83+
# ... wait for 5 minutes until the buffer is filled
84+
await asyncio.sleep(5)
7185
72-
# ... wait for 5 minutes until the buffer is filled
73-
await asyncio.sleep(5)
86+
# return an numpy array from the window
87+
array = window[time_start:time_end]
88+
# and use it to for example calculate the mean
89+
mean = array.mean()
7490
75-
# return an numpy array from the window
76-
a = window[time_start:time_end]
77-
# and use it to for example calculate the mean
78-
mean = a.mean()
91+
asyncio.run(run())
7992
```
8093
8194
Example: Create a polars data frame from a `MovingWindow`
8295
8396
```python
84-
# pylint: disable=import-error
8597
import polars as pl
8698
from datetime import datetime, timedelta, timezone
8799
88-
sample_receiver = Broadcast[Sample]("sample-data").new_receiver()
100+
async def send_mock_data(sender: Sender[Sample]) -> None:
101+
while True:
102+
await sender.send(Sample(datetime.now(tz=timezone.utc), 10.0))
103+
await asyncio.sleep(1.0)
89104
90-
# create a window that stores two days of data
91-
# starting at 1.1.23 with samplerate=1
92-
window = MovingWindow(
93-
size=timedelta(days=2),
94-
resampled_data_recv=sample_receiver,
95-
input_sampling_period=timedelta(seconds=1),
96-
)
105+
async def run() -> None:
106+
resampled_data_channel = Broadcast[Sample]("sample-data")
107+
resampled_data_receiver = resampled_data_channel.new_receiver()
108+
resampled_data_sender = resampled_data_channel.new_sender()
109+
110+
send_task = asyncio.create_task(send_mock_data(resampled_data_sender))
111+
112+
# create a window that stores two days of data
113+
# starting at 1.1.23 with samplerate=1
114+
window = MovingWindow(
115+
size=timedelta(days=2),
116+
resampled_data_recv=resampled_data_receiver,
117+
input_sampling_period=timedelta(seconds=1),
118+
)
119+
120+
# wait for one full day until the buffer is filled
121+
asyncio.sleep(60*60*24)
97122
98-
# wait for one full day until the buffer is filled
99-
asyncio.sleep(60*60*24)
123+
# create a polars series with one full day of data
124+
time_start = datetime(2023, 1, 1, tzinfo=timezone.utc)
125+
time_end = datetime(2023, 1, 2, tzinfo=timezone.utc)
126+
series = pl.Series("Jan_1", window[time_start:time_end])
100127
101-
# create a polars series with one full day of data
102-
time_start = datetime(2023, 1, 1, tzinfo=timezone.utc)
103-
time_end = datetime(2023, 1, 2, tzinfo=timezone.utc)
104-
s = pl.Series("Jan_1", window[time_start:time_end])
128+
asyncio.run(run())
105129
```
106130
"""
107131

0 commit comments

Comments
 (0)