-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEMPRISE.py
More file actions
2841 lines (2418 loc) · 121 KB
/
EMPRISE.py
File metadata and controls
2841 lines (2418 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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
"""
EMPRISE - EMergence of PRecISE numerosity representations in the human brain
Joram Soch, MPI Leipzig <soch@cbs.mpg.de>
2023-06-26, 15:31: get_onsets
2023-06-26, 16:39: get_confounds
2023-06-26, 18:03: get_mask_nii, get_bold_nii, get_events_tsv, get_confounds_tsv
2023-06-29, 11:21: load_mask, load_data
2023-06-29, 12:18: onsets_trials2blocks
2023-07-03, 09:56: load_data_all, average_signals, correct_onsets
2023-07-13, 10:11: average_signals
2023-08-10, 13:59: global variables
2023-08-21, 15:42: rewriting to OOP
2023-08-24, 16:36: standardize_confounds
2023-09-07, 16:20: standardize_signals
2023-09-12, 19:23: get_bold_gii, load_surf_data, load_surf_data_all
2023-09-14, 12:46: save_vol, save_surf
2023-09-21, 15:11: plot_surf
2023-09-26, 16:19: analyze_numerosity
2023-09-28, 11:18: threshold_maps
2023-09-28, 12:58: visualize_maps
2023-10-05, 19:12: rewriting to OOP
2023-10-05, 19:34: global variables
2023-10-05, 21:24: rewriting for MPI
2023-10-12, 12:02: threshold_maps, visualize_maps
2023-10-16, 10:56: threshold_maps, testing
2023-10-16, 14:41: load_data_all, load_surf_data_all, get_onsets, get_confounds
2023-10-26, 17:56: get_model_dir, get_results_file, load_mask_data, calc_runs_scans
2023-10-26, 21:06: get_mesh_files, get_sulc_files
2023-11-01, 14:50: get_sulc_files
2023-11-01, 17:53: threshold_and_cluster
2023-11-09, 11:30: refactoring
2023-11-17, 10:34: refactoring
2023-11-20, 12:56: get_mesh_files
2023-11-20, 16:02: threshold_and_cluster
2023-11-23, 12:57: analyze_numerosity
2023-11-28, 14:05: create_fsaverage_midthick, get_mesh_files
2023-11-30, 19:31: threshold_AFNI_cluster
2024-01-22, 17:08: run_GLM_analysis
2024-01-29, 11:50: run_GLM_analysis
2024-01-29, 15:49: threshold_SPM
2024-03-11, 11:08: get_onsets
2024-03-11, 15:34: run_GLM_analysis_group
2024-03-11, 16:44: threshold_SPM_group
2024-03-11, 17:37: refactoring
2024-04-04, 10:22: get_onsets, get_confounds
2024-05-14, 15:03: analyze_numerosity
2024-05-21, 10:35: calculate_Rsq
2024-06-25, 15:18: threshold_maps, threshold_and_cluster
2024-06-27, 13:39: threshold_maps, threshold_and_cluster
2024-07-01, 18:11: analyze_numerosity
"""
# import packages
#-----------------------------------------------------------------------------#
import os
import glob
import time
import numpy as np
import scipy as sp
import pandas as pd
import nibabel as nib
from nilearn import surface
from surfplot import Plot
import NumpRF
# determine location
#-----------------------------------------------------------------------------#
at_MPI = os.getcwd().startswith('/data/')
# define directories
#-----------------------------------------------------------------------------#
if at_MPI:
stud_dir = r'/data/pt_02495/emprise7t/'
data_dir = stud_dir
deri_out = r'/data/pt_02495/emprise7t/derivatives/'
else:
stud_dir = r'C:/Joram/projects/MPI/EMPRISE/'
data_dir = stud_dir + 'data/'
deri_out = data_dir + 'derivatives/'
deri_dir = data_dir + 'derivatives/'
tool_dir = os.getcwd() + '/'
# define identifiers
#-----------------------------------------------------------------------------#
sub = '001' # pilot subject
ses = 'visual' # pilot session
sess =['visual', 'audio', 'digits', 'spoken', 'congruent', 'incongruent']
task = 'harvey'
acq =['mprageised', 'fMRI1p75TE24TR2100iPAT3FS']
runs =[1,2,3,4,5,6,7,8]
spaces=['fsnative', 'fsaverage']
meshs =['inflated', 'pial', 'white', 'midthickness']
desc =['brain', 'preproc', 'confounds']
# define subject groups
#-----------------------------------------------------------------------------#
adults = ['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012']
childs = ['101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '116']
# specify scanning parameters
#-----------------------------------------------------------------------------#
TR = 2.1 # fMRI repetition time
mtr = 41 # microtime resolution (= bins per TR)
mto = 21 # microtime onset (= reference slice)
n = 145 # number of scans per run
b = 4*2*6 # number of blocks per run
num_epochs = 4 # number of epochs within run
num_scan_disc = 1 # number of scans to discard before first epoch
scans_per_epoch = int((n-num_scan_disc)/num_epochs)
blocks_per_epoch = int(b/num_epochs)
# specify thresholding parameters
#-----------------------------------------------------------------------------#
dAIC_thr = 0 # AIC diff must be larger than this
dBIC_thr = 0 # BIC diff must be larger than this
Rsq_def = 0.3 # R-squared must be larger than this
alpha_def = 0.05 # p-value must be smaller than this
mu_thr =[1, 5] # numerosity must be inside this range
fwhm_thr =[0, 24] # tuning width must be inside this range
beta_thr =[0, np.inf] # scaling parameter must be inside this range
crit_def = 'Rsqmb' # default thresholding option (see "threshold_maps")
# specify default covariates
#-----------------------------------------------------------------------------#
covs = ['trans_x', 'trans_y', 'trans_z', 'rot_x', 'rot_y', 'rot_z', \
'white_matter', 'csf', 'global_signal', \
'cosine00', 'cosine01', 'cosine02']
# class: subject/session
#-----------------------------------------------------------------------------#
class Session:
"""
A Session object is initialized by a subject ID and a session ID and then
allows for multiple operations performed on the data from this session.
"""
# function: initialize subject/session
#-------------------------------------------------------------------------#
def __init__(self, subj_id, sess_id):
"""
Initialize a Session from a Subject
sess = EMPRISE.Session(subj_id, sess_id)
subj_id - string; subject identifier (e.g. "001")
sess_id - string; session identifier (e.g. "visual")
sess - a Session object
o sub - the subject ID
o ses - the session ID
"""
# store subject ID and session name
self.sub = subj_id
self.ses = sess_id
# function: get "mask.nii" filenames
#-------------------------------------------------------------------------#
def get_mask_nii(self, run_no, space):
"""
Get Filename for Brain Mask NIfTI File
filename = sess.get_mask_nii(run_no, space)
run_no - int; run number (e.g. 1)
space - string; image space (e.g. "T1w")
filename - string; filename of "mask.nii.gz"
filename = sess.get_mask_nii(run_no, space) returns the filename of the
gzipped brain mask belonging to session sess, run run_no and in the
selected image space.
"""
# create filename
filename = deri_dir + 'fmriprep' + \
'/sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_space-' + space + '_desc-' + desc[0] + '_mask.nii.gz'
return filename
# function: get "bold.nii" filenames
#-------------------------------------------------------------------------#
def get_bold_nii(self, run_no, space=''):
"""
Get Filename for BOLD NIfTI Files
filename = sess.get_bold_nii(run_no, space)
run_no - int; run number (e.g. 1)
space - string; image space (e.g. "T1w")
filename - string; filename of "bold.nii.gz"
filename = sess.get_bold_nii(run_no, space) returns the filename of the
gzipped 4D NIfTI belonging to session sess and run run_no. If space is
non-empty, then the preprocessed images from the selected image space
will be returned. By default, space is empty.
"""
# create filename
if not space: # raw images in native space
filename = data_dir + 'sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_bold.nii.gz'
else: # preprocessed images in space
filename = deri_dir + 'fmriprep' + '/sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_space-' + space + '_desc-' + desc[1] + '_bold.nii.gz'
return filename
# function: get "bold.gii" filenames
#-------------------------------------------------------------------------#
def get_bold_gii(self, run_no, hemi='L', space='fsnative'):
"""
Get Filename for BOLD GIfTI Files
filename = sess.get_bold_gii(run_no, hemi, space)
run_no - int; run number (e.g. 1)
hemi - string; brain hemisphere (e.g. "L")
space - string; image space (e.g. "fsnative")
filename - string; filename of "bold.func.gii"
filename = sess.get_bold_gii(run_no, hemi, space) returns the filename
of the 4D GIfTI belonging to session sess, run run_no and brain hemi-
sphere hemi.
"""
# create filename
filename = deri_dir + 'fmriprep' + \
'/sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_hemi-' + hemi + '_space-' + space + '_bold.func.gii'
return filename
# function: get "events.tsv" filenames
#-------------------------------------------------------------------------#
def get_events_tsv(self, run_no):
"""
Get Filename for Events TSV File
filename = sess.get_events_tsv(run_no)
run_no - int; run number (e.g. 1)
filename - string; filename of "events.tsv"
filename = sess.get_events_tsv(run_no) returns the filename of the
tab-separated events file belonging to session sess and run run_no.
"""
# create filename
filename = data_dir + 'sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_events.tsv'
return filename
# function: get "timeseries.tsv" filenames
#-------------------------------------------------------------------------#
def get_confounds_tsv(self, run_no):
"""
Get Filename for Confounds TSV File
filename = sess.get_confounds_tsv(run_no)
run_no - int; run number (e.g. 1)
filename - string; filename of "timeseries.tsv"
filename = get_confounds_tsv(run_no) returns the filename of the
tab-separated confounds file belonging to session sess and run run_no.
"""
# create filename
filename = deri_dir + 'fmriprep' + \
'/sub-' + self.sub + '/ses-' + self.ses + '/func' + \
'/sub-' + self.sub + '_ses-' + self.ses + '_task-' + task + \
'_acq-' + acq[1] + '_run-' + str(run_no) + '_desc-' + desc[2] + '_timeseries.tsv'
return filename
# function: get mesh files
#-------------------------------------------------------------------------#
def get_mesh_files(self, space='fsnative', surface='inflated'):
"""
Get Filenames for GIfTI Inflated Mesh Files
mesh_files = sess.get_mesh_files(space, surface)
space - string; image space (e.g. "fsnative")
surface - string; surface image (e.g. "inflated")
mesh_files - dict; filenames of inflated mesh files
o left - string; left hemisphere mesh file
o right - string; left hemisphere mesh file
mesh_files = sess.get_mesh_files(space, surface) returns filenames for
mesh files from specified image space and cortical surface to be used
for surface plotting.
"""
# if native image space
if space == 'fsnative':
# specify mesh files
prep_dir = deri_dir + 'fmriprep'
mesh_path = prep_dir + '/sub-' + self.sub + '/anat' + \
'/sub-' + self.sub + '*' + '_hemi-'
mesh_file = mesh_path + 'L' + '_' + surface + '.surf.gii'
if not glob.glob(mesh_file):
for ses in sess:
mesh_path = prep_dir + '/sub-' + self.sub + '/ses-' + ses + '/anat' + \
'/sub-' + self.sub + '_ses-' + ses + '*' + '_hemi-'
mesh_file = mesh_path + 'L' + '_' + surface + '.surf.gii'
if glob.glob(mesh_file):
break
if not glob.glob(mesh_file):
mesh_files = {'left' : 'n/a', \
'right': 'n/a'}
else:
mesh_files = {'left' : glob.glob(mesh_path+'L'+'_'+surface +'.surf.gii')[0], \
'right': glob.glob(mesh_path+'R'+'_'+surface +'.surf.gii')[0]}
# if average image space
elif space == 'fsaverage':
# specify mesh dictionary
mesh_dict = {'inflated': 'infl', \
'pial': 'pial', \
'white': 'white', \
'midthickness': 'midthick'}
# specify mesh files
if surface not in mesh_dict.keys():
mesh_files = {'left' : 'n/a', \
'right': 'n/a'}
else:
free_dir = deri_out + 'freesurfer'
mesh_path = free_dir + '/fsaverage/' + mesh_dict[surface]
mesh_files = {'left' : mesh_path + '_left.gii', \
'right': mesh_path + '_right.gii'}
# return mesh files
return mesh_files
# function: get sulci files
#-------------------------------------------------------------------------#
def get_sulc_files(self, space='fsnative'):
"""
Get Filenames for FreeSurfer Sulci Files
sulc_files = sess.get_sulc_files(space)
space - string; image space (e.g. "fsnative")
sulc_files - dict; filenames of FreeSurfer sulci files
o left - string; left hemisphere sulci file
o right - string; left hemisphere sulci file
sulc_files = sess.get_sulc_files(space) returns filenames for FreeSurfer
sulci files from specified image space to be used for surface plotting.
"""
# if native image space
if space == 'fsnative':
# specify sulci files
free_dir = deri_dir + 'freesurfer'
sulc_path = free_dir + '/sub-' + self.sub + '/surf'
sulc_files = {'left' : sulc_path + '/lh.sulc', \
'right': sulc_path + '/rh.sulc'}
# if average image space
elif space == 'fsaverage':
# specify mesh files
free_dir = deri_out + 'freesurfer'
sulc_path = free_dir + '/fsaverage/sulc'
sulc_files = {'left' : sulc_path + '_left.gii', \
'right': sulc_path + '_right.gii'}
# return mesh files
return sulc_files
# function: load brain mask
#-------------------------------------------------------------------------#
def load_mask(self, run_no, space=''):
"""
Load Brain Mask NIfTI File
M = sess.load_mask(run_no, space)
run_no - int; run number (e.g. 1)
space - string; image space (e.g. "T1w")
M - 1 x V vector; values of the mask image
"""
# load image file
filename = self.get_mask_nii(run_no, space)
mask_nii = nib.load(filename)
# extract mask image
M = mask_nii.get_fdata()
M = M.reshape((np.prod(M.shape),), order='C')
return M
# function: load fMRI data
#-------------------------------------------------------------------------#
def load_data(self, run_no, space=''):
"""
Load Functional MRI NIfTI Files
Y = sess.load_data(run_no, space)
run_no - int; run number (e.g. 1)
space - string; image space (e.g. "T1w")
Y - n x V matrix; scan-by-voxel fMRI data
"""
# load image file
filename = self.get_bold_nii(run_no, space)
bold_nii = nib.load(filename)
# extract fMRI data
Y = bold_nii.get_fdata()
Y = Y.reshape((np.prod(Y.shape[0:-1]), Y.shape[-1]), order='C')
Y = Y.T
return Y
# function: load fMRI data (all runs)
#-------------------------------------------------------------------------#
def load_data_all(self, space=''):
"""
Load Functional MRI NIfTI Files from All Runs
Y = sess.load_data_all(space)
space - string; image space (e.g. "T1w")
Y - n x V x r array; scan-by-voxel-by-run fMRI data
"""
# prepare 3D array
for j, run in enumerate(runs):
filename = self.get_bold_nii(run, space)
if os.path.isfile(filename):
Y = self.load_data(run, space)
break
Y = np.zeros((Y.shape[0], Y.shape[1], len(runs)))
# load fMRI data
for j, run in enumerate(runs):
filename = self.get_bold_nii(run, space)
if os.path.isfile(filename):
Y[:,:,j] = self.load_data(run, space)
# select available runs
Y = Y[:,:,np.any(Y, axis=(0,1))]
return Y
# function: load surface fMRI data
#-------------------------------------------------------------------------#
def load_surf_data(self, run_no, hemi='L', space='fsnative'):
"""
Load Functional MRI GIfTI Files
Y = sess.load_surf_data(run_no, hemi, space)
run_no - int; run number (e.g. 1)
hemi - string; brain hemisphere (e.g. "L")
space - string; image space (e.g. "fsnative")
Y - n x V matrix; scan-by-vertex fMRI data
"""
# load image file
filename = self.get_bold_gii(run_no, hemi, space)
bold_gii = nib.load(filename)
# extract fMRI data
Y = np.array([y.data for y in bold_gii.darrays])
return Y
# function: load surface fMRI data (all runs)
#-------------------------------------------------------------------------#
def load_surf_data_all(self, hemi='L', space='fsnative'):
"""
Load Functional MRI GIfTI Files from All Runs
Y = sess.load_surf_data_all(hemi, space)
hemi - string; brain hemisphere (e.g. "L")
space - string; image space (e.g. "fsnative")
Y - n x V x r array; scan-by-vertex-by-run fMRI data
"""
# prepare 3D array
for j, run in enumerate(runs):
filename = self.get_bold_gii(run, hemi, space)
if os.path.isfile(filename):
Y = self.load_surf_data(run, hemi, space)
break
Y = np.zeros((Y.shape[0], Y.shape[1], len(runs)))
# load fMRI data
for j, run in enumerate(runs):
filename = self.get_bold_gii(run, hemi, space)
if os.path.isfile(filename):
Y[:,:,j] = self.load_surf_data(run, hemi, space)
# select available runs
Y = Y[:,:,np.any(Y, axis=(0,1))]
return Y
# function: get onsets and durations
#-------------------------------------------------------------------------#
def get_onsets(self, filenames=None):
"""
Get Onsets and Durations for Single Subject and Session, all Runs
ons, dur, stim = sess.get_onsets(filenames)
filenames - list of strings; "events.tsv" filenames
ons - list of arrays of floats; t x 1 vectors of onsets [s]
dur - list of arrays of floats; t x 1 vectors of durations [s]
stim - list of arrays of floats; t x 1 vectors of stimuli (t = trials)
ons, dur, stim = sess.get_onsets(filenames) loads the "events.tsv" file
belonging to session sess and returns lists of length number of runs,
containing, as each element, lists of length number of trials per run,
containing onsets and durations in seconds as well as stimuli in
numerosity.
"""
# prepare onsets, durations, stimuli as empty lists
ons = []
dur = []
stim = []
# prepare labels for trial-wise extraction
if self.ses == 'visual':
stimuli = {'1_dot': 1, '2_dot': 2, '3_dot': 3, '4_dot': 4, '5_dot': 5, '20_dot': 20}
elif self.ses == 'digits':
stimuli = {'1_digit': 1, '2_digit': 2, '3_digit': 3, '4_digit': 4, '5_digit': 5, '20_digit': 20}
elif self.ses == 'audio' or self.ses == 'spoken':
stimuli = {'1_audio': 1, '2_audio': 2, '3_audio': 3, '4_audio': 4, '5_audio': 5, '20_audio': 20}
elif self.ses == 'congruent' or self.ses == 'incongruent':
stimuli = { '2_mixed': 2, '3_mixed': 3, '4_mixed': 4, '5_mixed': 5, '20_mixed': 20}
# for all runs
for j, run in enumerate(runs):
# extract filename
if filenames is None:
filename = self.get_events_tsv(run)
else:
filename = filenames[j]
# if onset file exists
if os.path.isfile(filename):
# extract events of interest
events = pd.read_csv(filename, sep='\t')
events = events[events['trial_type']!='button_press']
for code in stimuli.keys():
events.loc[events['trial_type']==code+'_attn','trial_type'] = code
# save onsets, durations, stimuli
stims = [stimuli[trl] for trl in events['trial_type']]
ons.append(np.array(events['onset']))
dur.append(np.array(events['duration']))
stim.append(np.array(stims))
# return onsets
return ons, dur, stim
# function: get confound variables
#-------------------------------------------------------------------------#
def get_confounds(self, labels, filenames=None):
"""
Get Confound Variables for Single Subject and Session, all Runs
X_c = sess.get_confounds(labels, filenames)
labels - list of strings; confound file header entries
filenames - list of strings; "timeseries.tsv" filenames
X_c - n x c x r array; confound variables
(n = scans, c = variables, r = runs)
X_c = sess.get_confounds(filenames) loads the "timeseries.tsv" file
belonging to session sess and returns a scan-by-variable-by-run array
of those confound variables indexed by the list labels. The function
applies no preprocessing to the confounds.
"""
# prepare confound variables as zero matrix
c = len(labels)
r = len(runs)
X_c = np.zeros((n,c,r))
# for all runs
for j, run in enumerate(runs):
# extract filename
if filenames is None:
filename = self.get_confounds_tsv(run)
else:
filename = filenames[j]
# if confound file exists
if os.path.isfile(filename):
# save confound variables
confounds = pd.read_csv(filename, sep='\t')
for k, label in enumerate(labels):
X_c[:,k,j] = np.array(confounds[label])
# select available runs
X_c = X_c[:,:,np.any(X_c, axis=(0,1))]
# return confounds
return X_c
# class: model/space
#-----------------------------------------------------------------------------#
class Model(Session):
"""
A Model object is initialized by subject/session/space IDs and model name
and allows for multiple operations related to numerosity estimation.
"""
# function: initialize model
#-------------------------------------------------------------------------#
def __init__(self, subj_id, sess_id, mod_name, space_id='fsnative'):
"""
Initialize a Model applied to a Session
mod = EMPRISE.Model(subj_id, sess_id, mod_name, space_id)
subj_id - string; subject identifier (e.g. "001")
sess_id - string; session identifier (e.g. "visual")
mod_name - string; name for the model (e.g. "NumAna")
space_id - string; space identifier (e.g. "fsnative")
mod - a Session object
o sub - the subject ID
o ses - the session ID
o model - the model name
o space - the space ID
"""
# store subject/session/space IDs and model name
super().__init__(subj_id, sess_id) # inherit parent class
self.model = mod_name # configure child object
self.space = space_id
# function: model directory
#-------------------------------------------------------------------------#
def get_model_dir(self):
"""
Get Folder Name for Model
mod_dir = mod.get_model_dir()
mod_dir - string; directory where the model is saved
"""
# create folder name
nprf_dir = deri_out + 'numprf'
mod_dir = nprf_dir + '/sub-' + self.sub + '/ses-' + self.ses + '/model-' + self.model
return mod_dir
# function: results file
#-------------------------------------------------------------------------#
def get_results_file(self, hemi='L', fold='all'):
"""
Get Results Filename for Model
res_file = mod.get_results_file(hemi)
hemi - string; brain hemisphere (e.g. "L")
fold - string; data subset used ("all", "odd" or "even" runs)
res_file - string; results file into which the model is written
"""
# create filename
mod_dir = self.get_model_dir()
filepath = mod_dir + '/sub-' + self.sub + '_ses-' + self.ses + '_model-' + self.model + \
'_hemi-' + hemi + '_space-' + self.space + '_'
if fold in ['odd', 'even']:
filepath = filepath + 'runs-' + fold + '_'
res_file = filepath + 'numprf.mat'
return res_file
# function: calculate runs/scans
#-------------------------------------------------------------------------#
def calc_runs_scans(self, fold='all'):
"""
Calculate Number of Runs and Scans
r0, n0 = mod.calc_runs_scans(fold)
fold - string; data subset used ("all", "odd", "even" runs or "cv")
r0 - int; number of runs analyzed, depending on averaging across runs
n0 - int; number of scans per run, depending on averaging across epochs
"""
# load results file
res_file = self.get_results_file('L')
NpRF = sp.io.loadmat(res_file)
# count number of runs
r0 = 0
for run in runs:
filename = self.get_events_tsv(run)
if os.path.isfile(filename):
if (fold == 'all') or (fold == 'cv') or (fold == 'odd' and run % 2 == 1) or (fold == 'even' and run % 2 == 0):
r0 = r0 + 1
# Explanation: This is the number of available runs. Usually, there
# are 8 runs, but in case of removed data, there can be fewer runs.
# get number of scans
avg = list(NpRF['settings']['avg'][0,0][0,:])
# Explanation: This extracts averaging options from the model settings.
r0 = [r0,1][avg[0]]
# Explanation: If averaging across runs, there is only 1 (effective) run.
n0 = [n,scans_per_epoch][avg[1]]
# Explanation: If averaging across epochs, there are only 36 (effective) scans.
# return runs and scans
return r0, n0
# function: load in-mask data
#-------------------------------------------------------------------------#
def load_mask_data(self, hemi='L'):
"""
Load Functional MRI GIfTI Files and Mask
Y = sess.load_mask_data(hemi, space)
hemi - string; brain hemisphere (e.g. "L")
Y - n x v x r array; scan-by-vertex-by-run fMRI data
"""
# load and mask data
Y = self.load_surf_data_all(hemi, self.space)
M = np.all(Y, axis=(0,2))
Y = Y[:,M,:]
# return data and mask
return Y, M
# function: analyze numerosities
#-------------------------------------------------------------------------#
def analyze_numerosity(self, avg=[True, False], corr='iid', order=1, ver='V2', sh=False):
"""
Estimate Numerosities and FWHMs for Surface-Based Data
results = mod.analyze_numerosity(avg, corr, order, ver, sh)
avg - list of bool; see "NumpRF.estimate_MLE" (default: [True, False])
corr - string; see "NumpRF.estimate_MLE" (default: "iid")
order - int; see "NumpRF.estimate_MLE" (default: 1)
ver - string; version identifier (default: "V2")
sh - bool; split-half estimation (default: False)
results - dict of dicts; results filenames
o L - results for left hemisphere
o R - results for right hemisphere
results = mod.analyze_numerosity(avg, corr, order, ver, sh) loads the
surface-based pre-processed data belonging to model mod, estimates
tuning parameters using settings avg, corr, order, ver, sh and saves
results into a single-subject results directory.
The input parameter "sh" (default: False) specifies whether parameters
are estimated in a split-half sense (if True: separately for odd and
even runs) or across all runs (if False: across all available runs).
The input parameter "ver" (default: "V2") controls which version of
the routine is used (for details, see "NumpRF.estimate_MLE"):
V0: mu_grid = [3, 1]
fwhm_grid = [10.1, 5] (see "NumpRF.estimate_MLE_rgs")
V1: mu_grid = {0.05,...,6, 10,20,...,640,1280} (128)
fwhm_grid = {0.3,...,18, 24,48,96,192} (64)
V2: mu_grid = {0.8,...,5.2, 20} (90)
sig_grid = {0.05,...,3} (60)
V2-lin: mu_grid = {0.8,...,5.2, 20} (90)
sig_grid = {0.05,...,3} (60)
Note: "sig_grid" is calculated into FWHM values, if ver is "V2", and
into linear sigma values, if ver is "V2-lin" (see "NumpRF.estimate_MLE").
Note: "analyze_numerosity" uses the results dictionary keys "L" and "R"
which are identical to the hemisphere labels used by fMRIprep.
"""
# part 1: load subject data
#---------------------------------------------------------------------#
print('\n\n-> Subject "{}", Session "{}":'.format(self.sub, self.ses))
mod_dir = self.get_model_dir()
if not os.path.isdir(mod_dir): os.makedirs(mod_dir)
# load onsets
print(' - Loading onsets ... ', end='')
ons, dur, stim = self.get_onsets()
ons, dur, stim = onsets_trials2blocks(ons, dur, stim, 'closed')
print('successful!')
# load confounds
print(' - Loading confounds ... ', end='')
X_c = self.get_confounds(covs)
X_c = standardize_confounds(X_c)
print('successful!')
# specify grids
if ver == 'V0':
mu_grid = [ 3.0, 1.0]
fwhm_grid = [10.1, 5.0]
elif ver == 'V1':
mu_grid = np.concatenate((np.arange(0.05, 6.05, 0.05), \
10*np.power(2, np.arange(0,8))))
fwhm_grid = np.concatenate((np.arange(0.3, 18.3, 0.3), \
24*np.power(2, np.arange(0,4))))
elif ver == 'V2' or ver == 'V2-lin':
mu_grid = np.concatenate((np.arange(0.80, 5.25, 0.05), \
np.array([20])))
sig_grid = np.arange(0.05, 3.05, 0.05)
else:
err_msg = 'Unknown version ID: "{}". Version must be "V0" or "V1" or "V2"/"V2-lin".'
raise ValueError(err_msg.format(ver))
# specify folds
i = -1 # slice index (3rd dim)
if not sh: folds = {'all': []} # all runs vs. split-half
else: folds = {'odd': [], 'even': []}
for run in runs: # for all possible runs
filename = self.get_events_tsv(run)
if os.path.isfile(filename): # if data from this run exist
i = i + 1 # increase slice index
if not sh:
folds['all'].append(i) # add slice to all runs
else:
if run % 2 == 1: # add slice to odd runs
folds['odd'].append(i)
else: # add slice to even runs
folds['even'].append(i)
# part 2: analyze both hemispheres
#---------------------------------------------------------------------#
hemis = ['L', 'R']
results = {}
for hemi in hemis:
# load data
print('\n-> Hemisphere "{}", Space "{}":'.format(hemi, self.space))
print(' - Loading fMRI data ... ', end='')
Y, M = self.load_mask_data(hemi)
Y = standardize_signals(Y)
V = M.size
print('successful!')
# analyze all folds
results[hemi] = {}
for fold in folds:
# if fold contains runs
num_runs = len(folds[fold])
if num_runs > 0:
# get fold data
print('\n-> Runs "{}" ({} run{}: slice{} {}):'. \
format(fold, num_runs, ['','s'][int(num_runs>1)], \
['','s'][int(num_runs>1)], ','.join([str(i) for i in folds[fold]])))
print(' - Estimating parameters ... ', end='\n')
Y_f = Y[:,:,folds[fold]]
ons_f = [ons[i] for i in folds[fold]]
dur_f = [dur[i] for i in folds[fold]]
stim_f = [stim[i] for i in folds[fold]]
Xc_f = X_c[:,:,folds[fold]]
# analyze data
ds = NumpRF.DataSet(Y_f, ons_f, dur_f, stim_f, TR, Xc_f)
start_time = time.time()
if ver == 'V0':
mu_est, fwhm_est, beta_est, MLL_est, MLL_null, MLL_const, corr_est =\
ds.estimate_MLE_rgs(avg=avg, corr=corr, order=order, mu_grid=mu_grid, fwhm_grid=fwhm_grid)
elif ver == 'V1':
mu_est, fwhm_est, beta_est, MLL_est, MLL_null, MLL_const, corr_est =\
ds.estimate_MLE(avg=avg, corr=corr, order=order, mu_grid=mu_grid, fwhm_grid=fwhm_grid)
elif ver == 'V2':
mu_est, fwhm_est, beta_est, MLL_est, MLL_null, MLL_const, corr_est =\
ds.estimate_MLE(avg=avg, corr=corr, order=order, mu_grid=mu_grid, sig_grid=sig_grid, lin=False)
elif ver == 'V2-lin':
mu_est, fwhm_est, beta_est, MLL_est, MLL_null, MLL_const, corr_est =\
ds.estimate_MLE(avg=avg, corr=corr, order=order, mu_grid=mu_grid, sig_grid=sig_grid, lin=True)
if True:
k_est, k_null, k_const = \
ds.free_parameters(avg, corr, order)
end_time = time.time()
difference = end_time - start_time
del start_time, end_time
# save results (mat-file)
sett = str(avg[0])+','+str(avg[1])+','+str(corr)+','+str(order)
print('\n-> Runs "{}", Model "{}", Settings "{}":'.
format(fold, self.model, sett))
print(' - Saving results file ... ', end='')
filepath = mod_dir + '/sub-' + self.sub + '_ses-' + self.ses + \
'_model-' + self.model + '_hemi-' + hemi + '_space-' + self.space + '_'
if sh: filepath = filepath + 'runs-' + fold + '_'
results[hemi][fold] = filepath + 'numprf.mat'
res_dict = {'mod_dir': mod_dir, 'settings': {'avg': avg, 'corr': corr, 'order': order}, \
'mu_est': mu_est, 'fwhm_est': fwhm_est, 'beta_est': beta_est, \
'MLL_est': MLL_est, 'MLL_null': MLL_null, 'MLL_const': MLL_const, \
'k_est': k_est, 'k_null': k_null, 'k_const': k_const, \
'corr_est':corr_est,'version': ver, 'time': difference}
sp.io.savemat(results[hemi][fold], res_dict)
print('successful!')
del sett, res_dict
# save results (surface images)
para_est = {'mu': mu_est, 'fwhm': fwhm_est, 'beta': beta_est}
for name in para_est.keys():
print(' - Saving "{}" image ... '.format(name), end='')
para_map = np.zeros(V, dtype=np.float32)
para_map[M] = para_est[name]
surface = nib.load(self.get_bold_gii(1,hemi,self.space))
filename = filepath + name + '.surf.gii'
para_img = save_surf(para_map, surface, filename)
print('successful!')
del para_est, para_map, surface, filename, para_img
# return results filename
return results
# function: calculate R-squared maps
#-------------------------------------------------------------------------#
def calculate_Rsq(self, folds=['all', 'odd', 'even', 'cv']):
"""
Calculate R-Squared Maps for Numerosity Model
maps = mod.calculate_Rsq(folds)
folds - list of strings; runs for which to calculate
maps - dict of dicts; calculated R-squared maps
o all - dict of strings; all runs
o odd - dict of strings; odd runs
o even - dict of strings; even runs
o cv - dict of strings; cross-validated R-squared
o left - R-squared map for left hemisphere
o right - R-squared map for right hemisphere
maps = mod.calculate_Rsq(folds) loads results from numerosity analysis
and calculates R-squared maps for all runs in folds.
Note: "calculate_Rsq" uses the results dictionary keys "left" and "right"
which are identical to the hemisphere labels used by surfplot.
"""
# part 1: prepare calculations
#---------------------------------------------------------------------#
print('\n\n-> Subject "{}", Session "{}", Model "{}":'.format(self.sub, self.ses, self.model))
mod_dir = self.get_model_dir()
# specify slices
i = -1 # slice index (3rd dim)
slices = {'all': [], 'odd': [], 'even': []}
for run in runs: # for all possible runs
filename = self.get_events_tsv(run)
if os.path.isfile(filename): # if data from this run exist
i = i + 1 # increase slice index
slices['all'].append(i)
if run % 2 == 1: slices['odd'].append(i)
else: slices['even'].append(i)
# part 2: analyze both hemispheres
#---------------------------------------------------------------------#
hemis = {'L': 'left', 'R': 'right'}
maps = {}
for fold in folds: maps[fold] = {}
# for both hemispheres
for hemi in hemis.keys():
print(' - {} hemisphere:'.format(hemis[hemi]))
# for all folds
for fold in folds:
# load analysis results
filepath = mod_dir + '/sub-' + self.sub + '_ses-' + self.ses + \
'_model-' + self.model + '_hemi-' + hemi + '_space-' + self.space + '_'
if fold in ['odd', 'even']:
filepath = filepath + 'runs-' + fold + '_'
res_file = filepath + 'numprf.mat'
mu_map = filepath + 'mu.surf.gii'
NpRF = sp.io.loadmat(res_file)
surface = nib.load(mu_map)
mask = surface.darrays[0].data != 0
# calculate R-squared (all, odd, even)
avg = list(NpRF['settings']['avg'][0,0][0,:])
MLL1 = np.squeeze(NpRF['MLL_est'])
MLL00 = np.squeeze(NpRF['MLL_const'])
r0,n0 = self.calc_runs_scans(fold)
n1 = r0*n0
Rsq = NumpRF.MLL2Rsq(MLL1, MLL00, n1)
# calculate R-squared (cross-validated)