end2end_restingfMRI.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. function tests = end2end_restingfMRI
  2. % End-to-end test for resting dataset
  3. %__________________________________________________________________________
  4. % Copyright (C) 2018 Wellcome Centre for Human Neuroimaging
  5. % $Id: end2end_restingfMRI.m 7481 2018-11-09 15:36:57Z peter $
  6. tests = functiontests(localfunctions);
  7. % -------------------------------------------------------------------------
  8. function setupOnce(testCase)
  9. % Prepare path
  10. data_path = get_data_path();
  11. rest_path = fullfile(data_path,'spdcm');
  12. if exist(rest_path,'file')
  13. % Clear out existing analysis files
  14. P = spm_select('FPList', fullfile(rest_path,'glm'),'.*');
  15. for i = 1:size(P,1)
  16. spm_unlink(P(i,:));
  17. end
  18. P = spm_select('FPList', fullfile(rest_path,'glm_corrected'),'.*');
  19. for i = 1:size(P,1)
  20. spm_unlink(P(i,:));
  21. end
  22. else
  23. mkdir(rest_path);
  24. end
  25. % Download dataset
  26. spm_get_dataset('spm', 'spdcm', '', data_path);
  27. % -------------------------------------------------------------------------
  28. function test_CSD(testCase)
  29. spm('defaults','fmri')
  30. % Run GLM
  31. run_glm();
  32. % Specify DCMs
  33. specify_dcms();
  34. dcmmat = fullfile(get_data_path(),'spdcm','glm_corrected','DCM_full.mat');
  35. % Estimate single DCM
  36. clear matlabbatch;
  37. matlabbatch{1}.spm.dcm.estimate.dcms.subj.dcmmat = cellstr(dcmmat);
  38. matlabbatch{1}.spm.dcm.estimate.output.separate = struct([]);
  39. matlabbatch{1}.spm.dcm.estimate.est_type = 3;
  40. matlabbatch{1}.spm.dcm.estimate.fmri.analysis = 'csd';
  41. spm_jobman('run',matlabbatch);
  42. % Load created DCM
  43. DCM = load(dcmmat);
  44. DCM = DCM.DCM;
  45. % Check model fit (full model)
  46. DCM = spm_dcm_fmri_check(DCM,true);
  47. exp_var = DCM.diagnostics(1);
  48. max_A = DCM.diagnostics(2);
  49. testCase.assertTrue(exp_var > 95);
  50. testCase.assertTrue(max_A > 1);
  51. % -------------------------------------------------------------------------
  52. function run_glm()
  53. % Runs GLM and VOI extraction
  54. data_path = fullfile(get_data_path(), 'spdcm');
  55. % Initialise SPM
  56. spm('Defaults','fMRI');
  57. spm_jobman('initcfg');
  58. spm_get_defaults('cmdline',1);
  59. f = spm_select('FPList', fullfile(data_path,'func'), '^sw.*\.img$');
  60. RT = 2;
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. % INITIAL GLM FOR EXTRACTING WM / CSF REGRESSORS
  63. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  64. glmdir = fullfile(data_path,'glm');
  65. if ~exist(glmdir,'file'), mkdir(glmdir); end
  66. clear matlabbatch;
  67. % SPM specification
  68. matlabbatch{1}.spm.stats.fmri_spec.dir = cellstr(glmdir);
  69. matlabbatch{1}.spm.stats.fmri_spec.timing.units = 'scans';
  70. matlabbatch{1}.spm.stats.fmri_spec.timing.RT = RT;
  71. matlabbatch{1}.spm.stats.fmri_spec.sess.scans = cellstr(f);
  72. % SPM estimation
  73. matlabbatch{2}.spm.stats.fmri_est.spmmat = cellstr(fullfile(glmdir,'SPM.mat'));
  74. % ROI extraction
  75. matlabbatch{3}.spm.util.voi.spmmat = cellstr(fullfile(glmdir,'SPM.mat'));
  76. matlabbatch{3}.spm.util.voi.adjust = NaN;
  77. matlabbatch{3}.spm.util.voi.session = 1;
  78. matlabbatch{3}.spm.util.voi.name = 'CSF';
  79. matlabbatch{3}.spm.util.voi.roi{1}.sphere.centre = [ 0 -40 -5];
  80. matlabbatch{3}.spm.util.voi.roi{1}.sphere.radius = 6;
  81. matlabbatch{3}.spm.util.voi.roi{1}.sphere.move.fixed = 1;
  82. matlabbatch{3}.spm.util.voi.roi{2}.mask.image = cellstr(fullfile(glmdir,'mask.nii'));
  83. matlabbatch{3}.spm.util.voi.expression = 'i1 & i2';
  84. matlabbatch{4} = matlabbatch{3};
  85. matlabbatch{4}.spm.util.voi.name = 'WM';
  86. matlabbatch{4}.spm.util.voi.roi{1}.sphere.centre = [0 -24 -33];
  87. spm_jobman('run',matlabbatch);
  88. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  89. % SECOND GLM INCLUDING WM / CSF REGRESSORS
  90. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91. glmdir = fullfile(data_path,'glm_corrected');
  92. if ~exist(glmdir,'file'), mkdir(glmdir); end
  93. clear matlabbatch;
  94. % SPM specification
  95. matlabbatch{1}.spm.stats.fmri_spec.dir = cellstr(glmdir);
  96. matlabbatch{1}.spm.stats.fmri_spec.timing.units = 'scans';
  97. matlabbatch{1}.spm.stats.fmri_spec.timing.RT = RT;
  98. matlabbatch{1}.spm.stats.fmri_spec.sess.scans = cellstr(f);
  99. matlabbatch{1}.spm.stats.fmri_spec.sess.multi_reg = {
  100. fullfile(data_path,'glm','rp_rest0000.txt'),...
  101. fullfile(data_path,'glm','VOI_CSF_1.mat'),...
  102. fullfile(data_path,'glm','VOI_WM_1.mat'),...
  103. }';
  104. % SPM estimation
  105. matlabbatch{2}.spm.stats.fmri_est.spmmat = cellstr(fullfile(glmdir,'SPM.mat'));
  106. % ROI extraction
  107. matlabbatch{3}.spm.util.voi.spmmat = cellstr(fullfile(glmdir,'SPM.mat'));
  108. matlabbatch{3}.spm.util.voi.adjust = NaN;
  109. matlabbatch{3}.spm.util.voi.session = 1;
  110. matlabbatch{3}.spm.util.voi.name = 'PCC';
  111. matlabbatch{3}.spm.util.voi.roi{1}.sphere.centre = [0 -52 26];
  112. matlabbatch{3}.spm.util.voi.roi{1}.sphere.radius = 8;
  113. matlabbatch{3}.spm.util.voi.roi{2}.mask.image = cellstr(fullfile(glmdir,'mask.nii'));
  114. matlabbatch{3}.spm.util.voi.expression = 'i1 & i2';
  115. matlabbatch{4} = matlabbatch{3};
  116. matlabbatch{4}.spm.util.voi.name = 'mPFC';
  117. matlabbatch{4}.spm.util.voi.roi{1}.sphere.centre = [3 54 -2];
  118. matlabbatch{5} = matlabbatch{3};
  119. matlabbatch{5}.spm.util.voi.name = 'LIPC';
  120. matlabbatch{5}.spm.util.voi.roi{1}.sphere.centre = [-50 -63 32];
  121. matlabbatch{6} = matlabbatch{3};
  122. matlabbatch{6}.spm.util.voi.name = 'RIPC';
  123. matlabbatch{6}.spm.util.voi.roi{1}.sphere.centre = [48 -69 35];
  124. spm_jobman('run',matlabbatch);
  125. % -------------------------------------------------------------------------
  126. function specify_dcms()
  127. % SPM
  128. glm_dir = fullfile(get_data_path(),'spdcm','glm_corrected');
  129. SPM = fullfile(glm_dir,'SPM.mat');
  130. % VOIs
  131. xY = {fullfile(glm_dir,'VOI_PCC_1.mat');
  132. fullfile(glm_dir,'VOI_mPFC_1.mat');
  133. fullfile(glm_dir,'VOI_LIPC_1.mat');
  134. fullfile(glm_dir,'VOI_RIPC_1.mat')};
  135. n = 4; % Num regions
  136. nu = 1; % Num inputs
  137. % Connectivity matrices
  138. a = ones(n,n);
  139. b = zeros(n,n,nu);
  140. c = zeros(n,nu);
  141. d = zeros(n,n,0);
  142. % Specify model 1 (full)
  143. s = struct();
  144. s.name = 'full';
  145. s.u = [1 1 1]';
  146. s.delays = repmat(2,1,n);
  147. s.TE = 0.04;
  148. s.nonlinear = false;
  149. s.two_state = false;
  150. s.stochastic = false;
  151. s.centre = false;
  152. s.induced = 1;
  153. s.a = a;
  154. s.b = b;
  155. s.c = c;
  156. s.d = d;
  157. spm_dcm_specify(SPM,xY,s);
  158. % -------------------------------------------------------------------------
  159. function data_path = get_data_path()
  160. data_path = fullfile( spm('Dir'), 'tests', ...
  161. 'output');