|
| 1 | +""" |
| 2 | +This batch script analyses the Auditory fMRI dataset available from the |
| 3 | +SPM website: |
| 4 | + http://www.fil.ion.ucl.ac.uk/spm/data/auditory/ |
| 5 | +as described in the SPM manual: |
| 6 | + http://www.fil.ion.ucl.ac.uk/spm/doc/manual.pdf#Chap:data:auditory |
| 7 | +________________________________________________________________________ |
| 8 | +Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging |
| 9 | +
|
| 10 | +Matlab: Guillaume Flandin |
| 11 | +Python: Yael Balbastre |
| 12 | +""" |
| 13 | + |
| 14 | +import zipfile |
| 15 | +import os.path as op |
| 16 | +import spm |
| 17 | + |
| 18 | +num = spm.Array.from_any |
| 19 | + |
| 20 | +# Directory containing the Auditory data |
| 21 | +# ---------------------------------------------------------------------- |
| 22 | + |
| 23 | +url = 'https://www.fil.ion.ucl.ac.uk/spm/download/data/MoAEpilot/MoAEpilot.zip' |
| 24 | +data_path = op.dirname(__file__) |
| 25 | +zip_path = op.join(data_path, 'MoAEpilot.zip') |
| 26 | + |
| 27 | +print(f'{"Downloading Auditory dataset...":<40s}') |
| 28 | +spm.Runtime.call("urlwrite", url, zip_path) |
| 29 | +with zipfile.ZipFile(zip_path, 'r') as f: |
| 30 | + f.extractall(data_path) |
| 31 | +print(f" {'...done':30s}\n") |
| 32 | + |
| 33 | + |
| 34 | +# Initialise SPM |
| 35 | +# ---------------------------------------------------------------------- |
| 36 | +spm.spm('Defaults', 'fMRI') |
| 37 | +spm.spm_jobman('initcfg', nargout=0) |
| 38 | +# spm.spm_get_defaults('cmdline', true) |
| 39 | + |
| 40 | +# ====================================================================== |
| 41 | +# PREAMBLE: DUMMY SCANS |
| 42 | +# ====================================================================== |
| 43 | + |
| 44 | +f = spm.spm_select('FPList', op.join(data_path, 'fM00223'), r'^f.*\.img$') |
| 45 | + |
| 46 | +matlabbatch = spm.Cell() |
| 47 | + |
| 48 | +matlabbatch(0).cfg_basicio.file_dir.dir_ops.cfg_mkdir.parent = [data_path] |
| 49 | +matlabbatch(0).cfg_basicio.file_dir.dir_ops.cfg_mkdir.name = 'dummy' |
| 50 | + |
| 51 | +matlabbatch(0).cfg_basicio.file_dir.file_ops.file_move.files = f[:12] |
| 52 | +matlabbatch(0).cfg_basicio.file_dir.file_ops.file_move.action.moveto = [op.join(data_path, 'dummy')] |
| 53 | + |
| 54 | +spm.spm_jobman('run', matlabbatch, nargout=0) |
| 55 | + |
| 56 | +# ====================================================================== |
| 57 | +# SPATIAL PREPROCESSING |
| 58 | +# ====================================================================== |
| 59 | + |
| 60 | +f = spm.spm_select('FPList', op.join(data_path,'fM00223'), r'^f.*\.img$') |
| 61 | +a = spm.spm_select('FPList', op.join(data_path,'sM00223'), r'^s.*\.img$') |
| 62 | + |
| 63 | +matlabbatch = spm.Cell() |
| 64 | + |
| 65 | +# Realign |
| 66 | +# ---------------------------------------------------------------------- |
| 67 | +matlabbatch(0).spm.spatial.realign.estwrite.data = [f] |
| 68 | +matlabbatch(0).spm.spatial.realign.estwrite.roptions.which = num([0, 1]) |
| 69 | + |
| 70 | +# Coregister |
| 71 | +# ---------------------------------------------------------------------- |
| 72 | +matlabbatch(1).spm.spatial.coreg.estimate.ref = [spm.spm_file(f[0, 0], 'prefix', 'mean')] |
| 73 | +matlabbatch(1).spm.spatial.coreg.estimate.source = [a] |
| 74 | + |
| 75 | +# Segment |
| 76 | +# ---------------------------------------------------------------------- |
| 77 | +matlabbatch(2).spm.spatial.preproc.channel.vols = [a] |
| 78 | +matlabbatch(2).spm.spatial.preproc.channel.write = num([0, 1]) |
| 79 | +matlabbatch(2).spm.spatial.preproc.warp.write = num([0, 1]) |
| 80 | + |
| 81 | +# Normalise: Write |
| 82 | +# ---------------------------------------------------------------------- |
| 83 | +matlabbatch(3).spm.spatial.normalise.write.subj["def"] = [spm.spm_file(a, 'prefix', 'y_', 'ext', 'nii')] |
| 84 | +matlabbatch(3).spm.spatial.normalise.write.subj.resample = f |
| 85 | +matlabbatch(3).spm.spatial.normalise.write.woptions.vox = num([3, 3, 3]) |
| 86 | + |
| 87 | +matlabbatch(4).spm.spatial.normalise.write.subj["def"] = [spm.spm_file(a, 'prefix', 'y_', 'ext', 'nii')] |
| 88 | +matlabbatch(4).spm.spatial.normalise.write.subj.resample = [spm.spm_file(a, 'prefix', 'm', 'ext', 'nii')] |
| 89 | +matlabbatch(4).spm.spatial.normalise.write.woptions.vox = num([1, 1, 3]) |
| 90 | + |
| 91 | +# Smooth |
| 92 | +# ---------------------------------------------------------------------- |
| 93 | +matlabbatch(5).spm.spatial.smooth.data = spm.spm_file(f, 'prefix', 'w')[:, None] |
| 94 | +matlabbatch(5).spm.spatial.smooth.fwhm = num([6, 6, 6]) |
| 95 | + |
| 96 | +spm.spm_jobman('run', matlabbatch, nargout=0) |
| 97 | + |
| 98 | +# ====================================================================== |
| 99 | +# GLM SPECIFICATION, ESTIMATION, INFERENCE, RESULTS |
| 100 | +# ====================================================================== |
| 101 | + |
| 102 | +f = spm.spm_select('FPList', op.join(data_path,'fM00223'), r'^swf.*\.img$') |
| 103 | + |
| 104 | +matlabbatch = spm.Cell() |
| 105 | + |
| 106 | +# Output Directory |
| 107 | +# ---------------------------------------------------------------------- |
| 108 | +matlabbatch(0).cfg_basicio.file_dir.dir_ops.cfg_mkdir.parent = [data_path] |
| 109 | +matlabbatch(0).cfg_basicio.file_dir.dir_ops.cfg_mkdir.name = 'GLM' |
| 110 | + |
| 111 | +# Model Specification |
| 112 | +# ---------------------------------------------------------------------- |
| 113 | +matlabbatch(1).spm.stats.fmri_spec.dir = [op.join(data_path, 'GLM')] |
| 114 | +matlabbatch(1).spm.stats.fmri_spec.timing.units = 'scans' |
| 115 | +matlabbatch(1).spm.stats.fmri_spec.timing.RT = 7 |
| 116 | +matlabbatch(1).spm.stats.fmri_spec.sess.scans = f |
| 117 | +matlabbatch(1).spm.stats.fmri_spec.sess.cond.name = 'active' |
| 118 | +matlabbatch(1).spm.stats.fmri_spec.sess.cond.onset = num(range(6, 86, 12)) |
| 119 | +matlabbatch(1).spm.stats.fmri_spec.sess.cond.duration = 6 |
| 120 | + |
| 121 | +# Model Estimation |
| 122 | +# ---------------------------------------------------------------------- |
| 123 | +matlabbatch(2).spm.stats.fmri_est.spmmat = [op.join(data_path, 'GLM', 'SPM.mat')] |
| 124 | + |
| 125 | +# Contrasts |
| 126 | +# ---------------------------------------------------------------------- |
| 127 | +matlabbatch(3).spm.stats.con.spmmat = [op.join(data_path, 'GLM', 'SPM.mat')] |
| 128 | +matlabbatch(3).spm.stats.con.consess(0).tcon.name = 'Listening > Rest' |
| 129 | +matlabbatch(3).spm.stats.con.consess(0).tcon.weights = num([1, 0]) |
| 130 | +matlabbatch(3).spm.stats.con.consess(1).tcon.name = 'Rest > Listening' |
| 131 | +matlabbatch(3).spm.stats.con.consess(1).tcon.weights = num([-1, 0]) |
| 132 | + |
| 133 | +# Inference Results |
| 134 | +# ---------------------------------------------------------------------- |
| 135 | +matlabbatch(4).spm.stats.results.spmmat = [op.join(data_path, 'GLM', 'SPM.mat')] |
| 136 | +matlabbatch(4).spm.stats.results.conspec.contrasts = 1 |
| 137 | +matlabbatch(4).spm.stats.results.conspec.threshdesc = 'FWE' |
| 138 | +matlabbatch(4).spm.stats.results.conspec.thresh = 0.05 |
| 139 | +matlabbatch(4).spm.stats.results.conspec.extent = 0 |
| 140 | +matlabbatch(4).spm.stats.results.print = False |
| 141 | + |
| 142 | +# Rendering |
| 143 | +# ---------------------------------------------------------------------- |
| 144 | +matlabbatch(5).spm.util.render.display.rendfile = [op.join(spm.spm('Dir'), 'canonical', 'cortex_20484.surf.gii')] |
| 145 | +matlabbatch(5).spm.util.render.display.conspec.spmmat = [op.join(data_path, 'GLM', 'SPM.mat')] |
| 146 | +matlabbatch(5).spm.util.render.display.conspec.contrasts = 1 |
| 147 | +matlabbatch(5).spm.util.render.display.conspec.threshdesc = 'FWE' |
| 148 | +matlabbatch(5).spm.util.render.display.conspec.thresh = 0.05 |
| 149 | +matlabbatch(5).spm.util.render.display.conspec.extent = 0 |
| 150 | + |
| 151 | +spm.spm_jobman('run', matlabbatch, nargout=0) |
0 commit comments