|
1 | | -import logging |
| 1 | +import random |
2 | 2 | from math import isnan, nan |
3 | 3 | from uuid import uuid4 as new_uuid |
4 | 4 |
|
@@ -451,3 +451,29 @@ def test_timesnap_forward_restricts_range(conn, tmp_collection): |
451 | 451 | assert [1.0, 2.0] == [v.as_py() for v in values[str(s.uuid)]] |
452 | 452 | # Same result if skipping past end instead of to end. |
453 | 453 | assert values == ss.filter(end=int(2.9 * sec)).arrow_values() |
| 454 | + |
| 455 | + |
| 456 | +@pytest.mark.parametrize("freq", [(None), (30), (15), (1), (0.1)]) |
| 457 | +def test_timesnap_with_different_sampling_frequencies(freq, conn, tmp_collection): |
| 458 | + # 30hz data |
| 459 | + data_insert_freq = 30 |
| 460 | + period_for_data = int(1 / data_insert_freq * 1e9) |
| 461 | + stop = btrdb.utils.timez.currently_as_ns() |
| 462 | + start = stop - btrdb.utils.timez.ns_delta(minutes=10) |
| 463 | + t1 = [i for i in range(start, stop - period_for_data, period_for_data)] |
| 464 | + v1 = [ |
| 465 | + random.random() for _ in range(start, stop - period_for_data, period_for_data) |
| 466 | + ] |
| 467 | + s1 = conn.create(new_uuid(), tmp_collection, tags={"name": "s1"}) |
| 468 | + s2 = conn.create(new_uuid(), tmp_collection, tags={"name": "s2"}) |
| 469 | + stset = btrdb.stream.StreamSet([s1, s2]) |
| 470 | + data_map = {s.uuid: pa.table([t1, v1], names=["time", "value"]) for s in stset} |
| 471 | + stset.arrow_insert(data_map=data_map) |
| 472 | + df = stset.filter( |
| 473 | + start=start, end=stop, sampling_frequency=freq |
| 474 | + ).arrow_to_dataframe() |
| 475 | + total_points = df.shape[0] * df.shape[1] |
| 476 | + total_raw_pts = len(v1) * len(stset) |
| 477 | + expected_frac_of_pts = 1 if freq is None else freq / data_insert_freq |
| 478 | + actual_frac_of_pts = total_points / total_raw_pts |
| 479 | + assert np.isclose(expected_frac_of_pts, actual_frac_of_pts, rtol=0.001) |
0 commit comments