Skip to content

Commit 3ea962c

Browse files
authored
Add integration tests to check for misbehaving sampling frequencies. (#53)
* Add integration tests to check for misbehaving sampling frequencies. * fix int conversion of float.
1 parent 78046dc commit 3ea962c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

btrdb/stream.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ def filter(
17081708
key/value pairs for filtering streams based on tags
17091709
annotations : dict
17101710
key/value pairs for filtering streams based on annotations
1711-
sampling_frequency : int
1711+
sampling_frequency : float
17121712
The sampling frequency of the data streams in Hz, set this if you want timesnapped values.
17131713
17141714
Returns
@@ -2274,9 +2274,7 @@ def __init__(
22742274
):
22752275
self.start = to_nanoseconds(start) if start else None
22762276
self.end = to_nanoseconds(end) if end else None
2277-
self.sampling_frequency = (
2278-
int(sampling_frequency) if sampling_frequency else None
2279-
)
2277+
self.sampling_frequency = sampling_frequency if sampling_frequency else None
22802278

22812279
if self.start is None and self.end is None:
22822280
raise BTRDBValueError("A valid `start` or `end` must be supplied")

tests/btrdb_integration/test_streamset.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import logging
1+
import random
22
from math import isnan, nan
33
from uuid import uuid4 as new_uuid
44

@@ -451,3 +451,29 @@ def test_timesnap_forward_restricts_range(conn, tmp_collection):
451451
assert [1.0, 2.0] == [v.as_py() for v in values[str(s.uuid)]]
452452
# Same result if skipping past end instead of to end.
453453
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

Comments
 (0)