认知神经科学Improve Yourselves

SPM12--batch批量预处理数据

2018-07-31  本文已影响0人  Galory

实现的代码如下:

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

上一篇下一篇

猜你喜欢

热点阅读