-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_oddball_FIR.py
380 lines (327 loc) · 15.2 KB
/
proc_oddball_FIR.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 9 10:47:05 2018
@author: jelman
"""
import os
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="ticks")
from lmfit import minimize, Parameters, Parameter, report_fit
from datetime import datetime
import scipy as sci
import ast
sys.path.append('/home/jelman/netshare/K/code/Pupillometry/FIRDeconvolution/src')
from FIRDeconvolution import FIRDeconvolution
def get_offset_params():
params = Parameters()
# Offset parameters
params.add('offset', value=0, min=-2.5, max=2.5)
return params
def get_IRF_params():
params = Parameters()
# Pupil impulse response parameters
params.add('s1', value=5000., min=1e-28, max=1e8)
params.add('n1', value=10.1, min=6, max=15)
params.add('tmax1', value=0.930, min=0.1, max=3.0)
return params
def get_events(pupilprofile, eprime):
trg_onsets = eprime.index[eprime['Tone']==2]
std_onsets = eprime.index[eprime['Tone']==1][1:]
# Get event indices
trg_event_idx = pupilprofile.index.searchsorted(trg_onsets)
std_event_idx = pupilprofile.index.searchsorted(std_onsets)
trg_events = pupilprofile.index[trg_event_idx]
std_events = pupilprofile.index[std_event_idx]
return trg_events, std_events
def hp_filter(signal, sample_rate):
# High pass:
hp = 0.01
hp_cof_sample = hp / (sample_rate / 2)
bhp, ahp = sci.signal.butter(3, hp_cof_sample, btype='high')
signal_hp = sci.signal.filtfilt(bhp, ahp, signal)
return signal_hp
def single_pupil_IRF(params, x):
s1 = params['s1']
n1 = params['n1']
tmax1 = params['tmax1']
return s1 * ((x**n1) * (np.e**((-n1*x)/tmax1)))
def single_pupil_IRF_ls(params, x, data):
s1 = params['s1'].value
n1 = params['n1'].value
tmax1 = params['tmax1'].value
model = s1 * ((x**n1) * (np.e**((-n1*x)/tmax1)))
return model - data
def add_offset(events, offset):
events_new = ((events - pd.Timestamp(0)).astype('timedelta64[ms]') / 1000.) + offset
return events_new
def run_deconvolve(offset, events, event_names, signal_hp, interval):
"""
Run FIR deconvolution.
offset (float) : offset in sec between eprime and pupillometer timestamps
events (list) : list
"""
a = FIRDeconvolution(signal=signal_hp,
events=events, event_names=event_names, sample_frequency=15.0,
deconvolution_frequency = 15.,
deconvolution_interval=interval)
a.create_design_matrix()
a.regress()
a.betas_for_events()
a.calculate_rsq()
return a
def offset_fit_all(offset, trg_events, std_events, signal_hp, interval):
trg_new = add_offset(trg_events, offset)
std_new = add_offset(std_events, offset)
events = [np.sort(np.append(np.array(trg_new), np.array(std_new)))]
event_names = ["allevents"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
result = minimize(single_pupil_IRF_ls, irf_params, method='powell', args=(trial_x, response))
kernel = single_pupil_IRF(result.params, trial_x)
return result, response, kernel
def offset_fit_all_ls(params, trg_events, std_events, signal_hp, interval):
offset = params['offset'].value
trg_new = add_offset(trg_events, offset)
std_new = add_offset(std_events, offset)
events = [np.sort(np.append(np.array(trg_new), np.array(std_new)))]
event_names = ["allevents"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
result = minimize(single_pupil_IRF_ls, irf_params, method='powell', args=(trial_x, response))
kernel = single_pupil_IRF(result.params, trial_x)
return np.sum((response - kernel)**2)
def offset_std_all(offset, trg_events, std_events, signal_hp, interval):
trg_new = add_offset(trg_events, offset)
std_new = add_offset(std_events, offset)
events = [np.sort(np.append(np.array(trg_new), np.array(std_new)))]
event_names = ["allevents"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
kernel = single_pupil_IRF(irf_params.valuesdict(), trial_x)
return response, kernel
def offset_std_all_ls(params, trg_events, std_events, signal_hp, interval):
offset = params['offset'].value
trg_new = add_offset(trg_events, offset)
std_new = add_offset(std_events, offset)
events = [np.sort(np.append(np.array(trg_new), np.array(std_new)))]
event_names = ["allevents"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
kernel = single_pupil_IRF(irf_params.valuesdict(), trial_x)
return np.sum((response - kernel)**2)
def offset_fit_trg(offset, trg_events, signal_hp, interval):
trg_new = ((trg_events - pd.Timestamp(0)).astype('timedelta64[ms]') / 1000.) + offset
events = [trg_new]
event_names = ["targets"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
result = minimize(single_pupil_IRF_ls, irf_params, method='powell', args=(trial_x, response))
kernel = single_pupil_IRF(result.params, trial_x)
return result, response, kernel
def offset_fit_trg_ls(params, trg_events, signal_hp, interval):
offset = params['offset'].value
trg_new = ((trg_events - pd.Timestamp(0)).astype('timedelta64[ms]') / 1000.) + offset
events = [trg_new]
event_names = ["targets"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
result = minimize(single_pupil_IRF_ls, irf_params, method='powell', args=(trial_x, response))
kernel = single_pupil_IRF(result.params, trial_x)
return np.sum((response - kernel)**2)
def offset_std_trg(offset, trg_events, signal_hp, interval):
trg_new = ((trg_events - pd.Timestamp(0)).astype('timedelta64[ms]') / 1000.) + offset
events = [trg_new]
event_names = ["targets"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
kernel = single_pupil_IRF(irf_params.valuesdict(), trial_x)
return response, kernel
def offset_std_trg_ls(params, trg_events, signal_hp, interval):
offset = params['offset'].value
trg_new = ((trg_events - pd.Timestamp(0)).astype('timedelta64[ms]') / 1000.) + offset
events = [trg_new]
event_names = ["targets"]
a = run_deconvolve(offset, events, event_names, signal_hp, interval)
response = np.array(a.betas_per_event_type[0]).ravel()
# baseline the kernels:
response = response - response[0].mean()
trial_x = np.linspace(0, interval[1], len(response))
irf_params = get_IRF_params()
kernel = single_pupil_IRF(irf_params.valuesdict(), trial_x)
return np.sum((response - kernel)**2)
def plot_response_kernel(response, kernel, interval, title, plotdir):
# Turn interactive plotting off
plt.ioff()
# plot:
trial_x = np.linspace(0, interval[1], len(response))
f = plt.figure(figsize = (8,6))
ax = f.add_subplot(1,1,1)
ax.plot(trial_x, response, label='all response')
ax.plot(trial_x, kernel, label='all fit')
ax.set_xlabel('Time from event (s)')
ax.set_ylabel('Pupil size')
ax.axhline(0,color = 'k', lw = 0.5, alpha = 0.5)
ax.legend(loc=4)
ax.set_title(title)
sns.despine(offset=10)
fname = title + '_FIRplot.png'
outfile = os.path.join(plotdir, fname)
f.savefig(outfile)
plt.close(f)
def plot_glm(signal_hp, pred_events, title, plotdir):
# Turn interactive plotting off
plt.ioff()
# plot:
signal_x = np.arange(len(signal_hp))
f = plt.figure(figsize = (10,8))
ax = f.add_subplot(1,1,1)
ax.plot(signal_x, signal_hp, label='raw')
ax.plot(signal_x, pred_events[0], label='targets')
ax.plot(signal_x, pred_events[1], label='standards')
ax.plot(signal_x, np.sum(pred_events, axis=0), label='all')
ax.set_xlabel('Time (s)')
ax.set_ylabel('Pupil size')
ax.axhline(0,color = 'k', lw = 0.5, alpha = 0.5)
ax.legend()
ax.set_title(title)
sns.despine(offset=10)
fname = title + '_glmplot.png'
outfile = os.path.join(plotdir, fname)
f.savefig(outfile)
plt.close(f)
def create_regressors(series_time_idx, trg_events, std_events, offset, kernel):
offsettime = pd.Timedelta(offset, unit="s")
trg_idx = [series_time_idx.get_loc(i-offsettime, method="nearest") for i in trg_events]
std_idx = [series_time_idx.get_loc(i-offsettime, method="nearest") for i in std_events]
trg_reg = np.zeros(len(series_time_idx))
trg_reg[trg_idx] = 1
trg_reg_conv = sci.signal.fftconvolve(trg_reg, kernel, 'full')[:-(len(kernel)-1)]
std_reg = np.zeros(len(series_time_idx))
std_reg[std_idx] = 1
std_reg_conv = sci.signal.fftconvolve(std_reg, kernel, 'full')[:-(len(kernel)-1)]
regs = [trg_reg_conv, std_reg_conv]
return regs
def run_glm(signal_hp, regs):
intercept = np.ones(len(signal_hp))
design_matrix = np.matrix(np.vstack((intercept,[reg for reg in regs]))).T
betas = np.array(((design_matrix.T * design_matrix).I * design_matrix.T) * np.matrix(signal_hp).T).ravel()
pred_events = [betas[i+1]*regs[i] for i in range(len(betas)-1)]
return betas, pred_events
def calc_sess_stats(noblink_series, blinktimes, ao_eprime, fit=True):
"""
DIFF: Target max - Standard max
CNR1: Target max / Standard SD
CNR2: (Target max - Standard max) / Standard SD
CNR3: Target SD / Standard SD
CNR4: (Target max - Standard max) / Standard max
"""
subid, sess = noblink_series.name
noblink_series.index = pd.to_datetime(noblink_series.index)
blinktime_series = blinktimes[noblink_series.name]
eprime_sess = ao_eprime[(ao_eprime['Subject_ID']==subid) &
(ao_eprime['Session']==sess)]
noblink_series = noblink_series.dropna()
blinktime_series = blinktime_series.dropna()
eprime_sess.index = pd.to_datetime(list(eprime_sess.Tone_Onset), unit='ms')
trg_events, std_events = get_events(noblink_series, eprime_sess)
signal_hp = hp_filter(noblink_series, sample_rate)
# Estimate offset between e-prime and pupillometer timestamps
offset_params = get_offset_params()
if fit==True:
offset_result = minimize(offset_fit_all_ls, offset_params, method='powell', args=(trg_events, std_events, signal_hp, interval))
# offset_result = minimize(offset_fit_trg_ls, offset_params, method='powell', args=(trg_events, signal_hp, interval))
else:
offset_result = minimize(offset_std_all_ls, offset_params, method='powell', args=(trg_events, std_events, signal_hp, interval))
# offset_result = minimize(offset_std_trg_ls, offset_params, method='powell', args=(trg_events, signal_hp, interval))
offset = float(offset_result.params['offset'])
# Deconvolve and fit pupil IRF kernel
if fit==True:
result, response, kernel = offset_fit_all(offset, trg_events, std_events, signal_hp, interval)
# result, response, kernel = offset_fit_trg(offset, trg_events, signal_hp, interval)
else:
response, kernel = offset_std_all(offset, trg_events, std_events, signal_hp, interval)
# response, kernel = offset_std_trg(offset, trg_events, signal_hp, interval)
# Plot response and kernel
title = "%s_%s" %(subid, sess)
plot_response_kernel(response, kernel, interval, title, plotdir)
# create regressors:
regs = create_regressors(noblink_series.index, trg_events, std_events, offset, kernel)
# GLM:
betas, pred_events = run_glm(signal_hp, regs)
plot_glm(signal_hp, pred_events, title, plotdir)
resultdict = {'intercept_beta' : betas[0],
'targets_beta': betas[1],
'standards_beta': betas[2],
'offset': offset}
return pd.Series(resultdict)
def calc_subj_stats(noblinkdata, blinktimes, ao_eprime, fit):
subj_sess_snr = noblinkdata.apply(calc_sess_stats, args=(blinktimes, ao_eprime, fit))
subj_sess_snr = subj_sess_snr.T
subj_sess_snr.index = pd.MultiIndex.from_tuples(subj_sess_snr.index, names=("Subject","Session"))
subj_sess_snr['contrast_beta'] = subj_sess_snr['targets_beta'] - subj_sess_snr['standards_beta']
return subj_sess_snr
#----------------------------------------------------------------------------#
# Set filenames
noblinkdata_fname = "/home/jelman/netshare/VETSA_NAS/PROJ/LCIP/data/pupillometry/task_data/LCIP_Oddball_NoBlinkData20180212.csv"
blinktimes_fname = "/home/jelman/netshare/VETSA_NAS/PROJ/LCIP/data/pupillometry/task_data/LCIP_Oddball_BlinkTimes20180212.csv"
behav_fname = '/home/jelman/netshare/VETSA_NAS/PROJ/LCIP/data/behavioral/raw/oddball/OddballP300_LCI_Pilot_AllSubjects_12062017.csv'
plotdir = "/home/jelman/netshare/VETSA_NAS/PROJ/LCIP/data/pupillometry/task_data/STDplots/all"
outdir = "/home/jelman/netshare/VETSA_NAS/PROJ/LCIP/data/pupillometry/stats"
tstamp = datetime.now().strftime("%Y%m%d")
stats_fname = 'LCIP_Oddball_STDStats_AllEventsFit' + tstamp + '.csv'
# Set parameters
interval = [-1, 4]
sample_rate = 15.
fit = False
############################
### Start running script ###
############################
# Load data
noblinkdata = pd.read_csv(noblinkdata_fname, index_col=0, parse_dates=True)
blinktimes = pd.read_csv(blinktimes_fname, index_col=0, parse_dates=True)
ao_eprime = pd.read_csv(behav_fname)
ao_eprime.loc[:,'Tone_Onset'] = ao_eprime['Tone_Onset'] + 3000
# Convert strings to tuples for heirarchical column names
columns = pd.MultiIndex.from_tuples([ast.literal_eval(item) for item in noblinkdata.columns])
noblinkdata.columns = columns
columns = pd.MultiIndex.from_tuples([ast.literal_eval(item) for item in blinktimes.columns])
blinktimes.columns = columns
if not os.path.exists(plotdir):
os.makedirs(plotdir)
subj_sess_stats = calc_subj_stats(noblinkdata, blinktimes, ao_eprime, fit)
subj_sess_stats = subj_sess_stats.reset_index()
outfile = os.path.join(outdir, stats_fname)
subj_sess_stats.to_csv(outfile, index=False, header=True)