forked from LABSN-pubs/2015-JNE-Wronkiewicz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooledBCI_nSubjectsGain.py
1302 lines (1119 loc) · 61.7 KB
/
pooledBCI_nSubjectsGain.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#dpylint: disable-msg=C0103
'''
pooledBCI_nSubjectsGain.py
Plots performance as a function of number of subjects in pool
Requires that all labels being used have had std analysis run
previously (pooledBCI_Sens.py)
@Author: wronk
'''
import sys
import mne
from mne.simulation.source import generate_stc
from copy import deepcopy
from time import time, strftime
from os import environ, path as op
import numpy as np
from sklearn.lda import LDA
from sklearn.svm import SVC
from scipy.spatial import distance_matrix
import random
from scipy.spatial.distance import cdist
from scipy.io import loadmat, savemat
from scipy.stats import sem
from sklearn import preprocessing
from sklearn.cross_validation import StratifiedKFold
from sklearn.grid_search import GridSearchCV
from ldaReg import ldaRegWeights as ldaReg
import cPickle
import warnings
#Only show warnings once
warnings.simplefilter('once')
mne.set_log_level(False)
class FakeEvoked():
"""Make evoked-like class"""
def __init__(self, data, info, tmin=0.0, sfreq=1000.0):
self._data = data
self.info = deepcopy(info)
self.info['sfreq'] = sfreq
self.times = np.arange(data.shape[-1]) / sfreq + tmin
self._current = 0
self.ch_names = info['ch_names']
class FakeCov(dict):
def __init__(self, data, info, diag=False):
self.data = data
self['data'] = data
self['bads'] = info['bads']
self['names'] = info['ch_names']
self.ch_names = info['ch_names']
self['eig'] = None
self['eig_vec'] = None
self['diag'] = diag
subjectDir = op.join(environ['CODE_ROOT'], 'AnatomBCI_Mark')
structDir = op.join(environ['SUBJECTS_DIR'])
saveDir = op.join(environ['CODE_ROOT'], 'AnatomBCI_Mark',
'AnatomBCI_Figures_Python', 'PaperFig_poolSize')
subjectSet = []
subjectSet.append(['RON006_AKCLEE', 'RON007_AKCLEE', 'RON008_AKCLEE',
'RON010_AKCLEE', 'RON011_AKCLEE', 'RON014_AKCLEE',
'RON016_AKCLEE', 'RON021_AKCLEE'])
subjectSet.append(['AKCLEE_101', 'AKCLEE_103', 'AKCLEE_104', 'AKCLEE_105',
'AKCLEE_106', 'AKCLEE_107', 'AKCLEE_109', 'AKCLEE_110',
'AKCLEE_113', 'AKCLEE_114', 'AKCLEE_115', 'AKCLEE_118',
'AKCLEE_119', 'AKCLEE_120', 'AKCLEE_121', 'AKCLEE_122',
'AKCLEE_123', 'AKCLEE_124', 'AKCLEE_125', 'AKCLEE_126',
'AKCLEE_127'])
subjSetNum = 1
subjects = subjectSet[subjSetNum]
subjects = subjects[0:22]
n_max_ch = 74
poolSizes = [2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20] # Pool sizes for paper
#poolSizes = [2, 4, 5, 10, 15, 20]
#poolSizes = [2, 3, 4, 5]
assert max(poolSizes) < len(subjects), 'Max number of desired subjects in the \
pool is smaller than total number of subjects.'
#reinitialize forwards, inverse, covariance
doAnalysis = False
saveAnalysis = False
savePlots = False
re_init = True
saveIndividualInfo = False
saveConvBank = False
loadConvBank = True
clfSchemes = ['LDA', 'LDA_Reg', 'SVM']
clfScheme = clfSchemes[1]
plotsToGen = [1]
weightMethods = ['kde', 'centroid', 'unweighted']
labels_all = []
labelNames_motor = []
labelNames_p300 = []
labelNames_ssvep_auditory = []
labelNames_space_pitch = []
labelNames_motor = ['S_precentral-sup-part-lh',
'G_precentral_handMotor_radius_15mm-lh',
'G_precentral_handMotor_radius_10mm-lh',
'G_precentral_handMotor_radius_5mm-lh']
labelNames_p300 = ['G_front_middle-lh',
'S_front_inf-lh',
'G_pariet_inf-Supramar-lh',
'G_parietal_sup-lh',
'G_pariet_inf-Angular-lh',
'S_intrapariet_and_P_trans-lh']
labelNames_ssvep_auditory = ['Pole_occipital-lh',
'S_calcarine-lh',
'G_cuneus-lh',
'G_temp_sup-G_T_transv-lh']
labelNames_space_pitch = ['UDRon-UDStd_01-lh',
'LRRon-LRStd_01-rh']
labels_all.extend(labelNames_motor)
labels_all.extend(labelNames_p300)
labels_all.extend(labelNames_ssvep_auditory)
#labels_all.extend(labelNames_space_pitch)
labelList, _ = mne.labels_from_parc(subject='fsaverage', parc='aparc.a2009s')
labelList = [elem for elem in labelList
if elem.name in labels_all and elem.hemi == 'lh' and
'Jensen' not in elem.name]
for label_name in labelNames_motor:
if 'handMotor_radius' in label_name:
label_fname = op.join(structDir, 'fsaverage', 'label', label_name + '.label')
labelList.append(mne.read_label(label_fname, subject='fsaverage'))
for label_name in labelNames_space_pitch:
label_fname = op.join(structDir, 'fsaverage', 'label', label_name + '.label')
labelList.append(mne.read_label(label_fname, subject='fsaverage'))
#labelList = [elem for elem in labelList
# if (elem.name[0] == 'G' or elem.name[0] == 'S') and elem.hemi == 'lh' and
# 'Jensen' not in elem.name]
n_smooth = 5
lambda2 = 1. / 10000.
regFactors = [0.05]
n_jobs = 6
#######################################
# higher magnitude = faster rolloff with increasing distance
expFactor_centroid = -30
expFactor_KDE = -30
#######################################
# Activity simulation params
tstep = 1e-3
snr = -10
trial_counts = [40]
max_trials = max(trial_counts)
current_mag = 1.
repeats = 25 # 25 repeats used for paper quality
C_range = 10.0 ** np.arange(-6, -4)
gamma_range = 10.0 ** np.arange(-2, 3)
levelRatio = np.zeros((len(subjects), len(subjects)))
# If in debugging mode, make simulation faster
if len(subjects) < 6:
repeats = 1
snr = -10
trial_counts = [40]
#lenLabelSetToRun = 3
#label_inds = np.random.randint(0, len(labelList), (lenLabelSetToRun))
#labelList = [labelList[i] for i in label_inds]
#labelList = [label for label in labelList if 'UDRon' in label.name or
# 'LRRon' in label.name]
nLabels = len(labelList)
#load fsaverage information
fs_vertices = [np.arange(10242), np.arange(10242)]
n_src_fs = sum([len(i) for i in fs_vertices])
fs_srcs = mne.read_source_spaces(op.join(structDir, 'fsaverage', 'bem',
'fsaverage-5-src.fif'))
#File names
fileBanks = ['fwd_bank', 'fwdmat_bank', 'invMat_bank', 'noiseCov_bank'
'conv_bank', 'vdist_bank']
if(subjSetNum == 0):
cache_fname = op.join(subjectDir, 'RON__cache')
else:
cache_fname = op.join(subjectDir, 'AKCLEE__cache')
subjTxt_fname = op.join(cache_fname, 'included_subjects.txt')
subjBank_fname = op.join(cache_fname, 'banks.pkl')
#initialize lists
vertNum_bank = []
vertPos_bank = []
fwd_bank = []
fwdMat_bank = []
invMat_bank = []
conv_bank = []
convSrc_bank = []
#conv_bank = np.empty((len(subjects), len(subjects)), dtype=object)
label_bank = []
vdist_bank = []
noiseCov_bank = []
fakeEvoked_bank = []
fwdColorers = []
sensorNoise = []
start_time = time()
###############################################################################
### Initializing subject data ###
if doAnalysis:
# Score matrix
scoreDict = {'logRatio': {}, 'accuracy': {}}
meanDataDict = deepcopy(scoreDict)
labelAccuracy = {'accuracy': {}}
for method in weightMethods:
scoreDict['logRatio'] = {method: [] for method in weightMethods}
scoreDict['accuracy'][method] = []
labelAccuracy['accuracy'][method] = {method: []
for method in weightMethods}
if re_init:
print '!!! COMMENCE SIMULATION !!! (@ ' + strftime('%H:%M:%S') + ')'
print 'nSubjs:\t\t' + str(len(subjects))
print 'poolSizes:\t' + str(poolSizes)
print 'Trials:\t\t' + str(trial_counts)
print 'SNR:\t\t' + str(snr)
print 'nLabels:\t' + str(nLabels)
print 'nRepeats:\t' + str(repeats) + '\n'
print 'Processing fwd, inv, noise cov, etc:'
for si, subj in enumerate(subjects):
print ' ' + subj,
sys.stdout.flush()
#load/generate forwards
if(subjSetNum == 0):
fwd_fname = op.join(subjectDir, subj, subj + '-2-fwd.fif')
cov_fname = op.join(subjectDir, subj, subj + '-noise-cov.fif')
inv_fname = op.join(subjectDir, subj, subj + '_eeg-1-inv.fif')
else:
fwd_fname = op.join(subjectDir, subj, subj + '-7-fwd-eeg.fif')
cov_fname = op.join(subjectDir, subj, subj + '-noise-cov-eeg.fif')
inv_fname = op.join(subjectDir, subj, subj + '-inv-eeg-python.fif')
src_fname = op.join(structDir, subj, 'bem', subj + '-7-src.fif')
# Load forward solution #
fwd = mne.read_forward_solution(fwd_fname, force_fixed=True,
surf_ori=False)
fwd = mne.fiff.pick_types_forward(fwd, meg=False, eeg=True,
ref_meg=False, exclude='bads')
fwd_bank.append(fwd)
info = deepcopy(fwd['info'])
info['projs'] = []
vertices = [s['vertno'] for s in fwd['src']]
n_src = sum([len(v) for v in vertices])
# Load covariance and inverse
cov = mne.read_cov(cov_fname)
inv = mne.minimum_norm.read_inverse_operator(inv_fname)
# Generate forward matrix
d = np.zeros((len(info['ch_names']), 1))
fake_evoked = FakeEvoked(d, info)
d = np.eye(n_src)
stc = mne.SourceEstimate(data=d, vertices=vertices, tmin=0, tstep=1)
evoked = mne.simulation.generate_evoked(fwd, stc, fake_evoked, cov,
snr=np.inf)
fwdMat_bank.append(evoked.data)
# Generate inverse matrix
evoked.data = np.eye(len(info['ch_names']))
invApplied = mne.minimum_norm.apply_inverse(evoked=evoked,
inverse_operator=inv,
lambda2=lambda2,
method='MNE').data
invMat_bank.append(invApplied)
fakeEvoked_bank.append(evoked)
# Generate noise covariances
noiseCov_bank.append(FakeCov(np.cov(fwd['sol']['data']),
deepcopy(fwd['info'])))
#Load labels from parcellation
label_bank.append(mne.labels_from_parc(subj, parc='aparc.a2009s'))
label_bank[si][1][:] = [] # Clear out ROI color info
#Load custom labels too and add them onto the end
for label_name in labelNames_motor:
if 'handMotor_radius' in label_name:
label_fname = op.join(structDir, subj, 'label', label_name + '.label')
label_bank[si][0].append(mne.read_label(label_fname, subject=subj))
for label_name in labelNames_space_pitch:
#label_fname = op.join(structDir, subj, 'label', label_name + '.label')
#label_bank[si][0].append(mne.read_label(label_fname, subject=subj))
if 'UDStd' in label_name or 'LRStd' in label_name:
label_fname = op.join(structDir, subj, 'label', label_name + '.label')
tempLabel = mne.read_label(label_fname, subject=subj)
tempHemi = ([0, 1], [1, 0])[tempLabel.hemi == 'rh']
label_bank[si][tempHemi[0]].append(tempLabel)
# Generate distances between vertices
vert_coord = [fwd['src'][0]['rr'][vertices[0]],
fwd['src'][1]['rr'][vertices[1]]]
vertPos_bank.append(vert_coord)
# Compute distance between every source point and normalize
# Euclidean way
euclidean = False
if euclidean:
temp_dists = [distance_matrix(hemiVerts, hemiVerts)
for hemiVerts in vert_coord]
vdist_bank.append([hemi / hemi.max() for hemi in temp_dists])
else:
src = mne.read_source_spaces(fname=src_fname)
temp_dists = [src[hemi]['dist'][vertices[hemi]][:, vertices[hemi]].A
for hemi in range(len(src))]
vdist_bank.append([hemi / hemi.max() for hemi in temp_dists])
# Save vertices, channel names
vertNum_bank.append(vertices)
if(saveIndividualInfo):
'''
savemat(op.join(subjectDir, subj, subj + '_cache.mat'),
{'fwd_bank': fwd_bank, 'fwdMat_bank': fwdMat_bank})
'invMat_bank': invMat_bank, 'noiseCov_bank': noiseCov_bank,
'vdist_bank': vdist_bank, 'fakeEvoked_bank': fakeEvoked_bank,
'label_bank': label_bank})
'''
print '... ' + 'Done (' + str(si + 1) + '/' + str(len(subjects)) + ')'
else:
# Load all the subject info banks (instead of calculating them)
print 'Loading fwd, inv, noise cov, etc:'
'''
if not op.exists(subjTxt_fname) or not op.exists(subjBank_fname):
raise Exception('Missing bank file(s).')
with open(subjTxt_fname, 'r') as f_txt:
expected_subjects = [line.rstrip('\n') for line in f_txt]
f_txt.close()
if expected_subjects != subjects:
raise Exception('Cached subject bank does not match subjects being analyzed.')
with open(subjBank_fname, 'r') as f:
[fwd_bank, fwdMat_bank, invMat_bank, noiseCov_bank,
conv_bank, vdist_bank, fakeEvoked_bank, label_bank] = cPickle.load(f)
f.close()
print' Banks Loaded'
'''
# Compute or load conversion matrices
if(loadConvBank):
print 'Loading Conversion Matrices.'
conv_dict = loadmat(op.join(cache_fname, 'convBank_python.mat'))
conv_bank = conv_dict['convBank_python']
conv_bank = conv_bank[:len(subjects), :len(subjects)]
convSrc_bank = conv_dict['convBankSrc_python']
convSrc_bank = convSrc_bank[:len(subjects), :len(subjects)]
else:
print 'Computing Conversion Matrices:'
for sFrom, subjFrom in enumerate(subjects):
tempConv = []
tempConvSrc = []
#noiseIn = np.reshape(sensorNoise[sTo], (fwdMat_bank[sTo].shape[0], -1))
#covIn = np.cov(noiseIn)
covIn = noiseCov_bank[sFrom]['data']
levelIn = np.mean(np.sqrt(np.diag(covIn)))
for sTo, subjTo in enumerate(subjects):
if subjFrom != subjTo:
convMat = mne.compute_morph_matrix(subjFrom, subjTo,
vertices_from=vertNum_bank[sFrom],
vertices_to=vertNum_bank[sTo],
smooth=n_smooth)
# Forward * Src Conversion * Inverse
fullConvMat = np.dot(fwdMat_bank[sTo], convMat.A).dot(
invMat_bank[sFrom])
# Continue magnitude ratio calculation
transCov = fullConvMat.dot(covIn.dot(fullConvMat.T))
levelTrans = np.mean(np.sqrt(np.diag(transCov)))
levelRatio[sFrom, sTo] = levelIn / levelTrans
tempConv.append(levelRatio[sFrom, sTo] * fullConvMat)
tempConvSrc.append(convMat)
print ' ' + subjFrom + ' to ' + subjTo + ' ... Done',
print ' [' + str(sFrom) + ']' + '[' + str(sTo) + '] ' + str(fullConvMat.shape)
else:
tempConv.append([])
tempConvSrc.append([])
conv_bank.append(tempConv)
convSrc_bank.append(tempConvSrc)
#####################################
if(saveConvBank):
print 'Saving Conversion Matrices'
savemat(op.join(cache_fname, 'convBank_python.mat'),
{'convBank_python': conv_bank,
'convBankSrc_python': convSrc_bank})
'''
# Save all the subject info banks
if not op.exists(cache_fname):
makedirs(cache_fname)
with open(subjTxt_fname, 'w') as f_txt:
f_txt.writelines([subj + '\n' for subj in subjects])
f_txt.close()
#Open file and dump data
#for bank_fname in file_banks:
with open(subjBank_fname, 'w') as f:
cPickle.dump([fwd_bank, fwdMat_bank, invMat_bank, noiseCov_bank,
conv_bank, vdist_bank, fakeEvoked_bank, label_bank], f)
f.close()
print 'Banks Saved\n'
'''
###########################################################################
# Activity Simulation
rng = np.random.RandomState()
databank = np.zeros((len(labelList), len(trial_counts),
len(subjects), 2 * max(trial_counts),
fwd_bank[0]['nchan']))
print 'Simulating/Classifying Data'
### Iteration guide
# trials - number of training trials for the classifier
# Labels - each label in the parcellation
#SNR - several signal to noise ratios
#repeats- number of times to repeat the classification task
#subj - make each subj the subject of interest one time
for ti, n_trials in enumerate(trial_counts):
print ' ' + str(n_trials) + ' Trial Group [',
sys.stdout.flush()
current = np.ones((1, n_trials)) * current_mag
# nLabels x nRepeats x nSubjs x off and on classification
unweighted_logRatioBlock = np.zeros((len(labelList), len(poolSizes),
repeats, len(subjects),
2 * n_trials))
unweighted_accuracyBlock = np.zeros((len(labelList), len(poolSizes),
repeats, len(subjects)))
C_optimum = np.zeros((len(labelList), len(poolSizes), repeats,
len(subjects)))
g_optimum = np.zeros((len(labelList), len(poolSizes), repeats,
len(subjects)))
centroid_logRatioBlock = np.zeros((len(labelList), len(poolSizes),
repeats, len(subjects),
2 * n_trials))
centroid_accuracyBlock = np.zeros((len(labelList), len(poolSizes),
repeats, len(subjects)))
kde_logRatioBlock = np.zeros((len(labelList), len(poolSizes), repeats,
len(subjects), 2 * n_trials))
kde_accuracyBlock = np.zeros((len(labelList), len(poolSizes), repeats,
len(subjects)))
for li, label in enumerate(labelList):
for pi, poolSize in enumerate(poolSizes):
for ri in range(repeats):
trialBlock = []
powerMeas = []
for si, subj in enumerate(subjects):
# Generate evoked data (sensor space)
evoked_template = fakeEvoked_bank[si]
#######################################################
# Generate and store evoked data for one subject
tempHemi = ([0, 1], [1, 0])[label.hemi == 'rh']
labelInd = [l.name for l in label_bank[si][tempHemi[0]]].index(label.name)
stc = generate_stc(src=fwd_bank[si]['src'],
labels=[label_bank[si][tempHemi[0]][labelInd]],
stc_data=current, tmin=0, tstep=tstep)
evoked = mne.simulation.generate_evoked(fwd_bank[si], stc, evoked_template,
noiseCov_bank[si],
snr=snr, random_state=rng)
evoked_sig = mne.simulation.generate_evoked(fwd_bank[si], stc, evoked_template,
noiseCov_bank[si],
snr=np.inf, random_state=rng)
# generate evoked data by subtracting pure signal from
# evoked data
#evoked_0.data -= evoked_0_sig.data
trialBlock.append(np.array([(evoked.data - evoked_sig.data).T,
evoked.data.T]))
###########################################################
# Morph all data between all subjects
morphedData = []
for soi in range(len(subjects)):
# Get index subjects whose data will be morphed
otherSubjs = np.delete(range(len(subjects)), soi)
morphedData1Subj = np.empty((len(otherSubjs), 2,
n_trials,
len(fwdMat_bank[soi])))
# Morph data from all subjects to subj of interest
# (fwd * (conv * (inv * data)))
for ind, sj in enumerate(otherSubjs):
#make matrix to convert sensor data
#tempConverter = conv_bank[sj][soi]
morphedData1Subj[ind, 0, :, :] = \
conv_bank[sj][soi].dot(trialBlock[sj][0, :, :].T).T
#np.dot(tempConverter, trialBlock[sj][1, :, :].T).T
morphedData1Subj[ind, 1, :, :] = \
conv_bank[sj][soi].dot(trialBlock[sj][1, :, :].T).T
#np.dot(tempConverter, trialBlock[sj][1, :, :].T).T
# Morphed Data is [soi] x (nOtherSubj x on/off x trials x electrodes
morphedData.append(morphedData1Subj)
###########################################################
# BEGIN POOLED TRAINING
for soi in range(len(subjects)):
# Subselect training data to modify pool size
poolInds = random.sample(range(len(morphedData[soi])),
poolSize)
# Training data comes from other subjects
train_0 = np.reshape(morphedData[soi][poolInds][:, 0, :, :], (-1, fwdMat_bank[soi].shape[0]))
train_1 = np.reshape(morphedData[soi][poolInds][:, 1, :, :], (-1, fwdMat_bank[soi].shape[0]))
train_pool = np.r_[train_0, train_1]
y_train_pool = np.r_[np.zeros(len(train_0), dtype=np.int8),
np.ones(len(train_1), dtype=np.int8)]
# Test data comes from subject of interest
test_0 = trialBlock[soi][0, :, :]
test_1 = trialBlock[soi][1, :, :]
test_pool = np.r_[test_0, test_1]
y_test_pool = np.r_[np.zeros(len(test_0), dtype=np.int8),
np.ones(len(test_1), dtype=np.int8)]
#######################################################
### Unweighted Classifier
# For each subject, train on all other subjects and
# then test on subject of interest
if(clfScheme == clfSchemes[0]):
# Train and test LDA algorithm
clf_unwt = LDA()
clf_unwt.fit(train_pool, y_train_pool, store_covariance=False)
unweighted_accuracyBlock[li, pi, ri, soi] = \
clf_unwt.score(test_pool, y_test_pool)
elif(clfScheme == clfSchemes[1]):
# Train and test Regularized LDA algorithm
ldaFactors = ldaReg(train_pool, y_train_pool, regFactors)[:, :, 0]
test_pool_unwt = np.c_[np.ones((len(y_test_pool), 1)), test_pool]
LDAOutput = test_pool_unwt.dot(ldaFactors)
pred_unwt = ((LDAOutput[:, 1] - LDAOutput[:, 0]) > 0) * 1
unweighted_accuracyBlock[li, pi, ri, soi] = \
np.mean(pred_unwt == y_test_pool)
'''
# Train and test Regularized LDA algorithm
weights_all = ldaReg(train_pool, y_train_pool, regFactors)
test_pool_unwt = np.c_[np.ones((len(y_test_pool), 1)), test_pool]
for i in range(len(regFactors)):
LDAOutput = test_pool_unwt.dot(weights_all[:, :, i])
pred_unwt = ((LDAOutput[:, 1] - LDAOutput[:, 0]) > 0) * 1
unweighted_accuracyBlock[li, pi, ri, soi, i] = \
np.mean(pred_unwt == y_test_pool)
'''
elif(clfScheme == clfSchemes[2]):
# Train and test Support Vector Machine algorithm
# Scale inputs to [-1 1] as SVM is scale sensitive
scaler_unwt = preprocessing.data.StandardScaler().fit(train_pool)
train_pool_unwt = scaler_unwt.transform(train_pool)
test_pool_unwt = scaler_unwt.transform(test_pool)
clf_unwt = SVC(cache_size=2048)
'''
###################################################
# Grid Search
param_grid = [{'kernel': ['linear'], 'C': C_range}]
#cv = StratifiedKFold(y=y_train_pool, n_folds=3)
gridSearch = GridSearchCV(clf_unwt, param_grid=param_grid,
pre_dispatch=n_jobs)
gridSearch.fit(train_pool_unwt, y_train_pool)
#gridSearch.fit(train_pool, y_train_pool)
unweighted_accuracyBlock[li, pi, ri, soi] = \
gridSearch.score(test_pool_unwt, y_test_pool)
#print('Best Classifier is: ', gridSearch.best_estimator_)
C_optimum[li, pi, ri, soi] = gridSearch.best_estimator_.C
#g_optimum[li, pi, ri, soi] = gridSearch.best_estimator_.gamma
'''
###################################################
# Set parameter estimation
#clf_unwt.set_params(kernel='rbf', C=1000, gamma=5e-5
clf_unwt.set_params(kernel='linear', C=1e-1)
clf_unwt.fit(train_pool_unwt, y_train_pool)
unweighted_accuracyBlock[li, pi, ri, soi] = \
clf_unwt.score(test_pool_unwt, y_test_pool)
'''
y_pred_log_probs = lda.predict_log_proba(test_pool_scaled)
unweighted_logRatioBlock[li, pi, ri, soi, :] = \
(y_pred_log_probs[:, 0] - y_pred_log_probs[:, 1])
'''
#######################################################
### Centroid classifier
# Find label center and calculate centroid distances
h = ([0, 1], [1, 0])[label.hemi == 'rh']
labelInd = [l.name for l in label_bank[soi][h[0]]].index(label.name)
# Get label mean position for soi and find vertex closest to center
labelAvgPos = np.reshape(np.mean(a=label_bank[soi][h[0]][labelInd].pos, axis=0),
(-1, 3))
centerVertInd = np.argmin(cdist(labelAvgPos, vertPos_bank[soi][h[0]]))
# Pull distances from the most central point
dists = vdist_bank[soi][h[0]][centerVertInd]
# Make sure we calculate for both hemispheres
if(h[0] == 0):
dists = np.r_[dists, np.ones(len(vdist_bank[soi][h[1]])) * 10 * max(dists)]
else:
dists = np.r_[np.ones(len(vdist_bank[soi][h[1]])) * 10 * max(dists), dists]
centroidSrc_weights = np.exp(expFactor_centroid * dists ** 2)
centroid_weights = fwdMat_bank[soi].dot(centroidSrc_weights)
centroid_weights = np.abs(centroid_weights) / np.max(np.abs(centroid_weights))
if(clfScheme == clfSchemes[0]):
# Weight training and testing matrices
train_pool_cent = train_pool * centroid_weights
test_pool_cent = test_pool * centroid_weights
# Train and test LDA algorithm
clf_cent = LDA()
clf_cent.fit(train_pool_cent, y_train_pool,
store_covariance=False)
centroid_accuracyBlock[li, pi, ri, soi] = \
clf_cent.score(test_pool_cent, y_test_pool)
elif(clfScheme == clfSchemes[1]):
# Weight training and testing matrices
train_pool_cent = train_pool * centroid_weights
test_pool_cent = test_pool * centroid_weights
# Train and test Regularized LDA algorithm
ldaFactors = ldaReg(train_pool_cent, y_train_pool,
regFactors)[:, :, 0]
test_pool_cent = np.c_[np.ones((len(y_test_pool), 1)), test_pool_cent]
LDAOutput = test_pool_cent.dot(ldaFactors)
pred_cent = ((LDAOutput[:, 1] - LDAOutput[:, 0]) > 0) * 1
centroid_accuracyBlock[li, pi, ri, soi] = \
np.mean(pred_cent == y_test_pool)
elif(clfScheme == clfSchemes[2]):
### Train and test SVM
# Scale inputs to [-1 1] as SVM is scale sensitive
scaler_cent = preprocessing.data.StandardScaler().fit(train_pool)
train_pool_cent = scaler_cent.transform(train_pool) * centroid_weights
test_pool_cent = scaler_cent.transform(test_pool) * centroid_weights
clf_cent = SVC(cache_size=2048)
###################################################
# Grid Search
param_grid = [{'kernel': ['linear'], 'C': C_range}]
#cv = StratifiedKFold(y=y_train_pool, n_folds=3)
gridSearch = GridSearchCV(clf_cent,
param_grid=param_grid,
pre_dispatch=n_jobs)
gridSearch.fit(train_pool_cent, y_train_pool)
#gridSearch.fit(train_pool, y_train_pool)
centroid_accuracyBlock[li, pi, ri, soi] = \
gridSearch.score(test_pool_cent, y_test_pool)
'''
###################################################
# Set parameter estimation
#clf_cent.set_params(kernel='rbf', C=1000, gamma=5e-5
clf_cent.set_params(kernel='linear', C=1000)
clf_cent.fit(train_pool_cent, y_train_pool)
centroid_accuracyBlock[li, ri, soi] = \
clf_cent.score(test_pool_cent, y_test_pool)
'''
'''
#print('Best Classifier is: ', gridSearch.best_estimator_)
C_optimum[li, ri, soi] = gridSearch.best_estimator_.C
#g_optimum[li, ri, soi] = gridSearch.best_estimator_.gamma
'''
#######################################################
### KDE classifier
convertedLabelInds = np.zeros((len(invMat_bank[soi]),
len(otherSubjs)))
otherSubjs = np.delete(range(len(subjects)), soi)
# Get vertices for all subjects for the given label
for ind, otherSubj in enumerate(otherSubjs):
#hemi = 0 if label_bank[otherSubj][0][labelInd].hemi == 'lh' else 1
tempHemi = ([0, 1], [1, 0])[label.hemi == 'rh']
labelInds = label_bank[otherSubj][tempHemi[0]][labelInd].vertices
# generate binary index list that has 1 at inds where the label is
existingVerts = np.r_[np.in1d(vertNum_bank[otherSubj][tempHemi[0]], labelInds) * 1,
np.zeros(len(vertNum_bank[otherSubj][1]))]
# Convert vertices from all other subjects to soi
#convertedLabelInds[:, ind] = convSrc_bank[otherSubj][soi].dot(existingVerts)
convertedLabelInds[:, ind] = np.sum((convSrc_bank[otherSubj][soi].A)[:, existingVerts > 0], axis=1)
dipoleMags = np.mean(convertedLabelInds, axis=1)
hemiMags = [dipoleMags[:len(vdist_bank[soi][0])],
dipoleMags[len(vdist_bank[soi][0]):]]
# Compute KDE exponential weight
hemiWeights = []
for hemi in [0, 1]:
hemiWeights.append(np.sum((np.exp(expFactor_KDE *
vdist_bank[soi][hemi]) *
hemiMags[hemi]), axis=1))
kdeSrc_weights = np.r_[hemiWeights[0], hemiWeights[1]]
kde_weights = fwdMat_bank[soi].dot(kdeSrc_weights)
kde_weights = np.abs(kde_weights) / np.max(np.abs(kde_weights))
if(clfScheme == clfSchemes[0]):
# Weight training and testing matrices
train_pool_kde = train_pool * kde_weights
test_pool_kde = test_pool * kde_weights
# Train and test LDA algorithm
clf_kde = LDA()
clf_kde.fit(train_pool_kde, y_train_pool, store_covariance=False)
kde_accuracyBlock[li, pi, ri, soi] = \
clf_kde.score(test_pool_kde, y_test_pool)
elif(clfScheme == clfSchemes[1]):
# Weight training and testing matrices
train_pool_kde = train_pool * kde_weights
test_pool_kde = test_pool * kde_weights
# Train and test Regularized LDA algorithm
ldaFactors = ldaReg(train_pool_kde, y_train_pool, regFactors)[:, :, 0]
test_pool_kde = np.c_[np.ones((len(y_test_pool), 1)), test_pool_kde]
LDAOutput = test_pool_kde.dot(ldaFactors)
pred_kde = ((LDAOutput[:, 1] - LDAOutput[:, 0]) > 0) * 1
kde_accuracyBlock[li, pi, ri, soi] = \
np.mean(pred_kde == y_test_pool)
elif(clfScheme == clfSchemes[2]):
### Train and test SVM
# Scale inputs to [-1 1] as SVM is scale sensitive
scaler_kde = preprocessing.data.StandardScaler().fit(train_pool)
train_pool_kde = scaler_kde.transform(train_pool) * kde_weights
test_pool_kde = scaler_kde.transform(test_pool) * kde_weights
clf_kde = SVC(cache_size=2048)
###################################################
# Grid Search
param_grid = [{'kernel': ['linear'], 'C': C_range}]
#cv = StratifiedKFold(y=y_train_pool, n_folds=3)
gridSearch = GridSearchCV(clf_kde,
param_grid=param_grid,
pre_dispatch=n_jobs)
gridSearch.fit(train_pool_kde, y_train_pool)
#gridSearch.fit(train_pool, y_train_pool)
kde_accuracyBlock[li, pi, ri, soi] = \
gridSearch.score(test_pool_kde, y_test_pool)
C_optimum[li, pi, ri, soi] = gridSearch.best_estimator_.C
#print('Best Classifier is: ', gridSearch.best_estimator_)
#g_optimum[li, ri, soi] = gridSearch.best_estimator_.gamma
'''
###################################################
# Set parameter estimation
#clf_kde.set_params(kernel='rbf', C=1000, gamma=5e-5
clf_kde.set_params(kernel='linear', C=1000)
clf_kde.fit(train_pool_kde, y_train_pool)
kde_accuracyBlock[li, pi, ri, soi] = \
clf_kde.score(test_pool_kde, y_test_pool)
'''
print '=',
sys.stdout.flush()
print '] Done (' + str(ti + 1) + '/' + str(len(trial_counts)) + ')'
scoreDict['logRatio']['kde'].append(kde_logRatioBlock)
scoreDict['logRatio']['centroid'].append(centroid_logRatioBlock)
scoreDict['logRatio']['unweighted'].append(unweighted_logRatioBlock)
scoreDict['accuracy']['kde'].append(kde_accuracyBlock)
scoreDict['accuracy']['centroid'].append(centroid_accuracyBlock)
scoreDict['accuracy']['unweighted'].append(unweighted_accuracyBlock)
# Save performance results
if saveAnalysis:
# Pickle non-matrix objects
with open(op.join(cache_fname, 'nSubjGainMeanDataDict.pkl'), 'wb') as outfile:
cPickle.dump([scoreDict, weightMethods, labelList], outfile)
savemat(op.join(cache_fname, 'nSubjGainSimResults.mat'), {'snr': np.array(snr),
'trial_counts': np.array(trial_counts),
'poolSizes': np.array(poolSizes)})
elapsed_time = time() - start_time
m, s = divmod(elapsed_time, 60)
h, m = divmod(m, 60)
print 'Finished @ \t' + strftime('%H:%M:%S')
print 'Time Elasped: \t' + '%d:%02d:%02d' % (h, m, s)
else:
print 'Loading Analysis ',
# If we're not redoing the analysis, load performance results
# nSubjGainMeanDataDict contains complete score info (scoreDict),
# weightMethods, and labelList
# By default, will use label list from parameter section at beginning of
# script to define labels to plot
# scoreDict['accuracy']['classification method'] is list (of arrays) of length trialCounts
# Each array is (Label) x (Pool Size) x (Repeats) x (Subjects)
with open(op.join(cache_fname, 'nSubjGainMeanDataDict.pkl'), 'rb') as infile:
[scoreDict, weightMethods, loadedLabelList] = cPickle.load(infile)
# nSubjGainSimResults contains SNR, trial_counts, poolSizes
storedMat = loadmat(op.join(cache_fname, 'nSubjGainSimResults.mat'))
snr = np.atleast_1d(np.squeeze(storedMat['snr']))[0]
trial_counts = list(np.atleast_1d(np.squeeze(storedMat['trial_counts'])))
poolSizes = list(np.atleast_1d(np.squeeze(storedMat['poolSizes'])))
print ' ... Done'
###############################################################################
# Plot Prep
meanDataDict = {'logRatio': {}, 'accuracy': {}, 'stddev': {}}
labelDataDict = {'logRatio': {}, 'accuracy': {}, 'stddev': {}}
# Get mean accuracys for each classification method
for keyInd in range(len(scoreDict['accuracy'])):
tempScores = [np.mean(scoreDict['accuracy'][weightMethods[keyInd]][i], axis=(0, 2, 3))
for i in range(len(trial_counts))]
#for i in range(len(scoreDict['accuracy'][weightMethods[keyInd]]))]
tempStddev = [np.std(scoreDict['accuracy'][weightMethods[keyInd]][i], axis=(0, 2, 3))
for i in range(len(trial_counts))]
tempScores2 = [np.mean(scoreDict['accuracy'][weightMethods[keyInd]][i], axis=(2, 3))
for i in range(len(trial_counts))]
tempSEM2 = [sem(scoreDict['accuracy'][weightMethods[keyInd]][i], axis=(2))
for i in range(len(trial_counts))]
tempSEM2 = [np.mean(tempSEM2[i], axis=(2)) for i in range(len(trial_counts))]
# each classification method is trials
meanDataDict['accuracy'][weightMethods[keyInd]] = 100 * np.array(tempScores)
meanDataDict['stddev'][weightMethods[keyInd]] = 100 * np.array(tempStddev)
labelDataDict['accuracy'][weightMethods[keyInd]] = 100 * np.array(tempScores2)
labelDataDict['stddev'][weightMethods[keyInd]] = 100 * np.array(tempSEM2)
####################################
# Load label performances that used standard training (subject-specific)
# REQUIRES STD ANALYSIS IN pooledBCI_Sens.py HAS BEEN RUN
# Load data not pickled
storedMatStd = loadmat(op.join(cache_fname, 'simResults.mat'))
trial_countsStd = (np.squeeze(storedMatStd['trial_counts'])).tolist()
#accuracyDif = storedMat['accuracyDif']
#deltaAccuracy = storedMat['deltaAccuracy']
snrsStd = np.squeeze(storedMatStd['snrs']).tolist()
# Load pickled data from pooledBCI_Sens.py results
with open(op.join(cache_fname, 'stdScoreDict.pkl'), 'rb') as infile:
[stdDataDict, stdLabelList] = cPickle.load(infile)
stdLabelNames = [l.name for l in stdLabelList]
# Find labels from standard evaluation matching the label names here
# Account for names like G_precentral...-lh vs lh.G_precent...
labelInds = []
for label in labelList:
try:
labelInds.append(stdLabelNames.index(label.name))
except:
print 'Missing label ' + label.name
try:
closeName = label.name[0:-3]
ind = [i for i in range(len(stdLabelNames))
if closeName in stdLabelNames[i]]
if len(ind) == 1:
labelInds.append(ind[0])
print 'Close label: \'' + stdLabelNames.index[labelInds[0]] + '\' being used'
except:
'Missing label ' + label.name + ' and no close match found'
assert labelInds > 0, 'No labels found in std performance list.'
# Average label percentages across subjects/repeats and zip with label names
# stdPerf is (nBCILabels, nRepeats, nSubjects)
stdPerf = stdDataDict[trial_countsStd.index(40)][labelInds, snrsStd.index(-10), :, :]
stdPerfLabels = zip(np.mean(stdPerf, axis=(1, 2)), [stdLabelNames[ind] for ind in labelInds])
###############################################################################
# Plots
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable
import glob
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes, mark_inset
from pooledBCI_plotNSubjectGain import pooledBCI_plotNSubjectGain
from pooledBCI_plotPerfPanel import pooledBCI_plotPerfPanel
plt.close('all')
plt.ion()
plt.rcParams['pdf.fonttype'] = 42
classificationDetails = 'Source ' + clfScheme + ': ' + str(regFactors[0]) + \
' nSubjects=' + str(len(subjects)) + ' nLabels= ' + str(len(labelList))
nPlots = len(meanDataDict['accuracy'])
################################################################################
## Figure 0: Absolute performance
if 0 in plotsToGen:
print 'Plotting Figure 0',
# Call Figure 0 Function
#fig0 = pooledBCI_plotNSubjectGain(meanDataDict, poolSizes, trial_counts, weightMethods)
nPlots = len(trial_counts)
difMin, difMax = -10., 10.
ftsize = 14
figSize = (7 * nPlots, 7)
plotMarker = ['o', 'D', 's', '^']
plotColors = ['DodgerBlue', 'ForestGreen', 'Maroon', 'DarkMagenta']
fig0, axList = plt.subplots(ncols=nPlots, figsize=figSize)
fig0.tight_layout(h_pad=.5)
fig0.subplots_adjust(left=0.04, right=0.975, bottom=0.1, top=0.8)
'''
axesPad = .5
gridAx = ImageGrid(fig0, 111, nrows_ncols=(1, nPlots), axes_pad=axesPad,
share_all=True, label_mode="L", direction='row', add_all=True)
'''
for i in range(len(trial_counts)):
for j, method in enumerate(weightMethods):
axList[i].plot(poolSizes, meanDataDict['accuracy'][method][i, :], c=plotColors[j], linewidth=3,
markersize=11, markeredgewidth=1, alpha=.8, label=weightMethods[j])
if i == 0:
axList[i].set_ylabel('Accuracy (%)', fontsize=ftsize + 3)
#axList[i].text((poolSizes[-1] + poolSizes[-2])/2, 51, 'Chance', va='bottom', color='red', fontsize=ftsize + 4,
# ha='center')
axList[i].legend(loc='upper left', fancybox=True, shadow=True,
borderaxespad=1)
#axList[i].axhline(y=50, color='r', linewidth=4, ls='--', alpha=0.8)
axList[i].set_title(str(trial_counts[i]) + ' Trials/Subject',
fontsize=ftsize + 5)
axList[i].set_xlabel('Pool Size (subjects)', fontsize=ftsize + 3)
axList[i].set_xticks(poolSizes)
axList[i].set_yticks(range(65, 90, 5))
axList[i].xaxis.set_ticks_position('bottom')
axList[i].grid()
#gridAx.axes_llc.set_xticks(poolSizes)
#gridAx.axes_llc.set_xticklabels(poolSizes, fontsize=ftsize)
plt.suptitle('Accuracy for Different Training Pool Sizes\nSNR: ' +
str(snr), fontsize=ftsize + 10)
print ' ... Done'
###############################################################################
## Figure 1: BCI area performance as a function of changing pool size
if 1 in plotsToGen:
print 'Plotting Figure Set 1',
mpl.pyplot.autoscale(enable=False)
fig1List = []
surf = 'smoothwm'
surf = 'inflated_pre'
kdeData = labelDataDict['accuracy']['kde'][0]
kdeStddev = labelDataDict['stddev']['kde'][0]
### P300 ROIs, Dodger Blue and Lime Green
if len(labelNames_p300) > 0:
colors = ['#84E184', '#196619', '#1565B2', '#4BA6FF', '#061D33',
'#BCDEFF']
views = ['l', 'd', 'p']
legend = [['P300a (2)', '#32CD32'], ['P300b (4)', '#1E90FF']]
axRange = np.arange(75., 90., 5.)
axRange = np.insert(axRange, 0, 72.5)
axRange = np.append(axRange, 87.5)
# Find standard (subject specific) test results for each label
stdInds = [[zipLabel for zipPerf, zipLabel in stdPerfLabels].index(l)
for l in labelNames_p300]
stdPerfs_y = [stdPerfLabels[i][0] * 100 for i in stdInds]