-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetcoord_sides.py
More file actions
280 lines (240 loc) · 11.1 KB
/
getcoord_sides.py
File metadata and controls
280 lines (240 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import matplotlib.pyplot as plt
import numpy as np
from array import array
import ROOT
def graph_and_fit(x_first, y_first, x_second, y_second):
graph_first = ROOT.TGraph(len(x_first),x_first,y_first)
fit_function_first = ROOT.TF1("pol1_first","[0]+x*[1]")
graph_first.Fit("pol1_first","Q0")
graph_second = ROOT.TGraph(len(x_second),x_second,y_second)
fit_function_second = ROOT.TF1("pol1_second","[0]+x*[1]")
fit_function_second.FixParameter(1,fit_function_first.GetParameter(1))
graph_second.Fit("pol1_second","Q0")
return np.abs(fit_function_first.GetParameter(0) - fit_function_second.GetParameter(0))/np.sqrt(fit_function_first.GetParameter(1)*fit_function_first.GetParameter(1)+1) #distance between two parallel lines
filespath = "/home/luca/Desktop/Analisi/modules/Data_measurements/Batch3/"
max_files = 100
px_temp, py_temp, pz_temp = [], [], []
filenames = []
px, py, pz = [[]], [[]], [[]]
n_points_per_file = 24
n_files = 1
for k, filename in enumerate(os.listdir(filespath)):
if(k > max_files):
break
px_temp, py_temp, pz_temp = [], [], []
if os.path.isfile(os.path.join(filespath, filename)):
#print(filename)
filenames.append(filename)
n_files = n_files+1
with open(os.path.join(filespath, filename)) as file:
count = 0 # Reset the count for each file
for line in file:
if "point" in line:
line = line[45:] # Remove characters before the coordinates
coordinates = line.split()[:3] # Split by spaces and take the first three values
if len(coordinates) == 3:
x, y, z = map(float, coordinates)
px_temp.append(x)
py_temp.append(y)
pz_temp.append(z)
if len(px_temp) == n_points_per_file:
px.append(px_temp)
py.append(py_temp)
pz.append(pz_temp)
#some fitting and extraction of the distances
distance_left, distance_right, distance_top, distance_bottom = [], [], [], []
n_bins = 20
min_bin = .1
max_bin = .4
hist_total = ROOT.TH1F("total distances between sides","total distances between sides", n_bins, min_bin, max_bin)
hist_left = ROOT.TH1F("distances between sides on the left","distances between sides on the left", n_bins, min_bin, max_bin)
hist_right = ROOT.TH1F("distances between sides on the right","distances between sides on the right", n_bins, min_bin, max_bin)
hist_top = ROOT.TH1F("distances between sides on the top","distances between sides on the top", n_bins, min_bin, max_bin)
hist_bottom = ROOT.TH1F("distances between sides on the bottom","distances between sides on the bottom", n_bins, min_bin, max_bin)
cut_bottom = -0.25 #first set of points always has a point outside of ROI (bug in mitutoyo script)
for j in range(1,n_files):
print(filenames[j-1])
if(j>max_files):
break
x_bottom_right, y_bottom_right = array('d'), array('d')
x_bottom_left, y_bottom_left = array('d'), array('d')
x_right_top, y_right_top = array('d'), array('d')
x_right_bottom, y_right_bottom = array('d'), array('d')
x_top_right, y_top_right = array('d'), array('d')
x_top_left, y_top_left = array('d'), array('d')
x_left_top, y_left_top = array('d'), array('d')
x_left_bottom, y_left_bottom = array('d'), array('d')
for i in range(len(px[j])):
if i%2 != 0:
continue
#bottom
if i < 6:
if(px[j][i] < cut_bottom or px[j][i+1] < cut_bottom):
continue
if(px[j][i]>px[j][i+1]):
x_bottom_right.append(px[j][i])
y_bottom_right.append(py[j][i])
x_bottom_left.append(px[j][i+1])
y_bottom_left.append(py[j][i+1])
else:
x_bottom_right.append(px[j][i+1])
y_bottom_right.append(py[j][i+1])
x_bottom_left.append(px[j][i])
y_bottom_left.append(py[j][i])
#right
if i >= 6 and i < 12:
if(py[j][i]>py[j][i+1]):
x_right_top.append(px[j][i])
y_right_top.append(py[j][i])
x_right_bottom.append(px[j][i+1])
y_right_bottom.append(py[j][i+1])
else:
x_right_top.append(px[j][i+1])
y_right_top.append(py[j][i+1])
x_right_bottom.append(px[j][i])
y_right_bottom.append(py[j][i])
#top
if i>=12 and i < 18:
if(px[j][i]>px[j][i+1]):
x_top_right.append(px[j][i])
y_top_right.append(py[j][i])
x_top_left.append(px[j][i+1])
y_top_left.append(py[j][i+1])
else:
x_top_right.append(px[j][i+1])
y_top_right.append(py[j][i+1])
x_top_left.append(px[j][i])
y_top_left.append(py[j][i])
#left
if i>=18 and i<24:
if(py[j][i]>py[j][i+1]):
x_left_top.append(px[j][i])
y_left_top.append(py[j][i])
x_left_bottom.append(px[j][i+1])
y_left_bottom.append(py[j][i+1])
else:
x_left_top.append(px[j][i+1])
y_left_top.append(py[j][i+1])
x_left_bottom.append(px[j][i])
y_left_bottom.append(py[j][i])
#print(len(x_bottom_left),len(x_bottom_right),len(x_right_bottom),len(x_right_top),len(x_top_left),len(x_top_right),len(x_left_bottom),len(x_left_top))
#computing distances for each measurement and filling histograms
distance_bottom.append(graph_and_fit(x_first=x_bottom_right,y_first=y_bottom_right,x_second=x_bottom_left,y_second=y_bottom_left))
hist_bottom.Fill(distance_bottom[j-1])
hist_total.Fill(distance_bottom[j-1])
distance_right.append(graph_and_fit(x_first=x_right_top,y_first=y_right_top,x_second=x_right_bottom,y_second=y_right_bottom))
hist_right.Fill(distance_right[j-1])
hist_total.Fill(distance_right[j-1])
distance_top.append(graph_and_fit(x_first=x_top_right,y_first=y_top_right,x_second=x_top_left,y_second=y_top_left))
hist_top.Fill(distance_top[j-1])
hist_total.Fill(distance_top[j-1])
distance_left.append(graph_and_fit(x_first=x_left_top,y_first=y_left_top,x_second=x_left_bottom,y_second=y_left_bottom))
hist_left.Fill(distance_left[j-1])
hist_total.Fill(distance_left[j-1])
'''
for j in range(n_files):
#if j == 1:
# break
print(filenames[j])
#first distance, bottom
x_bottom_first, y_bottom_first = array('d'), array('d')
x_bottom_second, y_bottom_second = array('d'), array('d')
for i in range(len(px[j+1])):
if px[j+1][i] < 0 and py[j+1][i] < -5:
if py[j+1][i] < -9: #excluding the points in the top left
x_bottom_first.append(1.*px[j+1][i])
y_bottom_first.append(1.*py[j+1][i])
if px[j+1][i] > -0. and py[j+1][i] < -5:
x_bottom_second.append(px[j+1][i])
y_bottom_second.append(py[j+1][i])
distance_bottom.append(graph_and_fit(x_first=x_bottom_first,y_first=y_bottom_first,x_second=x_bottom_second,y_second=y_bottom_second))
hist_bottom.Fill(distance_bottom[j])
hist_total.Fill(distance_bottom[j])
#second distance, right
x_right_first, y_right_first = array('d'), array('d')
x_right_second, y_right_second = array('d'), array('d')
for i in range(len(px[j+1])):
if px[j+1][i] > 5. and py[j+1][i] < 0.: #excluding the points in the top left (px[i] <0.1 and py[i > -8])
x_right_first.append(1.*px[j+1][i])
y_right_first.append(1.*py[j+1][i])
if px[j+1][i] > 5. and py[j+1][i] > 0.:
x_right_second.append(px[j+1][i])
y_right_second.append(py[j+1][i])
distance_right.append(graph_and_fit(x_first=x_right_first,y_first=y_right_first,x_second=x_right_second,y_second=y_right_second))
hist_right.Fill(distance_right[j])
hist_total.Fill(distance_right[j])
#third distance, top
x_top_first, y_top_first = array('d'), array('d')
x_top_second, y_top_second = array('d'), array('d')
for i in range(len(px[j+1])):
if px[j+1][i] > -.1 and py[j+1][i] > 5: #excluding the points in the top left (px[i] <0.1 and py[i > -8])
x_top_first.append(1.*px[j+1][i])
y_top_first.append(1.*py[j+1][i])
if px[j+1][i] <-.1 and py[j+1][i] > 5:
x_top_second.append(px[j+1][i])
y_top_second.append(py[j+1][i])
distance_top.append(graph_and_fit(x_first=x_top_first,y_first=y_top_first,x_second=x_top_second,y_second=y_top_second))
hist_top.Fill(distance_top[j])
hist_total.Fill(distance_top[j])
#fourth distance, left
x_left_first, y_left_first = array('d'), array('d')
x_left_second, y_left_second = array('d'), array('d')
for i in range(len(px[j+1])):
if px[j+1][i] < -5. and py[j+1][i] > -.1: #excluding the points in the top left (px[i] <0.1 and py[i > -8])
x_left_first.append(1.*px[j+1][i])
y_left_first.append(1.*py[j+1][i])
if px[j+1][i] < -5. and py[j+1][i] < -.1:
x_left_second.append(px[j+1][i])
y_left_second.append(py[j+1][i])
distance_left.append(graph_and_fit(x_first=x_left_first,y_first=y_left_first,x_second=x_left_second,y_second=y_left_second))
hist_left.Fill(distance_left[j])
hist_total.Fill(distance_left[j])
'''
c = ROOT.TCanvas("c","multigraph",200,10,700,500)
c.Divide(2,2)
c.cd(1)
hist_bottom.GetXaxis().SetTitle("Distance Between Sides [mm]")
hist_bottom.Draw("h")
c.cd(2)
hist_right.GetXaxis().SetTitle("Distance Between Sides [mm]")
hist_right.Draw("h")
c.cd(3)
hist_top.GetXaxis().SetTitle("Distance Between Sides [mm]")
hist_top.Draw("h")
c.cd(4)
hist_left.GetXaxis().SetTitle("Distance Between Sides [mm]")
hist_left.Draw("h")
c1 = ROOT.TCanvas("all distances between sides")
c1.cd()
hist_total.GetXaxis().SetTitle("Distance Between Sides [mm]")
hist_total.Draw("h")
input("press any key to close")
#plt.scatter(px, py, label='Scatter Plot', color='blue', marker='o')
#plt.xlabel('X-axis')
#plt.ylabel('Y-axis')
#plt.show()
#
#distances_x = []
#distances_y = []
#for i in range(int(len(px)/n_points_per_file)):
# distances_x.append(-1*px[i*n_points_per_file]+px[i*n_points_per_file+1]-2.)
# distances_x.append(px[i*n_points_per_file+4]-px[i*n_points_per_file+5]-2.)
# distances_y.append(-1*py[i*n_points_per_file+2]+py[i*n_points_per_file+3]-2.)
# distances_y.append(py[i*n_points_per_file+6]-py[i*n_points_per_file+7]-2.)
# Create histograms for X and Y distances
#plt.figure(figsize=(12, 4))
#plt.subplot(1, 2, 1)
#plt.hist(distances_x, bins=20, color='blue', edgecolor='black')
#plt.xlabel('Distance along X-axis')
#plt.ylabel('Frequency')
#plt.title('Histogram of X Distances')
#
#plt.subplot(1, 2, 2)
#plt.hist(distances_y, bins=20, color='green', edgecolor='black')
#plt.xlabel('Distance along Y-axis')
#plt.ylabel('Frequency')
#plt.title('Histogram of Y Distances')
#
#plt.tight_layout()
#plt.show()