📄 fc.m
字号:
function [ResultMaps] = fc(ADataDir,AMaskFilename, AROIDef,AResultFilename, ACovariablesDef)
% Functional connectivity
%AROIList would be treated as a mask in which time courses would be averaged to produce a new time course representing the ROI area
% Input:
% ADataDir where the 3d+time dataset stay, and there should be 3d EPI functional image files. It must not contain / or \ at the end.
% AMaskFilename the mask file name, I only compute the point within the mask
% AROIList the mask list , ROI list definition
% AResultFilename the output filename
% ACovariablesDef
% Output:
% AResultFilename the filename of funtional connectivity result
%-----------------------------------------------------------
% Copyright(c) 2007~2010
% State Key Laboratory of Cognitive Neuroscience and Learning in Beijing Normal University
% Written by Xiao-Wei Song
% http://resting-fmri.sourceforge.net
%dawnwei.song@gmail.com, 20070903
%-----------------------------------------------------------
if nargin~=5,
error(' Error using ==> fc. 4 arguments wanted.');
end
theElapsedTime =cputime;
fprintf('\nComputing functional connectivity with:\t"%s"', ADataDir);
[AllVolume,VoxelSize,theImgFileList, Origin] =rest_to4d(ADataDir);
% examin the dimensions of the functional images and set mask
nDim1 = size(AllVolume,1); nDim2 = size(AllVolume,2); nDim3 = size(AllVolume,3);
BrainSize = [nDim1 nDim2 nDim3];
sampleLength = size(theImgFileList,1);
%20070512 Saving a big 3D+time Dataset to small pieces by its first dimension to make this process run at least
% put pieces of 4D dataset to the temp dir determined by the current time
theTempDatasetDirName =sprintf('fc_%d', fix((1e4) *rem(now, 1) ));
theTempDatasetDir =[tempdir theTempDatasetDirName] ;
ans=rmdir(theTempDatasetDir, 's');%suppress the error msg
mkdir(tempdir, theTempDatasetDirName); %Matlab 6.5 compatible
AROIList =AROIDef;
if iscell(AROIDef), %ROI wise, compute corelations between regions
%ROI time course retrieving, 20070903
theROITimeCourses =zeros(sampleLength, size(AROIDef,1));
for x=1:size(AROIDef,1),
fprintf('\n\t ROI time courses retrieving through "%s".', AROIDef{x});
IsDefinedROITimeCourse =0;
if rest_SphereROI( 'IsBallDefinition', AROIDef{x})
%The ROI definition is a Ball definition
maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef{x}, BrainSize, VoxelSize, Origin);
elseif exist(AROIDef{x},'file')==2 % Make sure the Definition file exist
[pathstr, name, ext, versn] = fileparts(AROIDef{x});
if strcmpi(ext, '.txt'),
tmpX=load(AROIDef{x});
if size(tmpX,2)>1,
%Average all columns to make sure tmpX only contain one column
tmpX = mean(tmpX')';
end
theROITimeCourses(:, x) =tmpX;
IsDefinedROITimeCourse =1;
elseif strcmpi(ext, '.img')
%The ROI definition is a mask file
maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef{x});
else
error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef{x}));
end
else
error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef{x}));
end
if ~IsDefinedROITimeCourse,% I need retrieving the ROI averaged time course manualy
maskROI =find(maskROI);
% [rangeX, rangeY, rangeZ] = ind2sub(size(maskROI), find(maskROI));
% theTimeCourses =zeros(length(unique(rangeX)), length(unique(rangeY)), length(unique(rangeZ)));
for t=1:sampleLength,
theTimePoint = squeeze(AllVolume(:,:,:, t));
theTimePoint = theTimePoint(maskROI);
if ~isempty(theTimePoint),
theROITimeCourses(t, x) =mean(theTimePoint);
end
end %The Averaged Time Course within the ROI now comes out! 20070903
end%if ~IsDefinedROITimeCourse
end%for
%Save the ROI averaged time course to disk for further study
[pathstr, name, ext, versn] = fileparts(AResultFilename);
theROITimeCourseLogfile =[fullfile(pathstr,['ROI_', name]), '.txt'];
save(theROITimeCourseLogfile, 'theROITimeCourses', '-ASCII', '-DOUBLE','-TABS')
%If there are covariables
theCovariables =[];
if exist(ACovariablesDef.ort_file, 'file')==2,
theCovariables =load(ACovariablesDef.ort_file);
%Add polynomial in the baseline model according to 3dfim+.pdf
thePolOrt=[];
if ACovariablesDef.polort>=0,
thePolOrt =(1:sampleLength)';
thePolOrt =repmat(thePolOrt, [1, (1+ACovariablesDef.polort)]);
for x=1:(ACovariablesDef.polort+1),
thePolOrt(:, x) =thePolOrt(:, x).^(x-1) ;
end
end
%Regress out the covariables
theCovariables =[thePolOrt, theCovariables];
theROITimeCourses =Brain1D_RegressOutCovariables(theROITimeCourses, theCovariables);
elseif ~isempty(ACovariablesDef.ort_file) && ~all(isspace(ACovariablesDef.ort_file)),
warning(sprintf('\n\nCovariables definition text file "%s" doesn''t exist, please check! I won''t regress out the covariables this time.', ACovariablesDef.ort_file));
end
%Calcute the corelation matrix and save to a text file
ResultMaps =corrcoef(theROITimeCourses);
save([AResultFilename, '.txt'], 'ResultMaps', '-ASCII', '-DOUBLE','-TABS')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else %Voxel wise, compute corelations between one ROI time course and the whole brain
%ROI time course retrieving, 20070903
fprintf('\n\t ROI time course retrieving through "%s".', AROIDef);
AROITimeCourse = zeros(sampleLength, 1);
IsDefinedROITimeCourse =0;
if rest_SphereROI( 'IsBallDefinition', AROIDef)
%The ROI definition is a Ball definition
maskROI =rest_SphereROI( 'BallDefinition2Mask' , AROIDef, BrainSize, VoxelSize, Origin);
elseif exist(AROIDef,'file')==2 % Make sure the Definition file exist
[pathstr, name, ext, versn] = fileparts(AROIDef);
if strcmpi(ext, '.txt'),
tmpX=load(AROIDef);
if size(tmpX,2)>1,
%Average all columns to make sure tmpX only contain one column
tmpX = mean(tmpX')';
end
AROITimeCourse =tmpX;
IsDefinedROITimeCourse =1;
elseif strcmpi(ext, '.img')
%The ROI definition is a mask file
maskROI =rest_loadmask(nDim1, nDim2, nDim3, AROIDef);
else
error(sprintf('REST doesn''t support the selected ROI definition now, Please check: \n%s', AROIDef));
end
else
error(sprintf('Wrong ROI definition, Please check: \n%s', AROIDef));
end
if ~IsDefinedROITimeCourse,% I need retrieving the ROI averaged time course manualy
maskROI =find(maskROI);
% [rangeX, rangeY, rangeZ] = ind2sub(size(maskROI), find(maskROI));
% theTimeCourses =zeros(length(unique(rangeX)), length(unique(rangeY)), length(unique(rangeZ)));
for t=1:sampleLength,
theTimePoint = squeeze(AllVolume(:,:,:, t));
theTimePoint = theTimePoint(maskROI);
AROITimeCourse(t) =mean(theTimePoint);
end %The Averaged Time Course within the ROI now comes out! 20070903
%Make sure the ROI averaged time course is an col vector
AROITimeCourse =reshape(AROITimeCourse, sampleLength,1);
end
%Save the ROI averaged time course to disk for further study
[pathstr, name, ext, versn] = fileparts(AResultFilename);
theROITimeCourseLogfile =[fullfile(pathstr,['ROI_', name]), '.txt'];
save(theROITimeCourseLogfile, 'AROITimeCourse', '-ASCII', '-DOUBLE','-TABS')
%Save 3D+time Dataset's pieces to disk after ROI time course retrieved
Save1stDimPieces(theTempDatasetDir, AllVolume, 'dim1_');
clear AllVolume;%Free large memory
%mask selection, added by Xiaowei Song, 20070421
fprintf('\n\t Load mask "%s".', AMaskFilename);
mask=rest_loadmask(nDim1, nDim2, nDim3, AMaskFilename);
fprintf('\n\t Build functional connectivity mask.\tWait...');
mask =logical(mask);%Revise the mask to ensure that it contain only 0 and 1
mask = repmat(mask, [1, 1, 1, sampleLength]);
%Save mask pieces to disk to make this program at least run
Save1stDimPieces(theTempDatasetDir, mask, 'mask_');
fprintf('\n\t Build Covariables time course.\tWait...');
%If there are covariables, 20071002
theCovariables =[];
if exist(ACovariablesDef.ort_file, 'file')==2,
theCovariables =load(ACovariablesDef.ort_file);
%Add polynomial in the baseline model according to 3dfim+.pdf
thePolOrt=[];
if ACovariablesDef.polort>=0,
thePolOrt =(1:sampleLength)';
thePolOrt =repmat(thePolOrt, [1, (1+ACovariablesDef.polort)]);
for x=1:(ACovariablesDef.polort+1),
thePolOrt(:, x) =thePolOrt(:, x).^(x-1) ;
end
end
% switch ACovariablesDef.polort,
% case 0,
% thePolOrt =ones(sampleLength, 1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -