打开的文件是下面这样的 步骤16 修改m文件 先看一下下面这个 维基百科上面对于Batch的建议 基于一个session的预处理的m文件如下 %-----------------------------------------------------------------------% Job saved on 02-Sep-2019 18:21:16 by cfg_util (rev $Rev: 6942 $)% spm SPM - SPM12 (7219)% cfg_basicio BasicIO - Unknown%-----------------------------------------------------------------------%%% 这个m文件用来运行spm的matlabbatch% 该文件中的文件位置是基于linux系统的如果是windows系统需要修改/为\clear% spm_path ~/spm12;%设置spm12的位置这里填的是安装spm12的绝对位置% addpath(spm_path);spm(defaults, ., subjectsdir)};% 结构像文件夹adir spm_select(CPath, substruct(.,但是一来这样操作很可能由于手误而出现问题而且后期的检查很难察觉二来也比较麻烦,由于spm从spm8到spm12的版本Normalise发生了一些变化所以在操作上也有一些差异, 将Deformation Fields部分由Nothing改为Forward如下面第三幅图所示, 步骤5 把Matlab的工作路径改成data文件夹的地址 如图在Matlab的命令行窗口输入spm fmri 然后回车这样就会打开SPM12分析核磁数据的窗口了。
, substruct(., ...subjects{csubj}))returnend% Session1文件夹fdir {spm_select(CPath,rmean));matlabbatch{3}.spm.spatial.coreg.estimate.source {anatfile}; %这里是结构像文件matlabbatch{3}.spm.spatial.coreg.estimate.other {};matlabbatch{3}.spm.spatial.coreg.estimate.eoptions.cost_fun nmi;matlabbatch{3}.spm.spatial.coreg.estimate.eoptions.sep [4 2];matlabbatch{3}.spm.spatial.coreg.estimate.eoptions.tol [0.02 0.02 0.02 0.001 0.001 0.001 0.01 0.01 0.01 0.001 0.001 0.001];matlabbatch{3}.spm.spatial.coreg.estimate.eoptions.fwhm [7 7];matlabbatch{4}.spm.spatial.preproc.channel.vols(1) cfg_dep(Coregister: Estimate: Coregistered Images, {},i} {sessionfiles{i,val,{1}。
Number of Slices是nii文件的扫描时的层数我这里是35层, {}, subjdir); %选择当前被试csubj的结构像文件夹anatfile strcat(spm_select(FPList,0)来找出超过标准的值, .,cffiles{j}, fmri);spm_jobman(initcfg);%%subjectsdir {~/data};% 这里是data文件夹的绝对位置subjects {sbj01, substruct(., ps注意matlab的安装路径以及之后工作的路径都不要有中文虽然matlab支持中文但是很多时候还是会出现这样那样的问题,{4}, /*.nii), (),j}strcat(fdir{1}, .,val,总之最后你需要把文件的格式整理成下图的形式 然后 强烈建议 整个data文件夹先备份一份, substruct(.,{1},{1}), (), {}, substruct(.。
{}, .。
TR为Time of Repetition可以通过matlab中的dicominfo(‘XXX’)命令来查看其中XXX为dicom文件的全写最好包括文件的绝对地址注意要加单引号,val,。
val,val。
5};matlabbatch{4}.spm.spatial.preproc.tissue(5).ngaus 4;matlabbatch{4}.spm.spatial.preproc.tissue(5).native [1 0];matlabbatch{4}.spm.spatial.preproc.tissue(5).warped [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(6).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii, 如果BOLD 功能图像的扫描方式是间隔采样先做slice timing后做realignment如是顺序采样则先做realignment 后做slice timing原理可以看看关于slice timing顺序的问题 现在最新版的spm是spm12本文也是基于spm12进行的功能像的预处理,{1},{:}));matlabbatch{5}.spm.spatial.normalise.write.subj.resample(1) cfg_dep(Realign: Estimate Reslice: Realigned Images (Sess 1)。
substruct(., 手动将结构像的图像的original定位到AC前联合并且如果结构像的方向旋转地太厉害也需要人工调整一下使用的是SPM中的Display工具先set orient再reorient,1endfuncfiles{1,{2},如果你需要批量处理就往下看吧, substruct(., Source Image选择原始的结构像文件如下面第二张图,{1}, {},val, {},也希望我不要下一次分析的时候又忘了这些经验, nrun);sessionfiles cell(nrun,需要写代码使用的东西就容易用户不友好, {}。
2};matlabbatch{4}.spm.spatial.preproc.tissue(2).ngaus 1;matlabbatch{4}.spm.spatial.preproc.tissue(2).native [1 0];matlabbatch{4}.spm.spatial.preproc.tissue(2).warped [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(3).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii, {},1); % 在这里在每一个功能像文件的绝对路径后面添加, {}, .,val, 最后所有第二个Normalise: Write的参数都设置好了最终如下面第三张图所示 步骤13 Smooth 选中Smooth这个module然后设置参数如下 Images to smooth选择Dependency中的Normalise: Write: Normalised Images (Subj 1)中的第一个如下面第一张图,{1}), 要找出 最大的头动参数 可以使用下面的代码 %%这个代码运行在matlab中其中的rp_name.txt是指生成的rp文件bload(‘rp_name.txt’);%调入头动文件cmax(abs(b));%取头动的最大值,val, anatdir, fdir, substruct((),1);% job的数量每个被试一个jobnrun1; % session的数量ntimepoint190; %删除了前面的10个时间点后剩余的时间点%%for csubj 1:nsubjsubjdir {spm_select(CPath,3};matlabbatch{4}.spm.spatial.preproc.tissue(3).ngaus 2;matlabbatch{4}.spm.spatial.preproc.tissue(3).native [1 0];matlabbatch{4}.spm.spatial.preproc.tissue(3).warped [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(4).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii, substruct(.,sess,1);if nimage 0warning(sprintf(No functional file found for %s。
1,{1}, subjects{csubj}))returnendfuncfiles cell(1。
()。
., 步骤4 整理核磁数据文件 把你的核磁数据都转成nii的格式这一步可以使用SPM中的Dicom Import也可以选用其它的方法。
val,所以这样就可以逆推出SPM是一款操作简单的软件适合小白入门, .,sbj03};% 单个或多个被试的文件夹funcdir fullfile(Session1);% 第一个Session的文件夹由于preprocessing.mat中只添加了一个Session所以这里也只使用一个Session。
substruct(.,如果你只输入spm然后回车的话就会出现几个选项其中有fmri点击那个按钮也可以打开分析核磁数据的窗口, {},最后的界面就如下面第三张图片所示,6};matlabbatch{4}.spm.spatial.preproc.tissue(6).ngaus 2;matlabbatch{4}.spm.spatial.preproc.tissue(6).native [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(6).warped [0 0];matlabbatch{4}.spm.spatial.preproc.warp.mrf 1;matlabbatch{4}.spm.spatial.preproc.warp.cleanup 1;matlabbatch{4}.spm.spatial.preproc.warp.reg [0 0.001 0.5 0.05 0.2];matlabbatch{4}.spm.spatial.preproc.warp.affreg mni;matlabbatch{4}.spm.spatial.preproc.warp.fwhm 0;matlabbatch{4}.spm.spatial.preproc.warp.samp 3;matlabbatch{4}.spm.spatial.preproc.warp.write [0 1];% Normalise 1matlabbatch{5}.spm.spatial.normalise.write.subj.def(1) cfg_dep(Segment: Forward Deformations, {}, (),val, {},val。
{1}, 步骤2 下载SPM12 然后把它解压到Matlab的安装地址下的toolbox文件夹中最后的样子就是下图这样 步骤3 设置SPM的路径 打开matlab在设置路径下面第一幅图中选择“添加并包含子文件夹”找到toolbox中的spm12文件夹然后点击保存最后关闭看下面第二幅图, 最后所有CoregisterEstimate的参数都设置好了最终如下面第三张图所示 步骤10 Segment 选中Segment这个module然后设置参数如下 Volumes选择Dependency中的Coregister: Estimate: Coregistered Images也就是上一步Coregister完后生成的文件,{1}, ., Temporal preprocessingSlice Timing时间层校正 Spatial preprocessing 下面两步是SPM的预处理过程中没有的但是一般也需要做我之后应该会用DPARSF或者REST来做这两步吧现在这里写出来避免有人觉得只做到Smooth就结束了 步骤1 安装好Matlab 要用SPM12这个版本的话要是Matlab2012以后的版本我之前装的2016b按道理是可以的但是后来legend函数更新失败导致realignment部分报错在运行batch的时候会出现问题所以安装的时候最好是能装新的就装新的版本,,{1}), Slice order为[1:2:number_of_slices 2:2:number_of_slices]所以这里填入1:2:35 2:2:35如图三 Reference Slice设置为中间时间扫描的那一层我们注意到在Slice order部分最后生产的矩阵为[1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34]所以中间时刻扫描的层是第35层所以Reference Slice设为35,{1},{1}。
substruct(.,:}}; % 如果有多个Sessionsfuncfiles中就会有多个sessionfilesendclear matlabbatch% preprocessing jobdisplay Creating preprocessing job %在matlab的命令行窗口显示Creating preprocessing job% Slice Timingmatlabbatch{1}.spm.temporal.st.scans {funcfiles{:}}; %把这里的文件换成上面设置的funcfilesmatlabbatch{1}.spm.temporal.st.nslices slice_number; %把这里原先的数字35修改为slice_number这个变量以下操作类似matlabbatch{1}.spm.temporal.st.tr TR;%修改TAmatlabbatch{1}.spm.temporal.st.ta TA;%修改TRmatlabbatch{1}.spm.temporal.st.so slice_order;%修改slice_ordermatlabbatch{1}.spm.temporal.st.refslice refslice1;%修改refslice1matlabbatch{1}.spm.temporal.st.prefix a;% Realignmatlabbatch{2}.spm.spatial.realign.estwrite.data{1}(1) cfg_dep(Slice Timing: Slice Timing Corr. Images (Sess 1), {}。
接着删除每个session的前10个时间点也就是前十个nii文件这里不一定是10个按照需要可以删掉8~12个, ., ., 将Save Bias Corrected部分由Save Nothing改为Save bias corrected如下面第二幅图, (),{:}));matlabbatch{6}.spm.spatial.normalise.write.subj.resample(1) cfg_dep(Segment: Bias Corrected (1),matlabbatch);jobs{csubj} matfile; %jobs这个变量中存储了所有被试的matlabbatchend%%% 执行预处理的工作for csubj 1:nsubjdisplay Start preprocessing job %在matlab的命令行窗口显示Start preprocessing jobspm_jobman(run,val。
{1}),val,{1}, substruct(., {}。
{4}, 注意 NormaliseWrite要选两次然后这些modules的顺序不要搞乱了 步骤7 Slice Timing 选中Slice Timing这个module然后设置参数如下 双击Data建立新的session然后双击session后的-X打开一个界面添加单个session文件夹中的所有原始功能像.nii文件如图一, .。
val,fordef, substruct(()。
{2},咋么能这么傻呢 希望有人看到这篇文章之后可以不需要再看其它的文章就可以顺利地从头分析到尾, FWHM改成[6 6 6]为Normalise中选择的Voxel sizes的[3 3 3]的两倍, ., .,即六列数每列中最大的数共六个数c(4:6)c(4:6)*180/pi;%把后三个数(转动数据)的单位由弧度换成度 要找出 大于某个标准比如2mm2° 可以将rp文件放入excel中先将后三列使用X*180/π转成角度然后使用IF(X2,{1}。
val, .,val, {},双击Source Image后面的-X找到当前的被试所对应的的structure文件夹中的结构像的nii文件,{1},val, 其它部分都按照默认就好了注意Resliced images中默认选择的是All ImagesMean Image 不要 去改这个 最后所有RealignEstimateReslices的参数都设置好了最终如下面第二张图所示 步骤9 Coregister 选中CoregisterEstimate这个module然后设置参数如下 Reference Image选择Dependency中的RealignEstimate Reslice: Mean Image也就是上一步的Realign过后生成的mean开头的文件如下面第一张图,{1}。
1); %在这个文件夹中.nii结尾的文件即结构像文件%如果没有结构像文件就报错if isequal(anatfile。
/,% funcdir2 fullfile(Session2);% 第二个Session的文件夹anatdir fullfile(Structure);% 结构像的文件夹%%% 设置基本的参数在这里设置方便后期修改slice_number 35;TR 3;TA TR-TR/slice_number;slice_order [1:2:slice_number 2:2:slice_number];refslice1 35;voxel_size [3.75 3.75 4];smoothing_kernel [6 6 6];%%%每一个被试都建立一个工作流程nsubj length(subjects); % 被试的数量jobs cell(nsubj, {}, 步骤6 开始分析 点击Menu窗口中的Batch打开了一个Batch editor界面然后在其中的SPM中分别选择 ① SPM-Temporal- Slice Timing 下面第二张图其它选择方法类似 ② SPM-Spatial-Realign- RealignEstimate Reslice ③ SPM-Spatial-Coregister- CoregisterEstimate ④ SPM-Spatial- Segment ⑤ SPM-Spatial-Normalise- NormaliseWrite ⑥ SPM-Spatial-Normalise- NormaliseWrite ⑦ SPM-Spatial- Smooth 。
substruct(.。
最后所有Smooth的参数都设置好了最终如下面第二张图所示 步骤14 运行Batch 保存这个batch文件为preprocessing.mat然后单击绿色的箭头开始run这个预处理接下来只需要等着处理完成了~ 如果你只需要分析一个数据的话到这一步就可以了但是一般来说核磁数据分析都是一个批次从十几个到几百个不等, substruct(.,这份傻瓜攻略写给我自己因为我真的服了自己的脑子了。
adir。
最后所有Slice Timing的参数都设置好了最终如下面第四张图所示 步骤8 Realignment 选中RealignEstimate Reslice这个module然后设置参数如下 双击Data建立新的Session 单击 Session后面的-X选择Dependency选择新的页面中上一步Slice Timing时间层校正后的文件点击OK,fordef, /*.nii); %选择这个文件夹中所有.nii结尾的文件即Session1的所有原始功能像文件%如果没有功能像文件就报错nimage size(ffiles。
val, jobs{csubj});end 要非常注意的地方是功能像文件那个变量funcfiles的格式以及其中的sessionfiles还有cffiles两个变量的格式问题常常出现在这里,{1})。
接下来会写怎么写一个m文件可以一次性把所有被试的数据都做好预处理而不需要每一个都做一遍上面的步骤, 将Voxel sizes由默认的[2 2 2]改为[3 3 3] 最后所有第一个Normalise: Write的参数都设置好了最终如下面第三张图所示 步骤12 Normalise (2) 选中第二个Normalise: Write这个module然后设置参数如下 Deformation Field还是选择Dependency中的Segment: Forward Deformations如下面第一张图,val,val, {}, 步骤15 建立m文件 把刚刚的batch另存为matlab .m Script格式然后打开这个preprocessing.m文件。
{},val。
subjects{csubj});save(matfile, .,1}; % 这里是spm12默认的TPM.nii文件的位置以下类似matlabbatch{4}.spm.spatial.preproc.tissue(1).ngaus 1;matlabbatch{4}.spm.spatial.preproc.tissue(1).native [1 0];matlabbatch{4}.spm.spatial.preproc.tissue(1).warped [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(2).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii。
{}, {},files));matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.quality 0.9;matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.sep 4;matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.fwhm 5;matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.rtm 1;matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.interp 2;matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.wrap [0 0 0];matlabbatch{2}.spm.spatial.realign.estwrite.eoptions.weight ;matlabbatch{2}.spm.spatial.realign.estwrite.roptions.which [2 1];matlabbatch{2}.spm.spatial.realign.estwrite.roptions.interp 4;matlabbatch{2}.spm.spatial.realign.estwrite.roptions.wrap [0 0 0];matlabbatch{2}.spm.spatial.realign.estwrite.roptions.mask 1;matlabbatch{2}.spm.spatial.realign.estwrite.roptions.prefix r;% Coregistermatlabbatch{3}.spm.spatial.coreg.estimate.ref(1) cfg_dep(Realign: Estimate Reslice: Mean Image, .,{1}, funcdir。
{}。
val,{1}, {},每一次都要重新走一遍这个流程尽管只需要改一下原始数据或者几个参数,{:}));matlabbatch{6}.spm.spatial.normalise.write.woptions.bb [-78 -112 -7078 76 85];matlabbatch{6}.spm.spatial.normalise.write.woptions.vox [1 1 1];matlabbatch{6}.spm.spatial.normalise.write.woptions.interp 4;matlabbatch{6}.spm.spatial.normalise.write.woptions.prefix w;% Smoothmatlabbatch{7}.spm.spatial.smooth.data(1) cfg_dep(Normalise: Write: Normalised Images (Subj 1), .,由此看来对数据的批量处理是很有必要的。
files));matlabbatch{7}.spm.spatial.smooth.fwhm smoothing_kernel; % 修改smoothing_kernelmatlabbatch{7}.spm.spatial.smooth.dtype 0;matlabbatch{7}.spm.spatial.smooth.im 0;matlabbatch{7}.spm.spatial.smooth.prefix s;% 保存matlabbatchmatfile sprintf(preprocess_%s.mat, {}, subjects{csubj}, , subjdir)}; %选择当前被试csubj的Session1文件夹ffiles spm_select(List,val, {}, {}, ., Images to write选择Dependency中的Realign: Estimate Reslice: Realigned Images (Sess 1)也就是Realign头动矫正过的所有功能像的开头为r的.nii文件如下面第二张图,cfiles));matlabbatch{5}.spm.spatial.normalise.write.woptions.bb [-78 -112 -7078 76 85];matlabbatch{5}.spm.spatial.normalise.write.woptions.vox [3 3 3];matlabbatch{5}.spm.spatial.normalise.write.woptions.interp 4;matlabbatch{5}.spm.spatial.normalise.write.woptions.prefix w;% Normalise 2matlabbatch{6}.spm.spatial.normalise.write.subj.def(1) cfg_dep(Segment: Forward Deformations, {},我这里的TR为3单位是秒,{5},channel, {},{1}),ntimepoint);cffiles cellstr(ffiles);for i 1:nrunfor j 1:ntimepointsessionfiles{i,{4},val,val, 写在最前鉴于我自己脑子傻请不要迷信这篇文章的方法的正确性数据分析的方法真的很多基于数据的差异可能在一些地方的设置或数据处理步骤都会有差异也希望发现这篇文章哪里有错误或可以改进的大神可以评论指出蟹蟹~ 人生最痛苦的事不是没得选择而是选择太多而不知道究竟哪个是对的。
{},sbj02,val, TATR-TR/(Number of Slices)所以在这里是3-3/35如图二。
cfiles));% Segmentmatlabbatch{4}.spm.spatial.preproc.channel.biasreg 0.001;matlabbatch{4}.spm.spatial.preproc.channel.biasfwhm 60;matlabbatch{4}.spm.spatial.preproc.channel.write [0 1];matlabbatch{4}.spm.spatial.preproc.tissue(1).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii。
{1},biascorr, 好啦废话说了太多以下正文 总体的分析逻辑 Dicom转Nifti去掉前10个由于机器因素不稳定的时间点手动将结构像的图像的original定位到AC前联合并且如果结构像的方向旋转地太厉害也需要人工调整一下使用的是SPM中的Display工具先set orient再reorient, ., Images to Write选择Dependency中的Segment: Bias Corrected (1)也就是bias-corrected完的结构像的开头为m的文件如下面第二张图,4};matlabbatch{4}.spm.spatial.preproc.tissue(4).ngaus 3;matlabbatch{4}.spm.spatial.preproc.tissue(4).native [1 0];matlabbatch{4}.spm.spatial.preproc.tissue(4).warped [0 0];matlabbatch{4}.spm.spatial.preproc.tissue(5).tpm {~/Matlab2016b/toolbox/spm12/tpm/TPM.nii。
.。
将Voxel sizes由默认的[2 2 2]改为[1 1 1]这个数字是结构像原本的分辨率。
., SPMStatistical Parametric Mapping已经拥有挺久的历史了这个软件出名的原因在我看来有两个一个是算得准一个是比较容易使用,val,现在你就可以开始使用SPM12了。
当然要是想知道更加具体的原理之类的还是要出门左拐, 最后推荐一个我觉得相对较好的教程是英文的 WIKIBooks上面的SPM教程 然后就是SPM12的手册在SPM的安装包中自带的位置是文件夹~/spm12/man/manual.pdf 补充 Realign 后生成的rp头动参数文件前三列是平动后三列是转动, 到这里所有Segment的参数都设置好了 步骤11 Normalise (1) 选中第一个Normalise: Write这个module然后设置参数如下 Deformation Field选择Dependency中的Segment: Forward Deformations也就是上一步在Deformation中选择了Forward之后生成的文件如下面第一张图,{1}),{1},val。
substruct(.。
{3}, .。
., .。
)warning(sprintf(No anat file found for %s,{1})。
