-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_preparation.m
130 lines (108 loc) · 3.36 KB
/
main_preparation.m
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
% Preparations for parcellation.
% 2018-6-19 15:37:19
%% Download the NIFTI toolbox and add it to path.
% You might do it manually in case of error.
url='http://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/8797/versions/28/download/zip/NIfTI_20140122.zip';
filename='NIfTI_20140122.zip';
if ~exist(filename,'file')
urlwrite(url,filename); % download the NIFTI toolbox, 0.42 MB
end
unzip('NIfTI_20140122.zip','NIfTI_20140122'); % uncompress the toolbox
addpath('NIfTI_20140122/'); % add to path
%% the subject ID
sFile=dir('data/sub*');
nSub=length(sFile);
sSub=zeros(nSub,1);
tic;
for iSub=1:nSub
filename=sFile(iSub,1).name;
cSub=str2num(filename(4:8));
sSub(iSub,1)=cSub;
perct(toc,iSub,nSub);
end
%% the initialized cluster numbers
sK=[50:50:1000];
nK=length(sK);
%% Divide the subjects into two equal groups randomly.
nRep=3; % this operation is repeated for 3 times
nPart=2; % divide the subjects in two groups
% rng(1);
tmp=[];
for iRep=1:nRep
tmp=[tmp;randperm(40)];
end
tmp=tmp';
randset=tmp(1:floor(nSub/2),:);
save('randset_1.mat','randset');
randset=tmp(floor(nSub/2)+1:40,:);
save('randset_2.mat','randset');
%% number of parallel workers
import java.lang.*;
nCPU=Runtime.getRuntime.availableProcessors; % number of CPUs
nWorker=floor(nCPU*0.90); % use 90% CPUs
%% save some useful information
save('sInfo.mat','sSub','nSub','sK','nK','nRep','nPart','nWorker');
%% Generate a 3*3*3 cubic searchlight mask.
r=1;
nvSL=(2*r+1)^3; % number of voxels in a searchlight
riSL=zeros(nvSL,3);
count=0;
for i=-r:r
for j=-r:r
for k=-r:r
count=count+1;
riSL(count,:)=[i,j,k];
end
end
end
save('parc_searchlight.mat','riSL','nvSL');
%% Save some useful information of the graymatter mask.
file_mask='graymatter.nii';
nii=load_untouch_nii(file_mask);
img_gray=double(nii.img);
msk_gray=img_gray~=0;
ind_gray=find(msk_gray);
num_gray=length(ind_gray);
siz=size(msk_gray);
save('parc_graymatter.mat','msk_gray','ind_gray','num_gray','siz');
%% The coordinates of the adjacent voxels.
% This is determined by the graymatter mask and the searchlight.
load('parc_graymatter.mat');
load('parc_searchlight.mat');
nM=num_gray;
% adjacency matrix
fprintf('Calculating the coordinates of the adjacent voxels.\n');
cav=zeros(nM,nvSL);
tic;
for iM=1:nM
% subscript of a single index in graymatter
[i,j,k]=ind2sub(siz,ind_gray(iM));
% searchlight constraint
ix=riSL+repmat([i,j,k],nvSL,1);
% box constraint
tmp=all(ix>=ones(nvSL,3),2) & all(ix<=repmat(siz,nvSL,1),2);
ix=ix(tmp,:);
% single index
% for voxels within the union of the searchlight and the box
index=sub2ind(siz,ix(:,1),ix(:,2),ix(:,3));
% mask constraint
% convert the index in 3D space to that in the mask
[tmp,index]=ismember(index,ind_gray);
index=index(tmp);
ix=ix(tmp,:);
% save the coordinates of the adjacent voxels, cav
cav(iM,1:length(index))=index';
perct(toc,iM,nM,20);
end
% the binary format of cav
fprintf('Calculating the binary format of CAV.\n');
n=size(cav,1);
cavb=sparse(n,n);
tic;
for i=1:n
tmp=cav(i,:);
tmp(tmp==0)=[];
cavb(i,tmp)=1;
perct(toc,i,n,20);
end
save('parc_cavb.mat','cav','cavb');