SPM12批量处理预处理数据

之前写过一篇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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容

  • 不够强大才会犹豫 陷入了一个又一个的怪圈中,想要跳出的时候发现自己如同第一次下水时那样无从着力。听到那些长辈的...
    没有故事的cime阅读 272评论 0 0
  • 原文来自:https://dkvirus.gitbooks.io/-npm/content/di-si-zhang...
    神秘者007阅读 441评论 0 0