-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis_selma_iss.py
81 lines (60 loc) · 2.89 KB
/
analysis_selma_iss.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
import nibabel as nib
import numpy as np
import os
groups = 1 #TODO change for 1 or 34 groups
basedir = '/home/sellug/wrkgrp/Selma/CamCAN_movie/'
ngroups_dir = basedir + ('highpass_filtered_intercept2/') + str(groups) + 'groups/'
groupdir = ngroups_dir + 'preGSBS/age_groups/'
meanfolder = 'mean/'
SL = 5204
stride = 2
radius = 3
min_vox = 15
params_name = 'stride' + str(stride) + '_' + 'radius' + str(radius) + '_minvox' + str(min_vox)
coordinates = np.load(basedir + 'masks/searchlights/SL_voxelCoordinates_' + params_name + '.npy',allow_pickle=True)
searchlights = np.load(basedir + 'masks/searchlights/SL_voxelsIndices_'+ params_name + '.npy',allow_pickle= True)
ISS = np.full([SL,groups],0).astype(float)
for GR in range(groups):
datadir = groupdir + 'GR' + str(GR) + '/hyperaligned/'
# get mean image
mean_file = (datadir + meanfolder + 'mean_wholeBrain_allSubs_GR' + str(GR) + '.npy')
mean_img = np.load(mean_file)
# get nr of subjects per group based on hyperaligned .nii files in one group folder
# Get list of subjects
allfiles = os.listdir(datadir)
namelist = []
for names in allfiles:
if names.endswith(".nii"):
namelist.append(names)
namelist.sort()
nr_sub = len(namelist)
correlation_coefficients = np.full([nr_sub, len(searchlights)], 0).astype(float)
for s in range(nr_sub):
sub = namelist[s]
img = nib.load(datadir + sub)
img_sub = img.get_fdata()
# Loop through all searchlights
for SL_idx, voxel_indices in enumerate(searchlights):
print('group ' + str(GR) + ' subject ' + str(s) + ' SLindex ' + str(SL_idx))
vox_coords = coordinates[voxel_indices]
data_SL_mean = []
data_SL_subj = []
for x, y, z in vox_coords:
data_SL_mean.append(mean_img[x, y, z, :])
data_SL_subj.append(img_sub[x, y, z, :])
data_SL_mean = np.transpose(np.asarray(data_SL_mean)) # Go to time x voxel
data_SL_subj = np.transpose(np.asarray(data_SL_subj))
multiplied_img = data_SL_mean * nr_sub
leave1out = multiplied_img - data_SL_subj
#get correlation between 1 subject and the group minus that subject
correlation_coefficient = np.corrcoef(data_SL_subj.flatten(), leave1out.flatten())[0,1]
correlation_coefficients[s, SL_idx] = correlation_coefficient
# Get mean correlation across subjects in this group
group_mean_correlation = np.mean(correlation_coefficients, axis=0)
#np.save(datadir + meanfolder + 'group_mean_correlation_per_SL_GR' + str(GR), group_mean_correlation)
ISS[:,GR] = group_mean_correlation
np.save(groupdir + 'all_groups_mean_correlation_per_SL', ISS)
# Save individual ISS values per SL if doing analyses over 1 group
if groups == 1:
np.save(groupdir + 'ISS_perSL_perSubj', correlation_coefficients)
print("Done")