SPM12--batch批量预处理数据

实现的代码如下:

code.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
type={'1' '2' '3' '4' '5' '6' '7' '8' '9' '10'};

for i=1:length(type)
    sub = i;


nrun = 1; % enter the number of runs here
jobfile = {'C:\Users\xuwhe\Desktop\pre_fmri\files\batch\code_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

code_job.m

%-----------------------------------------------------------------------
% Job saved on 31-Jul-2018 16:26:14 by cfg_util (rev $Rev: 6460 $)
% spm SPM - SPM12 (6906)
% cfg_basicio BasicIO - Unknown
%-----------------------------------------------------------------------

global type sub

inputpath=['C:\Users\xuwhe\Desktop\pre_fmri\data_batch\1dicom\' num2str(sub)];
outputpath =['C:\Users\xuwhe\Desktop\pre_fmri\data_batch\1dicom\' num2str(sub) '\output'];

%选取raw data
pathname1=[inputpath '\0002\'];
sdir1=dir([pathname1,'*.IMA']);%选取IMA
for i=1:length(sdir1)
    imgfile1{i,1}=[pathname1 sdir1(i).name];
end

%选取raw data
pathname2=[inputpath '\0003\'];
sdir2=dir([pathname2,'*.IMA']);%选取IMA
for i=1:length(sdir2)
    imgfile2{i,1}=[pathname2 sdir2(i).name];
end

%选取raw data
pathname3=[inputpath '\0004\'];
sdir3=dir([pathname3,'*.IMA']);%选取IMA
for i=1:length(sdir3)
    imgfile3{i,1}=[pathname3 sdir3(i).name];
end

%选取raw data
pathnameT1=[inputpath '\0005\'];
sdirT1=dir([pathnameT1,'*.IMA']);%选取IMA
for i=1:length(sdirT1)
    imgfileT1{i,1}=[pathnameT1 sdirT1(i).name];
end


%%
matlabbatch{1}.spm.util.import.dicom.data = imgfile1
%%
matlabbatch{1}.spm.util.import.dicom.root = 'flat';
matlabbatch{1}.spm.util.import.dicom.outdir = {[outputpath '\RUN1\']};
matlabbatch{1}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{1}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{1}.spm.util.import.dicom.convopts.icedims = 0;
%%
matlabbatch{2}.spm.util.import.dicom.data = imgfile2
%%
matlabbatch{2}.spm.util.import.dicom.root = 'flat';
matlabbatch{2}.spm.util.import.dicom.outdir = {[outputpath '\RUN2\']};
matlabbatch{2}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{2}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{2}.spm.util.import.dicom.convopts.icedims = 0;
%%
matlabbatch{3}.spm.util.import.dicom.data = imgfile3
%%
matlabbatch{3}.spm.util.import.dicom.root = 'flat';
matlabbatch{3}.spm.util.import.dicom.outdir = {[outputpath '\RUN3\']};
matlabbatch{3}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{3}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{3}.spm.util.import.dicom.convopts.icedims = 0;
%%
matlabbatch{4}.spm.util.import.dicom.data = imgfileT1
%%
matlabbatch{4}.spm.util.import.dicom.root = 'flat';
matlabbatch{4}.spm.util.import.dicom.outdir = {[outputpath '\T1\']};
matlabbatch{4}.spm.util.import.dicom.protfilter = '.*';
matlabbatch{4}.spm.util.import.dicom.convopts.format = 'nii';
matlabbatch{4}.spm.util.import.dicom.convopts.icedims = 0;
matlabbatch{5}.spm.temporal.st.scans{1}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{5}.spm.temporal.st.scans{2}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{5}.spm.temporal.st.scans{3}(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{5}.spm.temporal.st.nslices = 33;
matlabbatch{5}.spm.temporal.st.tr = 2;
matlabbatch{5}.spm.temporal.st.ta = 1.93939393939394;
matlabbatch{5}.spm.temporal.st.so = [1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32];
matlabbatch{5}.spm.temporal.st.refslice = 33;
matlabbatch{5}.spm.temporal.st.prefix = 'a';
matlabbatch{6}.spm.spatial.realign.estwrite.data{1}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 1)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{6}.spm.spatial.realign.estwrite.data{2}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 2)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{2}, '.','files'));
matlabbatch{6}.spm.spatial.realign.estwrite.data{3}(1) = cfg_dep('Slice Timing: Slice Timing Corr. Images (Sess 3)', substruct('.','val', '{}',{5}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{3}, '.','files'));
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.sep = 4;
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.interp = 2;
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
matlabbatch{6}.spm.spatial.realign.estwrite.eoptions.weight = '';
matlabbatch{6}.spm.spatial.realign.estwrite.roptions.which = [2 1];
matlabbatch{6}.spm.spatial.realign.estwrite.roptions.interp = 4;
matlabbatch{6}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
matlabbatch{6}.spm.spatial.realign.estwrite.roptions.mask = 1;
matlabbatch{6}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
matlabbatch{7}.spm.spatial.coreg.estimate.ref(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{7}.spm.spatial.coreg.estimate.source(1) = cfg_dep('Realign: Estimate & Reslice: Mean Image', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean'));
matlabbatch{7}.spm.spatial.coreg.estimate.other(1) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 1)', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{1}, '.','rfiles'));
matlabbatch{7}.spm.spatial.coreg.estimate.other(2) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 2)', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{2}, '.','rfiles'));
matlabbatch{7}.spm.spatial.coreg.estimate.other(3) = cfg_dep('Realign: Estimate & Reslice: Resliced Images (Sess 3)', substruct('.','val', '{}',{6}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{3}, '.','rfiles'));
matlabbatch{7}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
matlabbatch{7}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
matlabbatch{7}.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{7}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
matlabbatch{8}.spm.tools.oldnorm.estwrite.subj.source(1) = cfg_dep('DICOM Import: Converted Images', substruct('.','val', '{}',{4}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','files'));
matlabbatch{8}.spm.tools.oldnorm.estwrite.subj.wtsrc = '';
matlabbatch{8}.spm.tools.oldnorm.estwrite.subj.resample(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{7}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles'));
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.template = {'D:\software\neuroscience\Matlab2016b\toolbox\spm12\toolbox\OldNorm\T1.nii,1'};
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.weight = '';
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.smosrc = 8;
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.smoref = 0;
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.regtype = 'mni';
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.cutoff = 25;
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.nits = 16;
matlabbatch{8}.spm.tools.oldnorm.estwrite.eoptions.reg = 1;
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.preserve = 0;
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.bb = [-78 -112 -70
                                                         78 76 85];
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.vox = [2 2 2];
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.interp = 1;
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.wrap = [0 0 0];
matlabbatch{8}.spm.tools.oldnorm.estwrite.roptions.prefix = 'w';
matlabbatch{9}.spm.spatial.smooth.data(1) = cfg_dep('Old Normalise: Estimate & Write: Normalised Images (Subj 1)', substruct('.','val', '{}',{8}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
matlabbatch{9}.spm.spatial.smooth.fwhm = [8 8 8];
matlabbatch{9}.spm.spatial.smooth.dtype = 0;
matlabbatch{9}.spm.spatial.smooth.im = 0;
matlabbatch{9}.spm.spatial.smooth.prefix = 's';

注意事项:以上代码是基于我的文件结构,因为这批数据有三个session,所以有三个RUN文件夹。具体的用SPM12生成batch会在另一篇博文继续更新。

image.png
image.png
image.png

参考网址:


20180731

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念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

推荐阅读更多精彩内容