SPM12批量处理预处理数据

作者: Galory | 来源:发表于2018-09-02 10:33 被阅读0次

    之前写过一篇https://www.jianshu.com/p/8f16cbbfd962,当时只是初步学会,还未熟练应用。
    经过几次实践,现总结下通用的流程。
    从一个问题出发:
    问题:我想计算海马体积、内嗅皮层体积和内嗅皮层厚度等等。
    思路:经调研,使用软件CAT12和FreeSurfer都可以处理,但FreeSurfer更加耗时,而且之前我也总结过CAT12与Freesurfer的对比https://github.com/Galory/daily-paper-neuroscience/blob/master/2018/08/29.md。决定选择使用CAT12。

    过程:
    1.先用一个人的数据保存一个对所有人的数据选择好要进行的处理,如下图:

    image.png
    Tips:如果下一步的数据需要用到上一步的结果,当你选择时会有一个Dependency供你选择,如下图:
    image.png

    2.选择File->Save batch and script来保存一个对待处理的数据通用的batch文件,如下图,我这里保存为batch.mbatch_job.m

    image.png

    3.编辑batch.mbatch_job.m这两个文件,使用一个循环一次性把这些数据处理完。

    于是在原本的batch.m文件里,添加如下的代码实现循环:

    % By - Galory  Email - 996377370a@gmail.com
    
    % List of open inputs
    
    global sub type
    % My files are named as 1 2 3 4 5 6 7 8 9 10 11 ... 24
    type={'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24'};
    
    for i=1:length(type)
        sub = i;
    end
    

    解释一下,上边length(type)就是要循环的次数,之后再把原来的代码放入这个for循环中的sub = i;end之间。


    batch_job.m文件里添加如下代码:

    global sub
    
    inputpath=['C:/Users/xuwhe/Desktop/t1/' num2str(sub)];
    
    %选取raw data
    pathname1=[inputpath '/'];
    sdir1=dir([pathname1,'*.nii']);%select .nii file
    for i=1:length(sdir1)
        imgfile1{i,1}=[pathname1 sdir1(i).name];
    end
    
    

    注意这里是根据自己的文件夹结构来设置,比如我这里是这样的:


    image.png

    会发现我的文件布局结构是所有数据都在C:/Users/xuwhe/Desktop/t1/路径下,因为提前给每个人编好了号,每个人对应一个数字的文件夹,这里就是1、2、3......24;而在这每个数字命名的文件夹之下就是待处理的.nii文件,这里采用的是通配符*.nii选取nii文件。


    修改完毕,逐一核对一下该设置路径的位置有没有正确读取,可以先拿一两个人测试。
    之后运行batch_job.m就可以了。


    这里附上完整代码:
    batch.m


    % By - Galory  Email - 996377370a@gmail.com
    
    % List of open inputs
    
    global sub type
    % My files are named as 1 2 3 4 5 6 7 8 9 10 11 ... 24
    type={'1' '2' '3' '4' '5' '6' '7' '8' '9' '10' '11' '12' '13' '14' '15' '16' '17' '18' '19' '20' '21' '22' '23' '24'};
    
    for i=1:length(type)
        sub = i;
    
    
    nrun = 1; % enter the number of runs here
    jobfile = {'C:/Users/xuwhe/Desktop/t/batch_job.m'};
    jobs = repmat(jobfile, 1, nrun);
    inputs = cell(0, nrun);
    for crun = 1:nrun
    end
    spm('defaults','fmri');
    spm_jobman('run',jobs,inputs{:});
    end
    

    batch_job.m:

    %-----------------------------------------------------------------------
    % Job saved on 30-Aug-2018 23:46:58 by cfg_util (rev $Rev: 6460 $)
    % spm SPM - SPM12 (6906)
    % cfg_basicio BasicIO - Unknown
    %-----------------------------------------------------------------------
    
    global sub
    
    inputpath=['C:/Users/xuwhe/Desktop/t1/' num2str(sub)];
    
    %选取raw data
    pathname1=[inputpath '/'];
    sdir1=dir([pathname1,'*.nii']);%select .nii file
    for i=1:length(sdir1)
        imgfile1{i,1}=[pathname1 sdir1(i).name];
    end
    
    matlabbatch{1}.spm.tools.cat.estwrite.data = imgfile1;
    matlabbatch{1}.spm.tools.cat.estwrite.nproc = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.opts.tpm = {'D:/software/neuroscience/Matlab2016b/toolbox/spm12/tpm/TPM.nii'};
    matlabbatch{1}.spm.tools.cat.estwrite.opts.affreg = 'mni';
    matlabbatch{1}.spm.tools.cat.estwrite.opts.biasstr = 0.5;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.APP = 1070;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.LASstr = 0.5;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.gcutstr = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.registration.darteltpm = {'D:/software/neuroscience/Matlab2016b/toolbox/spm12/toolbox/cat12/templates_1.50mm/Template_1_IXI555_MNI152.nii'};
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.registration.shootingtpm = {'D:/software/neuroscience/Matlab2016b/toolbox/spm12/toolbox/cat12/templates_1.50mm/Template_0_IXI555_MNI152_GS.nii'};
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.registration.regstr = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.vox = 1.5;
    matlabbatch{1}.spm.tools.cat.estwrite.extopts.restypes.fixed = [1 0.1];
    matlabbatch{1}.spm.tools.cat.estwrite.output.surface = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.ROImenu.atlases.neuromorphometrics = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.ROImenu.atlases.lpba40 = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.ROImenu.atlases.cobra = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.ROImenu.atlases.hammers = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.GM.native = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.output.GM.mod = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.GM.dartel = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.output.WM.native = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.output.WM.mod = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.WM.dartel = 0;
    matlabbatch{1}.spm.tools.cat.estwrite.output.bias.warped = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.jacobian.warped = 1;
    matlabbatch{1}.spm.tools.cat.estwrite.output.warps = [0 0];
    matlabbatch{2}.spm.spatial.smooth.data(1) = cfg_dep('CAT12: Segmentation: mwp1 Image', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','tiss', '()',{1}, '.','mwp', '()',{':'}));
    matlabbatch{2}.spm.spatial.smooth.fwhm = [8 8 8];
    matlabbatch{2}.spm.spatial.smooth.dtype = 0;
    matlabbatch{2}.spm.spatial.smooth.im = 0;
    matlabbatch{2}.spm.spatial.smooth.prefix = 's';
    matlabbatch{3}.spm.tools.cat.tools.calcvol.data_xml(1) = cfg_dep('CAT12: Segmentation: CAT Report', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','catreport', '()',{':'}));
    matlabbatch{3}.spm.tools.cat.tools.calcvol.calcvol_TIV = 1;
    matlabbatch{3}.spm.tools.cat.tools.calcvol.calcvol_name = 'TIV.txt';
    matlabbatch{4}.spm.tools.cat.stools.surfresamp.data_surf(1) = cfg_dep('CAT12: Segmentation: Left Thickness', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','lhthickness', '()',{':'}));
    matlabbatch{4}.spm.tools.cat.stools.surfresamp.merge_hemi = 1;
    matlabbatch{4}.spm.tools.cat.stools.surfresamp.mesh32k = 1;
    matlabbatch{4}.spm.tools.cat.stools.surfresamp.fwhm_surf = 15;
    matlabbatch{4}.spm.tools.cat.stools.surfresamp.nproc = 0;
    matlabbatch{5}.spm.tools.cat.stools.surfextract.data_surf(1) = cfg_dep('CAT12: Segmentation: Left Central Surface', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','lhcentral', '()',{':'}));
    matlabbatch{5}.spm.tools.cat.stools.surfextract.GI = 1;
    matlabbatch{5}.spm.tools.cat.stools.surfextract.FD = 0;
    matlabbatch{5}.spm.tools.cat.stools.surfextract.SD = 1;
    matlabbatch{5}.spm.tools.cat.stools.surfextract.nproc = 0;
    matlabbatch{6}.spm.tools.cat.stools.surf2roi.cdata{1}(1) = cfg_dep('CAT12: Segmentation: Left Thickness', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','lhthickness', '()',{':'}));
    
    

    编辑于20180902

    相关文章

      网友评论

        本文标题:SPM12批量处理预处理数据

        本文链接:https://www.haomeiwen.com/subject/ronpwftx.html