Preprocess.m 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. % Preprocess.m
  2. %
  3. % This funciton preprocesses data of the studyforrest data set. It searches in
  4. % the provided directory for the anatomy and ses-movie/func directories.
  5. %
  6. % input:
  7. % dir - directory of the subject to preprocess (if empty processes the current directory)
  8. % runId - the run index to process (varies from 1 to 8)
  9. function Preprocess(dir, runId)
  10. if (~isempty(dir) && ~strcmp(dir(end),'/'))
  11. dir = [dir '/'];
  12. end
  13. curDir = '';
  14. if (~strcmp(dir(1),'/'))
  15. curDir = pwd;
  16. curDir = [curDir '/'];
  17. end
  18. numOfVolumes = 451;
  19. anatomyScan = [curDir dir 'anatomy/highres001.nii,1'];
  20. scansDir = [curDir dir 'ses-movie/func/'];
  21. regex = [num2str(runId) '_bold.nii'];
  22. scans = spm_select('ExtFPList', scansDir, regex, 1:numOfVolumes);
  23. assert(size(scans,1) <= numOfVolumes, 'Detected more files than asked. Check if file is already preprocessed.');
  24. if (size(scans,1) == 0)
  25. warning(['Could not find any scans for directory ' dir ' and run ID ' num2str(runId)]);
  26. return;
  27. end
  28. % Realign - Estimate and Write (mean)
  29. matlabbatch{1}.spm.spatial.realign.estwrite.data{1} = cellstr(scans);
  30. %%
  31. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.quality = 0.9;
  32. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.sep = 4;
  33. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.fwhm = 5;
  34. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.rtm = 1;
  35. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.interp = 2;
  36. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.wrap = [0 0 0];
  37. matlabbatch{1}.spm.spatial.realign.estwrite.eoptions.weight = '';
  38. matlabbatch{1}.spm.spatial.realign.estwrite.roptions.which = [0 1];
  39. matlabbatch{1}.spm.spatial.realign.estwrite.roptions.interp = 4;
  40. matlabbatch{1}.spm.spatial.realign.estwrite.roptions.wrap = [0 0 0];
  41. matlabbatch{1}.spm.spatial.realign.estwrite.roptions.mask = 1;
  42. matlabbatch{1}.spm.spatial.realign.estwrite.roptions.prefix = 'r';
  43. % Coregister - Estimate
  44. matlabbatch{2}.spm.spatial.coreg.estimate.ref = cellstr(anatomyScan);
  45. matlabbatch{2}.spm.spatial.coreg.estimate.source(1) = cfg_dep('Realign: Estimate & Reslice: Mean Image', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','rmean'));
  46. matlabbatch{2}.spm.spatial.coreg.estimate.other(1) = cfg_dep('Realign: Estimate & Reslice: Realigned Images (Sess 1)', substruct('.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','sess', '()',{1}, '.','cfiles'));
  47. matlabbatch{2}.spm.spatial.coreg.estimate.eoptions.cost_fun = 'nmi';
  48. matlabbatch{2}.spm.spatial.coreg.estimate.eoptions.sep = [4 2];
  49. matlabbatch{2}.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];
  50. matlabbatch{2}.spm.spatial.coreg.estimate.eoptions.fwhm = [7 7];
  51. % Normalize - Estimate and write
  52. matlabbatch{3}.spm.spatial.normalise.estwrite.subj.vol = cellstr(anatomyScan);
  53. matlabbatch{3}.spm.spatial.normalise.estwrite.subj.resample(1) = cfg_dep('Coregister: Estimate: Coregistered Images', substruct('.','val', '{}',{2}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('.','cfiles'));
  54. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.biasreg = 0.0001;
  55. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.biasfwhm = 60;
  56. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.tpm = {'/home/ga58sux/workspace/spm/spm12/tpm/TPM.nii'};
  57. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.affreg = 'mni';
  58. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.reg = [0 0.001 0.5 0.05 0.2];
  59. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.fwhm = 0;
  60. matlabbatch{3}.spm.spatial.normalise.estwrite.eoptions.samp = 3;
  61. matlabbatch{3}.spm.spatial.normalise.estwrite.woptions.bb = [-78 -112 -70
  62. 78 76 85];
  63. matlabbatch{3}.spm.spatial.normalise.estwrite.woptions.vox = [3 3 3];
  64. matlabbatch{3}.spm.spatial.normalise.estwrite.woptions.interp = 4;
  65. matlabbatch{3}.spm.spatial.normalise.estwrite.woptions.prefix = 'w';
  66. matlabbatch{4}.spm.spatial.smooth.data(1) = cfg_dep('Normalise: Estimate & Write: Normalised Images (Subj 1)', substruct('.','val', '{}',{3}, '.','val', '{}',{1}, '.','val', '{}',{1}, '.','val', '{}',{1}), substruct('()',{1}, '.','files'));
  67. % Smooth data
  68. matlabbatch{4}.spm.spatial.smooth.fwhm = [8 8 8];
  69. matlabbatch{4}.spm.spatial.smooth.dtype = 0;
  70. matlabbatch{4}.spm.spatial.smooth.im = 0;
  71. matlabbatch{4}.spm.spatial.smooth.prefix = 's';
  72. % run created matlab batch
  73. spm_jobman('run', matlabbatch);
  74. end