-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.py
169 lines (129 loc) · 5.71 KB
/
visualization.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from scipy.interpolate import interp1d
from matplotlib.animation import FuncAnimation
from abc import abstractmethod
# matplotlib config
mpl.rcParams['axes.formatter.useoffset'] = False
def interpolate(array, new_size):
func = interp1d(np.linspace(0, 1, len(array)), array, axis=0)
return func(np.linspace(0, 1, new_size))
class AnimatableData:
@abstractmethod
def set_frame(self, i):
pass
@abstractmethod
def set_num_frames(self, num_frames):
pass
class AnimatableKSegmentData(AnimatableData):
# list of history of coordinates
def __init__(self, ax: plt.Axes, points_data, name: str, fix_scale: bool = True):
self.cur_line, = ax.plot([], [], 'r')
self.points_data = np.array(points_data)
mnx = self.points_data[:, :, 0].min()
mxx = self.points_data[:, :, 0].max()
mny = self.points_data[:, :, 1].min()
mxy = self.points_data[:, :, 1].max()
ax.legend([name])
ax.set_xlim(mnx, mxx)
ax.set_ylim(mny, mxy)
if fix_scale:
x_range = mxx - mnx
y_range = mxy - mny
ax.set_box_aspect(y_range / x_range)
def set_num_frames(self, num_frames):
self.points_data = interpolate(self.points_data, num_frames)
def set_frame(self, i):
self.cur_line.set_data(*self.points_data[i].swapaxes(0, 1))
return self.cur_line,
class AnimatableSegmentData(AnimatableData):
def __init__(self, ax: plt.Axes, p1_data, p2_data, name: str, fix_scale: bool = True):
self.cur_line, = ax.plot([], [], 'r')
self.p1_data = np.array(p1_data)
self.p2_data = np.array(p2_data)
mnx = min(*self.p1_data[:, 0], *self.p2_data[:, 0])
mxx = max(*self.p1_data[:, 0], *self.p2_data[:, 0])
mny = min(*self.p1_data[:, 1], *self.p2_data[:, 1])
mxy = max(*self.p1_data[:, 1], *self.p2_data[:, 1])
ax.legend([name])
ax.set_xlim(mnx, mxx)
ax.set_ylim(mny, mxy)
if fix_scale:
x_range = mxx - mnx
y_range = mxy - mny
ax.set_box_aspect(y_range / x_range)
def set_num_frames(self, num_frames):
self.p1_data = np.concatenate([
interpolate(self.p1_data[:, 0], num_frames)[:, None],
interpolate(self.p1_data[:, 1], num_frames)[:, None]], axis=1)
self.p2_data = np.concatenate([
interpolate(self.p2_data[:, 0], num_frames)[:, None],
interpolate(self.p2_data[:, 1], num_frames)[:, None]], axis=1)
def set_frame(self, i):
self.cur_line.set_data([self.p1_data[i, 0], self.p2_data[i, 0]],
[self.p1_data[i, 1], self.p2_data[i, 1]])
return self.cur_line,
class AnimatablePointData(AnimatableData):
def __init__(self, ax: plt.Axes, x_data, y_data, name: str, fix_scale: bool = True):
self.hist_line, = ax.plot([], [], 'g-')
self.cur_line, = ax.plot([], [], 'ro')
self.x_data = x_data
self.y_data = y_data
ax.legend([name + "_hist", name + "_curr"])
ax.set_xlim(min(x_data), max(x_data))
ax.set_ylim(min(y_data), max(y_data))
if fix_scale:
x_range = max(x_data) - min(x_data)
y_range = max(y_data) - min(y_data)
ax.set_box_aspect(y_range / x_range)
def set_num_frames(self, num_frames):
self.x_data = interpolate(self.x_data, num_frames)
self.y_data = interpolate(self.y_data, num_frames)
def set_frame(self, i):
self.hist_line.set_data(self.x_data[:i+1], self.y_data[:i+1])
self.cur_line.set_data([self.x_data[i]], [self.y_data[i]])
return self.cur_line, self.hist_line
class AnimatableLinePlot(AnimatableData):
def __init__(self, ax: plt.Axes, x_data, y_data, name: str, fix_scale: bool, plot_config="r"):
self.line, = ax.plot([], [], plot_config)
self.x_data = x_data
self.y_data = y_data
ax.legend([name])
ax.set_xlim(min(x_data), max(x_data))
ax.set_ylim(min(y_data), max(y_data))
if fix_scale:
x_range = max(x_data) - min(x_data)
y_range = max(y_data) - min(y_data)
ax.set_box_aspect(y_range / x_range)
def set_num_frames(self, num_frames):
self.x_data = interpolate(self.x_data, num_frames)
self.y_data = interpolate(self.y_data, num_frames)
def set_frame(self, i):
self.line.set_data(self.x_data[:i+1], self.y_data[:i+1])
return self.line,
class Animator:
def __init__(self, fig, interval, total_time, animatable_datas: ["AnimatableData"], speed=1):
self.fig = fig
self.animatable_datas = animatable_datas
self.interval = interval
self.num_frames = round((total_time/speed) * 1000 / interval)
for animatable in self.animatable_datas:
animatable.set_num_frames(self.num_frames)
def animate(self, i):
animators = []
for animatable in self.animatable_datas:
animators.extend(animatable.set_frame(i))
return tuple(animators)
if __name__ == "__main__":
# sample use-case
fig, ax = plt.subplots()
obj1 = AnimatableLinePlot(ax, x_data=[1, 2, 3, 4, 5], y_data=[5, 1, 2, 4, 10], name="random", fix_scale=True,
plot_config="r-")
obj2 = AnimatablePointData(ax, x_data=[4, 10, 1, 3, 2], y_data=[5, 1, 2, 4, 10], name="ball", fix_scale=True)
animator = Animator(fig=fig, interval=30, total_time=1, animatable_datas=[obj1])
anim = FuncAnimation(animator.fig,
animator.animate,
frames=animator.num_frames,
interval=animator.interval)
plt.show()