My Project
run_animation_layout.py
Go to the documentation of this file.
1 from run_animation_layout_support_functions import *
2 
3 
4 # pos = position, vel = velocity, pha = phase, bar = bar plot
5 
7  run_name,
8  shown_indexes,
9  shift_amount_per_frame = 1,
10  frames = 500,
11  live_plotting = True
12 ):
13  if("--friction" in sys.argv):
14  phase_data = load_friction_phase_data(run_name, "0")
15  else:
16  phase_data = load_phase_data(run_name)
17  position_data, velocity_data = seperate_pos_vel_data(phase_data)
18 
19  position_plot = make_single_line_line_plot(position_data, shown_indexes, ax_number = 0)
20  velocity_plot = make_single_line_line_plot(velocity_data, shown_indexes, ax_number = 1)
21  phase_plot = make_single_line_phase_plot(position_data, velocity_data, shown_indexes, ax_number = 2)
22  bar_plot = make_single_line_bar_plot(position_plot, ax_number = 3, line_number = 0)
23  spring_plot = make_single_line_spring_plot(position_plot, ax_number = 1, line_number = 0)
24  animation_plots = [position_plot, velocity_plot, phase_plot, bar_plot]
25 
26  bk_animation = BKAnimation(animation_plots, sleep_amount=0.001, max_lim_change=0.3)
27  bk_animation.init_live_plot((221, 222, 223, 224), (20,10), ["Position", "Velocity", "Phase", "Bar"])
28  bk_animation.update_subplots(update_lims=False)
29 
30 
31  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
32  def plot_frames(n = 0):
33 
34  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 0, n_line_number = plot_shape[1], index_type = 'y')
35  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 2, n_line_number = plot_shape[1], index_type = 'xy')
36  #bk_animation.change_x(1, 0, get_axis="y")
37  bk_animation.change_y(3, 0)
38  if(n % 10 == 0):
39  bk_animation.check_lims(0, "y")
40  bk_animation.check_lims(1, "y")
41  bk_animation.check_lims(2, "x")
42  bk_animation.check_lims(2, "y")
43  else:
44  bk_animation.update_subplots()
45  sleep(bk_animation.sleep_amount)
46 
47 
48  if(live_plotting):
49  for i in range(0, frames):
50  plot_frames(i)
51  else:
52  ani = animation.FuncAnimation(bk_animation.fig, plot_frames,500,interval=30)
53  writer = animation.writers['ffmpeg'](fps=20)
54 
55  simulation_path = loader.get_simulation_path(run_name)
56  ani.save(simulation_path + '/animation.mp4',writer=writer,dpi=100)
57 
58 def pos_pha_spr(
59  run_name,
60  shown_indexes,
61  shift_amount_per_frame = 1,
62  frames = 500,
63  live_plotting = True,
64  sample_mode = False,
65  sample_range = [0,2*100]
66 ):
67  if(sample_mode):
68  if(not ppc.check_if_all_phase_data_exists(run_name)):
69  print(bcolors.WARNING + "Making phase date since it did not exist." + bcolors.ENDC)
70  ppc.create_phase_data(run_name)
71  phase_data_block = load_phase_data(run_name, "_block")[sample_range[0]:sample_range[1]]
72  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
73  else:
74  phase_data_block = load_phase_data(run_name)
75  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
76  phase_data_pad = load_phase_data(run_name, "_pad")
77  print(phase_data_pad)
78  position_data_pad = phase_data_pad[0]
79 
80 
81 
82  position_plot = make_single_line_line_plot(position_data, shown_indexes, ax_number = 0)
83  bar_plot = make_single_line_bar_plot(position_plot, ax_number = 1, line_number = 0)
84  phase_plot = make_single_line_phase_plot(position_data, velocity_data, shown_indexes, ax_number = 2)
85  spring_plot = make_single_line_spring_plot(position_plot, ax_number = 3, line_number = 0, pad_position = position_data_pad, common_indexes=shown_indexes)
86  animation_plots = [position_plot, bar_plot, phase_plot, spring_plot]
87 
88  bk_animation = BKAnimation(animation_plots, sleep_amount=0.001, max_lim_change=0.3)
89  bk_animation.init_live_plot((231, 232, 233, 212), (20,10), ["Position", "Bar", "Phase", "Spring"])
90  bk_animation.update_subplots(update_lims=False)
91 
92 
93  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
94  def plot_frames(n = 0):
95 
96  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 0, n_line_number = plot_shape[1], index_type = 'y')
97  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 2, n_line_number = plot_shape[1], index_type = 'xy')
98  bk_animation.shift_x_indexes(ax_number = 3, line_number = 2, shift_amount = shift_amount_per_frame)
99  bk_animation.change_y(1, 0)
100  bk_animation.change_x(3, 0, get_axis="y")
101  bk_animation.change_x(3, 1)
102  bk_animation.update_upper_springs(3, index_first_spring = 3)
103  if(n % 10 == 0):
104  print("hei")
105  # bk_animation.check_lims(0, "y")
106  # bk_animation.check_lims(1, "x")
107  # bk_animation.check_lims(1, "y")
108  bk_animation.update_subplots()
109  sleep(bk_animation.sleep_amount)
110 
111 
112  if(live_plotting):
113  for i in range(0, frames):
114  plot_frames(i)
115  else:
116  ani = animation.FuncAnimation(bk_animation.fig, plot_frames, frames = frames,interval=30)
117  writer = animation.writers['ffmpeg'](fps=20)
118 
119  simulation_path = loader.get_simulation_path(run_name)
120  ani.save(simulation_path + '/animation_vid_7.mp4',writer=writer,dpi=100)
121 
122 def pos_pos_spr(
123  run_name,
124  shown_indexes,
125  shift_amount_per_frame = 1,
126  frames = 500,
127  live_plotting = True,
128  sample_mode = False,
129  sample_range = [0,2*100]
130 ):
131  if(sample_mode):
132  if(not ppc.check_if_all_phase_data_exists(run_name)):
133  print(bcolors.WARNING + "Making phase date since it did not exist." + bcolors.ENDC)
134  ppc.create_phase_data(run_name)
135  phase_data_block = load_phase_data(run_name, "_block")[sample_range[0]:sample_range[1]]
136  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
137  else:
138  phase_data_block = load_phase_data(run_name)
139  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
140  phase_data_pad = load_phase_data(run_name, "_pad")
141  print(phase_data_pad)
142  position_data_pad = phase_data_pad[0]
143 
144 
145 
146  position_plot = make_single_line_line_plot(position_data, shown_indexes, ax_number = 0)
147  bar_plot = make_single_line_bar_plot(position_plot, ax_number = 1, line_number = 0)
148  position_pad_plot = make_single_line_line_plot([position_data_pad], shown_indexes, ax_number = 2)#, x_values = np.arange(len(position_data_pad)))
149  spring_plot = make_single_line_spring_plot(position_plot, ax_number = 3, line_number = 0, pad_position = position_data_pad, common_indexes=shown_indexes)
150  animation_plots = [position_plot, bar_plot, position_pad_plot, spring_plot]
151 
152  bk_animation = BKAnimation(animation_plots, sleep_amount=0.001, max_lim_change=0.3)
153  bk_animation.init_live_plot((231, 232, 233, 212), (20,10), ["Position", "Bar", "Position pad", "Spring"])
154  bk_animation.update_subplots(update_lims=False)
155 
156 
157  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
158  def plot_frames(n = 0):
159 
160  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 0, n_line_number = plot_shape[1], index_type = 'y')
161  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 2, n_line_number = 1, index_type = 'y')
162  bk_animation.shift_x_indexes(ax_number = 3, line_number = 2, shift_amount = shift_amount_per_frame)
163  bk_animation.change_y(1, 0)
164  bk_animation.change_x(3, 0, get_axis="y")
165  bk_animation.change_x(3, 1)
166  bk_animation.update_upper_springs(3, index_first_spring = 3)
167  bk_animation.update_subplots()
168  sleep(bk_animation.sleep_amount)
169 
170 
171  if(live_plotting):
172  for i in range(0, frames):
173  plot_frames(i)
174  else:
175  ani = animation.FuncAnimation(bk_animation.fig, plot_frames, frames = frames,interval=30)
176  writer = animation.writers['ffmpeg'](fps=20)
177 
178  simulation_path = loader.get_simulation_path(run_name)
179  ani.save(simulation_path + '/animation_vid_bk.mp4',writer=writer,dpi=100)
180 
182  run_name,
183  shown_indexes,
184  shift_amount_per_frame = 1,
185  frames =3699,
186  live_plotting = True,
187  sample_range = [0,2*100],
188  velocity_index = 8
189 ):
190  phase_data_block = load_friction_phase_data(run_name, "block_phase_" + str(velocity_index))
191  frames_per_velocity = phase_data_block.shape[1]
192  velocities = loadYaml(run_name, "/results/friction_phase/increment_run.yaml")["velocities"]
193  for i in range(velocity_index + 1, len(velocities)):
194  phase_data_block = np.concatenate((phase_data_block, load_friction_phase_data(run_name, "block_phase_" + str(i))), axis=1)
195  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
196 
197  number_of_data_points = len(position_data[0])
198  if(frames > number_of_data_points):
199  frames = number_of_data_points
200 
201  position_plot = make_single_line_line_plot(position_data, shown_indexes, ax_number = 0)
202  bar_plot = make_single_line_bar_plot(position_plot, ax_number = 1, line_number = 0)
203  phase_plot = make_single_line_phase_plot(position_data, velocity_data, shown_indexes, ax_number = 2)
204  spring_plot = make_single_line_spring_plot(position_plot, ax_number = 3, line_number = 0, common_indexes=shown_indexes)
205  animation_plots = [position_plot, bar_plot, phase_plot, spring_plot]
206 
207  bk_animation = BKAnimation(animation_plots, sleep_amount=0.000, max_lim_change=0.3)
208  bk_animation.init_live_plot((231, 232, 233, 212), (20,10), ["Position", "Bar", "Phase", "Spring"], show_plot = live_plotting, suptitle="Velocity: " + str(velocities[velocity_index]))
209 
210 
211  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
212  only_shift_first = False # used for data points after the remaining data points are fewer than the showed ones
213  only_shift_first_index = number_of_data_points - shown_indexes[1] - 1 # 0 indexation
214  def init_plot_frames():
215  bk_animation.update_subplots(update_lims=False)
216  def plot_frames(n):
217  nonlocal velocity_index, only_shift_first
218  bk_animation.update_suptitle("Velocity: %(velocity)1.3f | Frame: %(frame)i" % {'velocity': velocities[velocity_index], 'frame': n})
219  if(n % frames_per_velocity == 0 and n > 0):
220  velocity_index += 1
221 
222  if(n % only_shift_first_index == 0 and n > 0):
223  only_shift_first = True
224  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 0, n_line_number = plot_shape[1], index_type = 'y', only_shift_first = only_shift_first)
225  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 2, n_line_number = plot_shape[1], index_type = 'xy', only_shift_first = only_shift_first)
226  #bk_animation.shift_x_indexes(ax_number = 3, line_number = 2, shift_amount = shift_amount_per_frame)
227  bk_animation.change_y(1, 0)
228  bk_animation.change_x(3, 0, get_axis="y")
229  #bk_animation.change_x(3, 1)
230  bk_animation.update_upper_springs(3, index_first_spring = 1)
231 
232  bk_animation.update_subplots()
233  sleep(bk_animation.sleep_amount)
234 
235 
236  if(live_plotting):
237  init_plot_frames()
238  for i in range(0, frames):
239  plot_frames(i)
240  else:
241  ani = animation.FuncAnimation(bk_animation.fig, plot_frames, init_func = init_plot_frames, frames = frames,interval=30)
242  writer = animation.writers['ffmpeg'](fps=20)
243 
244  simulation_path = loader.get_simulation_path(run_name)
245  ani.save(simulation_path + '/animation_vid_7.mp4',writer=writer,dpi=100)
246 
248  run_name,
249  shown_indexes,
250  shift_amount_per_frame = 1,
251  frames =3699,
252  live_plotting = True,
253  sample_range = [0,2*100],
254  velocity_index = 14
255 ):
256  phase_data_block = load_friction_phase_data(run_name, "block_phase_" + str(velocity_index))
257  phase_data_pad = load_friction_phase_data(run_name, "pad_phase_" + str(velocity_index))
258  frames_per_velocity = phase_data_block.shape[1]
259  velocities = loadYaml(run_name, "/results/friction_phase/increment_run.yaml")["velocities"]
260  for i in range(velocity_index + 1, len(velocities)):
261  phase_data_block = np.concatenate((phase_data_block, load_friction_phase_data(run_name, "block_phase_" + str(i))), axis=1)
262  phase_data_pad = np.concatenate((phase_data_pad, load_friction_phase_data(run_name, "pad_phase_" + str(i))), axis=1)
263 
264  position_data, velocity_data = seperate_pos_vel_data(phase_data_block)
265  if("--nopad" in sys.argv):
266  phase_shape_block = phase_data_block.shape
267  block_average = np.zeros(phase_shape_block[1])
268  number_of_blocks = int(phase_shape_block[0]/2)
269  for i in range(0, number_of_blocks):
270  block_average += phase_data_block[2*i]
271  position_data_pad = block_average/number_of_blocks
272 
273  position_data_pad_title = "Block avg. position"
274  position_data_pad_y_label = "$\\frac{\sum_1^n u_i}{n}$"
275  show_pad = False
276  index_first_spring = 1
277  else:
278  position_data_pad = phase_data_pad[0]
279  position_data_pad_title = "Pad position"
280  position_data_pad_y_label = "$x$"
281  show_pad = True
282  index_first_spring = 3
283 
284 
285  number_of_data_points = len(position_data[0])
286  if(frames > number_of_data_points):
287  frames = number_of_data_points
288 
289  position_plot = make_single_line_line_plot(position_data, shown_indexes, ax_number = 0, y_min = -3.0, y_max = 2.0, x_label="Time steps shown", y_label="$u_i$")
290  bar_plot = make_single_line_bar_plot(position_plot, ax_number = 1, line_number = 0, x_label="Block", y_label="$u_i$")
291  position_pad_plot = make_single_line_line_plot([position_data_pad], shown_indexes, ax_number = 2, y_min = -3.0, y_max = 2.0, x_label="Time steps shown", y_label=position_data_pad_y_label)#, x_values = np.arange(len(position_data_pad)))
292  spring_plot = make_single_line_spring_plot(position_plot, ax_number = 3, line_number = 0, common_indexes=shown_indexes, pad_position=position_data_pad, x_label="$x$", y_label="Model", show_pad = show_pad)
293  animation_plots = [position_plot, bar_plot, position_pad_plot, spring_plot]
294 
295  bk_animation = BKAnimation(animation_plots, sleep_amount=0.000, max_lim_change=0.3)
296  bk_animation.init_live_plot((231, 232, 233, 212), (20,10), ["Position", "Bar", position_data_pad_title, "Spring"], show_plot = live_plotting, suptitle="Velocity: " + str(velocities[velocity_index]))
297 
298 
299  plot_shape = [len(animation_plots), len(position_plot.single_line_list)]
300  only_shift_first = False # used for data points after the remaining data points are fewer than the showed ones
301  only_shift_first_index = number_of_data_points - shown_indexes[1] - 1 # 0 indexation
302  def init_plot_frames():
303  bk_animation.update_subplots(update_lims=False)
304  def plot_frames(n):
305  nonlocal velocity_index, only_shift_first
306  bk_animation.update_suptitle("Velocity: %(velocity)1.3f | Frame: %(frame)i" % {'velocity': velocities[velocity_index], 'frame': n})
307  if(n % frames_per_velocity == 0 and n > 0):
308  velocity_index += 1
309 
310  if(n % only_shift_first_index == 0 and n > 0):
311  only_shift_first = True
312  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 0, n_line_number = plot_shape[1], index_type = 'y', only_shift_first = only_shift_first)
313  shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number = 2, n_line_number = 1, index_type = 'y', only_shift_first = only_shift_first)
314  #bk_animation.shift_x_indexes(ax_number = 3, line_number = 2, shift_amount = shift_amount_per_frame)
315  bk_animation.change_y(1, 0)
316  bk_animation.change_x(3, 0, get_axis="y")
317  if(show_pad):
318  bk_animation.change_x(3, 1)
319  bk_animation.update_upper_springs(3, index_first_spring = index_first_spring)
320 
321  bk_animation.update_subplots()
322  sleep(bk_animation.sleep_amount)
323 
324 
325  if(live_plotting):
326  init_plot_frames()
327  for i in range(0, frames):
328  plot_frames(i)
329  # if((i+1)%10 == 0):
330  # input()
331  else:
332  ani = animation.FuncAnimation(bk_animation.fig, plot_frames, init_func = init_plot_frames, frames = frames,interval=30)
333  writer = animation.writers['ffmpeg'](fps=20)
334 
335  simulation_path = loader.get_simulation_path(run_name)
336  ani.save(simulation_path + '/animation_vid_7.mp4',writer=writer,dpi=100)
337 
338 if __name__ == "__main__":
339  shown_indexes = [0, 100]
340  if(len(sys.argv) >= 2):
341  live_plotting = False
342  sample_mode = False
343  if('-l' in sys.argv):
344  live_plotting = True
345  if('-d' in sys.argv):
346  sample_mode = True
347  #pos_vel_pha_bar(sys.argv[1], shown_indexes, live_plotting=live_plotting)
348  if('--friction' in sys.argv):
349  #pos_pha_spr_friction(sys.argv[1], shown_indexes, live_plotting=live_plotting)
350  pos_pos_spr_friction(sys.argv[1], shown_indexes, live_plotting=live_plotting)
351  else:
352  pos_pos_spr(sys.argv[1], shown_indexes, live_plotting=live_plotting, sample_mode=sample_mode)
def pos_pha_spr_friction(run_name, shown_indexes, shift_amount_per_frame=1, frames=3699, live_plotting=True, sample_range=[0, velocity_index=8)
def make_single_line_spring_plot(single_plot, ax_number, line_number, pad_position=[], common_indexes=[], height_of_plot=0.1, x_label="", y_label="", show_pad=False)
def make_single_line_bar_plot(single_plot, ax_number, line_number, x_label="", y_label="")
def pos_vel_pha_bar(run_name, shown_indexes, shift_amount_per_frame=1, frames=500, live_plotting=True)
def pos_pos_spr(run_name, shown_indexes, shift_amount_per_frame=1, frames=500, live_plotting=True, sample_mode=False, sample_range=[0)
def pos_pos_spr_friction(run_name, shown_indexes, shift_amount_per_frame=1, frames=3699, live_plotting=True, sample_range=[0, velocity_index=14)
def make_single_line_phase_plot(y_data, x_data, common_indexes, ax_number, x_label="", y_label="")
def make_single_line_line_plot(y_data, common_indexes, ax_number, x_values=[], y_min=0, y_max=0, x_label="", y_label="")
def pos_pha_spr(run_name, shown_indexes, shift_amount_per_frame=1, frames=500, live_plotting=True, sample_mode=False, sample_range=[0)
def shift_z_indexes_of_lines_in_ax(bk_animation, shift_amount_per_frame, ax_number, n_line_number, index_type, only_shift_first=False)