[go: up one dir, main page]

Menu

[r28]: / PyLablib / eye.py  Maximize  Restore  History

Download this file

283 lines (243 with data), 9.1 kB

  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
"""A module that collects routines to process eye position."""
import pylab
# Some diagnostic functions
def eye_sample_insert_interval(R):
tt = R.data['Trials']['eyeXData']['Trial Time']
n_trials = len(tt)
d_esii = pylab.array([],dtype=float)
for tr in range(n_trials):
d_esii = pylab.concatenate((d_esii,pylab.diff(tt[tr])))
return d_esii
def eye_sample_count_per_packet(R):
dv = R.data['Trials']['eyeXData']['Data Values']
n_trials = len(dv)
p_cnt = []
for tr in range(n_trials):
n_packets = len(dv[tr])
for pak in range(n_packets):
p_cnt.append(len(dv[tr][pak]))
return pylab.array(p_cnt,dtype=float)
def eye_calibrations(R):
"""Return us the eye calibration transform for each trial.
Input:
R - lablib data file from lablib.py
Output:
M - tr x 2 x 2 array
m11, m12
m21, m22
C - tr x 2 array
tx, ty
"""
cal = R.data['Trials']['eyeCalibrationData']['cal']
tX = cal['tX']
tY = cal['tY']
m21 = cal['m21']
m22 = cal['m22']
m11 = cal['m11']
m12 = cal['m12']
n_trials = len(tX)
M = pylab.zeros((n_trials,2,2), dtype=float)
C = pylab.zeros((n_trials,2), dtype=float)
for tr in range(n_trials):
M[tr,0,0] = m11[tr][0][0]
M[tr,0,1] = m12[tr][0][0]
M[tr,1,0] = m21[tr][0][0]
M[tr,1,1] = m22[tr][0][0]
C[tr,0] = tX[tr][0][0]
C[tr,1] = tY[tr][0][0]
return M,C
def fix_window(R):
"""Return the fixation window for each trial."""
fx_x = R.data['Trials']['fixWindowData']['windowDeg']['origin']['x']
fx_y = R.data['Trials']['fixWindowData']['windowDeg']['origin']['y']
fx_w = R.data['Trials']['fixWindowData']['windowDeg']['size']['width']
fx_h = R.data['Trials']['fixWindowData']['windowDeg']['size']['height']
n_trials = len(fx_x)
fix_w = pylab.zeros((n_trials,5,2),dtype=float)
for tr in range(n_trials):
x = fx_x[tr][0][0]
y = fx_y[tr][0][0]
w = fx_w[tr][0][0]
h = fx_h[tr][0][0]
fix_w[tr,:,0] = pylab.array([x, x+w, x+w, x, x])
fix_w[tr,:,1] = pylab.array([y, y, y+h, y+h, y])
return fix_w
def raw_eye_xy(R, old_format = None):
"""Return the raw x,y data points without calibration for diagnostic purposes."""
if dat_format == None:
dvx = R.data['Trials']['eyeXData']['Data Values']
dvy = R.data['Trials']['eyeYData']['Data Values']
elif dat_format == 'fixate':
#Annoying old format
dvxy = R.data['Trials']['eyeData']['Data Values']
n_trials = len(dvxy)
dvx = []
dvy = []
for tr in range(n_trials):
dvx_tr = []
dvy_tr = []
for pak in range(len(dvxy[tr])):
dvx_tr.append(dvxy[tr][pak][::2])
dvy_tr.append(dvxy[tr][pak][1::2])
dvx.append(dvx_tr)
dvy.append(dvy_tr)
n_trials = len(dvx)
all_x = []
all_y = []
for tr in range(n_trials):
raw_x = []
raw_y = []
for pak in range(len(dvx[tr])):
raw_x += dvx[tr][pak]
for pak in range(len(dvy[tr])):
raw_y += dvy[tr][pak]
if len(dvx[tr]) != len(dvy[tr]):
print 'eye.eye_xy: trial %d : unequal number of packets x=%d y=%d' %(tr, len(dvx[tr]), len(dvy[tr]))
uncal_x = pylab.array(raw_x, dtype=float)
uncal_y = pylab.array(raw_y, dtype=float)
if uncal_x.size != uncal_y.size:
print 'eye.eye_xy: trial %d : unequal number of samples x=%d y=%d' %(tr, uncal_x.size, uncal_y.size)
minnsamp = min(uncal_x.size, uncal_y.size)
uncal_x = uncal_x[:minnsamp]
uncal_y = uncal_y[:minnsamp]
all_x.append(pylab.array(uncal_x, dtype=float))
all_y.append(pylab.array(uncal_y, dtype=float))
return all_x, all_y
def eye_xy(R, M, C, old_format = False):
"""Give us eye position data for each trial.
Inputs:
R - lablib data structure from reader
M - m matrix
C - c matrix
old_format - if False then we load the usual MstimPair dat files where the
eye xy is stored in eyeXData and eyeYData
if True then we look for it in eyeData
Outputs:
all_x - list of arrays of the eyeposition for each trial
all_y - list of arrays of the eyeposition for each trial
"""
if old_format == False:
dvx = R.data['Trials']['eyeXData']['Data Values']
dvy = R.data['Trials']['eyeYData']['Data Values']
else:
#Annoying old format
dvxy = R.data['Trials']['eyeData']['Data Values']
n_trials = len(dvxy)
dvx = []
dvy = []
for tr in range(n_trials):
dvx_tr = []
dvy_tr = []
for pak in range(len(dvxy[tr])):
dvx_tr.append(dvxy[tr][pak][::2])
dvy_tr.append(dvxy[tr][pak][1::2])
dvx.append(dvx_tr)
dvy.append(dvy_tr)
n_trials = len(dvx)
all_x = []
all_y = []
for tr in range(n_trials):
raw_x = []
raw_y = []
for pak in range(len(dvx[tr])):
raw_x += dvx[tr][pak]
for pak in range(len(dvy[tr])):
raw_y += dvy[tr][pak]
if len(dvx[tr]) != len(dvy[tr]):
print 'eye.eye_xy: trial %d : unequal number of packets x=%d y=%d' %(tr, len(dvx[tr]), len(dvy[tr]))
uncal_x = pylab.array(raw_x, dtype=float)
uncal_y = pylab.array(raw_y, dtype=float)
if uncal_x.size != uncal_y.size:
print 'eye.eye_xy: trial %d : unequal number of samples x=%d y=%d' %(tr, uncal_x.size, uncal_y.size)
minnsamp = min(uncal_x.size, uncal_y.size)
uncal_x = uncal_x[:minnsamp]
uncal_y = uncal_y[:minnsamp]
#The formula that JOhn uses for the calibration is (counter to matrix notation)
# x = m_11 x + m_21 y + tx
# y = m_12 x + m_22 y + ty
# Note the back-diagonal terms are flipped!
#x = M[tr,0,0]*uncal_x + M[tr,0,1]*uncal_y + C[tr,0]#WRONG CODE!
#y = M[tr,1,0]*uncal_x + M[tr,1,1]*uncal_y + C[tr,1]
x = M[tr,0,0]*uncal_x + M[tr,1,0]*uncal_y + C[tr,0]
y = M[tr,0,1]*uncal_x + M[tr,1,1]*uncal_y + C[tr,1]
all_x.append(pylab.array(x, dtype=float))
all_y.append(pylab.array(y, dtype=float))
return all_x, all_y
def eye_xy_selected(all_x, all_y, trial_no, start_ms, stop_ms, f_samp = 200.0):
"""For the given trial give us the eye samples between the start_ms and stop_ms
which are equal sized arrays.
start_ms, stop_ms - trial times (from lablib)
Returns-
x, y - list of arrays for the eye pos
mx, my - array of mean eye pos"""
x = [None] * start_ms.size
y = [None] * start_ms.size
mx = pylab.zeros(start_ms.size,dtype=float)
my = pylab.zeros(start_ms.size,dtype=float)
for n in range(start_ms.size):
start_idx = int(f_samp * start_ms[n]/1000.0)
stop_idx = int(f_samp * stop_ms[n]/1000.0)
x[n] = all_x[trial_no][start_idx:stop_idx]
mx[n] = x[n].mean()
y[n] = all_y[trial_no][start_idx:stop_idx]
my[n] = y[n].mean()
return x, y, mx, my
def fixation_box_dwell_times(R):
"""Return us the fixation box dwell start and end times. We assume that
'fixate' indicates the entry of the eye into the fixation box, and that
'saccade' indicates the exit of the eye from the box. If 'fixate'
is empty, no fixation occured (-1 is filled) if 'saccade' is empty, the
fixation was not broken until the trial stopped (-1 is filled)."""
# if R.data['Trials'].has_key('fixate'):
f_on = R.data['Trials']['fixate']['Trial Time']
if R.data['Trials'].has_key('saccade'):
f_off = R.data['Trials']['saccade']['Trial Time']
else:
f_off = [[] for n in range(len(f_on))]
# else:
# f_on = R.data['Trials']['fixOn']['Trial Time']
# f_off = R.data['Trials']['fixOff']['Trial Time']
n_trials = len(f_on)
dwell_times = pylab.zeros((n_trials,2)) # Start and stop
for tr in range(n_trials):
st = f_on[tr]
if len(st) == 1:
st = st[0]
else:
st = -1
nd =f_off[tr]
if len(nd) == 1:
nd = nd[0]
else:
nd = -1
dwell_times[tr,:] = [st, nd]
return dwell_times
def fixation_box_samples(all_x, all_y, fix_w, dwell_times, f_samp = 200.0):
"""Collect all x and ys for all trials for when the eye is within the fixation
box."""
n_trials = len(all_x)
in_fix_box_x = pylab.array([],dtype=float)
in_fix_box_y = pylab.array([],dtype=float)
for tr in range(n_trials):
if dwell_times[tr,0] >= 0:
# We got a fixation
start_idx = int(f_samp * dwell_times[tr,0]/1000.0)
end_idx = -1
if dwell_times[tr,1] >= 0:
end_idx = int(f_samp * dwell_times[tr,1]/1000.0) - 5
in_fix_box_x = pylab.concatenate((in_fix_box_x, all_x[tr][start_idx:end_idx]))
in_fix_box_y = pylab.concatenate((in_fix_box_y, all_y[tr][start_idx:end_idx]))
return in_fix_box_x, in_fix_box_y
def plot_eye_pos(trial_no, all_x, all_y, fix_w, dwell_times, f_samp = 200.0):
"""."""
n = trial_no
pylab.plot(all_x[n], all_y[n], c='gray')
pylab.plot(fix_w[n,:,0], fix_w[n,:,1],'k:')
if dwell_times[n,0] >= 0:
# We got a fixation
start_idx = int(f_samp * dwell_times[n,0]/1000.0)
end_idx = -1
if dwell_times[n,1] >= 0:
end_idx = int(f_samp * dwell_times[n,1]/1000.0) - 5
pylab.plot(all_x[n][start_idx:end_idx], all_y[n][start_idx:end_idx], 'k')
pylab.axis('scaled')