-
Notifications
You must be signed in to change notification settings - Fork 0
/
scr_extract_and_correct_events.py
233 lines (175 loc) · 9.41 KB
/
scr_extract_and_correct_events.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
# -*- coding: utf-8 -*-
"""
Due to delay and JITTER of the presentation of visual frames, event files must
be created and corrected for. This simultaneously gives us corrected reaction
times.
Output:
scratch/events.fif/SUB_ID/task-correve.fif
scratch/RTs.csv/SUB_ID/task-corrRT.csv
Created on Thu Sep 26 10:25:28 2013
@author: cjb
"""
from database import Query
from analysis_dict import Anadict
from mne import find_events, write_events
from mne.fiff import Raw, pick_channels
import numpy as np
import csv
from sys import exit as sysexit
import os
import errno
def check_path_exists(chkpath):
try:
os.makedirs(chkpath)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(chkpath):
pass
else:
raise
def write_RTs(fname, data):
fp = open(fname, 'w')
writ = csv.writer(fp)
writ.writerows(data)
fp.close()
def write_frame_delays(fname, data):
fp = open(fname, 'w')
for d in data:
fp.write("%.0f\n" % d)
fp.close()
def _next_crossing(a,lowlim):
for a_ind,val in enumerate(a):
if val > lowlim:
return a_ind
print 'ERROR: No analogue trigger found within %d samples of the digital trigger' % a_ind
print 'Cannot continue, aborting...'
sysexit(-1)
#def _find_next_analogue_trigger(raw, ind, lim, ana_channel='MISC001',
# maxdelay_samps=200):
#print 'Nothing here yet'
#pick = pick_channels(raw.info['ch_names'], include=ana_channel)
#ana_data, _ = raw[pick,ind:ind+maxdelay_samps]
def _find_next_analogue_trigger(ana_data, ind, lim, maxdelay_samps=100):
return _next_crossing(ana_data[ind:ind+maxdelay_samps].squeeze(),lim)
def _find_analogue_trigger_limit(ana_data):
return ana_data.mean()
proj_code = 'MINDLAB2013_01-MEG-AttentionEmotionVisualTracking'
stimchan = 'STI101'
anachan = 'MISC001'
VERBOSE=True
# These are the triggers that are followed by an image
# FB03_neutral, FB03_angry,FB10_neutral, FB10_angry,
# no_target_S03, blurred face (FFA only) no_target_S10, targets at 6 positions
imtriggers = np.r_[10,11,20,21,100,150,200,np.arange(111,117), np.arange(211,217)]
db = Query(proj_code=proj_code,verbose=True)
anadict = Anadict(db, verbose=False)
# I sent an empty room file from another subject in to 007, but it screwed up the
# series numbering! On 26 Sep I /really/ messed it up and have to get help from
# Jesper to fix it! This script ran with the below line (before major cockup),
# so assume it is OK and that 007 will be fixed.
## anadict.analysis_dict['007_SGF']['001.VS_1a_1']['raw'] = '/projects/MINDLAB2013_01-MEG-AttentionEmotionVisualTracking/raw/0007/20130910_000000/MEG/001.VS_1a_1/files/PROJ0103_SUBJ0007_SER001_FILESNO001.fif'
for subj in anadict.analysis_dict.keys():
for task in anadict.analysis_dict[subj]['raw'].keys():
if 'empty' in task:
pass # this correction only applies to the actual paradigm
else:
#elif '003' in task:
raw_names = anadict.analysis_dict[subj]['raw'][task]['files']
# Initialize these to empty lists for each task
frame_delays_list = []
eve_list = [] # -eve.fif filename
RTs_list = []
for ii_raw,raw_name in enumerate(sorted(raw_names)):
fnum_raw = "%02d" % ii_raw
# Reading events
raw = Raw(raw_name, preload=False)
events = find_events(raw, stim_channel=stimchan, min_duration=0.002)
# This returns, oddly the sample indices to which
# raw.first_samp has been ADDED! So to get the actual index
# into raw[:,__], we need to subtract the first_samp!
corr_events = events.copy()
frame_delays = np.zeros(events.shape[0])
if not 'FFA' in task:
# Get total number of responses (should be 192!)
codes = events[:,2]
lefts = codes==2
rights = codes==3
resps = lefts + rights
responses = np.zeros((resps.sum(),4))
if resps.sum() != 192:
print 'Warning: Total number of responses is not 192! (%d)' % resps.sum()
if VERBOSE: print "Loading analogue channel data and deciding on threshold..."
pick = pick_channels(raw.info['ch_names'], include=anachan)
ana_data, _ = raw[pick,:]
ana_data = ana_data[0]
triglimit = _find_analogue_trigger_limit(ana_data)
if VERBOSE: print "Analogue data trigger limit set to %.2f" % triglimit
# Scan through all events, even though not terribly pretty
# The list isn't that huge to warrant any coolness here
row=0
resp=0
prev_target = -1
prev_target_time = -1
prev_trigger = -1
for ind, before, after in events:
# Now check whether to search for the frame trigger
# Also: since we'll want to fix the reaction times, search also for
# the response strigger
corr_ind = ind
if after == 2 or after == 3:
RT = raw.index_as_time(ind) - prev_target_time
if prev_target == after:
responses[resp,:] = np.r_[resp+1,True,prev_trigger,RT]
else:
responses[resp,:] = np.r_[resp+1,False,prev_trigger,RT]
resp += 1
elif after in imtriggers:
raw_ind = ind - raw.first_samp # now these really are indices into raw!
# anatrig_ind = _find_next_analogue_trigger(raw, raw_ind,triglimit
# ana_channel='MISC001',
# maxdelay_samps=200)
anatrig_ind = _find_next_analogue_trigger(ana_data, raw_ind,triglimit ,
maxdelay_samps=100)
corr_ind += anatrig_ind
corr_events[row,0] = corr_ind
frame_delays[row] = anatrig_ind
if not 'FFA' in task:
# Check that this is not a feedback pic!
if after != 10 and after != 11 and after != 20 and after != 21:
if after == 100 or after == 200:
prev_target = 2
else:
prev_target = 3
prev_target_time = raw.index_as_time(corr_ind)
prev_trigger = after
row += 1
corr_events_path = anadict._scratch_folder + '/events.fif/' + subj + '/raw'
check_path_exists(corr_events_path)
if len(raw_names) > 1:
corr_events_fname = corr_events_path + '/' + task + '-' + fnum_raw + '-eve.fif'
else:
corr_events_fname = corr_events_path + '/' + task + '-eve.fif'
write_events(corr_events_fname, corr_events)
eve_list.append(corr_events_fname)
frame_delays_path = anadict._result_folder + '/frame_delays/' + subj
check_path_exists(frame_delays_path)
if len(raw_names) > 1:
frame_delays_fname = frame_delays_path + '/' + task + '-' + fnum_raw +'-frame_delays.txt' # This is is samples, so 1ms when 1kHz sampling
else:
frame_delays_fname = frame_delays_path + '/' + task + '-frame_delays.txt' # This is is samples, so 1ms when 1kHz sampling
write_frame_delays(frame_delays_fname, frame_delays)
frame_delays_list.append(frame_delays)
if not 'FFA' in task:
responses_path = anadict._result_folder + '/RTs.csv/' + subj
check_path_exists(responses_path)
if len(raw_names) > 1:
responses_fname = responses_path + '/' + task + '-' + fnum_raw + '-corrRTs.csv'
else:
responses_fname = responses_path + '/' + task + '-RTs.csv'
write_RTs(responses_fname, responses)
RTs_list.append(responses)
dict_entry = anadict.analysis_dict[subj]['raw'][task]
dict_entry.update({'frame_delays': frame_delays_list })
dict_entry.update({'events': eve_list})
if not 'FFA' in task:
dict_entry.update({'RTs': RTs_list})
anadict.save('Added frame delays to raw files. Also added file name references to corrected (raw) event files and reaction times.')