forked from IntrAnatSEEGSoftware/IntrAnat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patientdatafilterwidget.py
1148 lines (957 loc) · 59.3 KB
/
patientdatafilterwidget.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Widget to select patients from the DB using available data as filters : needs an access
# to brainvisa database that is already set up ! (brainvisa axon.initializeProcesses()
#
# (c) Inserm U836 2012-2014 - Manik Bhattacharjee
#
# License GNU GPL v3
#
#
from PyQt4 import QtGui, QtCore, uic
import sys, json, pickle, numpy, csv
import math
from collections import OrderedDict
import pdb
from soma import aims
from brainvisa import axon
from brainvisa.data.readdiskitem import ReadDiskItem
from brainvisa.data.writediskitem import WriteDiskItem
from readElecLocalCSVFile import readElecLocalCSVFile
from readSulcusLabelTranslationFile import *
class PatientDataFilterWidget (QtGui.QWidget):
def __init__(self, app=None):
QtGui.QWidget.__init__(self)
self.ui = uic.loadUi("patientDataFilter.ui", self)
self.pushedWidgets = []
self.subjects = {}
#self.selectedSubjects=[]
self.filters=[]
self.filtersAtlases = {}
self.pushedWidget = []
self.greenDotIcon = QtGui.QIcon('greendot.png')
self.filterProtocolCombo.currentIndexChanged.connect(self.updatePatientFilters)
self.filterSiteCombo.currentIndexChanged.connect(self.updatePatientFilters)
self.filterYearCombo.currentIndexChanged.connect(self.updatePatientFilters)
self.filterLocaMarsAtlas.currentIndexChanged.connect(self.updatePatientFilters)
self.filterCognitionLoca.currentIndexChanged.connect(self.updatePatientFilters)
self.filterCognitionType.currentIndexChanged.connect(self.updatePatientFilters)
self.filterResection.currentIndexChanged.connect(self.updatePatientFilters)
self.filterStimulationLoca.currentIndexChanged.connect(self.updatePatientFilters)
self.filterStimulationFreq.currentIndexChanged.connect(self.updatePatientFilters)
self.filterStimulationType.currentIndexChanged.connect(self.updatePatientFilters)
self.FilterAtlascomboBox.currentIndexChanged.connect(self.changeAtlasfiltration)
self.filterAddPatientButton.clicked.connect(lambda:self.moveSelectedItemsToOtherListWidget(self.filteredPatientList, self.selectedPatientList))
self.filterRemovePatientButton.clicked.connect(lambda:self.moveSelectedItemsToOtherListWidget(self.selectedPatientList, self.filteredPatientList))
self.filterAddGreenPatientButton.clicked.connect(lambda:self.moveFilteredItemsToOtherListWidget(self.filteredPatientList, self.selectedPatientList, self.filters, reverse=False))
self.filterRemoveRedPatientButton.clicked.connect(lambda:self.moveFilteredItemsToOtherListWidget(self.selectedPatientList, self.filteredPatientList, self.filters, reverse=True))
self.patientSelectAllButton.clicked.connect(lambda:self.selectAllListItems(self.filteredPatientList, True) )
self.patientDeselectAllButton.clicked.connect(lambda:self.selectAllListItems(self.filteredPatientList, False))
self.selectedSelectAllButton.clicked.connect(lambda:self.selectAllListItems(self.selectedPatientList, True))
self.selectedDeselectAllButton.clicked.connect(lambda:self.selectAllListItems(self.selectedPatientList, False))
self.t1preButton.clicked.connect(lambda: self.toggleFilterWidget('T1pre', self.t1preButton))
self.t1postButton.clicked.connect(lambda: self.toggleFilterWidget('T1post', self.t1postButton))
self.t2preButton.clicked.connect(lambda: self.toggleFilterWidget('T2pre', self.t2preButton))
self.t2postButton.clicked.connect(lambda: self.toggleFilterWidget('T2post', self.t2postButton))
self.petpreButton.clicked.connect(lambda: self.toggleFilterWidget('PETpre', self.petpreButton))
self.ctpostButton.clicked.connect(lambda: self.toggleFilterWidget('CTpost', self.ctpostButton))
self.implantationButton.clicked.connect(lambda: self.toggleFilterWidget('implantation', self.implantationButton))
self.mniButton.clicked.connect(lambda: self.toggleFilterWidget('MNI', self.mniButton))
self.mniParcelsButton.clicked.connect(self.mniParcelsGeneration)
self.seegButton.clicked.connect(self.toggleSeeg)
self.seegManipCombo.currentIndexChanged.connect(self.seegManipChanged)
self.t1preButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.t1postButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.t2preButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.t2postButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.petpreButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.ctpostButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.implantationButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.mniButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.seegButton.setStyleSheet("background-color: rgb(90, 90, 90);")
self.populateFromDB()
def getSelectedPatientsNames(self):
"""Returns a list of names (string) of all selected patients"""
return sorted([str(self.selectedPatientList.item(i).text()) for i in xrange(self.selectedPatientList.count())])
def getSelectedPatients(self):
""" Returns a list of ReadDiskItems representing the selected subjects"""
return [self.subjects[p]['rdi'] for p in self.getSelectedPatientsNames()]
def findExistingVolumeAcquisitions(self, volType, subjectName, subjectData):
"""For a subjectName, find all acquisitions in the DB of the type volType ('Raw T1 MRI') and store them in subjectData[acquisition]"""
rdi = ReadDiskItem( volType, 'aims readable volume formats', requiredAttributes={'subject':subjectName, 'center':subjectData['center']} )
volumes = (list( rdi._findValues( {}, None, False ) ))
for v in volumes:
try:
acq = v.attributes()['acquisition'].split('_')[0]
subjectData[str(acq)] = v
except:
print "Acquisition not defined for "+repr(v.fullPath())
def populateFromDB(self):
"""Fills the list of patients and necessary information about patients to be able to filter"""
rdi = ReadDiskItem( 'Subject', 'Directory',requiredAttributes={'_ontology':'brainvisa-3.2.0'} )
subjects = list( rdi._findValues( {}, None, False ) )
self.subjects = dict([(s.attributes()['subject'], {'rdi':s, 'center':s.attributes()['center']}) for s in subjects])
protocols = list(set([s.attributes()['center'] for s in subjects]))
# Fill the combos
self.filterProtocolCombo.clear()
self.filterProtocolCombo.addItems(['*',]+sorted(protocols))
# Update the basic filters
sites = ['*',] + sorted(set([s.split('_')[0] for s in self.subjects]))
years = ['*',] + sorted(set([s.split('_')[1] for s in self.subjects if len(s.split('_')) > 1]))
loca = ['*']
loca_FS = ['*']
loca_BM = ['*']
cogniType = ['*']
resec = ['*']
resec_FS = ['*']
resec_BM = ['*']
stimFreq = ['*']
stimType = ['*']
self.filterSiteCombo.clear()
self.filterSiteCombo.addItems(sites)
self.filterYearCombo.clear()
self.filterYearCombo.addItems(years)
#self.filterLocaMarsAtlas.addItems(loca)
#self.filterCognition.addItems(cogni)
# Get info for all filters
seegManips = set()
for s,subj in self.subjects.iteritems():
print(s)
# Find the T1s/T2s/CT/PET
for modality in ['T2 MRI', 'CT', 'PET', 'Raw T1 MRI']:
self.findExistingVolumeAcquisitions(modality, s, subj)
# Find MNI transform if T1pre is there
if 'T1pre' in subj:
rdi = ReadDiskItem( 'SPM2 normalization matrix', 'Matlab file' )
di = rdi.findValue(subj['T1pre'])
rdi2 = ReadDiskItem('SPM normalization deformation field', 'NIFTI-1 image')
di2 = rdi2.findValue(subj['T1pre'])
if di is not None:
subj['MNI'] = di
if di2 is not None:
subj['MNI'] = di2
# Find Electrode Implantation
rdi = ReadDiskItem( 'Electrode implantation', 'Electrode Implantation format', requiredAttributes={'subject':s, 'center':subj['center']} )
elecs = list(rdi._findValues( {}, None, False ) )
if len(elecs) > 0:
subj['implantation'] = elecs[0]
else:
wditxtmnipos = ReadDiskItem('Electrode Implantation Position TXT', 'Text file', requiredAttributes={'ref_name':'MNI'})
dimnipos=wditxtmnipos.findValue(subj['rdi'])
wditxtmniname = ReadDiskItem('Electrode Implantation Name TXT', 'Text file', requiredAttributes={'ref_name':'MNI'})
dimniname = wditxtmniname.findValue(subj['rdi'])
if dimnipos is not None:
subj['implantationMNI'] = (dimnipos,dimniname)
else:
wdi_csvnew = ReadDiskItem('Final Export Dictionaries','CSV file')
rdi_csvnew= wdi_csvnew.findValue(subj['rdi'])
subj['implantationCSV'] = rdi_csvnew
# Find Electrode Label
rdi_elec = ReadDiskItem('Electrodes Labels','Electrode Label Format',requiredAttributes={'subject':s, 'center':subj['center']})
list_di_elec = list(rdi_elec.findValues( {}, None, False ) )
#loca_patient = []
loca_patient = {}
loca_patient_FS = {}
loca_patient_BM = {}
loca_bipole_patient = {}
loca_bipole_patient_FS = {}
loca_bipole_patient_BM = {}
info_label_elec = None
if len(list_di_elec) > 0:
di_elec = rdi_elec.findValue(subj['T1pre'])
#subj['elec_label'] = di_elec
#read the json file
fin = open(di_elec.fullPath(),'r')
info_label_elec = json.loads(fin.read())
fin.close()
if 'plots_by_label' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_by_label'].iteritems():
if len(vv):
loca.append(kk)
#loca_patient.append(kk)
loca_patient.update({kk:vv})
if 'plots_by_label_FS' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_by_label_FS'].iteritems():
if len(vv):
loca_FS.append(kk)
#loca_patient_FS.append(kk)
loca_patient_FS.update({kk:vv})
if 'plots_by_label_BM' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_by_label_BM'].iteritems():
if len(vv):
loca_BM.append(kk)
#loca_patient_BM.append(kk)
loca_patient_BM.update({kk:vv})
#on fait la meme chose avec les bipoles pour les stims (puis la cognition)
if 'plots_bipolar_by_label' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_bipolar_by_label'].iteritems():
if len(vv):
#loca_patient.append(kk)
loca_bipole_patient.update({kk:vv})
if 'plots_bipolar_by_label_FS' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_bipolar_by_label_FS'].iteritems():
if len(vv):
#loca_patient.append(kk)
loca_bipole_patient_FS.update({kk:vv})
if 'plots_bipolar_by_label_BM' in info_label_elec.keys():
for kk,vv in info_label_elec['plots_bipolar_by_label_BM'].iteritems():
if len(vv):
#loca_patient.append(kk)
loca_bipole_patient_BM.update({kk:vv})
#add freesurfer and broadmann
subj['rdi_elec_label'] = list_di_elec[0]
else:
subj['rdi_elec_label'] = None
subj['elec_label'] = loca_patient
subj['elec_label_FS'] = loca_patient_FS
subj['elec_label_BM'] = loca_patient_BM
subj['bipole_label'] = loca_bipole_patient
subj['bipole_label_FS'] = loca_bipole_patient_FS
subj['bipole_label_BM'] = loca_bipole_patient_BM
rdi_resec_label = ReadDiskItem('Resection Description','Resection json',requiredAttributes={'subject':s, 'center':subj['center']})
list_di_resec_label = list(rdi_resec_label.findValues( {}, None, False ) )
resec_patient = []
resec_patient_FS = []
if len(list_di_resec_label) > 0:
di_resec_label = rdi_resec_label.findValue(subj['T1pre'])
fin = open(di_resec_label.fullPath(),'r')
info_label_resec = json.loads(fin.read())
fin.close()
if 'mars_atlas' in info_label_resec.keys():
for kk,vv in info_label_resec['mars_atlas'].iteritems():
if len(vv):
resec.append(kk)
resec_patient.append(kk)
#add freesurfer
if 'Freesurfer' in info_label_resec.keys():
if 'Freesurfer not calculated' in info_label_resec['Freesurfer']:
pass
else:
for kk,vv in info_label_resec['Freesurfer'].iteritems():
if len(vv):
resec_FS.append(kk)
resec_patient_FS.append(kk)
subj['rdi_resec_label'] = list_di_resec_label[0]
else:
subj['rdi_resec_label'] = None
subj['resec_label'] = resec_patient
subj['resec_label_FS'] = resec_patient_FS
# Find SEEG manips
rdi = ReadDiskItem( 'Raw SEEG recording', ['EEG TRC format', 'Elan EEG format'], requiredAttributes={'subject':s, 'center':subj['center']} )
records = list(rdi._findValues( {}, None, False ) )
if len(records) > 0:
for rec in records:
subj['seeg_'+str(rec.attributes()['experiment'])] = rec
manips = [str(rec.attributes()['experiment']) for rec in records]
seegManips.update(manips)
rdi_stimresult_label = ReadDiskItem('Electrodes SEEG Labels','Electrode sEEG Label Format',requiredAttributes={'subject':s, 'center':subj['center']})
list_di_stimresult_label = list(rdi_stimresult_label.findValues( {}, None, False ) )
seeg_label_loca = []
seeg_label_type = []
seeg_label_freq = []
seeg_label_all = []
if len(list_di_stimresult_label)>0:
by_default_list = [u'Motor',u'Sensitive',u'Sensory',u'Vegetative',u'Emotional',u'Experiencial','Superior functions']
by_default_list2 = [u'no response', u'pathological', u'seizure sensation']
full_list = by_default_list + by_default_list2
di_stimresult_label = rdi_stimresult_label.findValue(subj['T1pre'])
fin = open(di_stimresult_label.fullPath(),'r')
info_seeg_label = json.loads(fin.read())
fin.close()
#la il faut que je retourne les infos dans tous les sens c'est la merde ...
for kk,vv in info_seeg_label['contacts'].iteritems():
#seeg_label_loca =
for freqVal in info_seeg_label['contacts'][kk]['cell'].keys():
if freqVal not in stimFreq:
stimFreq.append(freqVal)
#seeg_label_freq.append(freqVal)
list_inter = [x for x in by_default_list if info_seeg_label['contacts'][kk]['cell'][freqVal][x]['value'] != 0]
list_inter2 = info_seeg_label['contacts'][kk]['cell'][freqVal][u'Type of response']['fontcolor']
max_index = list_inter2[1:].index(max(list_inter2[1:]))
try:
if list_inter2[max_index+1] == 0:
resp1 = by_default_list2[0]
elif max_index == 0:
resp1 = by_default_list2[2]
elif max_index == 2:
resp1 = by_default_list2[1]
except:
pdb.set_trace()
seeg_label_all.append((kk.title(),freqVal,resp1,list_inter))
try:
if 'MarsAtlas' in info_label_elec['plots_label_bipolar'][kk.title()].keys():
seeg_label_all[-1] = seeg_label_all[-1] + (info_label_elec['plots_label_bipolar'][kk.title()]['MarsAtlas'][1],)
else:
seeg_label_all[-1] = seeg_label_all[-1] + (u'not estimated',)
except:
pdb.set_trace()
if 'Freesurfer' in info_label_elec['plots_label_bipolar'][kk.title()].keys():
seeg_label_all[-1] = seeg_label_all[-1] + (info_label_elec['plots_label_bipolar'][kk.title()]['Freesurfer'][1],)
else:
seeg_label_all[-1] = seeg_label_all[-1] + (u'not estimated',)
if 'Broadmann' in info_label_elec['plots_label_bipolar'][kk.title()].keys():
seeg_label_all[-1] = seeg_label_all[-1] + (info_label_elec['plots_label_bipolar'][kk.title()]['Broadmann'][1],)
else:
seeg_label_all[-1] = seeg_label_all[-1] + (u'not estimated',)
#[True if info_seeg_label['contacts'][kk]['cell'][freqVal][x]['value'] != 0 else False for x in by_default_list]
#pdb.set_trace()
subj['seeg_label_all'] = seeg_label_all
if 'full_list' in locals():
stimInter = set(stimType)
full_listInter = set(full_list)
newstimtype = stimInter.union(full_listInter)
stimType = sorted(list(newstimtype))
#stimType = stimType + full_list
#remove duplicate #la on récupère les clés ? ou après ? où est-ce que je gère les infos des contacts ?
locas = list(set(loca))
locas.sort()
locas_FS = list(set(loca_FS))
locas_FS.sort()
locas_BM = list(set(loca_BM))
locas_BM.sort()
self.filterCognitionLoca.clear()
self.filterCognitionType.clear()
self.filterCognitionLoca.addItems(locas)
self.filterCognitionType.addItems(cogniType)
self.filterStimulationLoca.clear()
self.filterStimulationType.clear()
self.filterStimulationFreq.clear()
self.filterStimulationLoca.addItems(locas)
self.filterStimulationType.addItems(stimType)
self.filterStimulationFreq.addItems(stimFreq)
resecs = list(set(resec))
resecs.sort()
resecs_FS = list(set(resec_FS))
resecs_FS.sort()
resecs_BM = list(set(resec_BM))
resecs_BM.sort()
self.filtersAtlases.update({'locaMA':locas,'locaFS':locas_FS,'locaBM':locas_BM,'resecMA':resecs,'resecFS':resecs_FS,'resecBM':resecs_BM})
self.filterLocaMarsAtlas.clear()
self.filterLocaMarsAtlas.addItems(locas)
#self.filterCognition.clear()
#self.filterCognition.addItems(cognis)
self.filterResection.clear()
self.filterResection.addItems(resecs)
if len(seegManips) > 0:
self.seegManipCombo.clear()
self.seegManipCombo.addItems(sorted(list(seegManips)))
self.updatePatientFilters()
def toggleSeeg(self):
"""If SEEG is clicked, select the manip as a filter"""
manip = str(self.seegManipCombo.currentText())
if manip is None or manip == "":
return
self.toggleFilterWidget('seeg_'+manip, self.seegButton)
if 'seeg_'+manip in self.filters:
self.seegManipCombo.setItemIcon(self.seegManipCombo.currentIndex(), self.greenDotIcon)
else:
self.seegManipCombo.setItemIcon(self.seegManipCombo.currentIndex(), None)
def seegManipChanged(self, idx):
self.toggleWidget(self.seegButton, 'seeg_' + str(self.seegManipCombo.currentText()) in self.filters)
def toggleFilterWidget(self, filterName, widg):
"""Adds/Removes a filter and push/unpush the widget"""
onOrOff = self.toggleFilter(filterName)
self.toggleWidget(widg, onOrOff)
def toggleWidget(self, widg, on = True):
"""Sets the background color of the widget to green and add it to pushedWidgets, or reverse if it is already pushed"""
if not on:
widg.setStyleSheet("background-color: rgb(90, 90, 90);")
self.pushedWidget.remove(widg)
else:
widg.setStyleSheet("background-color: rgb(120, 255, 120);")
#widg.setStyleSheet("border: 2px solid #88ff88;")
self.pushedWidget.append(widg)
def toggleFilter(self, filterName, updateUi = True):
"""Sets or unsets a filter, then update the ui"""
if filterName in self.filters:
self.filters.remove(filterName)
else:
self.filters.append(filterName)
if updateUi:
self.updatePatientFilters()
return filterName in self.filters
def getFilters(self):
return self.filters
def matchFilters(self, subj, filters):
return all([subj.has_key(f) for f in filters])
def filterSubjectsBasic(self):
"""Filtering subject list using basic factors (protocol, year, site)"""
subs = self.subjects.keys()
if str(self.filterProtocolCombo.currentText()) != '*':
subs = [s for s in subs if self.subjects[s]['center'] == str(self.filterProtocolCombo.currentText())]
if str(self.filterSiteCombo.currentText()) != '*':
subs = [s for s in subs if s.split('_')[0] == str(self.filterSiteCombo.currentText())]
if str(self.filterYearCombo.currentText()) != '*':
subs = [s for s in subs if len(s.split('_')) > 1 and s.split('_')[1] == str(self.filterYearCombo.currentText())]
if self.FilterAtlascomboBox.currentIndex() == 0:
if str(self.filterLocaMarsAtlas.currentText()) != '*':
subs = [s for s in subs if 'elec_label' in self.subjects[s] and str(self.filterLocaMarsAtlas.currentText()) in self.subjects[s]['elec_label'].keys()]
if str(self.filterResection.currentText()) != '*':
subs = [s for s in subs if 'resec_label' in self.subjects[s] and str(self.filterResection.currentText()) in self.subjects[s]['resec_label']]
elif self.FilterAtlascomboBox.currentIndex() == 1:
if str(self.filterLocaMarsAtlas.currentText()) != '*':
subs = [s for s in subs if 'elec_label_FS' in self.subjects[s] and str(self.filterLocaMarsAtlas.currentText()) in self.subjects[s]['elec_label_FS'].keys()]
if str(self.filterResection.currentText()) != '*':
subs = [s for s in subs if 'resec_label_FS' in self.subjects[s] and str(self.filterResection.currentText()) in self.subjects[s]['resec_label_FS']]
elif self.FilterAtlascomboBox.currentIndex() == 2:
if str(self.filterLocaMarsAtlas.currentText()) != '*':
subs = [s for s in subs if 'elec_label_BM' in self.subjects[s] and str(self.filterLocaMarsAtlas.currentText()) in self.subjects[s]['elec_label_BM'].keys()]
if str(self.filterResection.currentText()) != '*':
subs = [s for s in subs if 'resec_label_BM' in self.subjects[s] and str(self.filterResection.currentText()) in self.subjects[s]['resec_label_BM']]
if str(self.filterStimulationLoca.currentText())!= '*' or str(self.filterStimulationFreq.currentText()) != '*' or str(self.filterStimulationType.currentText()) != '*' :
subs = [s for s in subs if 'seeg_label_all' in self.subjects[s]]
CondiLoca = str(self.filterStimulationLoca.currentText()) == '*'
CondiFreq = str(self.filterStimulationFreq.currentText()) == '*'
CondiType = str(self.filterStimulationType.currentText()) == '*'
subs_toremove = []
inter_stim = {}
for ind_sub in subs:
isthereloc = [x for x in self.subjects[ind_sub]['seeg_label_all'] if (str(self.filterStimulationLoca.currentText())==x[4+self.FilterAtlascomboBox.currentIndex()] or CondiLoca) and (str(self.filterStimulationFreq.currentText())==x[1] or CondiFreq) and (str(self.filterStimulationType.currentText())==x[2] or str(self.filterStimulationType.currentText()) in x[3] or CondiType)]
if len(isthereloc)==0:
subs_toremove.append(ind_sub)
else:
inter_stim.update({ind_sub:isthereloc})
#for bipole_info in self.sub
[subs.remove(x) for x in subs_toremove]
if str(self.filterCognitionLoca.currentText()) != '*':
print "no cognition load in the data base for now"
subs = [s for s in subs if s not in [str(self.selectedPatientList.item(idx).text()) for idx in range(self.selectedPatientList.count())]]
self.filteredPatientList.clear()
self.filteredPatientList.addItems(sorted(subs))
def colorFilterListWidget(self, listwidget, selected):
for idx in range(listwidget.count()):
it = listwidget.item(idx)
if str(it.text()) in selected:
it.setBackground(QtGui.QColor(150,255,150))
else:
it.setBackground(QtGui.QColor(255,150,150))
def updatePatientFilters(self):
"""Colors the 'patient list' and 'selected patient list' items in green if they match the filters and updates the content of patient's list"""
# Apply basic filters to the main patient list first and update the UI
self.filterSubjectsBasic()
# Filter the list of subjects and filter them
filteredPatients = [s for s,subj in self.subjects.iteritems() if self.matchFilters(subj, self.filters)]
# Color the patients according to the filters
self.colorFilterListWidget(self.filteredPatientList, filteredPatients)
self.colorFilterListWidget(self.selectedPatientList, filteredPatients)
def moveSelectedItemsToOtherListWidget(self, lwFrom, lwTo):
"""Takes the selected items of the list 'from' and adds them to the 'to' list"""
for idx in reversed(range(lwFrom.count())):
it = lwFrom.item(idx)
if lwFrom.isItemSelected(it):
lwTo.addItem(str(it.text()))
lwFrom.takeItem(idx)
lwTo.sortItems()
def moveFilteredItemsToOtherListWidget(self, lwFrom, lwTo, filters, reverse=False):
"""Takes the items from lwFrom (QListWidget) and move those that match the filters to lwTo. If reverse is true, the items that don't match will move"""
filtered = [s for s,subj in self.subjects.iteritems() if self.matchFilters(subj, filters)]
for idx in reversed(range(lwFrom.count())):
it = lwFrom.item(idx)
if (not reverse and str(it.text()) in filtered) or (reverse and str(it.text()) not in filtered):
lwTo.addItem(str(it.text()))
lwFrom.takeItem(idx)
lwTo.sortItems()
def selectAllListItems(self, listwidget, select = True):
"""Selects or deselects all items of a QListWidget (select = False to deselect)"""
for idx in range(listwidget.count()):
listwidget.setItemSelected(listwidget.item(idx), select)
def mniParcelsGeneration(self):
#patient selected
PatsSelected = self.getSelectedPatientsNames()
subject_to_perform = []
subjects_ok = []
for ind_pat in PatsSelected:
#first we look if it's not already done
if 'rdi_elec_label' in self.subjects[ind_pat].keys():
if self.subjects[ind_pat]['rdi_elec_label'] is not None:
fin = open(str(self.subjects[ind_pat]['rdi_elec_label']),'r')
info_label_elec = json.loads(fin.read())
fin.close()
if 'plots_label' in info_label_elec.keys():
if "BroadmannDilate" in info_label_elec['plots_label'][info_label_elec['plots_label'].keys()[0]].keys():
subjects_ok.append(ind_pat)
print("BrodmannDilate already estimated for this patient %s"%ind_pat)
else:
subject_to_perform.append(ind_pat)
else:
subject_to_perform.append(ind_pat)
else:
subject_to_perform.append(ind_pat)
else:
subject_to_perform.append(ind_pat)
#if not we look if MNI position have been estimated already
subject_possible_to_perform = {}
subject_not_performable = []
for ind_pat in subject_to_perform:
if 'implantation' in self.subjects[ind_pat].keys():
fin = open(str(self.subjects[ind_pat]['implantation']),'r')
try:
info_implant = json.loads(fin.read())
except:
fin.close()
fin = open(str(self.subjects[ind_pat]['implantation']),'r')
info_implant = pickle.load(fin)
fin.close()
if 'plotsMNI' in info_implant.keys():
subject_possible_to_perform.update({ind_pat:info_implant['plotsMNI']})
else:
print("MNI estimation has to be done in locateElectrodes for %s, to sensitive to duplicate these functions at differente place"%ind_pat)
subject_not_performable.append(ind_pat)
elif 'implantationMNI' in self.subjects[ind_pat].keys():
#pdb.set_trace()
MNI_pos = numpy.loadtxt(str(self.subjects[ind_pat]['implantationMNI'][0]))
a=open(str(self.subjects[ind_pat]['implantationMNI'][1]),"rU")
MNI_name=a.read().splitlines()
a.close()
info = [[MNI_name[x],MNI_pos[x].tolist()] for x in range(len(MNI_name))]
subject_possible_to_perform.update({ind_pat:info})
#MNI_name =
#subject_possible_to_perform.update({ind_pat:info_implant['plotsMNI']})
elif 'implantationCSV' in self.subjects[ind_pat].keys():
# pdb.set_trace()
print("refaire %s"%str(ind_pat))
#subject_possible_to_perform.update({ind_pat:info_implant['plotsMNI']})
else:
subject_not_performable.append(ind_pat)
matrix_MNI_Nativ = numpy.matrix([[ -1., 0., 0., 90.],[0., -1., 0., 91.],[0., 0., -1., 109.],[0., 0., 0., 1.]])
#la blague maintenant c'est d'ouvrir un csv et de rajouter des infos mni sans perdre les infos "patients specifique"
for ind_pat in subject_possible_to_perform.keys():
print(ind_pat)
plotMNI_sorted = sorted(subject_possible_to_perform[ind_pat], key=lambda plot_number: plot_number[0])
plotMNI_Dict = dict(plotMNI_sorted)
#make bipolar montage
plot_dict_MNI_Native = {}
for vv,kk in plotMNI_Dict.iteritems():
inter_pos = [kk[0], kk[1], kk[2], 1]
inter_pos = numpy.matrix(inter_pos).reshape([4,1])
result_pos = numpy.dot(matrix_MNI_Nativ,inter_pos)
plot_dict_MNI_Native.update({vv:[result_pos.tolist()[0][0],result_pos.tolist()[1][0],result_pos.tolist()[2][0]]})
info_plotMNI_bipolaire= []
for pindex in range(1,len(plotMNI_sorted)):
previous_contact = "".join([i for i in plotMNI_sorted[pindex-1][0] if not i.isdigit()])
current_contact = "".join([i for i in plotMNI_sorted[pindex][0] if not i.isdigit()])
if previous_contact == current_contact:
info_plotMNI_bipolaire.append((plotMNI_sorted[pindex][0]+' - '+ plotMNI_sorted[pindex-1][0],(numpy.array(plot_dict_MNI_Native[plotMNI_sorted[pindex][0]])+numpy.array(plot_dict_MNI_Native[plotMNI_sorted[pindex-1][0]]))/2 ))
info_plotMNI_bipolaireSB= {}
for pindex in range(1,len(plotMNI_sorted)):
previous_contact = "".join([i for i in plotMNI_sorted[pindex-1][0] if not i.isdigit()])
current_contact = "".join([i for i in plotMNI_sorted[pindex][0] if not i.isdigit()])
if previous_contact == current_contact:
info_plotMNI_bipolaireSB.update({plotMNI_sorted[pindex][0]+' - '+ plotMNI_sorted[pindex-1][0]:(numpy.array(plotMNI_Dict[plotMNI_sorted[pindex][0]])+numpy.array(plotMNI_Dict[plotMNI_sorted[pindex-1][0]]))/2 })
info_plotMNI_bipolaire_Dict =dict(info_plotMNI_bipolaire)
#open MNI filtersAtlases
#pdb.set_trace()
Hammers_parcels_names = readSulcusLabelTranslationFile('parcels_label_name_Hammers.txt')
AAL_parcels_names = readSulcusLabelTranslationFile('parcels_label_name_AAL.txt')
AALDilate_parcels_names = readSulcusLabelTranslationFile('parcels_label_name_AALDilate.txt')
vol_AAL = aims.read('MNI_Atlases/rAALSEEG12.nii')
vol_AALDilate = aims.read('MNI_Atlases/rAALSEEG12Dilate.nii')
vol_BroadmannDilate = aims.read('MNI_Atlases/rBrodmannSEEG3spm12.nii')
vol_Broadmann = aims.read('MNI_Atlases/rbrodmann.nii')
vol_Hammers = aims.read('MNI_Atlases/rHammersSEEG12.nii')
sphere_size = 3 #en mm
nb_voxel_sphere_MNI = [sphere_size, sphere_size, sphere_size]
#c'est là que ça va pas ...
#il faut que je recharge celui du patient
plots_label = {}
if 'rdi_elec_label' in self.subjects[ind_pat].keys():
if self.subjects[ind_pat]['rdi_elec_label'] is not None:
fin = open(str(self.subjects[ind_pat]['rdi_elec_label']),'r')
info_label_elec = json.loads(fin.read())
fin.close()
plots_label = info_label_elec['plots_label']
else:
plots_label = {}
else:
plots_label = {}
for pindex in range(len(plotMNI_sorted)):
plot_pos_pix_MNI = [round(plot_dict_MNI_Native[plotMNI_sorted[pindex][0]][i]) for i in range(3)]
##MNI Atlases
##AAL
voxel_within_sphere_AAL = [round(vol_AAL.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepAAL = [x for x in voxel_within_sphere_AAL if x != 0 and not math.isnan(x)]
##AALDilate
voxel_within_sphere_AALdilate = [round(vol_AALDilate.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepAALDilate = [x for x in voxel_within_sphere_AALdilate if x != 0 and not math.isnan(x)]
##Broadmann
voxel_within_sphere_Broadmann = [round(vol_Broadmann.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepBroadmann = [x for x in voxel_within_sphere_Broadmann if x != 0 and not math.isnan(x)]
#Brodmann dilate
voxel_within_sphere_Broadmanndilate = [round(vol_BroadmannDilate.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepBroadmannDilate = [x for x in voxel_within_sphere_Broadmanndilate if x != 0 and not math.isnan(x)]
##Hammers
voxel_within_sphere_Hammers = [round(vol_Hammers.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepHammers = [x for x in voxel_within_sphere_Hammers if x != 0 and not math.isnan(x)]
##prendre le label qui revient le plus en dehors de zero (au cas où il y en ait plusieurs)
from collections import Counter
if not voxel_to_keepAAL:
label_AAL_name = "not in a AAL parcel"
label_AAL = round(vol_AAL.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepAAL).most_common(1)[0]
label_AAL = most_common
label_AAL_name = AAL_parcels_names[label_AAL]
if not voxel_to_keepAALDilate:
label_AALDilate_name = "not in a AALDilate parcel"
label_AALDilate = round(vol_AALDilate.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepAALDilate).most_common(1)[0]
label_AALDilate = most_common
label_AALDilate_name = AALDilate_parcels_names[label_AALDilate]
if not voxel_to_keepBroadmann:
label_Broadmann_name = "not in a Broadmann parcel"
label_Broadmann = round(vol_Broadmann.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepBroadmann).most_common(1)[0]
label_Broadmann = most_common
#label_Broadmann_name = unicode(label_Broadmann)
if plot_pos_pix_MNI[0]>90:
label_Broadmann_name = unicode(label_Broadmann+100)
else:
label_Broadmann_name = unicode(label_Broadmann)
if not voxel_to_keepBroadmannDilate:
label_BroadmannDilate_name = "not in a Broadmann parcel"
label_BroadmannDilate = round(vol_BroadmannDilate.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepBroadmannDilate).most_common(1)[0]
label_BroadmannDilate = most_common
#label_Broadmann_name = unicode(label_Broadmann)
if plot_pos_pix_MNI[0]>90:
label_BroadmannDilate_name = unicode(label_BroadmannDilate+100)
else:
label_BroadmannDilate_name = unicode(label_BroadmannDilate-48)
if not voxel_to_keepHammers:
label_Hammers_name = "not in a Hammers parcel"
label_Hammers = round(vol_Hammers.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepHammers).most_common(1)[0]
label_Hammers = most_common
label_Hammers_name = Hammers_parcels_names[label_Hammers]
#la j'ecrase les infos marsatlas etc ... sans raison
#plots_label[plotMNI_sorted[pindex][0]]={'MarsAtlas':(0,'not calculated'),'Freesurfer':(0,'not calculated'),'Hippocampal Subfield':(0,'not calculated'),'GreyWhite':(0,'not calculated'),'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'Hammers':(label_Hammers,label_Hammers_name),'Resection':(0,'not calculated')}
try:
plots_label[plotMNI_sorted[pindex][0]].update({'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'BroadmannDilate':(label_BroadmannDilate,label_BroadmannDilate_name),'Hammers':(label_Hammers,label_Hammers_name)})
except:
plots_label[plotMNI_sorted[pindex][0]]={'MarsAtlas':(0,'not calculated'),'Freesurfer':(0,'not calculated'),'Hippocampal Subfield':(0,'not calculated'),'GreyWhite':(0,'not calculated'),'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'BroadmannDilate':(label_BroadmannDilate,label_BroadmannDilate_name),'Hammers':(label_Hammers,label_Hammers_name),'Resection':(0,'not calculated')}
#je remets l'ancienne ligne ?
plot_name = [x[0] for x in plotMNI_sorted]
plots_by_label_BM = dict([(Lab,[p for p in plot_name if plots_label[p]['Broadmann'][1]==Lab]) for Lab in [unicode("%1.1f"%x) for x in range(0,100)]])
plots_by_label_HM = dict([(Lab,[p for p in plot_name if plots_label[p]['Hammers'][1]==Lab]) for Lab in Hammers_parcels_names.values()])
plots_by_label_AAL = dict([(Lab,[p for p in plot_name if plots_label[p]['AAL'][1]==Lab]) for Lab in AAL_parcels_names.values()])
plots_by_label_AALDilate = dict([(Lab,[p for p in plot_name if plots_label[p]['AALDilate'][1]==Lab]) for Lab in AALDilate_parcels_names.values()])
sphere_size_bipole = 5
nb_voxel_sphere_MNI = [sphere_size, sphere_size, sphere_size]
plots_label_bipolar = {}
if 'rdi_elec_label' in self.subjects[ind_pat].keys():
if self.subjects[ind_pat]['rdi_elec_label'] is not None:
fin = open(str(self.subjects[ind_pat]['rdi_elec_label']),'r')
info_label_elec = json.loads(fin.read())
fin.close()
plots_label_bipolar = info_label_elec['plots_label_bipolar']
else:
pass #pdb.set_trace()
else:
pass #pdb.set_trace()
for pindex in range(len(info_plotMNI_bipolaire)):
plot_pos_pix_MNI = [round(info_plotMNI_bipolaire[pindex][1][i]) for i in range(3)]
##MNI Atlases
##AAL
voxel_within_sphere_AAL = [round(vol_AAL.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepAAL = [x for x in voxel_within_sphere_AAL if x != 0 and not math.isnan(x)]
##AALDilate
voxel_within_sphere_AALdilate = [round(vol_AALDilate.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepAALDilate = [x for x in voxel_within_sphere_AALdilate if x != 0 and not math.isnan(x)]
##Broadmann
voxel_within_sphere_Broadmann = [round(vol_Broadmann.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepBroadmann = [x for x in voxel_within_sphere_Broadmann if x != 0 and not math.isnan(x)]
#Brodmann dilate
voxel_within_sphere_Broadmanndilate = [round(vol_BroadmannDilate.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepBroadmannDilate = [x for x in voxel_within_sphere_Broadmanndilate if x != 0 and not math.isnan(x)]
##Hammers
voxel_within_sphere_Hammers = [round(vol_Hammers.value(plot_pos_pix_MNI[0]+vox_i,plot_pos_pix_MNI[1]+vox_j,plot_pos_pix_MNI[2]+vox_k)) for vox_k in range(-nb_voxel_sphere_MNI[2],nb_voxel_sphere_MNI[2]+1) for vox_j in range(-nb_voxel_sphere_MNI[1],nb_voxel_sphere_MNI[1]+1) for vox_i in range(-nb_voxel_sphere_MNI[0],nb_voxel_sphere_MNI[0]+1) if math.sqrt(vox_i**2+vox_j**2+vox_k**2) < sphere_size]
voxel_to_keepHammers = [x for x in voxel_within_sphere_Hammers if x != 0 and not math.isnan(x)]
##prendre le label qui revient le plus en dehors de zero (au cas où il y en ait plusieurs)
from collections import Counter
if not voxel_to_keepAAL:
label_AAL_name = "not in a AAL parcel"
label_AAL = round(vol_AAL.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepAAL).most_common(1)[0]
label_AAL = most_common
label_AAL_name = AAL_parcels_names[label_AAL]
if not voxel_to_keepAALDilate:
label_AALDilate_name = "not in a AALDilate parcel"
label_AALDilate = round(vol_AALDilate.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepAALDilate).most_common(1)[0]
label_AALDilate = most_common
label_AALDilate_name = AALDilate_parcels_names[label_AALDilate]
if not voxel_to_keepBroadmann:
label_Broadmann_name = "not in a Broadmann parcel"
label_Broadmann = round(vol_Broadmann.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepBroadmann).most_common(1)[0]
label_Broadmann = most_common
#label_Broadmann_name = unicode(label_Broadmann)
if plot_pos_pix_MNI[0]>90:
label_Broadmann_name = unicode(label_Broadmann+100)
else:
label_Broadmann_name = unicode(label_Broadmann)
if not voxel_to_keepBroadmannDilate:
label_BroadmannDilate_name = "not in a Broadmann parcel"
label_BroadmannDilate = round(vol_BroadmannDilate.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepBroadmannDilate).most_common(1)[0]
label_BroadmannDilate = most_common
#label_Broadmann_name = unicode(label_Broadmann)
if plot_pos_pix_MNI[0]>90:
label_BroadmannDilate_name = unicode(label_BroadmannDilate+100)
else:
label_BroadmannDilate_name = unicode(label_BroadmannDilate-48)
if not voxel_to_keepHammers:
label_Hammers_name = "not in a Hammers parcel"
label_Hammers = round(vol_Hammers.value(plot_pos_pix_MNI[0],plot_pos_pix_MNI[1],plot_pos_pix_MNI[2]))
else:
most_common,num_most_common = Counter(voxel_to_keepHammers).most_common(1)[0]
label_Hammers = most_common
label_Hammers_name = Hammers_parcels_names[label_Hammers]
#plots_label_bipolar[info_plotMNI_bipolaire[pindex][0]]={'MarsAtlas':(0,'not calculated'),'Freesurfer':(0,'not calculated'),'Hippocampal Subfield':(0,'not calculated'),'GreyWhite':(0,'not calculated'),'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'Hammers':(label_Hammers,label_Hammers_name),'Resection':(0,'not calculated')}
try:
plots_label_bipolar[info_plotMNI_bipolaire[pindex][0]].update({'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'BroadmannDilate':(label_BroadmannDilate,label_BroadmannDilate_name),'Hammers':(label_Hammers,label_Hammers_name)})
except:
plots_label_bipolar[info_plotMNI_bipolaire[pindex][0]]={'MarsAtlas':(0,'not calculated'),'Freesurfer':(0,'not calculated'),'Hippocampal Subfield':(0,'not calculated'),'GreyWhite':(0,'not calculated'),'AAL':(label_AAL,label_AAL_name),'AALDilate':(label_AALDilate,label_AALDilate_name),'Broadmann':(label_Broadmann,label_Broadmann_name),'BroadmannDilate':(label_BroadmannDilate,label_BroadmannDilate_name),'Hammers':(label_Hammers,label_Hammers_name),'Resection':(0,'not calculated')}
plot_name_bip = [x[0] for x in info_plotMNI_bipolaire]
plots_bipolar_by_label_BM = dict([(Lab,[p for p in plot_name_bip if plots_label_bipolar[p]['Broadmann'][1]==Lab]) for Lab in [unicode("%1.1f"%x) for x in range(0,100)]])
plots_bipolar_by_label_HM = dict([(Lab,[p for p in plot_name_bip if plots_label_bipolar[p]['Hammers'][1]==Lab]) for Lab in Hammers_parcels_names.values()])
plots_bipolar_by_label_AAL = dict([(Lab,[p for p in plot_name_bip if plots_label_bipolar[p]['AAL'][1]==Lab]) for Lab in AAL_parcels_names.values()])
plots_bipolar_by_label_AALDilate = dict([(Lab,[p for p in plot_name_bip if plots_label_bipolar[p]['AALDilate'][1]==Lab]) for Lab in AALDilate_parcels_names.values()])
wdi_csv = ReadDiskItem('Final Export Dictionaries','CSV file',requiredAttributes={'subject':ind_pat})
rdi_csv = list(wdi_csv.findValues({},None,False))
if len(rdi_csv)>0:
#print c'est la galère
info_previous_csv = readElecLocalCSVFile(infile=str(rdi_csv[0]))
else:
info_previous_csv = None
wdi_csvnew = WriteDiskItem('Final Export Dictionaries','CSV file')
rdi_csvnew= wdi_csvnew.findValue(self.subjects[ind_pat]['rdi'])
with open(str(rdi_csvnew), 'w') as csvfile:
#fieldnames=['MarsAtlas','GreyWhite','Resection']
writer = csv.writer(csvfile, delimiter='\t')
writer.writerow([u'Contacts Positions'])
if info_previous_csv is not None:
info_mniMarsAtlas = info_previous_csv[0]['MarsAtlas']
info_mniFreesurfer = info_previous_csv[0]['Freesurfer']
info_mniHippoFS = info_previous_csv[0]['HippoSubfieldFreesurfer']
else:
info_mniMarsAtlas = 'not performed'
info_mniFreesurfer = 'not performed'
info_mniHippoFS = 'not performed'
writer.writerow([u'Use of MNI Template','MarsAtlas',info_mniMarsAtlas,'Freesurfer',info_mniFreesurfer,'HippoSubfieldFreesurfer',info_mniHippoFS])
#add a row with "MNI or Patient for MarsAtlas and Freesurfer
try:
list_to_write = set(info_previous_csv[1]['monopolar'][info_previous_csv[1]['monopolar'].keys()[0]].keys())
except:
list_to_write = set([])
list_by_default = set([u'contact','MarsAtlas', 'MarsAtlasFull', 'Freesurfer', 'Hippocampal Subfield','GreyWhite','AAL', 'AALDilate', 'Broadmann', 'BroadmannDilate', 'Hammers', 'Resection', 'MNI','T1pre Scanner Based'])
diff_list = list(list_to_write.difference(list_by_default))
full_list = [u'contact','MarsAtlas', 'MarsAtlasFull', 'Freesurfer', 'Hippocampal Subfield','GreyWhite', 'ALL', 'AALDilate', 'Broadmann', 'BroadmannDilate', 'Hammers', 'Resection', 'MNI','T1pre Scanner Based']
full_list.extend(diff_list)
writer.writerow(full_list)
try:
dict_sorted_tmp = OrderedDict(sorted(info_previous_csv[1]['monopolar'].items()))
except:
dict_sorted_tmp = OrderedDict(sorted(plots_label.items()))
new_dict_sorted_tmp = {}
for kk,vv in dict_sorted_tmp.items():
new_dict_sorted_tmp.update({kk:{}})
[new_dict_sorted_tmp[kk].update({jj:ll[1]}) for jj,ll in vv.items()]
dict_sorted_tmp = OrderedDict(sorted(new_dict_sorted_tmp.items()))
try:
for kk,vv in dict_sorted_tmp.iteritems():
listwrite = [kk]
if 'MarsAtlas' in vv.keys():
listwrite.append(vv['MarsAtlas'])
else:
listwrite.append('not performed')
if 'MarsAtlasFull' in vv.keys():
listwrite.append(vv['MarsAtlasFull'])
else:
listwrite.append('not performed')
if 'Freesurfer' in vv.keys():
listwrite.append(vv['Freesurfer'])
else:
listwrite.append('not performed')
if 'Hippocampal Subfield' in vv.keys():
listwrite.append(vv['Hippocampal Subfield'])
else:
listwrite.append('not performed')
if 'GreyWhite' in vv.keys():
listwrite.append(vv['GreyWhite'])
else:
listwrite.append('not performed')
if 'AAL' in vv.keys():
listwrite.append(vv['AAL'])
else:
listwrite.append(plots_label[kk]['AAL'][1])
if 'AALDilate' in vv.keys():
listwrite.append(vv['AALDilate'])
else:
listwrite.append(plots_label[kk]['AALDilate'][1])
#if 'Broadmann' in vv.keys():
# listwrite.append(vv['Broadmann'])
#else:
listwrite.append(plots_label[kk]['Broadmann'][1])
#if 'BroadmannDilate' in vv.keys():
#listwrite.append(vv['BroadmannDilate'])
#else:
listwrite.append(plots_label[kk]['BroadmannDilate'][1])
if 'Hammers' in vv.keys():
listwrite.append(vv['Hammers'])
else:
listwrite.append(plots_label[kk]['Hammers'][1])
if 'Resection' in vv.keys():
listwrite.append(vv['Resection'])
else:
listwrite.append('not performed')
#[listwrite.append(x[1]) for x in vv.values()]
listwrite.append([float(format(plotMNI_Dict[kk][i],'.3f')) for i in range(3)])
listwrite.append('not performed, csv generated from groupDisplay')
if len(full_list)>12:
for i_supp in range(len(full_list)-14):
listwrite.append(vv[full_list[14+i_supp]])
writer.writerow(listwrite)
except:
pdb.set_trace()
writer.writerow([])
writer.writerow([])
try:
dict_sorted_tmp = OrderedDict(sorted(info_previous_csv[1]['bipolar'].items()))
except:
dict_sorted_tmp = OrderedDict(sorted(plots_label_bipolar.items()))
new_dict_sorted_tmp = {}
for kk,vv in dict_sorted_tmp.items():
new_dict_sorted_tmp.update({kk:{}})
[new_dict_sorted_tmp[kk].update({jj:ll[1]}) for jj,ll in vv.items()]
dict_sorted_tmp = OrderedDict(sorted(new_dict_sorted_tmp.items()))