-
Notifications
You must be signed in to change notification settings - Fork 1
/
acquisition.py
297 lines (255 loc) · 9.05 KB
/
acquisition.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import serial
import numpy as np
import os
import matplotlib.pyplot as plt
class SerialDataClass:
def __init__(self):
self.to_connect = True
self.word = ''
self.file_path = ''
self.ser = ''
self.data_dict = {}
self.n_data = 201
self.commands = {
"setWord" : self.setWord,
"getData" : self.readSerialData,
"computeAverage" : self.computeAverage,
"showData" : self.showData,
"cleanData" : self.cleanData,
"compareData" : self.compareData,
"help" : self.print_help,
"exit" : self.stop
}
def computeAverage(self):
print("Les moyennes sont :")
avs = []
for each_key, each_val in self.data_dict.items():
av = sum(each_val) / len(each_val)
# print("\t-", each_key, " : ", av)
avs.append(av)
rev_table = avs[::-1]
print(rev_table[1::])
print("Temps : ", avs[-1])
def showData(self):
# To show and clean the data
# Find bad data
bad_index = []
for each_key, each_val in self.data_dict.items():
average = np.mean(each_val)
std = np.std(each_val)
for idx, val in enumerate(each_val):
error = abs(val-average)
if error > 3*std:
if idx not in bad_index:
bad_index.append(idx)
# Create plot lists
x = []
y = []
colors = []
for each_key, each_val in self.data_dict.items():
av = sum(each_val) / len(each_val)
for idx, val in enumerate(each_val):
y.append(val)
x.append(each_key)
if idx in bad_index:
colors.append("red")
else:
colors.append("blue")
y.append(av)
x.append(each_key)
colors.append("green")
# Plot
plt.scatter(x, y, c = colors)
plt.title('Data du mot : ' + self.word)
plt.xlabel('Indice')
plt.ylabel('Valeurs')
plt.show()
# Process
if len(bad_index) > 0:
print("Bad data found at index : ", bad_index)
ans = input("Do you want to remove them? (y/n) : ")
if ans == 'y':
for each_key, each_val in self.data_dict.items():
for index in sorted(bad_index, reverse=True):
del each_val[index]
self.saveData()
else:
print("No bad data found")
def cleanData(self):
bad_index = []
for each_key, each_val in self.data_dict.items():
average = np.mean(each_val)
std = np.std(each_val)
if each_key == 40:
print("40")
for idx, val in enumerate(each_val):
error = abs(val-average)
if error > 3*std:
if idx not in bad_index:
bad_index.append(idx)
print("Index des mauvaises données : ", bad_index)
x = []
y = []
colors = []
for each_key, each_val in self.data_dict.items():
for idx, val in enumerate(each_val):
y.append(val)
x.append(each_key)
if idx in bad_index:
colors.append("red")
else:
colors.append("blue")
plt.scatter(x, y, c = colors)
plt.title('Data du mot : ' + self.word)
plt.xlabel('Indice')
plt.ylabel('Valeurs')
plt.show()
if len(bad_index) > 0:
ans = input("Do you want to remove them? (y/n) : ")
if ans == 'y':
for each_key, each_val in self.data_dict.items():
for index in sorted(bad_index, reverse=True):
del each_val[index]
self.saveData()
else:
print("No bad data found")
def compareData(self):
print("Comparing data ...")
x = []
y = []
colors = []
x_av = []
y_av = []
colors_av = []
for each_key, each_val in self.data_dict.items():
av = sum(each_val) / len(each_val)
y_av.append(av)
x_av.append(each_key)
colors_av.append("green")
self.ser.flushInput()
self.ser.flushOutput()
if self.word != '':
try:
while True:
serialData = self.ser.readline().decode('utf-8')
print(serialData)
if serialData.startswith('#'):
x_data = []
y_data = []
colors_data = []
data = serialData.replace(' ', '')
data = data.split(',')
data[0] = data[0][1:-1]
data[-1] = data[-1].rstrip(', \n\r')
for idx, each_data in enumerate(data):
if each_data is not '':
y_data.append(float(each_data))
x_data.append(idx)
colors_data.append("blue")
x = x_av + x_data
y = y_av + y_data
colors = colors_av + colors_data
plt.scatter(x, y, c = colors)
plt.title('Data du mot : ' + self.word)
plt.xlabel('Indice')
plt.ylabel('Valeurs')
plt.show()
self.ser.flushInput()
self.ser.flushOutput()
except KeyboardInterrupt:
print("Interrupted")
else:
print("Set word first")
# Set word
def setWord(self, word_to_set):
self.word = word_to_set
print("Word set to :", self.word)
self.file_path = os.path.join("Data", self.word+'.npy')
self.initialize_dict()
# Read Serial data
def readSerialData(self):
print("Reading data ...")
self.ser.flushInput()
self.ser.flushOutput()
data_count = 1
if self.word != '':
try:
while True:
serialData = self.ser.readline().decode('utf-8')
if serialData.startswith('#'):
print("Data #", data_count)
data_count += 1
print(serialData)
data = serialData.replace(' ', '')
data = data.split(',')
data[0] = data[0][1:-1]
data[-1] = data[-1].rstrip(', \n\r')
for each_key, each_vals in self.data_dict.items():
each_vals.append(float(data[each_key]))
# print("Data #", data_count, end = '')
except KeyboardInterrupt:
print("Interrupted")
self.saveData()
else:
print("Set word first")
def saveData(self):
ans = input("Save data? (y/n) : ")
if ans is "y":
np.save(self.file_path, self.data_dict)
print("Data saved")
else:
pass
def getCommand(self):
command = input("\nEnter command : ")
command = command.split(' ')
func_name = command[0]
try:
arg = command[1]
except:
arg = ''
func = self.commands[func_name]
if arg != '':
func(arg)
else:
func()
# Connect to Serial Port
def connect(self):
port_open = False
print("Connecting...")
while not port_open:
try:
self.ser = serial.Serial("COM18", timeout=None, baudrate=115000, xonxoff=False, rtscts=False, dsrdtr=False)
self.ser.flushInput()
self.ser.flushOutput()
port_open = True
print("Connected to ", self.ser.name)
except:
print("Connection failed")
raise
# Load saved data
def initialize_dict(self):
if os.path.isfile(self.file_path):
self.data_dict = np.load(self.file_path, allow_pickle=True).item()
print("Data loaded")
else:
for i in range(0, self.n_data):
self.data_dict[i] = []
print("No data found, creating new set")
def stop(self):
os._exit(0)
def print_help(self):
print("Possible commands :")
for each_key in self.commands.keys():
print('\t', each_key)
def main():
serData = SerialDataClass()
if serData.to_connect:
serData.connect()
while True:
try:
serData.getCommand()
except KeyboardInterrupt:
print("End of program")
os._exit(0)
if __name__ == "__main__":
main()