Processing fMRI Data with SPM
SPM Processing Steps
Tutorial videos (extremely helpful): https://www.youtube.com/playlist?list=PLIQIswOrUH689KpRPCa5-h6U-m9CddWM6
Pre-Processing
Note: Pre-processing steps can be done individually, but setting up all steps together as a batch is much easier (see end of document for batching instructions)
Install MatLab and SPM if not already done, launch Matlab, add SPM folder to path, type spm into matlab command line (which should launch SPM), open the fMRI GUI
Visual check of data
Display select anatomical image
Check for Gibbs Ringing Artifacts – looks like ripples in a pond
Other abnormalities
Display select a single functional image
Check for extremely light or dark spots, image distortions
Can also check for movement
Check reg
Select first volume of functional file
Once loaded, right click on image, select rest of volumes
Run through images by clicking > button in bottom corner
Realignment – align individual volumes together
Estimate and re-slice
Generally leave defaults as they are, including num passes (register to mean)
Data field new session (these refer to the functional runs, same number of sessions as same number of functional runs)
To add to session, double click on it
Navigate to subject’s functional folder
In filter field, type run number. This is specific to the way the file is saved (i.e. sometimes its run1, sometimes run_1 etc.) Do this so you only select volumes for the right run (i.e. each session should only contain volumes from one run)
In frame, write 1:180 (or 1:n, n being however many volumes each run has)
Right click, select all, click done
Repeat procedure for all sessions (all runs)
Press green “play” button at top of batch editor to run realignment
When it’s finished, it will generate motion graph showing how much subject moved in all dimensions
Remove runs in which subject moved more than size of voxel (2 mm)
Slice Timing Correction
This is mandatory if you are doing DCM, but you CAN skip it
Slice Timing
Data add sessions (same number as runs)
Click on a session, navigate to functional folder – you’ll see results of realignment here, indicated by “r” prefix on data file
Select volumes for realigned single run (using filter and volumes commands)
Do the same procedure for remaining sessions
Double click on number of slices, enter value corresponding to number of slices taken (for blindfMRI study, this was 69)
Enter TR
Enter TA following formula in help window TR-(TR/#slices)
Slice order manually enter slice times previously generated
Slice time enter as time point (i.e. 0)
Leave filename as is
Co-register
Co-register (register and re-slice)
Reference image image that remains stationary, representative of functional data
Source image moved around, structural image
Double click on the reference image, navigate to functional folder, select mean functional image (should only be one, for first run)
Double click on source image, navigate to anatomical folder, select anatomical scan
Click green go button
Segment
Segment anatomical image in order to align the different tissue types when normalized
Click segment button
Double click volumes button, navigate to anatomical folder, select co-registered anatomical image (i.e. image that was created in last step, should have r pre-fix)
Save bias corrected – change from save nothing to save bias corrected
Change deformation fields to forward
Click green go button
Normalize
Select Normalize (write)
Click on Data field
Create new subject
Select deformation field you created in last step in anatomical folder
y_####
images to write – select all of realigned and slice-time corrected images (ar prefix files in functional folder, all volumes)
Check output – click check reg
Select one of functional volumes that has “w” prefix
Navigate to spm/canonical and select any of the avg structural images
Smoothing
Click on smooth button
Click on Images to Smooth
Select warped functional images
All volumes
All runs
Leave other defaults ar
Click green go button
Use check reg button to check
Batching
Create pre-processing batch for each subject
Click “Batch”
Click BasicIO Fire/Dir Operations File Operations Named File Selector
Input Name – give name appropriate for study
E.G. for a study with 5 runs of functional data, run1run2run3run4run5files
Files sets
Select File Sets and click “new file set” from bottom window until you have the correct number of file sets (i.e. file sets for each run of functional data)
For each file set, click empty <-X button and then specify
Navigate to functional directory for first subject
Select first run .nii file
Fill remaining file sets with remaining run .nii files
Click SPM Spatial Realign Estimate & Reslice
Data
Create enough sessions for study (i.e. same number of sessions as number of runs
Click <-X button and then dependency
Select named file selector: inputname(1) -files
Fill in remaining sessions with remaining files
Check defaults – num passes should be register to mean
Slice Timing Correction – confirm if this needs to be done for your experiment. Mandatory for DCM
SPM Temporal Slice Timing
Add correct number of data sessions
Click <-X button, select dependency
Select input to be output from Realign step – Estimate and Realign, Resliced Images
Set number of slices (experiment specific)
Set TR and TA (experiment specific)
Slice Order and Reference Slice
Input slice times (experiment specific)
Set reference slice to 0 (experiment specific)
Co-register – estimate and reslice
SPM Spatial Coregister Estimate and Reslice
Reference image – dependency, output of Realign: Estimate and Reslice: Mean Image
Source Image = subject’s anatomical scan
Segment
SPM Spatial Segment
Under volumes, select Dependency, and then select the output from CoregisterL Estimate & Reslice, Coregistered Images
Save bias corrected ON
Deformation fields = forward
Normalize: Write
SPM Spatial Normalize Write
Deformation field = dependency, Segment: Forward Deformations
Images to Write
Dependency, Slice Timing: Slice Timing Corr. Images (all outputs)
Smooth
SPM Spatial Smooth
Images to write = dependency, Normalize: Write: Normalized Images
Save Batch for each subject within Subject’s folder
First level
Make First Level directories within each subject folder
In fMRI SPM, click Specify 1st-Level
Directory = set to first subject’s first level directory
Timing Parameters
Units for design = seconds
Interscan interval = TR
Leave other defaults
Data and Design
Create sessions for each run of functional data
Load scans – navigate to functional directory where pre-processing files for each subject are. Load all volumes of pre-processed (with prefix “swar”) run1 files.
Under conditions, add correct number of conditions (experiment specific)
Name each condition
For onsets, copy and paste condition onsets
These onset documents (for each subject, for each run, for each condition) will have to be created beforehand. The information is usually included in the csv output from the presentation program and can be extracted and organized from there. There are lab scripts available to do this.
Set durations (can be taken from onset document)
Repeat for remaining conditions
At end of session, you can add the movement parameters as a multiple regressor. SPM creates a file containing info about how much each subject moved during each run – should be in the functional folder with the rest of the preprocessing files, with a prefix “rp”.
Leave high-pass filter at default of 128 unless specific experiment requires otherwise
Complete for remaining sessions (runs)
Load in next run scans, create conditions, set timing onsets and durations, load movement parameter file as multiple regressor, etc.
Leave defaults at bottom of module as is (unless specified by experiment)
Save batch file into first-level directory for each subject.
Cluck green Go button
Once completed, click Estimate
Select SPM mat file – this will be the .spm file created in the previous step, located within the first-level analysis directory
Write residuals = YES
Hit Green Go button
Contrasts
Prior to doing the 2nd level analysis, you need to create contrasts for each subject. “Contrasts” refers to the statistical comparisons between conditions, tells you which regions in the brain show statistically significant activity in individual conditions, or conditions compared to other conditions.
You can do this by clicking Results in the SPM menu and opening the SPM.mat file that was created in the First Level subfolder for each subject (tedious for many contrasts and many subjects, batching is possible, explained in next step).
This opens the contrast manager
Click Define New Contrast
Give appropriate name that describes contrast (i.e. Condition1, Condition1-Condition2)
Contrast weights
The condition of interest is positive (i.e. 1)
The condition you want to compare against is negative (-1)
The conditions you aren’t interested in are given 0
Contrast has to sum to 0, except when looking at single condition alone
Value is weighted according to number of sessions (i.e. one session = 1, 5 sessions = 0.2)
Example: Condition1, Condition2, Condition3
Condition1 (alone)
1 0 0
Condition1-Condition2
1 -1 0
Condition3-Condition1
-1 0 1
Condition2 -Condition1+Condition3
-1 2 -1
Contrasts for multiple sessions
Repeat contrast setup according to number of sessions
Example: three sessions, three conditions, contrast = Condition1-Condition2
0.33 -0.33 0 0.33 -0.33 0 0.33 -0.33 0
This is SUPER TEDIOUS and prone to human error, better to set it up using the Batch Contrast Manager
Batch SPM Stats Contrast Manager
Create contrasts for each subject
Select SPM.mat file created during First Level analysis
Contrast Sessions – create sessions for however many contrasts you want
Select T-Contrast (or F-Contrast if that’s appropriate for your study)
Name each contrast appropriately
Weights Vector
Set up weights like you would in the examples above EXCEPT that contrast weight vectors will be padded with zeros to the correct length
EG for 6 condition experiment, comparing first two only (Con1-Con2)
1 -1 (you don’t need to enter remaining zeros)
Replicate over sessions – this replicates contrasts over all sessions (runs) for participant
Select replicate&scale – this replicates and scales vector weight according to number of sessions (runs)
You don’t need to scale vectors manually (i.e. entering weight of 0.2 for 5 sessions), it will do it for you.
Save contrast manager batch for each subject within first level subfolder
Click Green Go button
Second Level/Group Analysis
Create Second Level Directory – one for each group (if you have multiple groups)
Within second level, create subfolders for each contrast
In SPM menu, select Specify Second Level
Set directory to subfolder for first contrast
Design = one-sample t-test (for within group)
Scans – select all images for first contrast
Navigate to each subject directory and select con_0001 file that was created during first-level analysis
Hit green Go button
Click Estimate
Select SPM.mat file created in Second Level Contrast subfolder
Write residuals = no
Click Results in SPM menu
This will open contrast manager
Define new contrast
Name contrast (whatever the contrast was)
Enter vector weight of 1
Click done
This open results menu – advance through options as appropriate (apply masking (usually none), p value adjustment (none), threshold (0.001), extent threshold (0)
These value can be changed depending on experimental considerations
To compare groups – i.e. you have two groups and want to make a contrast comparing a condition between your groups
Create subfolder for group comparison and specific contrast subfolders within that subfolder
Select Specify 2nd Level
Set directory to subfolder for first contrast
Design
Select two-sample t-test
For Group 1 scans, navigate into each Group 1 subject directory and select the contrast image that is relevant to the second level contrast you are doing. For example, say you are interested in comparing activity in condition 1 between groups. You created a contrast for activity in condition 1 for each subject in the Batch Contrast Manager step above, select that contrast image for each Group 1 subject and then do the same for Group 2 scans.
Leave remaining defaults as is
Hit green Go Button
Make group contrast
Click Results
Select SPM.mat file from subfolder created in previous step
Select Define new contrast
Name Contrast (i.e. Group1Cond1-Group2Cond1)
Enter vector weights= 1 -1
Click done
Do same for reverse contrast
Group2Cond1-Group1Cond1)
Vector weights= -1 1
Click done
Double click contrast to see results (go through settings in Results menu as above)
Last updated
Was this helpful?