Skip to content

Commit 779e905

Browse files
EliEli
authored andcommitted
Revise and pass coarsen tests.
1 parent e8850db commit 779e905

3 files changed

Lines changed: 399 additions & 0 deletions

File tree

tests/test_coarsen.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import numpy as np
2+
import pandas as pd
3+
from vtools import ts_coarsen
4+
import pytest
5+
6+
def test_basic_grid_quantize_and_preserve():
7+
# We need the input to extend past 00:06 so resample("1min") produces 00:06:00.
8+
# Construct a preserved run (0.0) that exits at 00:06.
9+
t = pd.to_datetime([
10+
"2024-01-01 00:00:10", # non-preserved
11+
"2024-01-01 00:01:10", # enter preserved (0.0)
12+
"2024-01-01 00:05:50", # still preserved right before exit
13+
"2024-01-01 00:06:10", # exit preserved (nonzero)
14+
])
15+
df = pd.DataFrame({"x": [5.0, 0.0, 0.0, 5.0]}, index=t)
16+
17+
out = ts_coarsen(
18+
df,
19+
grid="1min",
20+
qwidth=0.1,
21+
preserve_vals=(0.0,),
22+
heartbeat_freq=None,
23+
hyst=0.5,
24+
)
25+
26+
# grid-aligned
27+
assert (out.index.second == 0).all()
28+
29+
# the pre-exit minute must exist (last preserved tick before leaving)
30+
assert pd.Timestamp("2024-01-01 00:05:00") in out.index
31+
32+
# and we should see something at/after the exit
33+
assert (out.index >= pd.Timestamp("2024-01-01 00:06:00")).any()
34+
35+
36+
37+
def test_no_preserve_means_simple_thin():
38+
t = pd.date_range("2020-01-01", periods=5, freq="1min")
39+
df = pd.DataFrame({"x": [1, 1, 1, 1, 1]}, index=t)
40+
41+
out = ts_coarsen(df, grid="1min", preserve_vals=(), heartbeat_freq=None)
42+
assert len(out) == 1
43+
44+
45+
def test_heartbeat_optional():
46+
t = pd.date_range("2020-01-01", periods=10, freq="10min")
47+
df = pd.DataFrame({"x": np.ones(len(t))}, index=t)
48+
49+
out = ts_coarsen(df, grid=None, heartbeat_freq="30min")
50+
assert len(out) > 1
51+
52+
out2 = ts_coarsen(df, grid=None, heartbeat_freq=None)
53+
assert len(out2) == 1
54+
55+
56+
57+
def test_use_original_vals_false_disallowed_with_grid():
58+
df = pd.DataFrame(
59+
{"x": [0.0, 0.02, 0.04]},
60+
index=pd.to_datetime(
61+
["2024-01-01 00:00:00", "2024-01-01 00:00:30", "2024-01-01 00:01:00"]
62+
),
63+
)
64+
65+
with pytest.raises(ValueError, match="use_original_vals=False is not supported"):
66+
ts_coarsen(
67+
df,
68+
grid="1min",
69+
qwidth=0.05,
70+
use_original_vals=False,
71+
heartbeat_freq=None,
72+
hyst=0.5,
73+
)

vtools/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@
3838
from vtools.functions.lag_cross_correlation import *
3939
from vtools.functions.envelope import *
4040
from vtools.functions.neighbor_fill import fill_from_neighbor,dfm_pack_params,save_dfm_params,load_dfm_params
41+
from vtools.functions.coarsen import ts_coarsen

0 commit comments

Comments
 (0)