1 | import numpy as np |
---|
2 | import matplotlib.pyplot as plt |
---|
3 | from scipy import stats |
---|
4 | |
---|
5 | |
---|
6 | global_index_figure = 0 |
---|
7 | |
---|
8 | def float_list(l): # convert a list of char in a list of float |
---|
9 | L = [] |
---|
10 | for i in l: |
---|
11 | L.append(float(i)) |
---|
12 | return(L) |
---|
13 | |
---|
14 | def product_list(L,x, b = 0): # linear convertion of a list return L*x + b (b is 0 as default) |
---|
15 | Lx = [] |
---|
16 | for i in L: |
---|
17 | Lx.append(i*x +b) |
---|
18 | return(Lx) |
---|
19 | |
---|
20 | |
---|
21 | def read_data(fichier): # program to read data from a file of data with position of motor and them Va, Vb, Vc and Vd |
---|
22 | fichier = open(fichier, "r") |
---|
23 | Data = fichier.read() |
---|
24 | Data_list = [[],[],[],[],[]] |
---|
25 | value = "" |
---|
26 | index = 0 |
---|
27 | for i in Data: |
---|
28 | if (i == " ") : |
---|
29 | if index == 0: |
---|
30 | Data_list[0].append(value) |
---|
31 | index +=1 |
---|
32 | elif index == 1: |
---|
33 | Data_list[1].append(value) |
---|
34 | #print(value) |
---|
35 | index += 1 |
---|
36 | elif index == 2: |
---|
37 | Data_list[2].append(value) |
---|
38 | index += 1 |
---|
39 | elif index == 3: |
---|
40 | Data_list[3].append(value) |
---|
41 | index += 1 |
---|
42 | else: |
---|
43 | Data_list[4].append(value) |
---|
44 | index = 0 |
---|
45 | value = "" |
---|
46 | elif (i == "\n") : |
---|
47 | value = "" |
---|
48 | else: |
---|
49 | value += i |
---|
50 | fichier.close() |
---|
51 | for i in range(5): |
---|
52 | Data_list[i] = float_list(Data_list[i]) |
---|
53 | Data_list[0] = product_list(Data_list[0],10**-6) |
---|
54 | for i in range(1,5): |
---|
55 | Data_list[i] = product_list(Data_list[i],1000) |
---|
56 | return(Data_list) |
---|
57 | |
---|
58 | |
---|
59 | |
---|
60 | def print_graph(L): # program to print graph of data set in a list with [[position][Va][Vb][Vc][Vd]], also do a linear regretion and print the equation |
---|
61 | # a = np.array([L[0][i],L[1][i]] for i in range(len(L[0]))) |
---|
62 | global global_index_figure |
---|
63 | fig1 = plt.figure(global_index_figure) |
---|
64 | global_index_figure += 1 |
---|
65 | plt.clf() |
---|
66 | lr1 = stats.linregress(L[0],L[1]) # return tuple (pente,ordonnee a l'origine, coef de correlation, p-value, erreur standard de l'estimation) |
---|
67 | plt.plot(L[0],L[1], "r", label = "BMP_ref") |
---|
68 | plt.plot(L[0],[lr1.slope*i + lr1.intercept for i in L[0]], "r--", label = "BMP_ref regression line, \nerror = "+str(round(lr1.stderr,3))+",\ncorrelation coefficient = "+str(round(lr1.rvalue,3))) |
---|
69 | |
---|
70 | lr2 = stats.linregress(L[0],L[2]) |
---|
71 | plt.plot(L[0],L[2], "b", label = "BMP_impr") |
---|
72 | plt.plot(L[0],[lr2.slope*i + lr2.intercept for i in L[0]], "b--", label = "BMP_impr regression line, \nerror = "+str(round(lr2.stderr,3))+",\ncorrelation coefficient = "+str(round(lr2.rvalue,3))) |
---|
73 | |
---|
74 | lr3 = stats.linregress(L[0],L[3]) |
---|
75 | plt.plot(L[0],L[3], "g", label = "BMP_ref") |
---|
76 | plt.plot(L[0],[lr3.slope*i + lr3.intercept for i in L[0]], "g--", label = "BMP_ref regression line, \nerror = "+str(round(lr3.stderr,3))+",\ncorrelation coefficient = "+str(round(lr3.rvalue,3))) |
---|
77 | |
---|
78 | lr4 = stats.linregress(L[0],L[4]) |
---|
79 | plt.plot(L[0],L[4], "m", label = "BMP_impr") |
---|
80 | plt.plot(L[0],[lr4.slope*i + lr4.intercept for i in L[0]], "m--", label = "BMP_impr regression line, \nerror = "+str(round(lr4.stderr,2))+",\ncorrelation coefficient = "+str(round(lr4.rvalue,3))) |
---|
81 | |
---|
82 | plt.legend() |
---|
83 | plt.xlabel("length in million motor step") #, fontsize=20) |
---|
84 | plt.ylabel("tension in mV") |
---|
85 | plt.gca().yaxis.set_tick_params(labelsize = 8) |
---|
86 | # plt.show() |
---|
87 | |
---|
88 | def residu(L): # program to do graph of residu of data give in a list with [[position][Va][Vb][Vc][Vd]] |
---|
89 | global global_index_figure |
---|
90 | fig1 = plt.figure(global_index_figure) |
---|
91 | global_index_figure += 1 |
---|
92 | plt.clf() |
---|
93 | plt.title("Residu") |
---|
94 | lr1 = stats.linregress(L[0],L[1]) # return tuple (pente (.slope),ordonnee a l'origine (.intercept), coef de correlation (.rvalue), p-value (.pvalue ?), erreur standard de l'estimation (.stderr)) |
---|
95 | for i in range(len(L[1])): |
---|
96 | L[1][i] = L[1][i] - lr1.slope*L[0][i] - lr1.intercept |
---|
97 | plt.plot(L[0],L[1], "r", label = "BMP_ref") |
---|
98 | |
---|
99 | lr2 = stats.linregress(L[0],L[2]) |
---|
100 | for i in range(len(L[2])): |
---|
101 | L[2][i] = L[2][i] - lr2.slope*L[0][i] - lr2.intercept |
---|
102 | plt.plot(L[0],L[2], "b", label = "BMP_impr") |
---|
103 | |
---|
104 | lr3 = stats.linregress(L[0],L[3]) |
---|
105 | for i in range(len(L[3])): |
---|
106 | L[3][i] = L[3][i] - lr3.slope*L[0][i] - lr3.intercept |
---|
107 | plt.plot(L[0],L[3], "g", label = "BMP_ref") |
---|
108 | |
---|
109 | lr4 = stats.linregress(L[0],L[4]) |
---|
110 | for i in range(len(L[4])): |
---|
111 | L[4][i] = L[4][i] - lr4.slope*L[0][i] - lr4.intercept |
---|
112 | plt.plot(L[0],L[4], "m", label = "BMP_impr") |
---|
113 | plt.legend() |
---|
114 | plt.xlabel("length in million motor step") |
---|
115 | plt.ylabel("tension in mV") |
---|
116 | # plt.show() |
---|
117 | |
---|
118 | """ |
---|
119 | ax2 = fig.add_axes((0.1,0.1,0.8,0.0)) |
---|
120 | ax2.yaxis.set_visible(False) # hide the yaxis |
---|
121 | ax2.set_xticklabels(10*L[0]) |
---|
122 | ax2.set_xlabel("lenght in mm") |
---|
123 | plt.show() |
---|
124 | """ |
---|
125 | |
---|
126 | def min_max(L,mini = None ,maxi = None): #program to cut a list L = [[position][Va][Vb][Vc][Vd]] with minimum value of position = mini and maximum value of position = maxi |
---|
127 | if mini is None : |
---|
128 | mini = L[0][0] |
---|
129 | if maxi is None : |
---|
130 | maxi = L[0][len(L[0])-1] |
---|
131 | if mini > maxi: |
---|
132 | mini,maxi = maxi,mini |
---|
133 | index_list = [] |
---|
134 | for i in range(len(L[0])): |
---|
135 | if L[0][i] >= mini and L[0][i] <= maxi : |
---|
136 | index_list.append(i) |
---|
137 | return([[L[0][i] for i in index_list],[L[1][i] for i in index_list],[L[2][i] for i in index_list],[L[3][i] for i in index_list],[L[4][i] for i in index_list]]) |
---|
138 | |
---|
139 | |
---|
140 | def convertion_step_mm(L,begin_mm,end_mm): # length convertion |
---|
141 | begin_step = L[0] |
---|
142 | end_step = L[len(L)-1] |
---|
143 | a = float((end_mm - begin_mm))/(end_step - begin_step) |
---|
144 | b = begin_mm - a*begin_step |
---|
145 | print(a,b) |
---|
146 | print(L) |
---|
147 | L = product_list(L, a, b) |
---|
148 | return(L) |
---|
149 | |
---|
150 | |
---|
151 | #L = [[i for i in range(51)] for j in range(5)] |
---|
152 | #print(convertion_step_mm(L[0],3.1,44520)) |
---|
153 | |
---|
154 | |
---|
155 | def print_data(fichier, min = None, max = None): |
---|
156 | L = min_max(read_data(fichier), mini = min, maxi = max) |
---|
157 | print_graph(L) |
---|
158 | #print(L) |
---|
159 | |
---|
160 | def print_residu(fichier, min = None, max = None): |
---|
161 | L = min_max(read_data(fichier), mini = min, maxi = max) |
---|
162 | residu(L) |
---|
163 | |
---|
164 | print_data("data/position_vs_tension_ch1-3_bpm_ref_ch2-4_bpm_impr_20180611_160153_0.txt",0.1,0.3) |
---|
165 | print_residu("data/position_vs_tension_ch1-3_bpm_ref_ch2-4_bpm_impr_20180611_160153_0.txt",0.1,0.3) |
---|
166 | |
---|
167 | #print_data("data/position_vs_tension_ch1-3_bpm_ref_ch2-4_bpm_impr_20180611_163036_0.txt") |
---|
168 | |
---|
169 | #print_data("data/position_vs_tension_ch1-3_bpm_ref_ch2-4_bpm_impr_vertical_acquisition_20180612_0.txt") |
---|
170 | #print_data("data/position_vs_tension_ch1-3_bpm_ref_ch2-4_bpm_impr_vertical_acquisition_20180612_0.txt") |
---|
171 | plt.show() |
---|
172 | |
---|