-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtw_plot.py
100 lines (76 loc) · 2.78 KB
/
dtw_plot.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial.distance import cdist
from tslearn import metrics
from utils import TIME_INTERVAL_CONFIG, load_dataset
def plot_soft_dtw_path(s_y1, s_y2, sz, type: str):
path, sim = metrics.soft_dtw_alignment(s_y1, s_y2, gamma=1)
plt.figure(1, figsize=(8, 8))
# definitions for the axes
left, bottom = 0.01, 0.1
w_ts = h_ts = 0.2
left_h = left + w_ts + 0.02
width = height = 0.65
bottom_h = bottom + height + 0.02
rect_s_y = [left, bottom, w_ts, height]
rect_gram = [left_h, bottom, width, height]
rect_s_x = [left_h, bottom_h, width, h_ts]
ax_gram = plt.axes(rect_gram)
ax_s_x = plt.axes(rect_s_x)
ax_s_y = plt.axes(rect_s_y)
mat = cdist(s_y1, s_y2)
ax_gram.imshow(path, origin="lower")
ax_gram.axis("off")
ax_gram.autoscale(False)
ax_s_x.plot(np.arange(sz), s_y2, "b-", linewidth=3.0)
ax_s_x.axis("off")
ax_s_x.set_xlim((0, sz - 1))
ax_s_y.plot(-s_y1, np.arange(sz), "b-", linewidth=3.0)
ax_s_y.axis("off")
ax_s_y.set_ylim((0, sz - 1))
plt.tight_layout()
plt.savefig(f"figs/{type}_soft_dtw_path_class.pdf")
plt.show()
plt.clf()
def plot_dtw_path(s_y1, s_y2, sz, type: str):
path, sim = metrics.dtw_path(
s_y1, s_y2
) # global_constraint="itakura", itakura_max_slope=2. # global_constraint="itakura", itakura_max_slope=2.
plt.figure(1, figsize=(8, 8))
# definitions for the axes
left, bottom = 0.01, 0.1
w_ts = h_ts = 0.2
left_h = left + w_ts + 0.02
width = height = 0.65
bottom_h = bottom + height + 0.02
rect_s_y = [left, bottom, w_ts, height]
rect_gram = [left_h, bottom, width, height]
rect_s_x = [left_h, bottom_h, width, h_ts]
ax_gram = plt.axes(rect_gram)
ax_s_x = plt.axes(rect_s_x)
ax_s_y = plt.axes(rect_s_y)
mat = cdist(s_y1, s_y2)
ax_gram.imshow(mat, origin="lower")
ax_gram.axis("off")
ax_gram.autoscale(False)
ax_gram.plot([j for (i, j) in path], [i for (i, j) in path], "w-", linewidth=3.0)
ax_s_x.plot(np.arange(sz), s_y2, "b-", linewidth=3.0)
ax_s_x.axis("off")
ax_s_x.set_xlim((0, sz - 1))
ax_s_y.plot(-s_y1, np.arange(sz), "b-", linewidth=3.0)
ax_s_y.axis("off")
ax_s_y.set_ylim((0, sz - 1))
plt.tight_layout()
plt.savefig(f"figs/{type}_dtw_path_class.pdf")
plt.show()
plt.clf()
if __name__ == "__main__":
for ti in TIME_INTERVAL_CONFIG:
X, y, _, _ = load_dataset(ti["time_interval_name"])
y1 = y[np.where(y == 1)[0]]
X1 = X[np.where(y == 1)[0]]
s_y1 = X1[1].reshape((-1, 1))
s_y2 = X1[0].reshape((-1, 1))
sz = s_y1.shape[0]
plot_dtw_path(s_y1, s_y2, sz, ti["time_interval_name"])
plot_soft_dtw_path(s_y1, s_y2, sz, ti["time_interval_name"])