@@ -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