Skip to content

Commit 17ca925

Browse files
committed
Update examples to have mock data, so that they are ready to run
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 7a7b946 commit 17ca925

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/frequenz/sdk/timeseries/_moving_window.py

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,37 @@ 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`
@@ -84,23 +97,35 @@ class MovingWindow:
8497
import polars as pl
8598
from datetime import datetime, timedelta, timezone
8699
87-
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)
88104
89-
# create a window that stores two days of data
90-
# starting at 1.1.23 with samplerate=1
91-
window = MovingWindow(
92-
size=timedelta(days=2),
93-
resampled_data_recv=sample_receiver,
94-
input_sampling_period=timedelta(seconds=1),
95-
)
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)
96122
97-
# wait for one full day until the buffer is filled
98-
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])
99127
100-
# create a polars series with one full day of data
101-
time_start = datetime(2023, 1, 1, tzinfo=timezone.utc)
102-
time_end = datetime(2023, 1, 2, tzinfo=timezone.utc)
103-
s = pl.Series("Jan_1", window[time_start:time_end])
128+
asyncio.run(run())
104129
```
105130
"""
106131

0 commit comments

Comments
 (0)