generated from NeuroTech-UCSD/Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
oz-speller.py
1693 lines (1558 loc) · 81.5 KB
/
oz-speller.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
SSVEP Offline + Realtime Experiment
Notes:
- Press esc to quit
- MAKE SURE refresh_rate IS SET TO YOUR MONITOR'S REFRESH RATE
- set use_dsi_lsl and make_predictions to True for regular use
- set use_dsi_lsl and make_predictions to False and dummy_mode to True for dummy mode
"""
from psychopy import visual, core
from psychopy.hardware import keyboard
import numpy as np
from scipy import signal
import yaml
import json
import random
import sys, time, serial, pickle
from pylsl import local_clock
sys.path.append('src') # if run from the root project directory
# █████████████████████████████████████████████████████████████████████████████
## VARIABLES
use_dsi7 = False
use_dsi_trigger = True
use_dsi_lsl = True
use_arduino = False # arduino photosensor for flashing timing test
use_cyton = False
use_photosensor = False
record_start_time = True
center_flash = False # whether the visual stimuli are only presented at the center of the screen
test_mode = True # whether the script indicates target squares and saves recorded data
home_screen = False
make_predictions = True # whether the script makes predictions using a pretrained model
dummy_mode = True
model = None
if make_predictions:
# with open("reports/trained_models/wsx32/fbtdca_1s.pkl", 'rb') as filehandler:
# with open("reports/trained_models/32-class_speller/DSI-7/Simon/fbtdca_1s6t.pkl", 'rb') as filehandler:
with open("reports/trained_models/32-class_speller/DSI-24/Aidan/fbtdca_1s.pkl", 'rb') as filehandler:
model = pickle.load(filehandler)
shuffled_positions = False
shuffled_initial_positions = False
random_positions = False
random_movements = False
random_linear_movements = False
width = 1536
height = 864
flash_mode = 'square' # 'sine', 'square', or 'chirp', 'dual band'
refresh_rate = 60.02 # refresh rate of the monitor
use_retina = False # whether the monitor is a retina display
stim_duration = 1.2 # in seconds
isi_duration = 1 # in seconds, used both pre and post stimulations
after_stim_padding = 0.0 # in seconds, stim remains but the data is discarded
n_per_class = 2
keyboard_classes = [(8, 0), (8, 0.5), (8, 1), (8, 1.5),
(9, 0), (9, 0.5), (9, 1), (9, 1.5),
(10, 0), (10, 0.5), (10, 1), (10, 1.5),
(11, 0), (11, 0.5), (11, 1), (11, 1.5),
(12, 0), (12, 0.5), (12, 1), (12, 1.5),
(13, 0), (13, 0.5), (13, 1), (13, 1.5),
(14, 0), (14, 0.5), (14, 1), (14, 1.5),
(15, 0), (15, 0.5), (15, 1), (15, 1.5), ]
dummy_keyboard_string = '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik,'
# keyboard_classes=[( 8,0),( 8,0.5),( 8,1),
# (10,0),(10,0.5),(10,1),
# (15,0),(15,0.5),(15,1),]
n_keyboard_classes = len(keyboard_classes)
# classes=[(8,0),(9,1.75),(10,1.5),(11,1.25),(12,1),(13,0.75),(14,0.5),(15,0.25),
# (8.2,0.35),(9.2,0.1),(10.2,1.85),(11.2,1.6),(12.2,1.35),(13.2,1.1),(14.2,0.85),(15.2,0.6),
# (8.4,0.7),(9.4,0.45),(10.4,0.2),(11.4,1.95),(12.4,1.7),(13.4,1.45),(14.4,1.2),(15.4,0.95),
# (8.6,1.05),(9.6,0.8),(10.6,0.55),(11.6,0.3),(12.6,0.05),(13.6,1.8),(14.6,1.55),(15.6,1.3),
# (8.8,1.4),(9.8,1.15),(10.8,0.9),(11.8,0.65),(12.8,0.4),(13.8,0.15),(14.8,1.9),(15.8,1.65)]
classes = keyboard_classes
data = []
run_count = 0
first_call = True
# █████████████████████████████████████████████████████████████████████████████
## FUNCTIONS
def get_content(dir="states/front_to_back.yaml", use_yaml=True):
if use_yaml:
with open(dir, "r") as file:
try:
content = yaml.safe_load(file)
return content
except yaml.YAMLError as exc:
print(exc)
else:
with open(dir, "r") as file:
content = json.load(file)
return content
def parse_chat_history(json_obj : dict):
chat_history_text = ''
content_list = json_obj['content']
line_count = 0
max_lines = 15
msg_start_ind = -1
for i_msg, msg in reversed(list(enumerate(content_list))):
n_lines = int(msg['n_lines'])
line_count += n_lines
if line_count <= max_lines:
msg_start_ind = i_msg
else:
break
content_list = content_list[msg_start_ind:]
for i_msg, msg in enumerate(content_list):
if i_msg != 0:
chat_history_text += '\n'
chat_history_text += msg['sender']
chat_history_text += ' '
chat_history_text += msg['timestamp']
chat_history_text += '\n'
chat_history_text += msg['text']
chat_history_text += '\n'
return chat_history_text
def update_text(new_text: str):
content = get_content()
with open("states/front_to_back.yaml", "w") as file:
try:
content['text'] += new_text
yaml.dump(content, file)
except yaml.YAMLError as exc:
print(exc)
def create_fixation_cross(size=50):
return visual.ShapeStim(
win=win,
units='pix',
size=size,
fillColor=[1, 1, 1],
lineColor=[1, 1, 1],
lineWidth=1,
vertices='cross',
name='off', # Used to determine state
pos=[0, 0]
)
def ms_to_frame(ms, fs):
dt = 1000 / fs
return np.round(ms / dt).astype(int)
def create_flickering_square(size=120, pos=[0, 0], color='white'):
return visual.Rect(
win=win,
units="pix",
width=size,
height=size,
fillColor=color,
# lineColor='white',
interpolate=False,
lineWidth=0,
pos=pos
)
def create_photosensor_dot(size=100):
return visual.Circle(
win=win,
units="pix",
radius=size,
fillColor='white',
lineColor='white',
lineWidth=1,
edges=32,
pos=(-(win_w / 2) + size, -((win_h / 2) - size))
)
def create_trial_sequence(n_per_class, classes=[(7.5, 0), (8.57, 0), (10, 0), (12, 0), (15, 0)]):
"""
Create a random sequence of trials with n_per_class of each class
Inputs:
n_per_class : number of trials for each class
Outputs:
seq : (list of len(10 * n_per_class)) the trial sequence
"""
seq = classes * n_per_class
random.seed()
random.shuffle(seq) # shuffles in-place
return seq
def create_keyboard():
keyboard = []
keyboard.extend(
[create_flickering_square(pos=[-width / 2 + 90 + i * 150, height / 2 - 90 - 200]) for i in range(10)])
keyboard.extend(
[create_flickering_square(pos=[-width / 2 + 90 + 70 + i * 150, height / 2 - 90 - 150 - 200]) for i in range(9)])
keyboard.extend(
[create_flickering_square(pos=[-width / 2 + 90 + 140 + i * 150, height / 2 - 90 - 300 - 200]) for i in
range(8)])
keyboard.extend([create_flickering_square(pos=[-width / 2 + 90 + 210, height / 2 - 90 - 450 - 200])])
keyboard.extend([visual.Rect(win=win, units='pix', width=100 * 5, height=100, fillColor='white', lineWidth=0,
pos=[-width / 2 + 130 * 5 + 70, height / 2 - 90 - 450 - 200])])
keyboard.extend(
[create_flickering_square(pos=[-width / 2 + 90 + 70 * 15 + i * 150, height / 2 - 90 - 450 - 200]) for i in
range(3)])
return keyboard
def create_9_keys(size=120, colors=[-1, -1, -1] * 9):
positions = []
positions.extend([[-width / 2 + 300, height / 2 - 90 - i * 270 - 80] for i in range(3)])
positions.extend([[-width / 2 + 450 + 300, height / 2 - 90 - i * 270 - 80] for i in range(3)])
positions.extend([[-width / 2 + 900 + 300, height / 2 - 90 - i * 270 - 80] for i in range(3)])
keys = visual.ElementArrayStim(win, nElements=9, elementTex=None, elementMask=None, units='pix', sizes=[size, size],
xys=positions, colors=colors)
return keys
def create_12_keys(size=120, colors=[-1, -1, -1] * 12):
positions = []
positions.extend([[-width / 2 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 450 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 900 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
keys = visual.ElementArrayStim(win, nElements=12, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_16_keys(size=120, colors=[-1, -1, -1] * 16):
positions = []
positions.extend([[-width / 2 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 200 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 400 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 600 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
keys = visual.ElementArrayStim(win, nElements=16, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_20_keys(size=120, colors=[-1, -1, -1] * 20):
positions = []
positions.extend([[-width / 2 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 200 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 400 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 600 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 800 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
keys = visual.ElementArrayStim(win, nElements=20, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_24_keys(size=120, colors=[-1, -1, -1] * 24):
positions = []
positions.extend([[-width / 2 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 200 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 400 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 600 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 800 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 1000 + 300, height / 2 - 90 - i * 200 - 80] for i in range(4)])
keys = visual.ElementArrayStim(win, nElements=24, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_28_keys(size=120, colors=[-1, -1, -1] * 28):
positions = []
positions.extend([[-width / 2 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 200 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 400 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 600 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 800 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 1000 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 1200 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
keys = visual.ElementArrayStim(win, nElements=28, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_32_keys(size=120, colors=[-1, -1, -1] * 33):
positions = []
positions.extend([[-width / 2 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 1 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 2 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 3 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 4 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 5 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 6 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 7 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[width / 2 - 40, height / 2 - 40]])
keys = visual.ElementArrayStim(win, nElements=33, elementTex=None, elementMask=None, units='pix',
sizes=[size, size], xys=positions, colors=colors)
return keys
def create_key_caps(text_strip, el_mask, phases, colors=[-1, -1, -1] * 26):
positions = []
positions.extend([[-width / 2 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 1 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 2 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 3 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 4 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 5 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 6 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[-width / 2 + 190 * 7 + 100, height / 2 - 90 - i * 200 - 80] for i in range(4)])
positions.extend([[width / 2 - 40, height / 2 - 40]])
els = visual.ElementArrayStim(
win=win,
units="pix",
nElements=33,
sizes=text_strip.shape,
xys=positions,
phases=phases,
colors=colors,
elementTex=text_strip,
elementMask=el_mask)
return els
# █████████████████████████████████████████████████████████████████████████████
if use_dsi_lsl:
from subprocess import Popen, PIPE
import signal as sig
import os
from pylsl import StreamInfo, StreamOutlet, StreamInlet, resolve_stream, resolve_streams, local_clock
from threading import Thread, Event
from multiprocessing import Process
def get_lsl_data(save_variable):
""" 'get data from lsl' and save them onto 'save_variable'
Continuously collect specified channels data from the Lab Streaming Layer(LSL),
not necessarily just the EEG.
(LSL is commonly used in EEG labs for different devices to push and pull
data from a shared network layer to ensure good synchronization and timing
across all devices)
Parameters
----------
save_variable : empty list --> [timepoints by channels]
where channels =
[timestamp, types[0]*num_channels_of_type[0] ...]
the variable to save the data onto
types: len(types) list of str
specifies the source types of the streams you want to get data from
Returns
-------
inlets : some length list of pylsl.StreamInlet objects
pull_thread : the thread instance that pulls data from the LSL constantly
Note
----
To properly end the pull_thread, call all inlet.close_stream() right before
you call board.stop_stream() If this isn't done, the program could freeze or
show error messages. Do not lose the inlets list
Examples
--------
>>> save_variable = []
>>> inlets, _ = get_eeg_lsl(save_variable) # to start pulling data from lsl
...
>>> for inlet in inlets:\
>>> inlet.close_stream()
>>> print(save_variable)
"""
streams = []
inlets = []
streams = resolve_streams()
for stream in streams:
inlets.append(StreamInlet(stream))
if inlets == None or len(inlets) == 0:
raise Exception("Error: no stream found.")
def save_sample(inlets, save_variable):
global record_start_time
while True:
row_data = [0]
inlet_idx = 0
while inlet_idx < len(inlets): # iterate through the inlets
sample, timestamp = inlets[inlet_idx].pull_sample()
if record_start_time:
with open("meta.csv", 'w') as csv_file:
csv_file.write('0,0,' + str(local_clock()) + '\n')
record_start_time = False
if (sample, timestamp) != (None, None):
if inlet_idx is (len(inlets) - 1):
row_data[0] = timestamp
row_data.extend(sample)
inlet_idx += 1 # move on to next inlet
else:
time.sleep(0.0001) # wait for 0.1 ms if the data is not there yet
# just to save some processing power
save_variable.append(row_data)
pull_thread = Thread(target=save_sample, args=(inlets, save_variable))
pull_thread.daemon = True
# pull_thread = Process(target = save_sample, args=(inlets, save_variable,), daemon=True)
pull_thread.start()
return inlets, pull_thread
# p = Popen([os.path.join(os.getcwd(), 'src', 'dsi2lsl-win', 'dsi2lsl.exe'), '--port=COM8',
# '--lsl-stream-name=mystream'],shell=True,stdin=PIPE) #COM4 or 8 for dsi-7 or COM12 for dsi-24
p = Popen(
[os.path.join(os.getcwd(), 'src', 'dsi2lsl-win', 'dsi2lsl.exe'), '--port=COM7', '--lsl-stream-name=mystream'],
shell=True, stdin=PIPE) # COM4 or 8 for dsi-7 or COM12 for dsi-24
with open("eeg.csv", 'w') as csv_file:
# csv_file.write('time, Pz, F4, C4, P4, P3, C3, F3, TRG\n') # For DSI-7
csv_file.write(
'time, P3, C3, F3, Fz, F4, C4, P4, Cz, Pz, Fp1, Fp2, T3, T5, O1, O2, X3, X2, F7, F8, X1, A2, T6, T4, '
'TRG\n') # For DSI-24
with open("meta.csv", 'w') as csv_file:
csv_file.write('')
time.sleep(15)
if use_dsi_trigger:
# dsi_serial = serial.Serial('COM2',115200) # 2 for serial trigger or 13 for trigger hub
dsi_serial = serial.Serial('COM8', 9600) # 2 for serial trigger or 13 for trigger hub
eeg = [] # receive_data() saves [timepoints by channels] here
print(resolve_streams())
inlets, _ = get_lsl_data(eeg)
# █████████████████████████████████████████████████████████████████████████████
## DSI-7
if use_dsi7:
import dsi, ctypes, multiprocessing, threading, serial
SampleCallback = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_double, ctypes.c_void_p)
@SampleCallback
def ExampleSampleCallback_Signals(headsetPtr, packetTime, userData):
global run_count
global data
global first_call
h = dsi.Headset(headsetPtr)
sample_data = [packetTime] # time stamp
sample_data.extend([ch.ReadBuffered() for ch in h.Channels()]) # channel voltages
data.append(sample_data)
run_count += 1
if first_call:
if sample_data[1] > 1e15: # if Pz saturation error happens
quit()
with open("meta.csv", 'w') as csv_file:
# csv_file.write(str(time.time()) + '\n')
csv_file.write('0,0,' + str(local_clock()) + '\n')
first_call = False
if run_count >= 300: # save data every second
run_count = 0
data_np = np.array(data)
with open("eeg.csv", 'a') as csv_file:
np.savetxt(csv_file, data_np, delimiter=',')
data = []
def record():
args = getattr(sys, 'argv', [''])
if sys.platform.lower().startswith('win'):
default_port = 'COM8' # COM4, COM8, COM9
else:
default_port = '/dev/cu.DSI7-0009.BluetoothSeri'
# first command-line argument: serial port address
if len(args) > 1:
port = args[1]
else:
port = default_port
# second command-line argument: name of the Source to be used as reference, or the word 'impedances'
if len(args) > 2:
ref = args[2]
else:
ref = ''
headset = dsi.Headset()
headset.Connect(port)
headset.SetSampleCallback(ExampleSampleCallback_Signals, 0)
headset.StartDataAcquisition()
with open("eeg.csv", 'w') as csv_file:
csv_file.write('time, ' + ', '.join([ch.GetName() for ch in headset.Channels()]) + '\n')
while True:
headset.Idle(2.0)
if __name__ == "__main__":
# recording = multiprocessing.Process(target=record,daemon=True)
recording = threading.Thread(target=record, daemon=True)
recording.start()
if use_dsi_trigger:
dsi_serial = serial.Serial('COM2', 115200)
time.sleep(10)
# █████████████████████████████████████████████████████████████████████████████
## Arduino Photosensor for Timing
if use_arduino:
from sys import executable
import os
from subprocess import Popen
# Popen([executable, os.path.join(os.getcwd(), 'run_arduino_photosensor.py')])
Popen([executable, os.path.join(os.getcwd(), 'scripts', 'run_arduino_photosensor.py')])
time.sleep(2)
# █████████████████████████████████████████████████████████████████████████████
## OpenBCI Cyton
if use_cyton:
import glob
from brainflow.board_shim import BoardShim, BrainFlowInputParams
from pylsl import StreamInfo, StreamOutlet, StreamInlet, resolve_stream, local_clock
from serial import Serial
from threading import Thread, Event
CYTON_BOARD_ID = 0
BAUD_RATE = 115200
ANALOGUE_MODE = '/2' # Reads from analog pins A5(D11), A6(D12) and if no
# wifi shield is present, then A7(D13) as well.
def find_openbci_port():
"""Finds the port to which the Cyton Dongle is connected to."""
# Find serial port names per OS
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
ports = glob.glob('/dev/ttyUSB*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/cu.usbserial*')
else:
raise EnvironmentError('Error finding ports on your operating system')
openbci_port = ''
for port in ports:
try:
s = Serial(port=port, baudrate=BAUD_RATE, timeout=None)
s.write(b'v')
line = ''
time.sleep(2)
if s.inWaiting():
line = ''
c = ''
while '$$$' not in line:
c = s.read().decode('utf-8', errors='replace')
line += c
if 'OpenBCI' in line:
openbci_port = port
s.close()
except (OSError, serial.SerialException):
pass
if openbci_port == '':
raise OSError('Cannot find OpenBCI port.')
else:
return openbci_port
def start_cyton_lsl():
""" 'start streaming cyton to lsl'
Stream EEG and analogue(AUX) data from Cyton onto the Lab Streaming
Layer(LSL).
(LSL is commonly used in EEG labs for different devices to push and pull
data from a shared network layer to ensure good synchronization and timing
across all devices)
Returns
-------
board : board instance for the amplifier board, in this case OpenBCI Cyton
push_thread : the thread instance that pushes data onto the LSL constantly
Note
----
To properly end the push_thread, call board.stop_stream(). If this isn't done,
the program could freeze or show error messages. Do not lose the board instance
Examples
--------
>>> board, _ = start_lsl() # to start pushing onto lsl
...
>>> board.stop_streaming() # to stop pushing onto lsl
"""
# print("Creating LSL stream for EEG. \nName: OpenBCIEEG\nID: OpenBCItestEEG\n")
info_eeg = StreamInfo('OpenBCIEEG', 'EEG', 8, 250, 'float32', 'OpenBCItestEEG')
# print("Creating LSL stream for AUX. \nName: OpenBCIAUX\nID: OpenBCItestEEG\n")
info_aux = StreamInfo('OpenBCIAUX', 'AUX', 3, 250, 'float32', 'OpenBCItestAUX')
outlet_eeg = StreamOutlet(info_eeg)
outlet_aux = StreamOutlet(info_aux)
params = BrainFlowInputParams()
params.serial_port = find_openbci_port()
board = BoardShim(CYTON_BOARD_ID, params)
board.prepare_session()
res_query = board.config_board('/0')
print(res_query)
res_query = board.config_board('//')
print(res_query)
res_query = board.config_board(ANALOGUE_MODE)
print(res_query)
board.start_stream(45000)
time.sleep(1)
stop_event = Event()
def push_sample():
start_time = local_clock()
sent_eeg = 0
sent_aux = 0
while not stop_event.is_set():
elapsed_time = local_clock() - start_time
data_from_board = board.get_board_data()
required_eeg_samples = int(250 * elapsed_time) - sent_eeg
eeg_data = data_from_board[board.get_eeg_channels(CYTON_BOARD_ID)]
datachunk = []
for i in range(len(eeg_data[0])):
datachunk.append(eeg_data[:, i].tolist())
stamp = local_clock()
outlet_eeg.push_chunk(datachunk, stamp)
sent_eeg += required_eeg_samples
required_aux_samples = int(250 * elapsed_time) - sent_aux
aux_data = data_from_board[board.get_analog_channels(CYTON_BOARD_ID)]
datachunk = []
for i in range(len(aux_data[0])):
datachunk.append(aux_data[:, i].tolist())
stamp = local_clock()
outlet_aux.push_chunk(datachunk, stamp)
sent_aux += required_aux_samples
time.sleep(0.02) # 20 ms
push_thread = Thread(target=push_sample)
push_thread.start()
return board, stop_event
def get_lsl_data(save_variable, types=['EEG', 'AUX']):
""" 'get data from lsl' and save them onto 'save_variable'
Continuously collect specified channels data from the Lab Streaming Layer(LSL),
not necessarily just the EEG.
(LSL is commonly used in EEG labs for different devices to push and pull
data from a shared network layer to ensure good synchronization and timing
across all devices)
Parameters
----------
save_variable : empty list --> [timepoints by channels]
where channels =
[timestamp, types[0]*num_channels_of_type[0] ...]
the variable to save the data onto
types: len(types) list of str
specifies the source types of the streams you want to get data from
Returns
-------
inlets : some length list of pylsl.StreamInlet objects
pull_thread : the thread instance that pulls data from the LSL constantly
Note
----
To properly end the pull_thread, call all inlet.close_stream() right before
you call board.stop_stream() If this isn't done, the program could freeze or
show error messages. Do not lose the inlets list
Examples
--------
>>> save_variable = []
>>> inlets, _ = get_eeg_lsl(save_variable) # to start pulling data from lsl
...
>>> for inlet in inlets:\
>>> inlet.close_stream()
>>> print(save_variable)
"""
streams = []
inlets = []
for stream_type in types:
streams.extend(resolve_stream('type', stream_type))
for stream in streams:
inlets.append(StreamInlet(stream))
if inlets == None or len(inlets) == 0:
raise Exception("Error: no stream found.")
def save_sample(inlets, save_variable):
global record_start_time
while True:
row_data = [0]
inlet_idx = 0
while inlet_idx < len(inlets): # iterate through the inlets
sample, timestamp = inlets[inlet_idx].pull_sample()
if record_start_time:
with open("meta.csv", 'w') as csv_file:
csv_file.write('0,0,' + str(local_clock()) + '\n')
record_start_time = False
if (sample, timestamp) != (None, None):
if inlet_idx is (len(inlets) - 1):
row_data[0] = timestamp
row_data.extend(sample)
inlet_idx += 1 # move on to next inlet
else:
time.sleep(0.0001) # wait for 0.1 ms if the data is not there yet
# just to save some processing power
save_variable.append(row_data)
pull_thread = Thread(target=save_sample, args=(inlets, save_variable))
pull_thread.daemon = True
pull_thread.start()
return inlets, pull_thread
with open("eeg.csv", 'w') as csv_file:
csv_file.write('time, N1P, N2P, N3P, N4P, N5P, N6P, N7P, N8P, D11, D12, D13\n')
with open("meta.csv", 'w') as csv_file:
csv_file.write('')
eeg = [] # receive_data() saves [timepoints by channels] here
# where channels are length 12 [timestamp, 8 EEG Channels, 3 AUX channels]
board, stop_cyton = start_cyton_lsl()
inlets, _ = get_lsl_data(eeg)
# █████████████████████████████████████████████████████████████████████████████
## Keyboard
if True:
import string
import numpy as np
import psychopy.visual
import psychopy.event
from psychopy import core
# letters = string.ascii_letters[:26]
# letters += '⌂'
# letters += '⎵'
# letters += ','
# letters += '.'
# letters += '↨'
# letters += '⌫'
# letters += ' '
# letters = 'AIQYBJRZCKS⌂DLT⎵EMU,FNV.GOW↨HPX⌫ '
# letters = 'AIQYBJRZCKS⌂DLT⎵EMU,FNV.GOW⤒HPX⌫ '
# letters = 'AIQYBJRZCKS⌂D⌫T⎵EMU,FNV.GOW⤒HPXL '
letters = 'AIQYBJRZCKS⌂D⌫T⎵EMULFNV.GOW⤒HPX, '
# letters2 = '19/+2(~-3)$⌂4:%=5;&<6"*>7!#↨8?⮐⌫ '
# letters2 = '19/+2(~-3)$⌂4:%=5;&<6"*>7!#⤓8?⮐⌫ '
# letters2 = '19/+2(~-3)$\'4⌫%=5;&<6"*>7!#⤓8?⮐: '
letters2 = '19/+2(~-3)$;4⌫%=5\'&<6"*>7!#⤓8?⮐: '
letters3 = '12341234123412341234⏳⌚⏰ ⎚⏩ ⌨✉⏪ ⌫ '
# letters3 = '12341234123412341234⑮⌚⏰ ⎚⏩ ⌨✉⏪ ⌫ '
win = psychopy.visual.Window(
size=(800, 800),
units="pix",
fullscr=False)
n_text = 33
text_cap_size = 119 # 34
text_strip_height = n_text * text_cap_size
text_strip = np.full((text_strip_height, text_cap_size), np.nan)
text_strip2 = np.full((text_strip_height, text_cap_size), np.nan)
text_strip3 = np.full((text_strip_height, text_cap_size), np.nan)
text = psychopy.visual.TextStim(win=win, height=60, font="Courier")
text2 = psychopy.visual.TextStim(win=win, height=60, font="Courier")
text3 = psychopy.visual.TextStim(win=win, height=60, font="Courier")
cap_rect_norm = [-(text_cap_size / 2.0) / (win.size[0] / 2.0), # left
+(text_cap_size / 2.0) / (win.size[1] / 2.0), # top
+(text_cap_size / 2.0) / (win.size[0] / 2.0), # right
-(text_cap_size / 2.0) / (win.size[1] / 2.0)] # bottom
# capture the rendering of each letter
for (i_letter, letter) in enumerate(letters):
text.text = letter.upper()
buff = psychopy.visual.BufferImageStim(
win=win,
stim=[text],
rect=cap_rect_norm)
i_rows = slice(i_letter * text_cap_size,
i_letter * text_cap_size + text_cap_size)
text_strip[i_rows, :] = (np.flipud(np.array(buff.image)[..., 0]) / 255.0 * 2.0 - 1.0)
# capture the rendering of each letter
for (i_letter, letter) in enumerate(letters2):
text2.text = letter.upper()
buff = psychopy.visual.BufferImageStim(
win=win,
stim=[text2],
rect=cap_rect_norm)
i_rows = slice(i_letter * text_cap_size,
i_letter * text_cap_size + text_cap_size)
text_strip2[i_rows, :] = (np.flipud(np.array(buff.image)[..., 0]) / 255.0 * 2.0 - 1.0)
# capture the rendering of each letter
for (i_letter, letter) in enumerate(letters3):
text3.text = letter.upper()
buff = psychopy.visual.BufferImageStim(
win=win,
stim=[text3],
rect=cap_rect_norm)
i_rows = slice(i_letter * text_cap_size,
i_letter * text_cap_size + text_cap_size)
text_strip3[i_rows, :] = (np.flipud(np.array(buff.image)[..., 0]) / 255.0 * 2.0 - 1.0)
# need to pad 'text_strip' to pow2 to use as a texture
new_size = max([int(np.power(2, np.ceil(np.log(dim_size) / np.log(2))))
for dim_size in text_strip.shape])
pad_amounts = []
for i_dim in range(2):
first_offset = int((new_size - text_strip.shape[i_dim]) / 2.0)
second_offset = new_size - text_strip.shape[i_dim] - first_offset
pad_amounts.append([first_offset, second_offset])
text_strip = np.pad(
array=text_strip,
pad_width=pad_amounts,
mode="constant",
constant_values=0.0)
text_strip = (text_strip - 1) * -1 # invert the texture mapping
text_strip2 = np.pad(
array=text_strip2,
pad_width=pad_amounts,
mode="constant",
constant_values=0.0)
text_strip2 = (text_strip2 - 1) * -1 # invert the texture mapping
text_strip3 = np.pad(
array=text_strip3,
pad_width=pad_amounts,
mode="constant",
constant_values=0.0)
text_strip3 = (text_strip3 - 1) * -1 # invert the texture mapping
# make a central mask to show just one letter
el_mask = np.ones(text_strip.shape) * -1.0
# start by putting the visible section in the corner
el_mask[:text_cap_size, :text_cap_size] = 1.0
# then roll to the middle
el_mask = np.roll(el_mask,
(int(new_size / 2 - text_cap_size / 2),) * 2,
axis=(0, 1))
# make a central mask to show just one letter
el_mask2 = np.ones(text_strip2.shape) * -1.0
# start by putting the visible section in the corner
el_mask2[:text_cap_size, :text_cap_size] = 1.0
# then roll to the middle
el_mask2 = np.roll(el_mask2,
(int(new_size / 2 - text_cap_size / 2),) * 2,
axis=(0, 1))
# make a central mask to show just one letter
el_mask3 = np.ones(text_strip3.shape) * -1.0
# start by putting the visible section in the corner
el_mask3[:text_cap_size, :text_cap_size] = 1.0
# then roll to the middle
el_mask3 = np.roll(el_mask3,
(int(new_size / 2 - text_cap_size / 2),) * 2,
axis=(0, 1))
# work out the phase offsets for the different letters
base_phase = ((text_cap_size * (n_text / 2.0)) - (text_cap_size / 2.0)) / new_size
phase_inc = (text_cap_size) / float(new_size)
phases = np.array([
(0.0, base_phase - i_letter * phase_inc)
for i_letter in range(n_text)])
win.close()
# █████████████████████████████████████████████████████████████████████████████
## EXPERIMENT
# if this script is run as a script rather than imported
if __name__ == "__main__":
kb = keyboard.Keyboard()
win = visual.Window(
size=[1920, 1080],
checkTiming=True,
allowGUI=False,
fullscr=True,
useRetina=use_retina
)
[win_w, win_h] = win.size
if use_retina:
win_w, win_h = win_w / 2, win_h / 2
if center_flash: # if we want the visual stimuli to be only presented at the center of the screen
fixation = create_fixation_cross()
square = create_flickering_square()
photosensor = create_photosensor_dot()
sequence = create_trial_sequence(n_per_class=n_per_class, classes=classes)
# square.color = (0, 1, 0)
for i_trial, (flickering_freq, phase_offset) in enumerate(sequence): # for each trial in the trail sequence
keys = kb.getKeys()
for thisKey in keys:
if thisKey == 'escape':
if use_dsi_lsl:
for inlet in inlets:
inlet.close_stream()
os.kill(p.pid, sig.CTRL_C_EVENT)
with open("eeg.csv", 'a') as csv_file:
np.savetxt(csv_file, eeg, delimiter=',')
if use_cyton:
for inlet in inlets:
inlet.close_stream()
stop_cyton.set()
board.stop_stream()
with open("eeg.csv", 'a') as csv_file:
np.savetxt(csv_file, eeg, delimiter=',')
core.quit()
trial_text = visual.TextStim(win, str(i_trial + 1) + '/' + str(len(sequence)), color=(-1, -1, -1),
colorSpace='rgb')
# 750ms fixation cross:
for frame in range(ms_to_frame(isi_duration * 1000, refresh_rate)):
# if frame == 0:
# with open("meta.csv", 'a') as csv_file:
# csv_file.write(str(flickering_freq)+', '+str(phase_offset) + ', ' + str(time.time()) + '\n')
fixation.draw()
trial_text.draw()
photosensor.color = (-1, -1, -1)
if use_photosensor:
photosensor.draw()
win.flip()
# 'stim_duration' seconds stimulation using flashing frequency approximation:
phase_offset_str = str(phase_offset)
phase_offset += 0.00001 # nudge phase slightly from points of sudden jumps for offsets that are pi
# multiples
stim_duration_frames = ms_to_frame((stim_duration + after_stim_padding) * 1000,
refresh_rate) # total number of frames for the stimulation
frame_indices = np.arange(stim_duration_frames) # the frames as integer indices
if flash_mode == 'square': # if we want to use binarized square wave visual stimuli
trial = signal.square(2 * np.pi * flickering_freq * (
frame_indices / refresh_rate) + phase_offset * np.pi) # frequency approximation formula
for i_frame, frame in enumerate(trial): # present the stimulation frame by frame
if i_frame == 0:
with open("meta.csv", 'a') as csv_file:
# csv_file.write(str(flickering_freq)+', '+phase_offset_str + ', ' + str(time.time()) +
# '\n')
csv_file.write(
str(flickering_freq) + ', ' + phase_offset_str + ', ' + str(local_clock()) + '\n')
if use_dsi_trigger and (use_dsi_lsl or use_dsi7): # send trigger signal to the trigger channel
msg = b'\x01\xe1\x01\x00\x01'
dsi_serial.write(msg)
square.color = (frame, frame, frame)
square.draw()
# photosensor.color = (frame, frame, frame)
photosensor.color = (1, 1, 1)
if use_photosensor:
photosensor.draw()
win.flip()
elif flash_mode == 'sine': # if we want to use smoothed sine wave visual stimuli
trial = np.sin(2 * np.pi * flickering_freq * (
frame_indices / refresh_rate) + phase_offset * np.pi) # frequency approximation formula
for frame in trial: # present the stimulation frame by frame
square.color = (frame, frame, frame)
square.draw()
photosensor.color = (frame, frame, frame)
if use_photosensor:
photosensor.draw()
win.flip()
elif flash_mode == 'chirp':
frame_times = np.linspace(0, stim_duration, int(stim_duration * refresh_rate))
trial = signal.chirp(frame_times, f0=10, f1=14, t1=5, method='linear')
for frame in trial: # present the stimulation frame by frame
square.color = (frame, frame, frame)
square.draw()
win.flip()
elif flash_mode == 'dual band':
flickering_freq2 = phase_offset
phase_offset = 0.00001
trial = signal.square(2 * np.pi * flickering_freq * (
frame_indices / refresh_rate) + phase_offset * np.pi) # frequency approximation formula
trial += signal.square(2 * np.pi * flickering_freq2 * (
frame_indices / refresh_rate) + phase_offset * np.pi) # frequency approximation formula
trial /= 2
for frame in trial: # present the stimulation frame by frame
square.color = (frame, frame, frame)
square.draw()
win.flip()
flickering_keyboard = create_32_keys()
flickering_keyboard_caps = create_key_caps(text_strip, el_mask, phases)
flickering_keyboard_caps2 = create_key_caps(text_strip2, el_mask2, phases)
flickering_keyboard_caps3 = create_key_caps(text_strip3, el_mask3, phases)
orig_keyboard_position = np.copy(flickering_keyboard.xys)
stim_duration_frames = ms_to_frame((stim_duration) * 1000,
refresh_rate) # total number of frames for the stimulation
frame_indices = np.arange(stim_duration_frames) # the frames as integer indices
flickering_frames = np.zeros((len(frame_indices), n_keyboard_classes))
linear_movement_vector = (np.random.random(size=[n_keyboard_classes, 2]) * 2 - 1) * 0.2
if shuffled_initial_positions:
np.random.seed(1)
r_pos = np.copy(orig_keyboard_position)
# print(np.where(r_pos[:, None] == orig_keyboard_position[None, :])[1])
# print([ np.where(np.logical_and((orig_keyboard_position==x)[:,1], (orig_keyboard_position==x)[:,0])==True)[
# 0][0] for x in r_pos])