function [MeanWmDrift,Ci95WmDrift] = ExtractWhiteMatterDrift(WhiteTpm,fMask,fImgs)
Vol_WhiteTpm = spm_vol(WhiteTpm);
P_WhiteTpm = spm_imatrix(Vol_WhiteTpm.mat);
VoxDim_WhiteTpm = abs(P_WhiteTpm(7:9));
Vol_fMask = spm_vol(fMask);
P_fMask = spm_imatrix(Vol_fMask.mat);
VoxDim_fMask = abs(P_fMask(7:9));
RelativeSize = round(VoxDim_fMask ./ VoxDim_WhiteTpm);
xyzDiff = ((RelativeSize - 1) ./ 2) .* VoxDim_WhiteTpm;
i_Coordinates_fVx = 0;
Coordinates_fVx = cell(i_Coordinates_fVx,1);
SamsDisp('Retrieving white matter coordinates in the functional volume');
for fVx_x = 1:Vol_fMask.dim(1,1)
for fVx_y = 1:Vol_fMask.dim(1,2)
for fVx_z = 1:Vol_fMask.dim(1,3)
fSample = spm_sample_vol(Vol_fMask,fVx_x,fVx_y,fVx_z,0);
if fSample > 0.9
fCoordinate_MNI = (Vol_fMask.mat * [fVx_x,fVx_y,fVx_z,1]')';
fCoordinate_MNI = fCoordinate_MNI(1,1:3);
TpmMni_xStart = fCoordinate_MNI(1,1) - xyzDiff(1,1);
TpmMni_xEnd = fCoordinate_MNI(1,1) + xyzDiff(1,1);
TpmMni_yStart = fCoordinate_MNI(1,2) - xyzDiff(1,2);
TpmMni_yEnd = fCoordinate_MNI(1,2) + xyzDiff(1,2);
TpmMni_zStart = fCoordinate_MNI(1,3) - xyzDiff(1,3);
TpmMni_zEnd = fCoordinate_MNI(1,3) + xyzDiff(1,3);
i_TmpVoxelMatrix_x = 0;
for TmpMni_x = TpmMni_xStart:VoxDim_WhiteTpm(1,1):TpmMni_xEnd
i_TmpVoxelMatrix_x = i_TmpVoxelMatrix_x + 1;
i_TmpVoxelMatrix_y = 0;
for TmpMni_y = TpmMni_yStart:VoxDim_WhiteTpm(1,2):TpmMni_yEnd
i_TmpVoxelMatrix_y = i_TmpVoxelMatrix_y + 1;
i_TmpVoxelMatrix_z = 0;
for TmpMni_z = TpmMni_zStart:VoxDim_WhiteTpm(1,3):TpmMni_zEnd
i_TmpVoxelMatrix_z = i_TmpVoxelMatrix_z + 1;
TmpCoordinate_Vx = (Vol_WhiteTpm.mat \ [TmpMni_x,TmpMni_y,TmpMni_z,1]')';
TmpCoordinate_Vx = round(TmpCoordinate_Vx(1,1:3));
Sample_Tpm = spm_sample_vol(Vol_WhiteTpm,TmpCoordinate_Vx(1,1),TmpCoordinate_Vx(1,2),TmpCoordinate_Vx(1,3),0);
TmpVoxelMatrix(i_TmpVoxelMatrix_x,i_TmpVoxelMatrix_y,i_TmpVoxelMatrix_z) =...
Sample_Tpm > 0.99;
end
end
end
if sum(sum(sum(TmpVoxelMatrix,1),2),3) == numel(TmpVoxelMatrix)
i_Coordinates_fVx = i_Coordinates_fVx + 1;
Coordinates_fVx{i_Coordinates_fVx,1} = [fVx_x,fVx_y,fVx_z];
end
end
end
end
end
n_Coordinates_fVx = i_Coordinates_fVx;
SamsDisp(sprintf('Identified %i unique white matter voxels in the functional volume',n_Coordinates_fVx));
SamsDisp('Populating white matter intensity matrix:');
WmIntensityMatrix = zeros(size(fImgs,1),n_Coordinates_fVx);
dispstat('','init');
for i_fImgs = 1:size(fImgs,1)
Vol_fImgs = spm_vol(fImgs{i_fImgs,1});
for i_Coordinate = 1:size(Coordinates_fVx,1)
WmIntensityMatrix(i_fImgs,i_Coordinate) =...
spm_sample_vol(Vol_fImgs,Coordinates_fVx{i_Coordinate,1}(1,1),Coordinates_fVx{i_Coordinate,1}(1,2),Coordinates_fVx{i_Coordinate,1}(1,3),0);
end
dispstat(sprintf(' ...Completed functional volume %i of %i',i_fImgs,size(fImgs,1)));
end
SamsDisp('Producing averages...');
MeanVoxelIntensities = nanmean(WmIntensityMatrix,1);
Wd = pwd;
cd(sprintf('%s%stoolbox%sstats%sstats',matlabroot,filesep,filesep,filesep));
SdVoxelIntensities = nanstd(WmIntensityMatrix,1,1);
WmDriftMatrix = zeros(size(WmIntensityMatrix));
for i_Scan = 1:1:size(WmIntensityMatrix,1)
for i_Voxel = 1:1:size(WmIntensityMatrix,2)
WmDriftMatrix(i_Scan,i_Voxel) = (WmIntensityMatrix(i_Scan,i_Voxel) - MeanVoxelIntensities(1,i_Voxel)) / SdVoxelIntensities(1,i_Voxel);
end
end
MeanWmDrift = nanmean(WmDriftMatrix,2);
Ci95WmDrift = (nanstd(WmDriftMatrix,0,2)./sqrt(size(WmDriftMatrix,2))).*1.96;
cd(Wd);
SamsDisp('Done!');
return
function [] = SamsDisp( Str )
Time = clock;
Year = sprintf('%04d',Time(1,1));
Month = sprintf('%02d',Time(1,2));
Day = sprintf('%02d',Time(1,3));
Hour = sprintf('%02d',Time(1,4));
Min = sprintf('%02d',Time(1,5));
Sec = sprintf('%02d',round(Time(1,6)));
SrtToPrint = sprintf('[%s/%s/%s - %s:%s:%s]: %s',Day,Month,Year,Hour,Min,Sec,Str);
disp(SrtToPrint);
return
function dispstat(TXT,varargin)
keepthis = 0;
keepprev = 0;
timestamp = 0;
init = 0;
if ~isstr(TXT)
return
end
persistent prevCharCnt;
if isempty(prevCharCnt)
prevCharCnt = 0;
end
if nargin == 0
return
elseif nargin > 1
for i = 2:nargin
eval([varargin{i-1} '=1;']);
end
end
if init == 1
prevCharCnt = 0;
return;
end
if isempty(TXT) && timestamp == 0
return
end
if timestamp == 1
c = clock;
txtTimeStamp = sprintf('%02d:%02d:%02d ',c(4),c(5),round(c(6)));
else
txtTimeStamp = '';
end
if keepprev == 1
prevCharCnt = 0;
end
TXT = strrep(TXT,'%','%%');
TXT = strrep(TXT,'\','\\');
TXT = [txtTimeStamp TXT '\n'];
fprintf([repmat('\b',1, prevCharCnt) TXT]);
nof_extra = length(strfind(TXT,'%%'));
nof_extra = nof_extra + length(strfind(TXT,'\\'));
nof_extra = nof_extra + length(strfind(TXT,'\n'));
prevCharCnt = length(TXT) - nof_extra;
if keepthis == 1
prevCharCnt = 0;
end
return