-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.py
executable file
·174 lines (122 loc) · 4.71 KB
/
animate.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
170
171
172
173
174
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.patches import Circle
import time
import datetime
#Plot single frame
def plot_box(init_values, data, box_shape, N):
fig, ax = plt.subplots()
ax.set_xlim(0, box_shape[0])
ax.set_ylim(0, box_shape[1])
ax.set_aspect('equal')
ax.tick_params(
axis='both',
which='both',
bottom=False,
left=False,
labelbottom=False,
labelleft=False)
circles= []
for i in range(N):
colour = init_values.iloc[i]['colour']
rad = init_values.iloc[i]['rad']
pos_str = 'r' + str(i)
r = data.iloc[0][pos_str][0]
circle = Circle(xy=r, radius=rad, color=colour)
ax.add_patch(circle)
circles.append(circle)
plt.show(fig)
fig.savefig('Box.pdf', dps=1000)
plt.close(fig)
def draw_circle(ax, r, rad, colour):
circle = Circle(xy=r, radius=rad, color=colour)
ax.add_patch(circle)
return circle
def update_anim(frame, init_values, data, N, ax, t1, verbose, update_freq,
frames):
ax.clear()
circles = []
for i in range(N):
colour = init_values.iloc[i]['colour']
rad = init_values.iloc[i]['rad']
pos_str = 'r' + str(i)
r = data.iloc[frame][pos_str]
circle = Circle(xy=r, radius=rad, color=colour)
ax.add_patch(circle)
circles.append(circle)
# circles.append(draw_circle(ax, r, rad, colour))
if verbose:
frame_update = np.linspace(1, update_freq-1, update_freq-1)
frame_update *= frames//update_freq
if frame in frame_update:
t2 = time.time()
total_t = t2 - t1
long_time = str(datetime.timedelta(seconds=total_t))
print(f'Animation progress: {(100 * frame/frames):.1f}%. '
f'Current time taken for animation: {long_time}')
return circles
def animate(init_values, data, box_shape, frames, N, t1, verbose, update_freq,
network, frame_lims, run_id):
fig, ax = plt.subplots()
ax.set_xlim(0, box_shape[0])
ax.set_ylim(0, box_shape[1])
ax.set_aspect('equal')
ax.tick_params(
axis='both',
which='both',
bottom=False,
left=False,
labelbottom=False,
labelleft=False)
anim = FuncAnimation(fig, update_anim, frames=frames, interval=2,
blit=True, fargs=(init_values, data, N, ax, t1,
verbose, update_freq, frames))
if network:
gif_name = f'./gifs/{run_id}_net_{frame_lims[0]}-{frame_lims[1]}.gif'
else:
gif_name = f'./gifs/{run_id}_sim_{frame_lims[0]}-{frame_lims[1]}.gif'
anim.save(gif_name, writer='pillow', fps=60)
def crop_data(data, lims, tot_frames):
if (lims[0] is None) or (lims[0] < 0):
lims[0] = 0
if (lims[1] is None) or (lims[1] > tot_frames):
lims[1] = tot_frames
return data.iloc[lims[0]:lims[1]], lims, (lims[1] - lims[0])
def main():
run_id = '1'
verbose = True
update_freq = 5 # Number of progress updates e.g. every 20% for 5
network = True # Use data from network (alternatively original simulation)
frame_lims = [None, 300] # Crop animation frames (use None for start/end)
if network:
# Data from network_sim.py
data = pd.read_pickle('network_data/' + run_id + '/network_values.pkl')
init_values = pd.read_pickle('network_data/' + run_id + '/init_values.pkl')
const_df = pd.read_pickle('network_data/' + run_id + '/consts.pkl')
else:
# Data from simulation.py
data = pd.read_pickle('sim_data/' + run_id + '/sim_values.pkl')
init_values = pd.read_pickle('sim_data/' + run_id + '/init_values.pkl')
const_df = pd.read_pickle('sim_data/' + run_id + '/consts.pkl')
box_shape = const_df['box_shape'][0]
steps = const_df['steps'][0]
tot_frames = steps + 1
N = const_df['N'][0]
data, frame_lims, frames = crop_data(data, frame_lims, tot_frames)
t1 = time.time()
animate(init_values, data, box_shape, frames, N, t1, verbose, update_freq,
network, frame_lims, run_id)
t2 = time.time()
total_t = t2 - t1
if verbose:
if total_t > 60:
long_time = str(datetime.timedelta(seconds=total_t))
print(f'Time taken for animation: {long_time}')
else:
print(f'Time taken for animation: {total_t:.2f}s')
if __name__ == '__main__':
main()