My Project
plotAveragedCalculatedFrictionAmplitude.py
Go to the documentation of this file.
1 from frictionImports import *
2 if("-confidence" in sys.argv):
3  print("import confidence code")
4  try:
5  import scipy.stats
6  except ImportError as e:
7  print("Error when trying to import scipy.stats: ", e)
8 
9 sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '/File')
10 from loadFromResults import LoadFile
11 python_path = os.path.dirname(os.path.realpath(__file__))
12 
13 colors_diverging_short = ['#d7191c','#fdae61','#ffffbf','#abd9e9','#2c7bb6']
14 colors_diverging_long = ['#a50026','#d73027','#f46d43','#fdae61','#fee090','#ffffbf','#e0f3f8','#abd9e9','#74add1','#4575b4','#313695']
15 colors_qualitative_short = ['#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e']
16 colors_qualitative_long = ['#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e','#e6ab02','#a6761d']
17 linestyles = ['-', '--', '-.', ':']
18 
19 
20 def getFileWithPaths(path):
21  yaml_file = ""
22  if('yaml' in path):
23  yaml_file = LoadFile.loadYamlStandAlone("", python_path, path)
24  path_components = [unique_path_component for unique_path_component in (yaml_file["unique_path_components"])]
25  print(path_components)
26  paths_to_amplitude_files = [yaml_file["general_path"] + str(unique_path_component) + "/" + yaml_file["result_path"] + yaml_file["file_name"] for unique_path_component in (yaml_file["unique_path_components"])]
27  else:
28  paths_to_amplitude_files = np.loadtxt(python_path + path, delimiter = "\n", dtype = bytes).astype(str)
29  print(paths_to_amplitude_files)
30  return (paths_to_amplitude_files, yaml_file)
31 
32 
33 def getYamlElement(
34  yaml_file,
35  yaml_key
36 ):
37  if(isinstance(yaml_file, dict)):
38  if(yaml_key in yaml_file.keys()):
39  return yaml_file[yaml_key]
40  else:
41  print("Tried to add title from yaml file, but there was no \"" + yaml_key + "\" key.")
42  return None
43 
44 
46  yaml_file,
47  yaml_key="suptitle"
48 ):
49  suptitle = getYamlElement(yaml_file, yaml_key)
50  if(suptitle is not None):
51  plt.suptitle(suptitle)
52 
53 
55  yaml_file,
56  yaml_key="unique_path_components"
57 ):
58  labels = getYamlElement(yaml_file, yaml_key)
59  if(labels is not None):
60  return labels
61 
63  yaml_file,
64  yaml_key="label_title"
65 ):
66  label_title = getYamlElement(yaml_file, yaml_key)
67  if(label_title is not None):
68  return label_title
69  else:
70  return ""
71 
72 
74  amplitude_files,
75  yaml_file=""
76 ):
77  if("-compare" in sys.argv):
78  colors = colors_qualitative_short
79  labels = ['BK: seed 113.', 'BK: seed 114']
80  else:
81  colors = colors_qualitative_long*len(amplitude_files)
82  labels = getLabelsFromYaml(yaml_file)
83  for i in range(0,len(amplitude_files)):
84  if(len(amplitude_files[i]) > 3):
85  plt.plot(amplitude_files[i][0], -amplitude_files[i][3], color = colors[i])
86  plt.errorbar(amplitude_files[i][0], amplitude_files[i][1], yerr=amplitude_files[i][2], ecolor='red', color=colors[i], label = labels[i], linestyle=linestyles[i%4])
87  if("-log" in sys.argv):
88  plt.xscale('log')
89  plt.ylabel("\$A\$")
90  plt.xlabel("\$\\nu\$")
91  if("-dipregion" in sys.argv):
92  plt.xlim(0.07,0.27)
93  plt.ylim(0,30)
94  plt.grid()
95  else:
96  plt.legend(title = getLabelTitleFromYaml(yaml_file))
97  addTitleFromYaml(yaml_file)
98  plt.show()
99 
100 
102  amplitude_files,
103  path_to_save_folder,
104  yaml_file=""
105 ):
106  number_of_runs = str(amplitude_files.shape[0])
107  label = "avg of " + number_of_runs + " runs"
108  color = '#2c7bb6'
109  ecolor = '#d7191c'
110 
111  amplitude_files = np.average(amplitude_files, 0)
112 
113  if(len(amplitude_files) > 3):
114  # plots average of firction value
115  plt.plot(amplitude_files[0], -amplitude_files[3], color = color)
116 
117  plt.errorbar(amplitude_files[0], amplitude_files[1], yerr=amplitude_files[2], ecolor=ecolor, label = label, color = color)
118 
119  if("-log" in sys.argv):
120  plt.xscale('log')
121 
122  plt.ylabel("\$A\$")
123  plt.xlabel("\$\\nu\$")
124  plt.legend()
125  addTitleFromYaml(yaml_file)
126  plt.show()
127 
128  if(not "-notsave" in sys.argv):
129  input("Now you are saving, press enter to continue.")
130  np.savetxt(python_path + "/../../../results" + path_to_save_folder + "average_" + number_of_runs + "_runs.csv", amplitude_files, delimiter = ',')
131 
132 
134  amplitude_files,
135  yaml_file
136 ):
137  print("Calculating the difference between the first and consecutive amplitude files.")
138  print(amplitude_files.shape)
139  if(len(amplitude_files.shape) == 3):
140  colors = colors_qualitative_short*len(amplitude_files)
141  amplitudes = amplitude_files[:,1,:]
142  if("-averagecompare" in sys.argv):
143  compared_to_file = np.average(amplitudes, axis = 0)
144  print(compared_to_file)
145  start_iteration = 0
146  else:
147  compared_to_file = amplitudes[0]
148  start_iteration = 1
149  compared_list = []
150  for i in range(start_iteration, len(amplitudes)):
151  difference = abs(amplitudes[i] - compared_to_file)
152  compared_list.append(difference)
153 
154  compared_list = np.array(compared_list)
155  labels = getLabelsFromYaml(yaml_file)[start_iteration:]
156  if("-saveinterval" in sys.argv):
157  scatterbar_x_values = [1/label for label in labels]
158  labels = ["1/" + str(label) for label in labels]
159  scatterbar_x_label = "\$Save frequency\$"
160  else:
161  scatterbar_x_values = labels
162  scatterbar_x_label = "\$Seed\$"
163  label_title = getLabelTitleFromYaml(yaml_file)
164  [plt.plot(amplitude_files[i,0,:],compared, label = labels[i], color = colors[i], linestyle=linestyles[i%4]) for i, compared in enumerate(compared_list)]
165  plt.legend(title = label_title)
166  if("-log" in sys.argv):
167  plt.xscale('log')
168  plt.ylabel("\$Error\$")
169  plt.xlabel("\$\\nu\$")
170  addTitleFromYaml(yaml_file)
171  plt.show()
172  [plt.plot(amplitude_files[i,0,:],np.cumsum(compared), label = labels[i], color = colors[i], linestyle=linestyles[i%4]) for i, compared in enumerate(compared_list)]
173  plt.ylabel("\$ Cumulative Error\$")
174  plt.xlabel("\$\\nu\$")
175  plt.legend(title = label_title)
176  if("-log" in sys.argv):
177  plt.xscale('log')
178  addTitleFromYaml(yaml_file)
179  #plt.ylim(0, 0.35)
180  plt.show()
181 
182  compared_sum = np.sum(compared_list, 1)
183 
184  #compared_sum
185  plt.scatter(scatterbar_x_values, compared_sum, color = colors_diverging_short[0])
186  plt.bar(scatterbar_x_values, compared_sum, color = colors_diverging_short[0])
187  plt.plot(scatterbar_x_values, compared_sum, color = colors_diverging_short[1], linestyle = linestyles[1])
188  plt.ylabel("\$ Cumulative Error\$")
189  plt.xlabel(scatterbar_x_label)
190  #plt.ylim(0, 0.35)
191  addTitleFromYaml(yaml_file)
192  plt.show()
193 
194  plt.bar(np.arange(len(compared_sum)),np.sort(compared_sum))
195  plt.show()
196 
197  plt.plot(amplitude_files[0,0,:],compared_to_file, color = colors_diverging_short[4], label = "1/1")
198  plt.ylabel("\$A\$")
199  plt.xlabel("\$\\nu\$")
200  plt.legend(title = label_title)
201  addTitleFromYaml(yaml_file)
202  if("-log" in sys.argv):
203  plt.xscale('log')
204  plt.show()
205 
207  amplitude_files,
208  yaml_file,
209  confidence=0.95
210 ):
211  label = "BK-pad: 95\% CI, 16 runs"
212  color = colors_qualitative_short[3]
213  amplitudes = amplitude_files[:,1,:]
214  mean, standard_error = np.mean(amplitudes, axis=0), scipy.stats.sem(amplitudes)
215  print(amplitudes.shape)
216  height = standard_error * scipy.stats.t.ppf((1 + confidence) / 2., len(amplitudes)-1)
217  plt.plot(amplitude_files[0,0,:],mean, color = color, label = label)
218  plt.fill_between(amplitude_files[0,0,:], mean+height, mean-height, color = color, alpha=.5)
219  if("-log" in sys.argv):
220  plt.xscale('log')
221  plt.ylabel("\$A\$")
222  plt.xlabel("\$\\nu\$")
223  plt.ylim(0, 45)
224  plt.legend()
225  addTitleFromYaml(yaml_file)
226  plt.show()
227  # matrix_CI = np.array([amplitude_files[0,0,:], mean, height, standard_error])
228  # print(matrix_CI.shape)
229  # np.savetxt(python_path + "/../../../results/" + yaml_file["save_CI_path"] + yaml_file["save_CI_name"], matrix_CI, delimiter = ',')
230 
231 
233  amplitude_files,
234  path_to_save_folder,
235  yaml_file
236 ):
237  if("-individual" in sys.argv):
238  plotIndividualAmplitudes(amplitude_files, yaml_file)
239 
240  if(not "-compare" in sys.argv):
241  plotAverageAmplitudes(amplitude_files, path_to_save_folder, yaml_file)
242 
243  if("-saveinterval" or "-averagecompare" in sys.argv):
244  calculateIntervalError(amplitude_files, yaml_file)
245  if("-confidence" in sys.argv):
246  mean_confidence_interval(amplitude_files, yaml_file)
247 
248 
249 
251  paths,
252  path_to_save_folder,
253  yaml_file
254 ):
255 
256  amplitude_files = np.array([np.loadtxt(python_path + "/../../../results/" + path, delimiter = ",") for path in paths])
257  plotAmplitudeFiles(amplitude_files, path_to_save_folder, yaml_file)
258 
259 
260 def run(path_to_paths, path_to_save_folder):
261  paths, yaml_file = getFileWithPaths(path_to_paths)
262  loadAmplitudeFiles(paths, path_to_save_folder, yaml_file)
263 
264 if __name__ =="__main__":
265  run(sys.argv[1], sys.argv[2])
def getLabelsFromYaml(yaml_file, yaml_key="unique_path_components")
def plotAverageAmplitudes(amplitude_files, path_to_save_folder, yaml_file="")
def getLabelTitleFromYaml(yaml_file, yaml_key="label_title")
def mean_confidence_interval(amplitude_files, yaml_file, confidence=0.95)
def loadAmplitudeFiles(paths, path_to_save_folder, yaml_file)
def plotAmplitudeFiles(amplitude_files, path_to_save_folder, yaml_file)