-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect.py
84 lines (67 loc) · 2.69 KB
/
collect.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
import os
import sys
import random
import time
import threading
from pylsl import StreamInlet, resolve_stream
import PySimpleGUI as sg
import pandas as pd
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join( BASE_DIR, "data" )
prompt_entries = [] # format of each entry is timestamp, wordprompt
def get_prompt():
# write the words being chosen into a list with timestamps
# write to csv afterwards
prompt = random.choice(WORDS)
prompt_entries.append([time.time(), prompt])
return prompt
def display_words():
# define experiment layout
layout = [
[sg.Text(get_prompt(), key="wordprompt", size=(50, 1), justification='center', font=("Helvetica", 25))],
[sg.Input(key='wordinput', change_submits=True, size=(50,1), justification='center', font=("Helvetica", 25))],
]
# Create the window
window = sg.Window("braindump",
layout, auto_size_text=True, finalize=True,
text_justification='center')
window.maximize()
sg.popup("Please type the prompted word in text area", title='braindump')
# Create an event loop
while True:
event, values = window.read()
# End program if user closes window or
# presses the OK button
if event == sg.WIN_CLOSED:
break
# if the input value is equal to
if window['wordinput'].get() == window['wordprompt'].get():
sg.popup("Word match! Displaying next word in 3", auto_close = True, auto_close_duration = 3)
# empty input field
window['wordinput'].update('')
# generate next prompt
window['wordprompt'].update(get_prompt())
window.close()
def activate_lsl_stream():
try:
# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = resolve_stream('type', 'EEG')
# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])
print("EEG Stream created")
except KeyboardInterrupt as e:
print("Ending program")
raise e
def write_outputs_to_csv(sessionname):
# get write the time list to a csv
prompt_df = pd.DataFrame(prompt_entries)
prompt_df.to_csv(DATA_DIR + '\\{}-wordprompts.csv'.format(sessionname),
index=False, header=['timestamp', 'wordprompt'])
if __name__ == '__main__':
if len(sys.argv) != 2:
raise ValueError("Specify an identifier for this session. Same with what was sent to curiarecorder")
activate_lsl_stream()
display_words()
write_outputs_to_csv(sys.argv[1])