-
Notifications
You must be signed in to change notification settings - Fork 1
/
StressTrialAnalysis_LDA.py
500 lines (400 loc) · 23.6 KB
/
StressTrialAnalysis_LDA.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
import numpy as np
import scipy as sp
import matplotlib as mpl
import tables
import sys
import statsmodels.api as sm
from neo import io
from PulseMonitorData import findIBIs, getIBIandPuilDilation
from scipy import signal
from scipy import stats
from matplotlib import mlab
import matplotlib.pyplot as plt
from basicAnalysis import plot_cov_ellipse, LDAforFeatureSelection
from csv_processing import get_csv_data_singlechannel
from probabilisticRewardTaskPerformance import FreeChoiceBehavior_withStressTrials
from spectralAnalysis import TrialAveragedPSD
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import train_test_split
from sklearn import metrics
from sklearn.metrics import roc_curve, auc
from sklearn.cross_validation import cross_val_score
hdf_filename = 'mari20161006_03_te2551.hdf'
hdf_filename_stim = 'mari20161006_05_te2553.hdf'
filename = 'Mario20161006'
block_num = 1
print filename
#TDT_tank = '/backup/subnetsrig/storage/tdt/'+filename
TDT_tank = '/home/srsummerson/storage/tdt/'+filename
hdf_location = '/storage/rawdata/hdf/'+hdf_filename
hdf_location_stim = '/storage/rawdata/hdf/'+hdf_filename_stim
#hdf_location = hdffilename
stim_freq = 100
lfp_channels = [29, 13, 27, 11, 25, 9, 10, 26, 12, 28, 14, 30, 20, 4, 18, 2, 63, 1, 17, 3]
lfp_channels = [11]
#bands = [[1,8],[8,12],[12,30],[30,55],[65,100]]
bands = [[30,55]]
'''
Load behavior data
'''
state_time, ind_center_states, ind_check_reward_states, all_instructed_or_freechoice, all_stress_or_not, successful_stress_or_not,trial_success, target, reward = FreeChoiceBehavior_withStressTrials(hdf_location)
state_time_stim, ind_center_states_stim, ind_check_reward_states_stim, all_instructed_or_freechoice_stim, all_stress_or_not_stim, successful_stress_or_not_stim,trial_success_stim, target_stim, reward_stim = FreeChoiceBehavior_withStressTrials(hdf_location_stim)
print "Behavior data loaded."
# Total number of trials
num_trials = ind_center_states.size
total_states = state_time.size
num_trials_stim = ind_center_states_stim.size
total_states_stim = state_time_stim.size
# Number of successful stress trials
tot_successful_stress = np.logical_and(trial_success,all_stress_or_not)
successful_stress_trials = float(np.sum(tot_successful_stress))/np.sum(all_stress_or_not)
tot_successful_stress_stim = np.logical_and(trial_success_stim,all_stress_or_not_stim)
successful_stress_trials_stim = float(np.sum(tot_successful_stress_stim))/np.sum(all_stress_or_not_stim)
# Number of successful non-stress trials
tot_successful_reg = np.logical_and(trial_success,np.logical_not(all_stress_or_not))
successful_reg_trials = float(np.sum(tot_successful_reg))/(num_trials - np.sum(all_stress_or_not))
# Response times for successful stress trials
ind_successful_stress = np.ravel(np.nonzero(tot_successful_stress)) # gives trial index, not row index
row_ind_successful_stress = ind_center_states[ind_successful_stress] # gives row index
ind_successful_stress_reward = np.ravel(np.nonzero(successful_stress_or_not))
row_ind_successful_stress_reward = ind_check_reward_states[ind_successful_stress_reward]
response_time_successful_stress = (state_time[row_ind_successful_stress_reward] - state_time[row_ind_successful_stress])/float(60) # hdf rows are written at a rate of 60 Hz
ind_successful_stress_stim = np.ravel(np.nonzero(tot_successful_stress_stim)) # gives trial index, not row index
row_ind_successful_stress_stim = ind_center_states_stim[ind_successful_stress_stim] # gives row index
ind_successful_stress_reward_stim = np.ravel(np.nonzero(successful_stress_or_not_stim))
row_ind_successful_stress_reward_stim = ind_check_reward_states_stim[ind_successful_stress_reward_stim]
response_time_successful_stress_stim = (state_time_stim[row_ind_successful_stress_reward_stim] - state_time_stim[row_ind_successful_stress_stim])/float(60) # hdf rows are written at a rate of 60 Hz
# Response time for all stress trials
ind_stress = np.ravel(np.nonzero(all_stress_or_not))
row_ind_stress = ind_center_states[ind_stress] # gives row index
row_ind_end_stress = np.zeros(len(row_ind_stress))
row_ind_end_stress = row_ind_stress + 2 # targ_transition state occurs two states later for unsuccessful trials
row_ind_end_stress[-1] = np.min([row_ind_end_stress[-1],len(state_time)-1]) # correct final incomplete trial
ind_stress_stim = np.ravel(np.nonzero(all_stress_or_not_stim))
row_ind_stress_stim = ind_center_states_stim[ind_stress_stim] # gives row index
row_ind_end_stress_stim = np.zeros(len(row_ind_stress_stim))
row_ind_end_stress_stim = row_ind_stress_stim + 2 # targ_transition state occurs two states later for unsuccessful trials
row_ind_end_stress_stim[-1] = np.min([row_ind_end_stress_stim[-1],len(state_time_stim)-1]) # correct final incomplete trial
for i in range(0,len(row_ind_successful_stress)):
ind = np.where(row_ind_stress == row_ind_successful_stress[i])[0]
row_ind_end_stress[ind] = row_ind_successful_stress_reward[i] # for successful trials, update with real end of trial
response_time_stress = (state_time[row_ind_end_stress] - state_time[row_ind_stress])/float(60)
for i in range(0,len(row_ind_successful_stress_stim)):
ind = np.where(row_ind_stress_stim == row_ind_successful_stress_stim[i])[0]
row_ind_end_stress_stim[ind] = row_ind_successful_stress_reward_stim[i] # for successful trials, update with real end of trial
response_time_stress_stim = (state_time_stim[row_ind_end_stress_stim] - state_time_stim[row_ind_stress_stim])/float(60)
# Response times for successful regular trials
ind_successful_reg = np.ravel(np.nonzero(tot_successful_reg))
row_ind_successful_reg = ind_center_states[ind_successful_reg]
ind_successful_reg_reward = np.ravel(np.nonzero(np.logical_not(successful_stress_or_not)))
row_ind_successful_reg_reward = ind_check_reward_states[ind_successful_reg_reward]
response_time_successful_reg = (state_time[row_ind_successful_reg_reward] - state_time[row_ind_successful_reg])/float(60)
# Response time for all regular trials
ind_reg = np.ravel(np.nonzero(np.logical_not(all_stress_or_not)))
row_ind_reg = ind_center_states[ind_reg]
row_ind_end_reg = np.zeros(len(row_ind_reg))
row_ind_end_reg = np.minimum(row_ind_reg + 5,total_states-1) # target_transition state occues two states later for successful trials
for i in range(0,len(row_ind_successful_reg)):
ind = np.where(row_ind_reg == row_ind_successful_reg[i])[0]
row_ind_end_reg[ind] = row_ind_successful_reg_reward[i]
response_time_reg = (state_time[row_ind_end_reg] - state_time[row_ind_reg])/float(60)
'''
Load syncing data for behavior and TDT recording
'''
print "Loading syncing data."
hdf_times = dict()
mat_filename = filename+'_b'+str(block_num)+'_syncHDF.mat'
sp.io.loadmat('/home/srsummerson/storage/syncHDF/'+mat_filename,hdf_times)
hdf_times_stim = dict()
mat_filename_stim = filename+'_b'+str(block_num + 1)+'_syncHDF.mat'
sp.io.loadmat('/home/srsummerson/storage/syncHDF/'+mat_filename_stim,hdf_times_stim)
'''
Load pupil dilation and heart rate data
'''
if filename == 'Mario20160320':
PupD_filename = '/home/srsummerson/storage/tdt/Mario20160320_plex/Mario20160320_Block-1_PupD.csv'
HrtR_filename = '/home/srsummerson/storage/tdt/Mario20160320_plex/Mario20160320_Block-1_HrtR.csv'
pupil_data = get_csv_data_singlechannel(PupD_filename)
pupil_samprate = 3051.8
pulse_data = get_csv_data_singlechannel(HrtR_filename)
pulse_samprate = 3051.8
lfp = dict()
lfp_samprate = 3051.8
else:
r = io.TdtIO(TDT_tank)
bl = r.read_block(lazy=False,cascade=True)
print "File read."
lfp = dict()
lfp_stim = dict()
# Get Pulse and Pupil Data
for sig in bl.segments[block_num-1].analogsignals:
if (sig.name == 'PupD 1'):
pupil_data = np.ravel(sig)
pupil_samprate = sig.sampling_rate.item()
if (sig.name == 'HrtR 1'):
pulse_data = np.ravel(sig)
pulse_samprate = sig.sampling_rate.item()
if (sig.name[0:4] == 'LFP1'):
channel = sig.channel_index
if channel in lfp_channels:
lfp_samprate = sig.sampling_rate.item()
lfp[channel] = np.ravel(sig)
for sig in bl.segments[block_num].analogsignals:
if (sig.name == 'PupD 1'):
pupil_data_stim = np.ravel(sig)
pupil_samprate_stim = sig.sampling_rate.item()
if (sig.name == 'HrtR 1'):
pulse_data_stim = np.ravel(sig)
pulse_samprate_stim = sig.sampling_rate.item()
if (sig.name[0:4] == 'LFP1'):
channel = sig.channel_index
if channel in lfp_channels:
lfp_samprate_stim = sig.sampling_rate.item()
lfp_stim[channel] = np.ravel(sig)
'''
Convert DIO TDT samples for pupil and pulse data for regular and stress trials
'''
# divide up analysis for regular trials before stress trials, stress trials, and regular trials after stress trials are introduced
hdf_rows = np.ravel(hdf_times['row_number'])
hdf_rows = [val for val in hdf_rows] # turn into a list so that the index method can be used later
dio_tdt_sample = np.ravel(hdf_times['tdt_samplenumber'])
dio_freq = np.ravel(hdf_times['tdt_dio_samplerate'])
hdf_rows_stim = np.ravel(hdf_times_stim['row_number'])
hdf_rows_stim = [val for val in hdf_rows_stim] # turn into a list so that the index method can be used later
dio_tdt_sample_stim = np.ravel(hdf_times_stim['tdt_samplenumber'])
dio_freq_stim = np.ravel(hdf_times_stim['tdt_dio_samplerate'])
# Convert DIO TDT sample numbers to for pupil and pulse data:
# if dio sample num is x, then data sample number is R*(x-1) + 1 where
# R = data_sample_rate/dio_sample_rate
pulse_dio_sample_num = (float(pulse_samprate)/float(dio_freq))*(dio_tdt_sample - 1) + 1
pupil_dio_sample_num = (float(pupil_samprate)/float(dio_freq))*(dio_tdt_sample - 1) + 1
lfp_dio_sample_num = (float(lfp_samprate)/float(dio_freq))*(dio_tdt_sample - 1) + 1
pulse_dio_sample_num_stim = (float(pulse_samprate_stim)/float(dio_freq_stim))*(dio_tdt_sample_stim - 1) + 1
pupil_dio_sample_num_stim = (float(pupil_samprate_stim)/float(dio_freq_stim))*(dio_tdt_sample_stim - 1) + 1
lfp_dio_sample_num_stim = (float(lfp_samprate_stim)/float(dio_freq_stim))*(dio_tdt_sample_stim - 1) + 1
state_row_ind_successful_stress = state_time[row_ind_successful_stress]
state_row_ind_successful_reg = state_time[row_ind_successful_reg]
pulse_ind_successful_stress = np.zeros(row_ind_successful_stress.size)
pupil_ind_successful_stress = np.zeros(row_ind_successful_stress.size)
lfp_ind_successful_stress = np.zeros(row_ind_successful_stress.size)
pulse_ind_successful_reg = []
pupil_ind_successful_reg = []
lfp_ind_successful_reg = []
state_row_ind_stress = state_time[row_ind_stress]
state_row_ind_reg = state_time[row_ind_reg]
pulse_ind_stress = np.zeros(row_ind_stress.size)
pupil_ind_stress = np.zeros(row_ind_stress.size)
lfp_ind_stress = np.zeros(row_ind_stress.size)
pulse_ind_reg = []
pupil_ind_reg = []
lfp_ind_reg = []
state_row_ind_successful_stress_stim = state_time_stim[row_ind_successful_stress_stim]
pulse_ind_successful_stress_stim = np.zeros(row_ind_successful_stress_stim.size)
pupil_ind_successful_stress_stim = np.zeros(row_ind_successful_stress_stim.size)
lfp_ind_successful_stress_stim = np.zeros(row_ind_successful_stress_stim.size)
state_row_ind_stress_stim = state_time_stim[row_ind_stress_stim]
pulse_ind_stress_stim = np.zeros(row_ind_stress_stim.size)
pupil_ind_stress_stim = np.zeros(row_ind_stress_stim.size)
lfp_ind_stress_stim = np.zeros(row_ind_stress_stim.size)
for i in range(0,len(row_ind_successful_stress)):
hdf_index = np.argmin(np.abs(hdf_rows - state_row_ind_successful_stress[i]))
pulse_ind_successful_stress[i] = pulse_dio_sample_num[hdf_index]
pupil_ind_successful_stress[i] = pupil_dio_sample_num[hdf_index]
lfp_ind_successful_stress[i] = lfp_dio_sample_num[hdf_index]
for i in range(0,len(row_ind_stress)):
hdf_index = np.argmin(np.abs(hdf_rows - state_row_ind_stress[i]))
pulse_ind_stress[i] = pulse_dio_sample_num[hdf_index]
pupil_ind_stress[i] = pupil_dio_sample_num[hdf_index]
lfp_ind_stress[i] = lfp_dio_sample_num[hdf_index]
if len(row_ind_successful_stress) > 0:
ind_start_stress = row_ind_successful_stress[0]
else:
ind_start_stress = np.inf
ind_start_all_stress = row_ind_stress[0]
for i in range(0,len(state_row_ind_successful_reg)):
hdf_index = np.argmin(np.abs(hdf_rows - state_row_ind_successful_reg[i]))
pulse_ind_successful_reg.append(pulse_dio_sample_num[hdf_index])
pupil_ind_successful_reg.append(pupil_dio_sample_num[hdf_index])
lfp_ind_successful_reg.append(lfp_dio_sample_num[hdf_index])
for i in range(0,len(state_row_ind_reg)):
hdf_index = np.argmin(np.abs(hdf_rows - state_row_ind_reg[i]))
pulse_ind_reg.append(pulse_dio_sample_num[hdf_index])
pupil_ind_reg.append(pupil_dio_sample_num[hdf_index])
lfp_ind_reg.append(lfp_dio_sample_num[hdf_index])
for i in range(0,len(row_ind_successful_stress_stim)):
hdf_index = np.argmin(np.abs(hdf_rows_stim - state_row_ind_successful_stress_stim[i]))
pulse_ind_successful_stress_stim[i] = pulse_dio_sample_num_stim[hdf_index]
pupil_ind_successful_stress_stim[i] = pupil_dio_sample_num_stim[hdf_index]
lfp_ind_successful_stress_stim[i] = lfp_dio_sample_num_stim[hdf_index]
for i in range(0,len(row_ind_stress_stim)):
hdf_index = np.argmin(np.abs(hdf_rows_stim - state_row_ind_stress_stim[i]))
pulse_ind_stress_stim[i] = pulse_dio_sample_num_stim[hdf_index]
pupil_ind_stress_stim[i] = pupil_dio_sample_num_stim[hdf_index]
lfp_ind_stress_stim[i] = lfp_dio_sample_num_stim[hdf_index]
'''
Process pupil and pulse data
'''
# Find IBIs and pupil data for all successful stress trials.
samples_pulse_successful_stress = np.floor(response_time_successful_stress*pulse_samprate) #number of samples in trial interval for pulse signal
samples_pupil_successful_stress = np.floor(response_time_successful_stress*pupil_samprate)
samples_lfp_successful_stress = np.floor(response_time_successful_stress*lfp_samprate)
ibi_stress_mean, ibi_stress_std, pupil_stress_mean, pupil_stress_std, nbins_ibi_stress, ibi_stress_hist, nbins_pupil_stress, pupil_stress_hist = getIBIandPuilDilation(pulse_data, pulse_ind_successful_stress,samples_pulse_successful_stress, pulse_samprate,pupil_data, pupil_ind_successful_stress,samples_pupil_successful_stress,pupil_samprate)
# Find IBIs and pupil data for all stress trials
samples_pulse_stress = np.floor(response_time_stress*pulse_samprate) #number of samples in trial interval for pulse signal
samples_pupil_stress = np.floor(response_time_stress*pupil_samprate)
samples_lfp_stress = np.floor(response_time_stress*lfp_samprate)
ibi_all_stress_mean, ibi_all_stress_std, pupil_all_stress_mean, pupil_all_stress_std, nbins_ibi_all_stress, ibi_all_stress_hist, nbins_pupil_all_stress, pupil_all_stress_hist = getIBIandPuilDilation(pulse_data, pulse_ind_stress,samples_pulse_stress, pulse_samprate,pupil_data, pupil_ind_stress,samples_pupil_stress,pupil_samprate)
# Find IBIs and pupil data for successful and all regular trials.
samples_pulse_successful_reg = np.floor(response_time_successful_reg*pulse_samprate)
samples_pupil_successful_reg = np.floor(response_time_successful_reg*pupil_samprate)
samples_lfp_successful_reg = np.floor(response_time_successful_reg*lfp_samprate)
ibi_reg_mean, ibi_reg_std, pupil_reg_mean, pupil_reg_std, nbins_ibi_reg, ibi_reg_hist, nbins_pupil_reg, pupil_reg_hist = getIBIandPuilDilation(pulse_data, pulse_ind_successful_reg,samples_pulse_successful_reg, pulse_samprate,pupil_data, pupil_ind_successful_reg,samples_pupil_successful_reg,pupil_samprate)
samples_pulse_reg = np.floor(response_time_reg*pulse_samprate)
samples_pupil_reg = np.floor(response_time_reg*pupil_samprate)
samples_lfp_reg = np.floor(response_time_reg*lfp_samprate)
ibi_all_reg_mean, ibi_all_reg_std, pupil_all_reg_mean, pupil_all_reg_std, nbins_ibi_all_reg, ibi_all_reg_hist, nbins_pupil_all_reg, pupil_all_reg_hist = getIBIandPuilDilation(pulse_data, pulse_ind_reg,samples_pulse_reg, pulse_samprate,pupil_data, pupil_ind_reg,samples_pupil_reg,pupil_samprate)
# Find IBIs and pupil data for all successful stress trials with stimulation.
samples_pulse_successful_stress_stim = np.floor(response_time_successful_stress_stim*pulse_samprate_stim) #number of samples in trial interval for pulse signal
samples_pupil_successful_stress_stim = np.floor(response_time_successful_stress_stim*pupil_samprate_stim)
samples_lfp_successful_stress_stim = np.floor(response_time_successful_stress_stim*lfp_samprate_stim)
ibi_stress_mean_stim, ibi_stress_std_stim, pupil_stress_mean_stim, pupil_stress_std_stim, nbins_ibi_stress_stim, ibi_stress_hist_stim, nbins_pupil_stress_stim, pupil_stress_hist_stim = getIBIandPuilDilation(pulse_data_stim, pulse_ind_successful_stress_stim,samples_pulse_successful_stress_stim, pulse_samprate_stim,pupil_data_stim, pupil_ind_successful_stress_stim,samples_pupil_successful_stress_stim,pupil_samprate_stim)
# Find IBIs and pupil data for all stress trials with stimulation.
samples_pulse_stress_stim = np.floor(response_time_stress_stim*pulse_samprate_stim) #number of samples in trial interval for pulse signal
samples_pupil_stress_stim = np.floor(response_time_stress_stim*pupil_samprate_stim)
samples_lfp_stress_stim = np.floor(response_time_stress_stim*lfp_samprate_stim)
ibi_all_stress_mean_stim, ibi_all_stress_std_stim, pupil_all_stress_mean_stim, pupil_all_stress_std_stim, nbins_ibi_all_stress_stim, ibi_all_stress_hist_stim, nbins_pupil_all_stress_stim, pupil_all_stress_hist_stim = getIBIandPuilDilation(pulse_data_stim, pulse_ind_stress_stim,samples_pulse_stress_stim, pulse_samprate_stim,pupil_data_stim, pupil_ind_stress_stim,samples_pupil_stress_stim,pupil_samprate_stim)
'''
Get power in designated frequency bands per trial
'''
lfp_power_successful_stress = []
lfp_power_stress = []
lfp_power_successful_reg = []
lfp_power_reg = []
X_successful_stress = []
X_stress = []
X_successful_reg = []
X_reg = []
X_successful_stim = []
X_stim = []
for i, ind in enumerate(lfp_ind_successful_stress):
trial_array = []
trial_array.append(pupil_stress_mean[i])
trial_array.append(ibi_stress_mean[i])
for chann in lfp_channels:
#freq, Pxx_den = signal.welch(lfp[chann][ind:ind+samples_lfp_successful_stress[i]], lfp_samprate, nperseg=1024)
freq, Pxx_den = signal.welch(lfp[chann][ind:ind+lfp_samprate/2], lfp_samprate, nperseg=1024) # take 0.5 s of data
for k, item in enumerate(bands):
freq_band = [Pxx_den[j] for j in range(len(freq)) if (item[0] <= freq[j] <= item[1])]
#trial_array.append(np.sum(freq_band))
lfp_power_successful_stress.append(np.sum(freq_band))
X_successful_stress.append(trial_array)
for i, ind in enumerate(lfp_ind_stress):
trial_array = []
trial_array.append(pupil_all_stress_mean[i])
trial_array.append(ibi_all_stress_mean[i])
for chann in lfp_channels:
#freq, Pxx_den = signal.welch(lfp[chann][ind:ind+samples_lfp_stress[i]], lfp_samprate, nperseg=1024)
freq, Pxx_den = signal.welch(lfp[chann][ind:ind+lfp_samprate/2], lfp_samprate, nperseg=1024) # take 0.5 s of data
for k, item in enumerate(bands):
freq_band = [Pxx_den[j] for j in range(len(freq)) if (item[0] <= freq[j] <= item[1])]
#trial_array.append(np.sum(freq_band))
lfp_power_stress.append(np.sum(freq_band))
X_stress.append(trial_array)
for i, ind in enumerate(lfp_ind_successful_reg):
trial_array = []
trial_array.append(pupil_reg_mean[i])
trial_array.append(ibi_reg_mean[i])
for chann in lfp_channels:
#freq, Pxx_den = signal.welch(lfp[chann][ind:ind+samples_lfp_successful_reg[i]], lfp_samprate, nperseg=1024)
freq, Pxx_den = signal.welch(lfp[chann][ind:ind+lfp_samprate/2], lfp_samprate, nperseg=1024) # take 0.5 s of data
for k, item in enumerate(bands):
freq_band = [Pxx_den[j] for j in range(len(freq)) if (item[0] <= freq[j] <= item[1])]
#trial_array.append(np.sum(freq_band))
lfp_power_successful_reg.append(np.sum(freq_band))
X_successful_reg.append(trial_array)
for i, ind in enumerate(lfp_ind_reg):
trial_array = []
trial_array.append(pupil_all_reg_mean[i])
trial_array.append(ibi_all_reg_mean[i])
for chann in lfp_channels:
#freq, Pxx_den = signal.welch(lfp[chann][ind:ind+samples_lfp_reg[i]], lfp_samprate, nperseg=1024)
freq, Pxx_den = signal.welch(lfp[chann][ind:ind+lfp_samprate/2], lfp_samprate, nperseg=1024) # take 0.5 s of data
for k, item in enumerate(bands):
freq_band = [Pxx_den[j] for j in range(len(freq)) if (item[0] <= freq[j] <= item[1])]
#trial_array.append(np.sum(freq_band))
lfp_power_reg.append(np.sum(freq_band))
X_reg.append(trial_array)
for i, ind in enumerate(lfp_ind_successful_stress_stim):
trial_array = []
trial_array.append(pupil_stress_mean_stim[i])
trial_array.append(ibi_stress_mean_stim[i])
X_successful_stim.append(trial_array)
for i, ind in enumerate(lfp_ind_stress_stim):
trial_array = []
trial_array.append(pupil_all_stress_mean_stim[i])
trial_array.append(ibi_all_stress_mean_stim[i])
X_stim.append(trial_array)
# Labels: 0 = regular, 1 = stress
X_successful_stress = np.array(X_successful_stress)
#X_successful_stress = (X_successful_stress - np.nanmean(X_successful_stress,axis=0))/np.nanstd(X_successful_stress,axis=0)
num_successful_stress = X_successful_stress.shape[0]
y_successful_stress = np.ones(num_successful_stress)
X_stress = np.array(X_stress)
#X_stress = (X_stress - np.nanmean(X_stress,axis=0))/np.nanstd(X_stress,axis=0)
num_stress = X_stress.shape[0]
y_stress = np.ones(num_stress)
X_successful_reg = np.array(X_successful_reg)
#X_successful_reg = (X_successful_reg - np.nanmean(X_successful_reg,axis=0))/np.nanstd(X_successful_reg,axis=0)
num_successful_reg = X_successful_reg.shape[0]
y_successful_reg = np.zeros(num_successful_reg)
X_reg = np.array(X_reg)
#X_reg = (X_reg - np.nanmean(X_reg,axis=0))/np.nanstd(X_reg,axis=0)
num_reg = X_reg.shape[0]
y_reg = np.zeros(num_reg)
X_successful_stim = np.array(X_successful_stim)
X_stim = np.array(X_stim)
X_successful = np.vstack([X_successful_reg, X_successful_stress])
y_successful = np.append(y_successful_reg,y_successful_stress)
X_all = np.vstack([X_reg, X_stress])
y_all = np.append(y_reg, y_stress)
clf_all = LinearDiscriminantAnalysis()
clf_all.fit(X_successful, y_successful)
scores = cross_val_score(LinearDiscriminantAnalysis(),X_successful,y_successful,scoring='accuracy',cv=10)
print "CV (10-fold) scores:", scores
print "Avg CV score:", scores.mean()
predict_stress = clf_all.predict(X_successful_stress)
print "Fraction of stress trials classified as stress:", np.sum(predict_stress)/len(predict_stress)
predict_stim = clf_all.predict(X_stim)
print "Fraction of all stimulation trials classified as stress:", np.sum(predict_stim)/len(predict_stim)
predict_stim = clf_all.predict(X_successful_stim)
print "Fraction of all successful stimulation trials classified as stress:", np.sum(predict_stim)/len(predict_stim)
"""
Decision boundary given by:
np.dot(clf.coef_, x) - clf.intercept_ = 0 according to
http://stackoverflow.com/questions/36745480/how-to-get-the-equation-of-the-boundary-line-in-linear-discriminant-analysis-wit
"""
#LDAforFeatureSelection(X_successful,y_successful,filename,block_num)
'''
Do regression as well: power is total power in beta band per trial
'''
x = np.vstack((np.append(ibi_all_reg_mean, ibi_all_stress_mean), np.append(pupil_all_reg_mean, pupil_all_stress_mean),
np.append(lfp_power_reg, lfp_power_stress)))
x = np.transpose(x)
x = sm.add_constant(x,prepend='False')
lfp_power_successful_reg = (lfp_power_successful_reg - np.nanmean(lfp_power_successful_reg))/np.nanstd(lfp_power_successful_reg)
lfp_power_successful_stress = (lfp_power_successful_stress - np.nanmean(lfp_power_successful_stress))/np.nanstd(lfp_power_successful_stress)
x_successful = np.vstack((np.append(ibi_reg_mean, ibi_stress_mean), np.append(pupil_reg_mean, pupil_stress_mean),
np.append(lfp_power_successful_reg, lfp_power_successful_stress)))
x_successful = np.transpose(x_successful)
x_successful = sm.add_constant(x_successful,prepend='False')
'''
print "Regression with all trials"
model_glm = sm.Logit(y_all,x)
fit_glm = model_glm.fit()
print fit_glm.summary()
'''
print "Regression with successful trials"
model_glm = sm.Logit(y_successful,x_successful)
fit_glm = model_glm.fit()
print fit_glm.summary()