fmri学习笔记|SPM 代码 循环

这篇博客介绍了如何使用代码自动化SPM的预处理操作,特别是平滑步骤。通过SPM Batch生成脚本,然后解析生成的`temp_smooth_job`和`temp_smooth`文件。博主详细解释了`temp_smooth_job`用于设置变量,而`temp_smooth`调用这些变量执行平滑操作。文章强调了如何修改输入和for循环来适应不同被试的数据,并提供了简化和合并脚本的方法。
摘要由CSDN通过智能技术生成

SPM 用代码跑操作(上)

提前说明一下,SPM中所有的操作都可以通过代码批量跑,逻辑都是一样的,下面举个最简单的例子,说一下整体的操作。
举例:假设需要做预处理中smooth的操作,共20个被试。

第一步生成脚本

我们一般操作都是通过SPM batch 窗口通过点点点进行需要的处理。
图一:X表示需要手动添加的内容,针对哪些image做后续的smooth。
图二:选择300个files。
图三:生成脚本,FIle<- save batch and script
图一
图一
在这里插入图片描述
图二
在这里插入图片描述
图三

第二步 看一下生成的脚本结构

生成脚本后会生成两个文件,temp_smooth 和 temp_smooth_job
在这里插入图片描述

temp_smooth_job

可以理解为temp_smooth_job文件是添加变量,比如要smooth哪些images,路径信息,平滑的一些参数,都是对变量进行赋值。
补充一下:这个赋值是将所有信息放到matlabbatch这个变量里,该变量是struct结构嵌套,如果想更好的理解,可以去学习了解一下Matlab中struct的数据类型的一些知识。
temp_smooth_job.m也可以直接跑,测试一下变量的赋值,只涉及变量赋值,不涉及SPM的操作。如下图
脚本内容 可以看到和batch窗口中信息是一一对应的
在这里插入图片描述
在这里插入图片描述
生成变量在这里插入图片描述
再补充一下SPM12 manual对job的解释
在这里插入图片描述

temp_smooth

先按照spm manual里说的方式解释一下怎么用代码跑。
temp_smooth是调用temp_smooth_job,跑smooth操作。
脚本内容
只解释用到的
L2跑循环的次数——这里举例有20个被试,nrun = 20
L3将temp_smooth_job的路径信息赋值进jobfile变量中
L4将jobfile信息进一步放到jobs中。
L6 输入信息,这里指job文件里要跑的image files(300个),跑循环就补充L6之后的内容,将每个被试的image files作为input
for crun = 1:nrun
% Named Directory Selector: Directory - cfg_files
inputs{1, crun} = MATLAB_CODE_TO_FILL_INPUT;
% Realign: Estimate & Reslice: Session - cfg_files
inputs{2, crun} = MATLAB_CODE_TO_FILL_INPUT;
% Coreg: Estimate: Source Image - cfg_files
inputs{3, crun} = MATLAB_CODE_TO_FILL_INPUT;

end
L9 跑smooth的操作,前面都算是准备步骤
在这里插入图片描述
补充一下SPM manual里的说明
Complete and run a pre-specified job
spm_jobman(’run’, job[, input1, input2 …])
This interface takes a job and asks for the input to any open configuration items one after another. If a list of appropriate inputs is supplied, these will be filled in. After all inputs are filled, the job will be run. Note that only items without a pre-set value will be filled (marked with <-X in the GUI). To force a item to to be filled, use “Edit:Clear Value” in the GUI or set its value to ’’ in the harvested job.
The job argument is very flexible, it can e.g. be a job variable, the name of a script creating a job variable, even a cell list of any mixture of variables and scripts. All job snippets found will be concatenated into a single job, the missing inputs will be filled and the resulting job will be run.
The batch system can generate a script skeleton for any loaded job. From the batch GUI, this feature is accessible via “File:Save Batch and Script”. This skeleton consists of a commented list of necessary inputs, a for loop to enter inputs for multiple runs or subjects and the code to initialise and run the job. An example is available in face_single_subject_script.m:

% List of open inputs
% Named Directory Selector: Directory - cfg_files
% Realign: Estimate & Reslice: Session - cfg_files
% Coreg: Estimate: Source Image - cfg_files
% fMRI model specification: Multiple conditions - cfg_files
nrun = X; % enter the number of runs here
jobfile = {fullfile(spm(’dir’),’man’,’batch’,’face_single_subject_template.m’)};
jobs = repmat(jobfile, 1, nrun);
inputs = cell(4, nrun);
for crun = 1:nrun
% Named Directory Selector: Directory - cfg_files
inputs{1, crun} = MATLAB_CODE_TO_FILL_INPUT;
% Realign: Estimate & Reslice: Session - cfg_files
inputs{2, crun} = MATLAB_CODE_TO_FILL_INPUT;
% Coreg: Estimate: Source Image - cfg_files
inputs{3, crun} = MATLAB_CODE_TO_FILL_INPUT;
% fMRI model specification: Multiple conditions - cfg_files
inputs{4, crun} = MATLAB_CODE_TO_FILL_INPUT;
end
spm(’defaults’,’fmri’);
spm_jobman(’run’,jobs,inputs{:});

The skeleton needs to be adapted to the actual data layout by adding MATLAB code which specifies the number of runs and the input data in the for loop.
Another example script and batch is available for the multimodal dataset, called multimodal_fmri_script.m and multimodal_fmri_template.m.
【SPM12 manual page494】

SPM 用代码跑操作(下)

到目前为止都是在理解层面,先看懂代码大概的意思,下面开始根据我们的需求去改代码
可以看到,按照spm manual给的代码示例可以跑多个被试的循环,但是每个被试的输入需要一个个写到for循环中的input里,也就是写将20个被试的images一个个赋值到input{ }。
所以为了简化这一步,写个循环读images,循环每跑一次,读取下一个被试的数据即可,不全部在循环中列出来。

下面记录怎么修改输入input和for循环

1 读被试文件夹

文件夹格式 001,002…020。
在这里插入图片描述

data_p
  • 5
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值