My Project
exampels_animation.py
Go to the documentation of this file.
1 from animation_run_imports import *
2 
4  x, #data
5  a, #amplitude
6  f, #ordinary frequency
7  p #phase
8 ):
9  return a * np.sin(np.pi * f * x + p)
10 
11 def new_test():
12  amplitude = 1
13  frequency = 0.01
14  phase = 1
15 
16  common_indexes = [0, 100]
17  ax_numbers = [0, 1, 2, 3]
18  shift_amount_per_frame = 3
19 
20  x = np.arange(8000.0)
21  sin_line_1 = create_sine_wave(x, amplitude, frequency, phase)
22  sin_line_2 = create_sine_wave(x, - amplitude, frequency, 2*phase)
23 
24  y_lim = {'min': -amplitude, 'max': amplitude}
25  x_lim = {'min': common_indexes[0], 'max': common_indexes[1]}
26 
27  single_line_1 = SingleLine(x, sin_line_1, starting_position={"x":0.5, "y": 0.0}, x_indexes = common_indexes, y_indexes = common_indexes, ax_number = ax_numbers[0], line_number = 0, marker = "x", marker_size=5.0)
28  single_line_2 = SingleLine(x, sin_line_2, starting_position={"x":0.0, "y": 0.0}, x_indexes = common_indexes, y_indexes = common_indexes, ax_number = ax_numbers[0], line_number = 1, marker = "^", marker_size=5.0)
29  single_line_3 = SingleLine(sin_line_1, sin_line_2, x_indexes = common_indexes, y_indexes = common_indexes, ax_number = ax_numbers[1], line_number = 0)
30 
31  single_plot_1 = SinglePlot(ax_numbers[0], y_lim=y_lim, x_lim=x_lim, single_line_list=[single_line_1, single_line_2])
32  #single_plot_1.append_single_line(1)
33 
34  single_plot_2 = SinglePlot(ax_numbers[1], y_lim=y_lim, x_lim=y_lim)
35  single_plot_2.append_single_line(single_line_3)
36 
37 
38  bar_values = np.array([single_plot_1.get_last_plotted_y_value(0), single_plot_1.get_last_plotted_y_value(1)])
39  bar_position = np.array([0, 1])
40  bar_lim = {'min': -0.5, 'max': 1.5}
41  line_dependencies = {'ax_numbers': [0], 'line_numbers': [[0,1]]}
42  single_line_bar = SingleLine(bar_position, bar_values, x_indexes=bar_position, y_indexes=bar_position, ax_number = ax_numbers[2], line_number = 0, line_dependencies=line_dependencies)
43  single_plot_3 = SinglePlot(ax_numbers[2], y_lim=y_lim, x_lim=bar_lim, single_line_list=[single_line_bar], plot_type = 'bar')
44 
45  single_line_spring = SingleLine(bar_values, \
46  bar_position, \
47  starting_position={"x":np.array([0.1, 0.0]), "y": 0.0}, \
48  x_indexes=bar_position, \
49  y_indexes=bar_position, \
50  ax_number = ax_numbers[3],
51  line_number = 0, \
52  line_dependencies=line_dependencies, \
53  marker = "s", marker_size = 10.0)
54  single_plot_4 = SinglePlot(ax_numbers[3], \
55  y_lim=bar_lim, \
56  x_lim=y_lim, \
57  single_line_list=[single_line_spring], \
58  plot_type = 'spring')
59 
60  bk_animation = BKAnimation([single_plot_1, single_plot_2, single_plot_3, single_plot_4])
61 
62  single_line_bar.print_all()
63  sleep(1)
64  bk_animation.init_live_plot((223, 221, 222, 224), (20,20), ["Position", "Phase", "Velocity", "Spring"])
65 
66  for i in range(0, 500):
67  bk_animation.shift_y_indexes(ax_numbers[0], 0, shift_amount_per_frame)
68  bk_animation.shift_y_indexes(ax_numbers[0], 1, shift_amount_per_frame)
69  bk_animation.shift_xy_indexes(ax_numbers[1], 0, shift_amount_per_frame)
70  bk_animation.change_y(ax_numbers[2], 0)
71  bk_animation.change_x(ax_numbers[3], 0, get_axis="y")
72 
73  bk_animation.update_subplots()
74  sleep(0.02)
75  print("NEW FRAME \n\n")
76 
78  y_values,
79  ax_number,
80  line_number,
81  common_indexes,
82  x_values = [],
83  line_dependencies = None
84 ):
85  if(len(x_values) == 0):
86  x_values = np.arange(len(y_values))
87  single_line = SingleLine(x_values, y_values, x_indexes = common_indexes, y_indexes = common_indexes, ax_number = ax_number, line_number = line_number, line_dependencies=line_dependencies)
88  return single_line
89 
90 
92  single_lines,
93  ax_number,
94  x_lim,
95  y_lim,
96  plot_type = 'line'
97 ):
98  single_plot = SinglePlot(ax_number, y_lim=y_lim, x_lim=x_lim, single_line_list=single_lines, plot_type=plot_type)
99  return single_plot
100 
101 def plot_blocks(run_name, number_of_blocks = 2, common_indexes = [0, 20], frames = 500, live_plotting = True):
102  parameters = loader.load_yaml_parameters(run_name)
103  phase_data = loader.load_simulation_output(parameters, run_name, "phase_plot_at_threshold", use_shared_names = False)
104  print(phase_data.shape)
105 
106 
107  y_lim_position = {'min': np.min(phase_data[number_of_blocks * 2]), 'max': np.max(phase_data[number_of_blocks * 2])}
108  y_lim_velocity = {'min': np.min(phase_data[number_of_blocks * 2 +1]), 'max': np.max(phase_data[number_of_blocks * 2+1])}
109  #y_lim_position = {'min': -0.7, 'max': -0.6}
110  x_lim = {'min': common_indexes[0], 'max': common_indexes[1]}
111  shift_amount_per_frame = 1
112 
113  position_lines = []
114  velocity_lines = []
115  for i in range(0, number_of_blocks):
116  position_lines.append(make_block_single_line(phase_data[2*i], 0, i, common_indexes))
117  velocity_lines.append(make_block_single_line(phase_data[2*i + 1], 3, i, common_indexes))
118  position_plot = make_block_single_plot(position_lines, 0, x_lim, y_lim_position)
119  velocity_plot = make_block_single_plot(velocity_lines, 3, x_lim, y_lim_velocity)
120 
121  phase_line = make_block_single_line(position_lines[0].y_values, 1, 0, common_indexes, x_values=velocity_lines[0].y_values)
122  phase_line_2 = make_block_single_line(position_lines[1].y_values, 1, 1, common_indexes, x_values=velocity_lines[1].y_values)
123  phase_plot = make_block_single_plot([phase_line, phase_line_2], 1, y_lim_velocity, y_lim_position)#{'min': -0.67, 'max': -0.645})
124 
125  bar_values = np.array([position_plot.get_last_plotted_y_value(0), position_plot.get_last_plotted_y_value(1)])
126  bar_position = np.array([0, 1,])
127  bar_lim = {'min': -0.5, 'max': 1.5}
128  line_dependencies = {'ax_numbers': [0], 'line_numbers': [[0,1]]}
129  bar_line = make_block_single_line(bar_values, 2, 0, bar_position, x_values=bar_position, line_dependencies=line_dependencies)
130  bar_plot = make_block_single_plot([bar_line], 2, x_lim=bar_lim, y_lim=y_lim_position, plot_type = 'bar')
131 
132  bk_animation = BKAnimation([position_plot, phase_plot, bar_plot, velocity_plot], sleep_amount=0.01, max_lim_change=0.5)
133  bk_animation.init_live_plot((221, 222, 224, 223), (20,10), ["Position", "Phase", "Position bar", "Velocity"])
134  print(bk_animation.get_last_plotted_y_value(2, 0))
135  sleep(0.5)
136  bk_animation.update_subplots(update_lims=True)
137  bk_animation.check_lims(1, 'x')
138  def plot_frames(n = 0):
139  bk_animation.check_lims(1, "x")
140  bk_animation.check_lims(1, "y")
141  bk_animation.shift_y_indexes(0, 0, shift_amount_per_frame)
142  bk_animation.shift_y_indexes(0, 1, shift_amount_per_frame)
143  bk_animation.shift_y_indexes(3, 0, shift_amount_per_frame)
144  bk_animation.shift_y_indexes(3, 1, shift_amount_per_frame)
145  bk_animation.shift_xy_indexes(1, 0, shift_amount_per_frame)
146  bk_animation.shift_xy_indexes(1, 1, shift_amount_per_frame)
147  print(bk_animation.get_last_plotted_y_value(2, 0))
148  bk_animation.change_y(2, 0)
149  bk_animation.update_subplots()
150  sleep(bk_animation.sleep_amount)
151 
152  if(live_plotting):
153  for i in range(0, frames):
154  plot_frames()
155  else:
156  ani = animation.FuncAnimation(bk_animation.fig, plot_frames,300,interval=30)
157  writer = animation.writers['ffmpeg'](fps=30)
158 
159  ani.save('demo.mp4',writer=writer,dpi=100)
160 
161 if __name__ == "__main__":
162  if('-demo' in sys.argv):
163  new_test()
164  elif(len(sys.argv) == 2):
165  plot_blocks(sys.argv[1])
def create_sine_wave(x, a, f, p)
def plot_blocks(run_name, number_of_blocks=2, common_indexes=[0, frames=500, live_plotting=True)
def make_block_single_line(y_values, ax_number, line_number, common_indexes, x_values=[], line_dependencies=None)
def make_block_single_plot(single_lines, ax_number, x_lim, y_lim, plot_type='line')