目录

  • SPM 用代码跑操作(上)
    • 第一步生成脚本
    • 第二步 看一下生成的脚本结构
      • temp_smooth_job
      • temp_smooth
  • SPM 用代码跑操作(下)
    • 下面记录怎么修改输入input和for循环
      • 1 读被试文件夹
      • 2 读取文件夹下的images
    • 合并temp_smooth_job和temp_smooth
  • 后续笔记/小tip

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_path='/Volumes/TOSHIBA EXT/analysis format';
parti_list = dir(data_path);
% 被试文件夹路径
parti_name ={};
for list = 1:length(parti_list)if parti_list(list).name(1) == '0' #文件夹第一个字母为0的,因为dir会列出._之类的路径信息,我们只要被试文件夹parti_name(list,1) ={parti_list(list).name};end
end
parti_name(cellfun(@isempty,parti_name))=[]; %精炼 被试名字 20*1

2 读取文件夹下的images

每个被试有三个任务(三个run)

单次循环时,读取run1里面的images
1、定位到001被试的run1文件夹
2、读取前缀为w的images,用来做smooth平滑

for i= 1:20
parti_path = [data_path, ['/' ,parti_name{i}]]; #被试001
x = spm_select('FPList',[parti_path,['/',run_name{1}]],'^w.*');#run_name{1} 可以直接改成run的命名,但是我的run1名字太长,所以写了代码让其保存到run_name{1}的位置。
temp_x = [];
for hdr = 1:length(x)if contains(x(hdr,:),"hdr") == 0 #只读取img后缀的图像,如果包含‘hdr’,略过,如果是nii后缀,直接读取所有含nii的文件即可。
temp_x = [temp_x;[x(hdr,:),',1']];end
end
x = temp_x;
matlabbatch{1}.spm.spatial.smooth.data = cellstr(x);
.....
end

注:x1 好像直接读取nii/img后缀的图像即可,但是,spm自动生成的脚本中
scans = { ‘…001…/…task_run3…/ra_005_001.img,1’},最后是’…img,1’,为了保持一致,所以x1 多写了个循环。
最后将x (300*1)赋值到smooth.data。

单次循环读取images后,将_job.m 里的相关参数设置放到该循环下即可。
那么下面对这个进行简化,并将_job.m 和.m 文件合并。

合并temp_smooth_job和temp_smooth

在(上)中可以看到,temp_smooth的主要作用是调用job和input进行SPM相关操作。
spm_jobman(’run’, job[, input1, input2 …])
This interface takes a job and asks for the input to any open configuration items one after another.
那么合并两个文件夹就直接将该语句添加到temp_smooth_job.m 文件最后。
需要修改原始代码,如下。这时相同的smooth操作不需要run/运行temp_smooth脚本,而直接在temp_smooth_job脚本中运行即可。合并到一起是为了方便循环修改输入。
这一步先改后改都一样

spm('defaults','FMRI');
spm_jobman('run',matlabbatch); #spm_jobman('run', jobs, inputs{:});

最后附上完整代码

%-----------------------------------------------------------------------
% Job saved on 03-Feb-2021 11:38:16 by cfg_util (rev $Rev: 7345 $)
% spm SPM - SPM12 (7771)
% cfg_basicio BasicIO - Unknown
%-----------------------------------------------------------------------
%%
clc
clear
data_path='/Volumes/TOSHIBA EXT/analysis format';
parti_list = dir(data_path);
% 被试文件夹路径
parti_name ={};
for list = 1:length(parti_list)if parti_list(list).name(1) == '0'parti_name(list,1) ={parti_list(list).name};end
end
parti_name(cellfun(@isempty,parti_name))=[]; %精炼 被试名字 20*1%%
for i = 1:20
% 每个被试的run 三个子文件夹
run_list = {};
run_name = {};
parti_path = [data_path, ['/' ,parti_name{i}]];
run_path = dir([data_path, ['/' ,parti_name{i}]]);
for r = 1:length(run_path)if run_path(r).name(1) == '5'if contains(run_path(r).name,'run')run_name(r,1) ={run_path(r).name};endend
end
run_name(cellfun(@isempty,run_name))=[];x = spm_select('FPList',[parti_path,['/',run_name{1}]],'^w.*');
temp_x = [];
for hdr = 1:length(x)if contains(x(hdr,:),"hdr") == 0 %只读取img后缀的图像,如果包含‘hdr’,略过,如果是nii后缀,直接读取所有含nii的文件即可。
temp_x = [temp_x;[x(hdr,:),',1']];end
end
x = temp_x;
matlabbatch{1}.spm.spatial.smooth.data = cellstr(x);%%
matlabbatch{1}.spm.spatial.smooth.fwhm = [8 8 8];
matlabbatch{1}.spm.spatial.smooth.dtype = 0;
matlabbatch{1}.spm.spatial.smooth.im = 0;
matlabbatch{1}.spm.spatial.smooth.prefix = 's';spm('defaults','FMRI');
spm_jobman('run',matlabbatch); %spm_jobman('run', jobs, inputs{:});
end

补充参考:数据分析总述 写的挺清楚 可以阅读看看
https://max.book118.com/html/2018/0714/8056006064001115.shtm

后续笔记/小tip

