-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSSVEP.py
146 lines (108 loc) · 4.69 KB
/
SSVEP.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
#author: John Naulty Jr.
#date: July 2014
#SSVEP class.
from psychopy import visual, core, event
import csv_collector
class SSVEP(object):
#init sets the window(mywin), and the frequency of the flashing(frame_on, frame_off)
#Frame duration in seconds = 1/monitorframerate(in Hz)
#Thus the fastest frame rate could be 1 frame on 1 frame off
#which equals 2/60 == 30Hz
#Flash frequency = refreshrate/(frame_on+frame_off)
def __init__(self, mywin= visual.Window([800, 600], fullscr=False, monitor='testMonitor',units='deg'),
frame_on=5, frame_off=5, trialdur = 5.0, port='/dev/ttyACM0',
fname='SSVEP.csv', numtrials=1, waitdur=2):
self.mywin = mywin
self.pattern1 = visual.GratingStim(win=self.mywin, name='pattern1',units='cm',
tex=None, mask=None,
ori=0, pos=[0, 0], size=10, sf=1, phase=0.0,
color=[1,1,1], colorSpace='rgb', opacity=1,
texRes=256, interpolate=True, depth=-1.0)
self.pattern2 = visual.GratingStim(win=self.mywin, name='pattern2',units='cm',
tex=None, mask=None,
ori=0, pos=[0, 0], size=10, sf=1, phase=0,
color=[-1,-1,-1], colorSpace='rgb', opacity=1,
texRes=256, interpolate=True, depth=-2.0)
self.fixation = visual.GratingStim(win=self.mywin, size = 0.3, pos=[0,0], sf=0, rgb=-1)
self.frame_on = frame_on
self.frame_off = frame_off
self.trialdur = trialdur
self.fname = fname
self.numtrials = numtrials
self.waitdur = waitdur
self.port = port
def collecting(self):
self.collector = csv_collector.CSVCollector(fname=self.fname, port= self.port)
self.collector.start()
def epoch(self, mark):
self.collector.tag(mark)
def stop(self):
self.mywin.close()
core.quit()
def start(self):
###Testing framerate grabber###
print self.mywin.getActualFrameRate()
self.Trialclock = core.Clock()
self.freq = 60/(self.frame_on+self.frame_off)
#start saving data from EEG device.
#self.collecting()
#possibly convert trialdur into frames given refresh rate (normally set at 60Hz)
self.framerate = self.mywin.getActualFrameRate()
#divison here makes it tricky
self.trialframes = self.trialdur/60
self.count = 0
while self.count<self.numtrials:
#reset tagging
self.should_tag = False
#self.epoch(0)
while self.Trialclock.getTime()<self.trialdur:
#draws square and fixation on screen.
self.fixation.setAutoDraw(True)
self.pattern1.setAutoDraw(True)
"""
###Tagging the data with the calculated frequency###
Attempting to only get 1 sample tagged, however, this is hard.
"""
"""alternative way to tag
if self.should_tag == False:
#self.epoch(self.freq)
self.epoch(70)
self.mywin.flip()
self.epoch(0)
self.should_tag = True
"""
#self.epoch(70)
for frameN in range(self.frame_on):
self.mywin.flip()
#another way to change color with 1 pattern
#self.pattern1.color *= -1
self.pattern1.setAutoDraw(False)
self.pattern2.setAutoDraw(True)
for frameN in range(self.frame_off):
self.mywin.flip()
self.pattern2.setAutoDraw(False)
#self.epoch(0)
#clean black screen off
self.mywin.flip()
#wait certain time for next trial
core.wait(self.waitdur)
#reset clock for next trial
self.Trialclock.reset()
#count number of trials
self.count+=1
"""
###Tagging the Data at end of stimulus###
"""
#self.collector.disconnect()
self.stop()
"""
Here are some test cases
Just run this program by itself if you don't want to use run.py
"""
if "__name__" == "__main__":
stimuli75Hz = SSVEP(frame_on=4, frame_off=4)
stimuli75Hz.start()
stimuli12=SSVEP(frame_on=3, frame_off=2)
stimuli12.start()
stimuli20=SSVEP(frame_on=2, frame_off=1)
stimuli20.start()