0001 function [] = reho(ADataDir, NVoxel, AMaskFilename, AResultFilename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046 if nargin~=4
0047 error(' Error using ==> reho. 4 arguments wanted.');
0048 end
0049 theElapsedTime =cputime;
0050
0051
0052
0053 if NVoxel ~= 27 & NVoxel ~= 19 & NVoxel ~= 7
0054 error('The second parameter should be 7, 19 or 27. Please re-exmamin it.');
0055 end
0056
0057
0058
0059 fprintf('\n\t Read these 3D EPI functional images.\twait...');
0060 [AllVolume,vsize,theImgFileList, Header] =rest_to4d(ADataDir);
0061 [nDim1 nDim2 nDim3 nDim4]=size(AllVolume);
0062 isize = [nDim1 nDim2 nDim3];
0063 mask=rest_loadmask(nDim1, nDim2, nDim3, AMaskFilename);
0064
0065
0066
0067
0068 theTempDatasetDirName =sprintf('ReHo_%d', fix((1e4) *rem(now, 1) ));
0069 theTempDatasetDir =[tempdir theTempDatasetDirName] ;
0070 ans=rmdir(theTempDatasetDir, 's');
0071 mkdir(tempdir, theTempDatasetDirName);
0072
0073 Save1stDimPieces(theTempDatasetDir, AllVolume, 'dim1_');
0074 clear AllVolume;
0075
0076
0077
0078 fprintf('\n\t Calculate the rank of time series on voxel by voxel');
0079
0080 NumPieces_Dim1=10;
0081 NumComputingCount =ceil(nDim1/NumPieces_Dim1);
0082 for x=1:(NumComputingCount),
0083 rest_waitbar(x/NumComputingCount, ...
0084 'Calculate the rank of time series on voxel by voxel. Please wait...', ...
0085 'REST working','Child','NeedCancelBtn');
0086
0087 theFilename =fullfile(theTempDatasetDir, sprintf('dim1_%.8d', x));
0088 theDim1Volume4D =Load1stDimVolume(theFilename);
0089 theDim1Volume4D =double(theDim1Volume4D);
0090
0091
0092 theDim1Volume4D=permute(theDim1Volume4D,[4,1,2,3]);
0093 [TimePoints,M,N,O]=size(theDim1Volume4D);
0094 [theDim1Volume4D,SortIndex] = sort(theDim1Volume4D,1);
0095 db=diff(theDim1Volume4D,1,1);
0096 clear theDim1Volume4D;
0097 db = db == 0;
0098 if x~=NumComputingCount
0099 mask_x = mask(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1), :,:);
0100 sumdb=squeeze(sum(db,1)).*mask_x;
0101 else
0102 mask_x = mask(((x-1)*NumPieces_Dim1+1):end, :,:);
0103 sumdb=sum(db,1);
0104 sumdb=reshape(sumdb,size(mask_x)).*mask_x;
0105 end
0106 SortedRanks=reshape(repmat([1:TimePoints]',M*N*O,1),[TimePoints,M,N,O]);
0107 if any(sumdb(:))
0108 TieAdjustIndex=find(sumdb);
0109 for i=1:length(TieAdjustIndex)
0110 [iM iN iO]=ind2sub([M N O],TieAdjustIndex(i));
0111 ranks=SortedRanks(:,iM,iN,iO);
0112 ties=db(:,iM,iN,iO);
0113 tieloc = [find(ties); TimePoints+2];
0114 maxTies = numel(tieloc);
0115 tiecount = 1;
0116 while (tiecount < maxTies)
0117 tiestart = tieloc(tiecount);
0118 ntied = 2;
0119 while(tieloc(tiecount+1) == tieloc(tiecount)+1)
0120 tiecount = tiecount+1;
0121 ntied = ntied+1;
0122 end
0123
0124 ranks(tiestart:tiestart+ntied-1) = ...
0125 sum(ranks(tiestart:tiestart+ntied-1)) / ntied;
0126 tiecount = tiecount + 1;
0127 end
0128 SortedRanks(:,iM,iN,iO)=ranks;
0129 end
0130 end
0131 clear db sumdb;
0132 SortIndexBase=reshape(([1:M*N*O]'-1).*TimePoints,[M,N,O]);
0133 SortIndexBase=repmat(SortIndexBase,[1,1,1,TimePoints]);
0134 SortIndexBase=permute(SortIndexBase,[4,1,2,3]);
0135 SortIndex=SortIndex+SortIndexBase;
0136 clear SortIndexBase;
0137 theDim1Volume4D(SortIndex)=SortedRanks;
0138 theDim1Volume4D=reshape(theDim1Volume4D,size(SortedRanks));
0139 clear SortIndex SortedRanks;
0140 theDim1Volume4D=uint16(theDim1Volume4D);
0141
0142 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0143 save(theFilename, 'theDim1Volume4D');
0144 fprintf('.');
0145
0146 end
0147
0148 for x=1:(NumComputingCount)
0149 rest_waitbar(x/(floor(nDim1/NumPieces_Dim1)+1), ...
0150 'Rank 3D Brain reconstructing. Please wait...', ...
0151 'REST working','Child','NeedCancelBtn');
0152 theFilename =fullfile(theTempDatasetDir, sprintf('result_%.8d', x));
0153 if x==1
0154 I=Load1stDimVolume(theFilename);
0155 else
0156 I=cat(2,I,Load1stDimVolume(theFilename));
0157 end
0158 fprintf('.');
0159 end
0160 fprintf('\n');
0161 [TimePoints,M,N,O]=size(I);
0162
0163 ans=rmdir(theTempDatasetDir, 's');
0164
0165
0166
0167 fprintf('\t Calculate the kcc on voxel by voxel for the data set.\n');
0168 K = zeros(M,N,O);
0169 switch NVoxel
0170 case 27
0171 for i = 2:M-1
0172 for j = 2:N-1
0173 for k = 2:O-1
0174 block = I(:,i-1:i+1,j-1:j+1,k-1:k+1);
0175 mask_block = mask(i-1:i+1,j-1:j+1,k-1:k+1);
0176 if all(mask_block(:))
0177 R_block=reshape(block,[],27);
0178 mask_R_block = R_block(:,reshape(mask_block,1,27) > 0);
0179 K(i,j,k) = f_kendall(mask_R_block);
0180 end
0181 end
0182 end
0183 rest_waitbar(i/M, ...
0184 sprintf('Calculate the kcc\nwait...'), ...
0185 'ReHo Computing','Child','NeedCancelBtn');
0186 end
0187 fprintf('\t The reho of the data set was finished.\n');
0188 rest_writefile(single(K),AResultFilename,isize,vsize,Header, 'single');
0189 case 19
0190 mask_cluster_19=ones(3,3,3);
0191 mask_cluster_19(1,1,1) = 0; mask_cluster_19(1,3,1) = 0; mask_cluster_19(3,1,1) = 0; mask_cluster_19(3,3,1) = 0;
0192 mask_cluster_19(1,1,3) = 0; mask_cluster_19(1,3,3) = 0; mask_cluster_19(3,1,3) = 0; mask_cluster_19(3,3,3) = 0;
0193
0194 for i = 2:M-1
0195 for j = 2:N-1
0196 for k = 2:O-1
0197 block = I(:,i-1:i+1,j-1:j+1,k-1:k+1);
0198 mask_block = mask(i-1:i+1,j-1:j+1,k-1:k+1);
0199 if all(mask_block(:))
0200 mask_block=mask_block.*mask_cluster_19;
0201
0202 R_block=reshape(block,[],27);
0203 mask_R_block = R_block(:,reshape(mask_block,1,27) > 0);
0204 K(i,j,k) = f_kendall(mask_R_block);
0205 end
0206 end
0207 end
0208 rest_waitbar(i/M, ...
0209 sprintf('Calculate the kcc\nwait...'), ...
0210 'ReHo Computing','Child','NeedCancelBtn');
0211 end
0212 fprintf('\t The reho of the data set was finished.\n');
0213 rest_writefile(single(K),AResultFilename,isize,vsize,Header, 'single');
0214 case 7
0215 mask_cluster_7=ones(3,3,3);
0216 mask_cluster_7(1,1,1) = 0; mask_cluster_7(1,2,1) = 0; mask_cluster_7(1,3,1) = 0; mask_cluster_7(1,1,2) = 0;
0217 mask_cluster_7(1,3,2) = 0; mask_cluster_7(1,1,3) = 0; mask_cluster_7(1,2,3) = 0; mask_cluster_7(1,3,3) = 0;
0218 mask_cluster_7(2,1,1) = 0; mask_cluster_7(2,3,1) = 0; mask_cluster_7(2,1,3) = 0; mask_cluster_7(2,3,3) = 0;
0219 mask_cluster_7(3,1,1) = 0; mask_cluster_7(3,2,1) = 0; mask_cluster_7(3,3,1) = 0; mask_cluster_7(3,1,2) = 0;
0220 mask_cluster_7(3,3,2) = 0; mask_cluster_7(3,1,3) = 0; mask_cluster_7(3,2,3) = 0; mask_cluster_7(3,3,3) = 0;
0221
0222 for i = 2:M-1
0223 for j = 2:N-1
0224 for k = 2:O-1
0225 block = I(:,i-1:i+1,j-1:j+1,k-1:k+1);
0226 mask_block = mask(i-1:i+1,j-1:j+1,k-1:k+1);
0227 if all(mask_block(:))
0228 mask_block=mask_block.*mask_cluster_7;
0229
0230 R_block=reshape(block,[],27);
0231 mask_R_block = R_block(:,reshape(mask_block,1,27) > 0);
0232 K(i,j,k) = f_kendall(mask_R_block);
0233 end
0234 end
0235 end
0236 rest_waitbar(i/M, ...
0237 sprintf('Calculate the kcc\nwait...'), ...
0238 'ReHo Computing','Child','NeedCancelBtn');
0239 end
0240 fprintf('\t The reho of the data set was finished.\n');
0241 rest_writefile(single(K),AResultFilename,isize,vsize,Header,'single');
0242 otherwise
0243 error('The second parameter should be 7, 19 or 27. Please re-exmamin it.');
0244 end
0245 Ken = K;
0246 theElapsedTime =cputime - theElapsedTime;
0247 fprintf('\n\tRegional Homogeneity computation over, elapsed time: %g seconds\n', theElapsedTime);
0248
0249
0250
0251 function B = f_kendall(A)
0252 nk = size(A); n = nk(1); k = nk(2);
0253 SR = sum(A,2); SRBAR = mean(SR);
0254 S = sum(SR.^2) - n*SRBAR^2;
0255 B = 12*S/k^2/(n^3-n);
0256
0257
0258 function Save1stDimPieces(ATempDir, A4DVolume, AFilenamePrefix)
0259
0260 NumPieces_Dim1=10;
0261 NumComputingCount =ceil(size(A4DVolume,1)/NumPieces_Dim1);
0262 for x = 1:(NumComputingCount),
0263
0264 rest_waitbar((x/NumComputingCount), ...
0265 'Cut one Big 3D+time Dataset into pieces of 3D+time Dataset Before ReHo. Please wait...', ...
0266 'REST working','Child','NeedCancelBtn');
0267
0268 theFilename =fullfile(ATempDir, sprintf('%s%.8d',AFilenamePrefix, x));
0269 if x~=NumComputingCount
0270 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):(x*NumPieces_Dim1), :,:,:);
0271 else
0272 the1stDim = A4DVolume(((x-1)*NumPieces_Dim1+1):end, :,:,:);
0273 end
0274 save(theFilename, 'the1stDim');
0275 end
0276
0277 function Result=Load1stDimVolume(AFilename)
0278
0279 Result =load(AFilename);
0280 theFieldnames=fieldnames(Result);
0281
0282 Result = Result.(theFieldnames{1});