My Project
phase_plot_creator.py
Go to the documentation of this file.
1 from phase_plot_imports import loader, np, sys
2 
4  run_name,
5  pad_phase_data = True,
6  block_phase_data = True
7 ):
8  parameters = loader.load_yaml_parameters(run_name)
9  threshold_dictionary = get_threshold_speed(parameters)
10  start_index = threshold_dictionary['threshold_speed_start_index']
11  indexes = make_indexes(start_index, threshold_dictionary, 100)
12  if(block_phase_data):
13  path_in_results_folder = run_name + "/results/phase_plot_at_threshold_block.csv"
14  position = loader.load_simulation_output(parameters, run_name, "block_position")
15  velocity = loader.load_simulation_output(parameters, run_name, "block_velocity")
16  phase_data = get_phase_selection(position, velocity, indexes)
17  save_phase_data(phase_data,run_name, path_in_results_folder, block_phase_data=True)
18  if(pad_phase_data):
19  path_in_results_folder = run_name + "/results/phase_plot_at_threshold_pad.csv"
20  position = loader.load_simulation_output(parameters, run_name, "pad_position")
21  velocity = loader.load_simulation_output(parameters, run_name, "pad_velocity")
22  phase_data = get_phase_selection(position, velocity, indexes)
23  save_phase_data(phase_data, run_name, path_in_results_folder)
24 
26  run_name,
27  phase_part = "_block"
28 ):
29  path_in_results_folder = run_name + "/results/phase_plot_at_threshold" + phase_part + ".csv"
30  if(loader.check_if_file_exist_in_run_folder(path_in_results_folder) and pad_or_block(phase_part)):
31  print("Found phase plot at: ", path_in_results_folder)
32 
33  parameters = loader.load_yaml_parameters(run_name)
34  phase_data = load_phase_data(run_name, phase_part)
35  if("_block" == phase_part):
36  position_selection = []
37  velocity_selection = []
38  for i in range(0, int(len(phase_data)/2)):
39  position_selection.append(phase_data[2*i])
40  velocity_selection.append(phase_data[2*i+1])
41  else:
42  position_selection = phase_data[0]
43  velocity_selection = phase_data[1]
44  return {'position_selection': position_selection, 'velocity_selection': velocity_selection, 'parameters': parameters}
45  else:
46  print("Sorry, the phase plot does not exist for this run.\n \
47  Remove the \"-load\"-flag to produce it.")
48  exit()
49 
51  parameters
52 ):
53  threshold_speed = loader.get_parameter(parameters, "threshold_speed")
54  speeds = loader.get_start_end_speed(parameters)
55  max_time = loader.get_parameter(parameters, "max_time")
56  save_interval = loader.get_parameter(parameters, "save_interval_dt")
57  dt = loader.get_parameter(parameters, "dt")
58  steps_in_time_unit = 1 / (dt * save_interval)
59  n_steps = np.floor(max_time * steps_in_time_unit)
60 
61  threshold_speed_start_index = int(np.ceil((threshold_speed - speeds['start_speed']) \
62  / (speeds['end_speed'] - speeds['start_speed']) \
63  * n_steps))
64  return {'threshold_speed': threshold_speed, \
65  'threshold_speed_start_index': threshold_speed_start_index, \
66  'start_speed': speeds['start_speed'], \
67  'end_speed': speeds['end_speed'], \
68  'n_steps': n_steps, \
69  'steps_in_time_unit': steps_in_time_unit}
70 
71 def make_indexes(
72  start_index,
73  threshold_dictionary,
74  time_units_from_start = 1000
75 ):
76  start_index = start_index + make_indexes_within_interval(start_index, threshold_dictionary, 50)
77  max_indexes_from_start = threshold_dictionary['n_steps'] - threshold_dictionary['threshold_speed_start_index']
78  if(start_index == max_indexes_from_start):
79  print("Can't have the start index be equal to the end index.")
80  quit()
81  end_index = start_index + make_indexes_within_interval(start_index, threshold_dictionary, time_units_from_start)
82  print(start_index, end_index)
83  #start_index += 5000
84  indexes = range(start_index,end_index)
85  return indexes
86 
88  start_index,
89  threshold_dictionary,
90  time_units_from_start
91 ):
92  indexes_from_start = np.floor(time_units_from_start * threshold_dictionary['steps_in_time_unit'])
93  max_indexes_from_start = threshold_dictionary['n_steps'] - threshold_dictionary['threshold_speed_start_index']
94  if(indexes_from_start > max_indexes_from_start):
95  print("Selected units greater than the size of the run. Using maximum amount of units.")
96  print("Selected: ", time_units_from_start)
97  print("Maximum: ", max_indexes_from_start / threshold_dictionary['steps_in_time_unit'])
98  indexes_from_start = max_indexes_from_start
99  return int(indexes_from_start)
100 
102  run_name,
103  check = "both"
104 ):
105  found_block = False
106  found_pad = False
107  if(check in ['block', 'both']):
108  path_in_results_folder = run_name + "/results/phase_plot_at_threshold_block.csv"
109  if(not loader.check_if_file_exist_in_run_folder(path_in_results_folder)):
110  return False
111  else:
112  found_block = True
113  if(check in ['pad', 'both']):
114  path_in_results_folder = run_name + "/results/phase_plot_at_threshold_pad.csv"
115  if(not loader.check_if_file_exist_in_run_folder(path_in_results_folder)):
116  return False
117  else:
118  found_pad = True
119  if(found_block and found_pad):
120  return True
121  else:
122  print("check-parameters must be one of [pad, block, both].")
123  return False
124 
125 
126 
127 
128 def save_phase_data(
129  phase_data,
130  run_name,
131  path_in_results_folder,
132  block_phase_data = False
133 ):
134  position_selection = phase_data['position']
135  velocity_selection = phase_data['velocity']
136  write_to_file = True
137  file_name = "/results/phase_plot_at_threshold_"
138  if(loader.check_if_file_exist_in_run_folder(path_in_results_folder) and not "-yes" in sys.argv):
139  write_to_file = bool_interaction_with_user("Phase file exists, to overwrite type\"yes\" or anything elso to skip:")
140  if(write_to_file):
141  if(block_phase_data and len(position_selection) > 1):
142  save_matrix =[]
143  for i in range(0, len(position_selection)):
144  save_matrix.append(position_selection[i])
145  save_matrix.append(velocity_selection[i])
146  save_matrix = np.array(save_matrix)
147  file_name += "block.csv"
148  else:
149  save_matrix = np.array([position_selection, velocity_selection])
150  file_name += "pad.csv"
151  loader.save_csv(run_name, file_name, save_matrix)
152  else:
153  print("Skipped printing to file.")
154 
156  output_to_user
157 ):
158  response_from_user = input(output_to_user)
159  if(response_from_user == "yes"):
160  return True
161  else:
162  return False
163 
165  position,
166  velocity,
167  indexes
168 ):
169  if(len(position.shape) == 1):
170  position_selection = position[indexes]
171  velocity_selection = velocity[indexes]
172  else:
173  position_selection = position[:, indexes]
174  velocity_selection = velocity[:, indexes]
175  return {'position': position_selection, 'velocity': velocity_selection}
176 
177 def pad_or_block(
178  phase_part
179 ):
180  return (phase_part in ["_pad", "_block"])
181 
182 def load_phase_data(
183  run_name,
184  phase_part = ""
185 ):
186  phase_data = lazy_load_data(\
187  run_name, \
188  "phase_plot_at_threshold" + phase_part, \
189  use_shared_names = False \
190  )
191  return phase_data
192 
193 def lazy_load_data(
194  run_name,
195  file_name,
196  use_shared_names
197 ):
198  parameters = loader.load_yaml_parameters(run_name)
199  phase_data = loader.load_simulation_output(\
200  parameters,\
201  run_name, \
202  file_name, \
203  use_shared_names = use_shared_names \
204  )
205  return phase_data
def make_indexes_within_interval(start_index, threshold_dictionary, time_units_from_start)
def make_indexes(start_index, threshold_dictionary, time_units_from_start=1000)
def lazy_load_data(run_name, file_name, use_shared_names)
def pad_or_block(phase_part)
def check_if_all_phase_data_exists(run_name, check="both")
def save_phase_data(phase_data, run_name, path_in_results_folder, block_phase_data=False)
def bool_interaction_with_user(output_to_user)
def get_threshold_speed(parameters)
def get_phase_selection(position, velocity, indexes)
def load_phase_data(run_name, phase_part="")
def create_phase_data(run_name, pad_phase_data=True, block_phase_data=True)
def load_phase_data_creator(run_name, phase_part="_block")