My Project
phase_plot_main.py
Go to the documentation of this file.
1 from phase_plot_imports import *
2 
3 from matplotlib.widgets import Slider
4 
5 
6 
8  parameters
9 ):
10  threshold_speed = loader.get_parameter(parameters, "threshold_speed")
11  speeds = loader.get_start_end_speed(parameters)
12  max_time = loader.get_parameter(parameters, "max_time")
13  save_interval = loader.get_parameter(parameters, "save_interval_dt")
14  dt = loader.get_parameter(parameters, "dt")
15  steps_in_time_unit = 1 / (dt * save_interval)
16  n_steps = np.floor(max_time * steps_in_time_unit)
17 
18  threshold_speed_start_index = int(np.ceil((threshold_speed - speeds['start_speed']) \
19  / (speeds['end_speed'] - speeds['start_speed']) \
20  * n_steps))
21  return {'threshold_speed': threshold_speed, \
22  'threshold_speed_start_index': threshold_speed_start_index, \
23  'start_speed': speeds['start_speed'], \
24  'end_speed': speeds['end_speed'], \
25  'n_steps': n_steps, \
26  'steps_in_time_unit': steps_in_time_unit}
27 
29  output_to_user
30 ):
31  response_from_user = input(output_to_user)
32  if(response_from_user == "yes"):
33  return True
34  else:
35  return False
36 
37 def save_phase_data(
38  position_selection,
39  velocity_selection,
40  run_name,
41  file_name = "/results/phase_plot_at_threshold_"
42 ):
43  write_to_file = True
44  if(loader.check_if_file_exist_in_run_folder(run_name, file_name) and not "-yes" in sys.argv):
45  write_to_file = bool_interaction_with_user("Phase file exists, to overwrite type\"yes\" or anything elso to skip:")
46  if(write_to_file):
47  if("-block" in sys.argv and len(position_selection) > 1):
48  save_matrix =[]
49  for i in range(0, len(position_selection)):
50  save_matrix.append(position_selection[i])
51  save_matrix.append(velocity_selection[i])
52  save_matrix = np.array(save_matrix)
53  file_name += "block.csv"
54  else:
55  save_matrix = np.array([position_selection, velocity_selection])
56  file_name += "pad.csv"
57  loader.save_csv(run_name, file_name, save_matrix)
58  else:
59  print("Skipped printing to file.")
60 
62  start_index,
63  threshold_dictionary,
64  time_units_from_start
65 ):
66  indexes_from_start = np.floor(time_units_from_start * threshold_dictionary['steps_in_time_unit'])
67  max_indexes_from_start = threshold_dictionary['n_steps'] - threshold_dictionary['threshold_speed_start_index']
68  if(indexes_from_start > max_indexes_from_start):
69  print("Selected units greater than the size of the run. Using maximum amount of units.")
70  print("Selected: ", time_units_from_start)
71  print("Maximum: ", max_indexes_from_start / threshold_dictionary['steps_in_time_unit'])
72  indexes_from_start = max_indexes_from_start
73  return int(indexes_from_start)
74 
75 def make_indexes(
76  start_index,
77  threshold_dictionary,
78  time_units_from_start = 1000
79 ):
80  start_index = start_index + make_indexes_within_interval(start_index, threshold_dictionary, 50)
81  max_indexes_from_start = threshold_dictionary['n_steps'] - threshold_dictionary['threshold_speed_start_index']
82  if(start_index == max_indexes_from_start):
83  print("Can't have the start index be equal to the end index.")
84  quit()
85  end_index = start_index + make_indexes_within_interval(start_index, threshold_dictionary, time_units_from_start)
86  print(start_index, end_index)
87  #start_index += 5000
88  indexes = range(start_index,end_index)
89  return indexes
90 
92  run_name
93 ):
94  if(loader.check_if_file_exist_in_run_folder(run_name, \
95  "/results/phase_plot_at_threshold.csv")):
96  path_in_results_folder = run_name + "/results/phase_plot_at_threshold.csv"
97  print("Found phase plot at: ", path_in_results_folder)
98 
99  phase_data = loader.load_file(loader.results_path + \
100  path_in_results_folder)
101  if("-block" in sys.argv):
102  position_selection = []
103  velocity_selection = []
104  for i in range(0, int(len(phase_data)/2)):
105  position_selection.append(phase_data[2*i])
106  velocity_selection.append(phase_data[2*i+1])
107  else:
108  position_selection = phase_data[0]
109  velocity_selection = phase_data[1]
110  parameters = loader.load_yaml_parameters(run_name)
111  return {'position_selection': position_selection, 'velocity_selection': velocity_selection, 'parameters': parameters}
112  else:
113  print("Sorry, the phase plot does not exist for this run.\n \
114  Remove the \"-load\"-flag to produce it.")
115  exit()
116 
118  run_name
119 ):
120  parameters = loader.load_yaml_parameters(run_name)
121  threshold_dictionary = get_threshold_speed(parameters)
122  start_index = threshold_dictionary['threshold_speed_start_index']
123  indexes = make_indexes(start_index, threshold_dictionary, 500)
124  if("-block" in sys.argv):
125  position = loader.load_simulation_output(parameters, run_name, "block_position")
126  velocity = loader.load_simulation_output(parameters, run_name, "block_velocity")
127  else:
128  position = loader.load_simulation_output(parameters, run_name, "pad_position")
129  velocity = loader.load_simulation_output(parameters, run_name, "pad_velocity")
130 
131  if(len(position.shape) == 1):
132  position_selection = position[indexes]
133  velocity_selection = velocity[indexes]
134  else:
135  position_selection = position[:, indexes]
136  velocity_selection = velocity[:, indexes]
137 
138  save_phase_data(position_selection, velocity_selection, run_name)
139 
140  return {'position_selection': position_selection, 'velocity_selection': velocity_selection, 'parameters': parameters}
141 
142 
144  single_position,
145  single_velocity,
146  color_number,
147  figure_number = 1,
148  custom_label = "",
149  custom_legend_title = ""
150 ):
151  color_map = plt.cm.Set1
152  plt.figure(figure_number)
153  plot_a, = plt.plot(single_position, single_velocity, color = color_map(color_number), label = "Phase plot" + custom_label)
154  plt.legend(title = custom_legend_title)
155  plt.xlabel("position")
156  plt.ylabel("velocity")
157  plt.figure(figure_number + 1)
158  plt.plot(single_position, color = color_map(color_number), label = "Position plot" + custom_label)
159  plt.plot(single_velocity, color = color_map(color_number), linestyle = "--", label = "Velocity plot" + custom_label)
160  plt.legend(title = custom_legend_title)
161  plt.xlabel("position/velocity")
162  plt.ylabel("surface velocity")
163  return plot_a
164 
165 def plot_phase(
166  position,
167  velocity,
168  figure_number,
169  custom_label = "",
170  custom_legend_title = "",
171  block_numbers = None
172 ):
173  if("-block" in sys.argv):
174  print(len(position))
175  print(block_numbers)
176  max_position_amplitude = []
177  max_velocity_amplitude = []
178  for counter, (position, velocity) in enumerate(zip(position, velocity)):
179  #plot_position_velocity(position, velocity, counter, 2 * figure_number, custom_label = custom_label + ". Block " + str(block_numbers[counter]), custom_legend_title=custom_legend_title)
180  if("-bar" in sys.argv):
181  max_position_amplitude.append(np.max(position))
182  max_velocity_amplitude.append(np.max(velocity))
183  if("-bar" in sys.argv):
184  plt.figure(100)
185  plt.bar(block_numbers, max_position_amplitude, align = "edge", width = 0.5, label = "Position")
186  plt.bar(block_numbers, max_velocity_amplitude, align = "edge", width = -0.5, label = "Velocity")
187  plt.legend(title = "Max amplitues")
188  else:
189  plot_position_velocity(position, velocity, 0, 1, custom_label = custom_label)
190 
191 
193  position,
194  velocity,
195  section_size,
196  custom_label = "",
197  custom_legend_title = "",
198  block_numbers = None
199 ):
200  n_elements = len(position)
201  n_sections = int(np.floor(n_elements/section_size))
202  rest = np.mod(n_elements, section_size)
203  for i in range(0, n_sections):
204  start = i*section_size
205  end = (i+1)*section_size
206  section = range(start, end)
207  plot_phase(position[section],\
208  velocity[section],\
209  i,\
210  custom_label = custom_label,\
211  custom_legend_title=custom_legend_title,\
212  block_numbers = block_numbers[start:end])
213  if(rest > 0):
214  section = range(-rest, 0)
215  print(start, end)
216  plot_phase(position[section],\
217  velocity[section],\
218  n_sections,\
219  custom_label = custom_label,\
220  custom_legend_title=custom_legend_title,\
221  block_numbers = block_numbers[-rest:])
222 
223 
225  position,
226  velocity,
227  custom_label = "",
228  custom_legend_title = "",
229  block_numbers = None
230 ):
231  plot_number = 0
232  plot_a = plot_position_velocity(position[plot_number], velocity[plot_number], 0, 0, \
233  custom_label = custom_label + ". Block " + str(block_numbers[0]), \
234  custom_legend_title=custom_legend_title)
235 
236  def update_section_frame(
237  val
238  ):
239  frame_number = int(np.floor(slider_frame.val))
240  # plot_a = plot_position_velocity(position[frame_number], velocity[frame_number], 0, 0, \
241  # custom_label = custom_label + ". Block " + str(block_numbers[frame_number]), \
242  # custom_legend_title=custom_legend_title)
243  plt.figure(0)
244  plot_a.set_ydata = velocity[frame_number]
245  plt.draw()
246 
247  frame = plt.axes([0.25, 0.1, 0.65, 0.03])
248  slider_frame = Slider(frame, 'Frame', 0, len(position), valinit = 0, valfmt='%d')
249  slider_frame.on_changed(update_section_frame)
250  plt.show()
251 
252 
253 
254 
255 
def get_threshold_speed(parameters)
def make_indexes_within_interval(start_index, threshold_dictionary, time_units_from_start)
def create_and_get_phase_data(run_name)
def plot_phase_intervals(position, velocity, section_size, custom_label="", custom_legend_title="", block_numbers=None)
def plot_position_velocity(single_position, single_velocity, color_number, figure_number=1, custom_label="", custom_legend_title="")
def plot_one_phase_at_the_time(position, velocity, custom_label="", custom_legend_title="", block_numbers=None)
def get_saved_phase_data(run_name)
def save_phase_data(position_selection, velocity_selection, run_name, file_name="/results/phase_plot_at_threshold_")
def make_indexes(start_index, threshold_dictionary, time_units_from_start=1000)
def bool_interaction_with_user(output_to_user)
def plot_phase(position, velocity, figure_number, custom_label="", custom_legend_title="", block_numbers=None)