1、修改contrast的脚本,报错weights vector[1,Inf] – [3,36]etc,类似报错是说正确输入要求一维向量,最大的可能是Ftest 和 Ttest原因。Ttest矩阵写在同一行,但是Ftest不是,修改脚本的时候如果想直接修改contrast条件,需要注意是否在Ttest相关变量上赋值Ftest矩阵,这就会导致维度不符。
解决:替换 tcon —— fcon(其他同理)

fmri学习笔记|SPM 代码 循环相关推荐

  1. python里while的用法_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  2. python中while的用法_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  3. (实验55)单片机,STM32F4学习笔记,代码讲解【网络通信实验】【正点原子】【原创】

    文章目录 其它文章链接,独家吐血整理 实验现象 主程序 LWIP初始化程序 代码讲解 其它文章链接,独家吐血整理 (实验3)单片机,STM32F4学习笔记,代码讲解[按键输入实验][正点原子][原创] ...

  4. autoit学习笔记---“While…WEnd”循环

    autoit学习笔记---"While-WEnd"循环 (2012-03-26 16:10:58) 标签: 杂谈 分类: 学习笔记之autoit "While-WEnd& ...

  5. while用法python_Python学习笔记之While循环用法分析

    本文实例讲述了Python学习笔记之While循环用法.分享给大家供大家参考,具体如下: 前面一篇<Python学习笔记之For循环用法>详细介绍了Python for循环,这里再来讲述一 ...

  6. (实验4)单片机,STM32F4学习笔记,代码讲解【串口实验】【正点原子】【原创】

    文章目录 其它文章链接,独家吐血整理 实验现象 主程序 串口中断程序 代码讲解 其它文章链接,独家吐血整理 (实验3)单片机,STM32F4学习笔记,代码讲解[按键输入实验][正点原子][原创] (实 ...

  7. openCV4.0 C++ 快速入门30讲学习笔记(自用 代码+注释)详细版

    课程来源:哔哩哔哩 环境:OpenCV4.5.1 + VS2019 目录 002.图像色彩空间转换 003.图像对象的创建与赋值 004.图像像素的读写操作 005.图像像素的算术操作(加减乘除4种不 ...

  8. (实验50)单片机,STM32F4学习笔记,代码讲解【串口IAP实验】【正点原子】【原创】

    文章目录 ❤2023重新理解记录 其它文章链接,独家吐血整理 实验现象 主程序 IAP初始化程序 代码讲解 文章目录 ❤2023重新理解记录 其它文章链接,独家吐血整理 实验现象 主程序 IAP初始化 ...

  9. (实验38)单片机,STM32F4学习笔记,代码讲解【SD卡实验】【正点原子】【原创】

    文章目录 其它文章链接,独家吐血整理 实验现象 主程序 SD卡驱动程序 代码讲解 其它文章链接,独家吐血整理 (实验3)单片机,STM32F4学习笔记,代码讲解[按键输入实验][正点原子][原创] ( ...

  10. (实验39)单片机,STM32F4学习笔记,代码讲解【FATFS实验】【正点原子】【原创】

    文章目录 其它文章链接,独家吐血整理 实验现象 主程序 FATFS初始化程序 代码讲解 其它文章链接,独家吐血整理 (实验3)单片机,STM32F4学习笔记,代码讲解[按键输入实验][正点原子][原创 ...

最新文章

  1. c# mysql fill_C#里sqlDataAdapter.fill(DataSet,String)的用法
  2. Python单元测试之pytest
  3. TCP协议抓包分析 -- wireshark
  4. 「C++」C++ Primer Plus 笔记:第十七章 输入、输出和文件
  5. 深入理解Spark 2.1 Core (四):运算结果处理和容错的原理与源码分析
  6. Siamese网络(孪生神经网络)详解
  7. Cloudera Manager agent无法启动,拒绝链接 Failed! trying again in 2 second(s): [Errno 111] Connection refuse
  8. 锁底层之内存屏障与原语指令
  9. QT5+ROS程序开发
  10. 以两种异步模型应用案例,深度解析Future接口
  11. ICON素材|装饰图标设计的技巧
  12. 高性能JavaScript(您值得一看)
  13. 拓扑排序:如何确定代码源文件的编译依赖关系
  14. SpringBoot使用RestTemplate 摘要认证
  15. 地址后面的sessionid怎么消除_富贵包的消除和改善头前倾,通过运动和减肥可以吗?...
  16. java 按照笔画排序,怎样用java把名单按姓氏笔画排序
  17. 基于android的手机订票系统设计,基于Android的火车票预订系统的设计与实现.doc
  18. 解决小程序view之间默认的空隙
  19. python中return返回值怎么累加_Python学习笔记函数之返回值和return语句
  20. 计算机录音机应用程序在哪,win10电脑自带录音在哪里打开

热门文章

  1. SQL Server 开发指南
  2. 奥比中光再度携手英伟达联合举办第三届3D视觉创新应用竞赛
  3. 3分钟用C语言教你写个‘浪漫烟花‘---特别漂亮
  4. 2018河南省第十一届ACM省赛之旅。。。
  5. 想跳槽涨薪的必看,Java就业指导(1)
  6. 【转】[中级]我对『PID算法』的理解 —— 原理介绍
  7. 程序员斗图专用表情包,做技术群里最靓的仔!
  8. ginapi服务器性能,基于gin web框架搭建RESTful API服务
  9. 业务需求——Excel转 Json 以及相关优化
  10. linux系统漏洞测试过程,Linux下bash破壳漏洞检测方法