-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupininterface.py
73 lines (50 loc) · 2.06 KB
/
dupininterface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import _dupin
import dupin as du
import ruptures as rpt
import time
from dupin.detect.costs import CostLinearFit
def compute_python_cost_matrix(signal: np.ndarray, min_size: int = 3):
cost = CostLinearFit(metric = "l2")
cost.fit(signal)
n_samples = signal.shape[0]
pycost_matrix = np.zeros((n_samples, n_samples))
for start in range(n_samples):
for end in range(start + min_size, n_samples):
pycost_matrix[start, end] = cost.error(start, end)
return pycost_matrix
def compute_cplus_cost_matrix(signal: np.ndarray):
algo = _dupin.DupinAlgo()
algo.num_bkps = 1;
algo.num_timesteps = signal.shape[0]
algo.num_parameters = signal.shape[1]
algo.datum = signal.tolist()
start1 = time.time()
algo.initialize_cost_matrix(algo.datum)
topcppbkps = algo.getTopDownBreakpoints()
end1 = time.time()
cpptime = end1-start1
print("cpptime: ", cpptime)
return np.array(algo.cost_matrix), topcppbkps
def generate_data():
data = (np.repeat([0, 8, 16, 24, 100], 240) + np.random.random(1200)).reshape((-1, 3))
return data
def test_dupin():
data = generate_data()
print(f"{data=}")
cppcost_matrix, topcppbkps = compute_cplus_cost_matrix(data)
pycost_matrix = compute_python_cost_matrix(data)
print ("diff: ", pycost_matrix - cppcost_matrix)
printdiff = np.allclose(cppcost_matrix, pycost_matrix, atol=1e-1, rtol= 1e-2)
print("Are the arrays the same?: ", printdiff)
start2 = time.time()
dynp = rpt.Dynp(custom_cost = du.detect.CostLinearFit("l2"), jump=1)
sweepsweep = du.detect.SweepDetector(dynp, 10)
pybkps = sweepsweep.fit(data)
end2 = time.time()
pythontime= end2-start2
print("pythontime: ", pythontime)
print(f"python breakpoints: {sweepsweep.change_points_} opt change points: {sweepsweep.opt_change_points_}")
print("Top Down C++ breakpoints:", topcppbkps)
if __name__ == "__main__":
test_dupin()