SPM12--batch批量預(yù)處理數(shù)據(jù)

實(shí)現(xiàn)的代碼如下:

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';

注意事項(xiàng):以上代碼是基于我的文件結(jié)構(gòu),因?yàn)檫@批數(shù)據(jù)有三個(gè)session疑苫,所以有三個(gè)RUN文件夾熏版。具體的用SPM12生成batch會(huì)在另一篇博文繼續(xù)更新。

image.png
image.png
image.png

參考網(wǎng)址:


20180731

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末捍掺,一起剝皮案震驚了整個(gè)濱河市撼短,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌挺勿,老刑警劉巖曲横,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異不瓶,居然都是意外死亡禾嫉,警方通過查閱死者的電腦和手機(jī)灾杰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來夭织,“玉大人吭露,你說我怎么就攤上這事∽鸲瑁” “怎么了讲竿?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)弄屡。 經(jīng)常有香客問我题禀,道長(zhǎng),這世上最難降的妖魔是什么膀捷? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任迈嘹,我火速辦了婚禮,結(jié)果婚禮上全庸,老公的妹妹穿的比我還像新娘秀仲。我一直安慰自己,他們只是感情好壶笼,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布神僵。 她就那樣靜靜地躺著,像睡著了一般覆劈。 火紅的嫁衣襯著肌膚如雪保礼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天责语,我揣著相機(jī)與錄音炮障,去河邊找鬼。 笑死坤候,一個(gè)胖子當(dāng)著我的面吹牛胁赢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播白筹,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼智末,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了遍蟋?” 一聲冷哼從身側(cè)響起吹害,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎虚青,沒想到半個(gè)月后它呀,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年纵穿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了下隧。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谓媒,死狀恐怖淆院,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情句惯,我是刑警寧澤土辩,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布,位于F島的核電站抢野,受9級(jí)特大地震影響拷淘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜指孤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一启涯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧恃轩,春花似錦结洼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至昧互,卻和暖如春挽铁,著一層夾襖步出監(jiān)牢的瞬間伟桅,已是汗流浹背敞掘。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留楣铁,地道東北人玖雁。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像盖腕,于是被迫代替她去往敵國(guó)和親赫冬。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容