Skip to content

Commit 96aa37f

Browse files
Test resampling in MovingWindow
Signed-off-by: Daniel Zullo <[email protected]>
1 parent 2d103d1 commit 96aa37f

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

tests/timeseries/test_moving_window.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55

66
import asyncio
77
from datetime import datetime, timedelta, timezone
8-
from typing import Sequence, Tuple
8+
from typing import Iterator, Sequence, Tuple
99

10+
import async_solipsism
1011
import numpy as np
12+
import pytest
13+
import time_machine
1114
from frequenz.channels import Broadcast, Sender
1215

1316
from frequenz.sdk.timeseries import Sample
1417
from frequenz.sdk.timeseries._moving_window import MovingWindow
18+
from frequenz.sdk.timeseries._resampling import ResamplerConfig
19+
20+
21+
# Setting 'autouse' has no effect as this method replaces the event loop for all tests in the file.
22+
@pytest.fixture()
23+
def event_loop() -> Iterator[async_solipsism.EventLoop]:
24+
"""Replace the loop with one that doesn't interact with the outside world."""
25+
loop = async_solipsism.EventLoop()
26+
yield loop
27+
loop.close()
28+
29+
30+
@pytest.fixture
31+
def fake_time() -> Iterator[time_machine.Coordinates]:
32+
"""Replace real time with a time machine that doesn't automatically tick."""
33+
with time_machine.travel(0, tick=False) as traveller:
34+
yield traveller
1535

1636

1737
async def push_lm_data(sender: Sender[Sample], test_seq: Sequence[float]) -> None:
@@ -75,3 +95,36 @@ async def test_access_window_by_ts_slice() -> None:
7595
time_start = datetime(2023, 1, 1, tzinfo=timezone.utc) + timedelta(seconds=3)
7696
time_end = time_start + timedelta(seconds=2)
7797
assert np.array_equal(window[time_start:time_end], np.array([3.0, 4.0])) # type: ignore
98+
99+
100+
# pylint: disable=redefined-outer-name
101+
async def test_resampling_window(fake_time: time_machine.Coordinates) -> None:
102+
"""Test resampling in MovingWindow."""
103+
channel = Broadcast[Sample]("net_power")
104+
sender = channel.new_sender()
105+
106+
window_size = timedelta(seconds=16)
107+
input_sampling = timedelta(seconds=1)
108+
output_sampling = timedelta(seconds=2)
109+
resampler_config = ResamplerConfig(
110+
resampling_period_s=output_sampling.total_seconds()
111+
)
112+
113+
window = MovingWindow(
114+
size=window_size,
115+
resampled_data_recv=channel.new_receiver(),
116+
input_sampling_period=input_sampling,
117+
resampler_config=resampler_config,
118+
)
119+
120+
stream_values = [4.0, 8.0, 2.0, 6.0, 5.0] * 100
121+
for value in stream_values:
122+
timestamp = datetime.now(tz=timezone.utc)
123+
sample = Sample(timestamp, float(value))
124+
await sender.send(sample)
125+
await asyncio.sleep(0.1)
126+
fake_time.shift(0.1)
127+
128+
assert len(window) == window_size / max(input_sampling, output_sampling)
129+
for value in window: # type: ignore
130+
assert 4.9 < value < 5.1

0 commit comments

Comments
 (0)