forked from AlbertojacGon/painless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_resting_EEG_COLD.py
executable file
·368 lines (305 loc) · 13.8 KB
/
5_resting_EEG_COLD.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 15 22:31:21 2023
includes softreset of the EEG
@author: albertogonzalezv
"""
import explorepy
import serial
import csv,os, time
import tkinter as tk
from datetime import datetime
from os import sep
import serial.tools.list_ports
from explorepy.tools import get_local_time
from explorepy.command import SoftReset
eeg_duration = 300
dev_name = "Explore_8532"
folder = "Resting_EEG_Cold"
now = datetime.now()
current_time = now.strftime("%Y_%m_%d_%H_%M_%S")
explore = None
serCPM = None
time_cont = 0
STOP =0
incomingCPM = []
# connect
def connect_explore(dev_name):
global explore
explore = explorepy.Explore()
explorepy.set_bt_interface('sdk')
explore.connect(device_name=dev_name_entry.get())
eeg_dev = True
if eeg_dev:
connect_button.config(state=tk.DISABLED)
start_button.config(state="normal")
def on_close():
global explore
if explore:
cmd = SoftReset()
explore.stream_processor.configure_device(cmd)
explore.disconnect()
if serCPM:
serCPM.close()
root.destroy()
def start_rec():
global folder, STOP, time_cont
if test_user():
user_id = id_entry.get()
explore.record_data(file_name=(folder + sep + user_id + current_time+'_' + str(time_cont)+'rsEEG'), duration=eeg_duration, file_type='csv')
explore.set_marker(code=888)
stop_button.config(state="normal")
start_button.config(state="disabled")
STOP = False
while time_cont<eeg_duration:
t0=time.time()
if not STOP:
while time.time()-t0<0.995:
root.update()
time.sleep(.001)
time_cont+=1
if time_cont<5:
explore.set_marker(code=888)
crono.config(text = ("Seconds " +str(time_cont) +'/'+str(eeg_duration)))
else: break
time.sleep(.001)
def stop_rec():
global STOP
explore.set_marker(code=889)
time.sleep(.01)
explore.stop_recording()
STOP = True
start_button.config(state="normal")
stop_button.config(state="disabled")
def test_user():
user_id = id_entry.get()
if user_id == '':
tk.messagebox.showerror("Error", "Please enter an ID")
return False
else:
return True
def start_save():
global folder
if not os.path.exists(folder):
os.makedirs(folder)
def save_comments():
global folder
if test_user():
if not os.path.exists(folder):
os.makedirs(folder)
user_id = id_entry.get()
notes = notes_entry.get()
with open((folder +sep+ user_id +'_' + current_time +'NOTESrsEEG.csv'), mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, 'COMMENT', notes,get_local_time()])
notes_save_button.config(state="disabled") # disable the button
notes_save_button.after(100, lambda: notes_save_button.config(state="normal"))
notes_entry.delete(0, tk.END)
def save_handUp():
global folder
user_id = id_entry.get()
with open((folder +sep+ user_id +'_' + current_time +'NOTESrsEEG.csv'), mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, 'HAND UP', ' ',get_local_time()])
handdwButt.config(state="normal")
handupButt.config(state="disabled")
def save_handDw():
global folder
user_id = id_entry.get()
with open((folder +sep+ user_id +'_' + current_time +'NOTESrsEEG.csv'), mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, 'HAND DOWN',' ', get_local_time()])
handdwButt.config(state="disabled")
handupButt.config(state="normal")
def save_pain():
global folder
user_id = id_entry.get()
with open((folder +sep+ user_id +'_' + current_time +'NOTESrsEEG.csv'), mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, 'FinalPain', pain_scale.get() ,get_local_time()])
pain_saved_label.config(text='Done')
def save_other(data1, data2):
global folder
user_id = id_entry.get()
with open((folder +sep+ user_id +'_' + current_time +'NOTES.csv'), mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([user_id, data1, data2, get_local_time()])
handdwButt.config(state="disabled")
handupButt.config(state="normal")
def stream():
#explore.acquire()
explore.visualize( bp_freq=(1, 30), notch_freq=50)
def impeds():
explore.measure_imp()
def send_dataCPM(data_out): # send by serial
if serCPM!=False:
for i in data_out:
serCPM.write(i.encode())
time.sleep(0.002)
def connectCPM():
global serCPM
if test_user():
portCPM_idx = portCPM_listbox.curselection()[0] # Obtiene el índice de la selección actual
portCPM = portCPM_listbox.get(portCPM_idx) # Obtiene el puerto seleccionado en el índice
baud_rate = 115200
try:
serCPM = serial.Serial(portCPM, baud_rate)
statusCPM_label.config(text="Cold Plate Connected to " + portCPM)
connectCPM_button.config(state="disabled")
disconnectCPM_button.config(state="normal")
SetCPButt.config(state="normal")
CPButt.config(state="normal")
handupButt.config(state="normal")
notes_save_button.config(state="normal")
pain_save_button.config(state="normal")
start_save()
#send_buttonCPM.config(state="normal")
send_dataCPM('C200') # temp to 20.0
time.sleep(0.005)
#temps_message.config(text="Select CPM Pain5 temp and wait for the Cold Plate to reach 10C")
except serial.SerialException:
statusCPM_label.config(text="Error: Could not connect to " + portCPM)
def disconnectCPM():
global serCPM
if serCPM is not None and serCPM.isOpen():
serCPM.close()
statusCPM_label.config(text="Disconnected from " + serCPM.port)
connectCPM_button.config(state="normal")
disconnectCPM_button.config(state="disabled")
serCPM = None
def refreshCPM():
portsCPM = serial.tools.list_ports.comports()
portCPM_listbox.delete(0, tk.END)
for portCPM, desc, hwid in sorted(portsCPM):
portCPM_listbox.insert(tk.END, portCPM)
def update_tempsCPM():
global incomingCPM, serCPM
if serCPM is not None and serCPM.isOpen():
serCPM.write('E'.encode())
time.sleep(0.005)
if serCPM.inWaiting():
incomingCPM = ''
while serCPM.inWaiting():
incomingCPM = incomingCPM + serCPM.read().decode()
temps_read = incomingCPM.split()
temp = (float(temps_read[0])/10)
tempCP_message.config(text=('Current Cold Plate Temp:'+ str(temp) + 'C'))
root.after(1000, update_tempsCPM)
def change_tempsCPM(newtemp):
send_dataCPM(('C'+str(newtemp)+'0')) # temp to 10.0
time.sleep(0.005)
def sendCPtemp():
dataS = send_entryCP.get()
if int(dataS)<10:
dataS = '0' + dataS
data = 'C' +dataS+ '0'
send_dataCPM(data)
save_other('ColdPlateTemp',dataS)
def coldTest():
global start_time
cont = 0
tWait = 30
start_time = time.time()
while cont<=tWait:
if (time.time()-start_time)>cont:
tempCP_message2.config(text=('Wait for:'+str(tWait-cont)))
cont+=1
tempCP_message2.update_idletasks()
root.update()
root = tk.Tk()
root.title("Resting EEG during Cold Pain")
root.iconbitmap('painlessLogo.ico')
connect_frame = tk.Frame(root)
connect_frame.pack(padx=0, pady=1)
portEXP_frame = tk.LabelFrame(connect_frame, bd=1,text="Explorepy Connect",font=('calibri', 16), relief=tk.SOLID,pady=10, padx=10)
portEXP_frame.grid(row=1, column =0 , rowspan=3, columnspan=2, padx=5)
dev_label = tk.Label(portEXP_frame, text="EEG device:")
dev_label.grid(row=0, column =0, padx=5)
dev_name_entry = tk.Entry(portEXP_frame)
dev_name_entry.insert(0, dev_name)
dev_name_entry.grid(row=0, column =1, columnspan=2, padx=5)
connect_button = tk.Button(portEXP_frame, text="Connect", command=lambda: connect_explore(dev_name_entry.get()))
connect_button.grid(row=1, column =1, columnspan=2, padx=5)
id_frame = tk.LabelFrame(connect_frame, bd=1,text="Participant ID",font=('calibri', 16), relief=tk.SOLID,pady=5, padx=10)
id_frame.grid(row=0, column =0 , rowspan=1, columnspan=2, padx=5)
id_label = tk.Label(id_frame, text="Participant ID:")
id_label.grid(row=0, column =0, padx=1)
id_entry = tk.Entry(id_frame)
id_entry.grid(row=0, column =1, padx=1)
portCPM_frame = tk.LabelFrame(connect_frame, bd=1,text="Cold Plate connection",font=('calibri', 16), relief=tk.SOLID,pady=10, padx=10)
portCPM_frame.grid(row=0, column =3 , rowspan=4, columnspan=4, padx=5)
# port_message = tk.Label(port_frame, text = ('Select de TCS port'), font=('calibri', 12))
# port_message.pack()
portCPM_label = tk.Label(portCPM_frame, text="Select Cold Plate Port:")
portCPM_label.grid(row=0, column = 0)
#port_listbox = tk.Listbox(port_frame, width=10, height=5)
portCPM_listbox = tk.Listbox(portCPM_frame, width=30, height=3, exportselection=False, relief=tk.RIDGE)
portCPM_listbox.grid(row=0, column = 2)
portCPM_scrollbar = tk.Scrollbar(portCPM_frame, orient=tk.VERTICAL)
portCPM_scrollbar.grid(row=0, column = 3)
portCPM_listbox.config(yscrollcommand=portCPM_scrollbar.set)
portCPM_scrollbar.config(command=portCPM_listbox.yview)
statusCPM_label = tk.Label(portCPM_frame , text="Cold Plate Not connected", bd=1, relief=tk.SUNKEN, anchor=tk.W)
statusCPM_label.grid(row=1, column = 0, columnspan=3)
buttonCPM_frame = tk.Frame(portCPM_frame)
buttonCPM_frame.grid(row=2, column = 0, columnspan=3)
connectCPM_button = tk.Button(buttonCPM_frame, text="Connect", command=connectCPM)
connectCPM_button.grid(row=2, column = 1)
disconnectCPM_button = tk.Button(buttonCPM_frame, text="Disconnect", command=disconnectCPM, state="disabled")
disconnectCPM_button.grid(row=2, column = 2)
refreshCPM_button = tk.Button(buttonCPM_frame, text="Refresh list", command=refreshCPM)
refreshCPM_button.grid(row=2, column = 3)
temps_frame = tk.LabelFrame(root, bd=1,text="Cold Plate temperature setting",font=('calibri', 16), relief=tk.SOLID,pady=10, padx=10)
temps_frame.pack(padx=10)
tempCP_m1 = tk.Label(temps_frame, text = ('Desired Cold Plate Temp:'), font=('calibri', 14))
tempCP_m1.grid(row=0, column=0, padx=(0,5))
valuesCP = list(range(2, 36))
send_varCP = tk.StringVar()
send_varCP.set(valuesCP[18]) # set initial value to first item in list
send_entryCP = tk.Spinbox(temps_frame, from_=2, to=36, increment=2, textvariable=send_varCP, width=3)
send_entryCP.grid(row=0, column=1, padx=(0,5))
SetCPButt = tk.Button(temps_frame, text="Set Temp", command=sendCPtemp,state="disabled")
SetCPButt.grid(row=0, column=2, padx=(0,5))
tempCP_message = tk.Label(temps_frame, text = ('Current Cold Plate Temp:'), font=('calibri', 14))
tempCP_message.grid(row=0, column=3, padx=(0,25))
CPButt = tk.Button(temps_frame, text="Start Cold Test", command=coldTest,state="disabled")
CPButt.grid(row=0, column=4, padx=(0,5))
tempCP_message2 = tk.Label(temps_frame, text = ('Wait for:'), font=('calibri', 14), width=12)
tempCP_message2.grid(row=0, column=5, padx=(25,5))
tempCP_m2 = tk.Label(temps_frame, text = ('If the pain intensity rating is higher than 5/10, increase the cold plate temperature by 2C (more warm). \nConversely, if the pain intensity rating is lower than 3/10, decrease the cold plate temperature by 2C (more cold). \nRepeat this process until the pain intensity is between 3/10 and 5/10'), font=('calibri', 12))
tempCP_m2.grid(row=1, column=0, columnspan=5)
notes_frame = tk.LabelFrame(root, bd=1,text="Annotations",font=('calibri', 16), relief=tk.SOLID,pady=10, padx=10)
notes_frame.pack(padx=10, pady=10, fill=tk.X)
notes_label = tk.Label(notes_frame, text="Comments:")
notes_label.grid(row=0, column=0, padx=(0,5))
notes_entry = tk.Entry(notes_frame)
notes_entry.grid(row=0, column=1, padx=(0,5))
notes_save_button = tk.Button(notes_frame, text="Save comments", command=save_comments,state="disabled")
notes_save_button.grid(row=0, column=2, padx=(0,5))
handupButt = tk.Button(notes_frame, text="Hand is Up!!", command=save_handUp,state="disabled")
handupButt.grid(row=0, column=3, padx=(0,5))
handdwButt = tk.Button(notes_frame, text="Hand is Down", command=save_handDw,state="disabled")
handdwButt.grid(row=0, column=4, padx=(0,5))
hand_m1 = tk.Label(notes_frame, text = "Pain intensity ratings should be stored with 'Save Comments'\n as soon as the participant lift the hand from the plate and when it is placed back on the cold plate", font=('calibri', 12))
hand_m1.grid(row=1, column=0, columnspan =5)
score_label = tk.Label(notes_frame, text="Final Pain level:", font=("Helvetica", 14))
score_label.grid(row=2, column=0, padx=(0,5))
pain_scale = tk.Scale(notes_frame, from_=0, to=10, length=300, orient=tk.HORIZONTAL, font=("Helvetica", 12))
pain_scale.grid(row=2, column=1,columnspan=2, padx=(0,5))
pain_save_button = tk.Button(notes_frame, text="Save Final Pain", command=save_pain,state="disabled", font=("Helvetica", 12))
pain_save_button.grid(row=2, column=3, padx=(0,5))
pain_saved_label = tk.Label(notes_frame, text=" ", font=("Helvetica", 12))
pain_saved_label.grid(row=2, column=4, padx=(0,5))
cronoFrame = tk.Frame(root)
cronoFrame.pack()
crono = tk.Label(cronoFrame, text=("Seconds 0/" + str(eeg_duration)), font=("Helvetica", 20))
crono.pack(pady=10)
start_button = tk.Button(cronoFrame , text="Start", font=("Helvetica", 18),state=tk.DISABLED, command=start_rec)
start_button.pack(side=tk.LEFT, padx=10, pady=(0,20))
stop_button = tk.Button(cronoFrame , text="Stop", font=("Helvetica", 18), state=tk.DISABLED, command=stop_rec)
stop_button.pack(side=tk.LEFT, padx=10, pady=(0,20))
refreshCPM()
update_tempsCPM()
root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()