# Processing fMRI Data with SPM

**SPM Processing Steps**

Tutorial videos (extremely helpful): [https://www.youtube.com/playlist?list=PLIQIswOrUH689KpRPCa5-h6U-m9CddWM6](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)

1. 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
2. Visual check of data
   1. Display select anatomical image
      1. Check for Gibbs Ringing Artifacts – looks like ripples in a pond
      2. Other abnormalities
   2. Display select a single functional image
      1. Check for extremely light or dark spots, image distortions
      2. Can also check for movement
         1. Check reg
         2. Select first volume of functional file
         3. Once loaded, right click on image, select rest of volumes
         4. Run through images by clicking > button in bottom corner
3. Realignment – align individual volumes together
   1. Estimate and re-slice
      1. Generally leave defaults as they are, including num passes (register to mean)
   2. Data field new session (these refer to the functional runs, same number of sessions as same number of functional runs)
   3. To add to session, double click on it
   4. Navigate to subject’s functional folder
   5. 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)
   6. In frame, write 1:180 (or 1:n, n being however many volumes each run has)
   7. Right click, select all, click done
   8. Repeat procedure for all sessions (all runs)
   9. Press green “play” button at top of batch editor to run realignment
   10. When it’s finished, it will generate motion graph showing how much subject moved in all dimensions
       1. Remove runs in which subject moved more than size of voxel (2 mm)
4. Slice Timing Correction
   1. This is mandatory if you are doing DCM, but you CAN skip it
   2. Slice Timing
   3. Data add sessions (same number as runs)
   4. Click on a session, navigate to functional folder – you’ll see results of realignment here, indicated by “r” prefix on data file
   5. Select volumes for realigned single run (using filter and volumes commands)
   6. Do the same procedure for remaining sessions
   7. Double click on number of slices, enter value corresponding to number of slices taken (for blindfMRI study, this was 69)
   8. Enter TR
   9. Enter TA following formula in help window TR-(TR/#slices)
   10. Slice order manually enter slice times previously generated
   11. Slice time enter as time point (i.e. 0)
   12. Leave filename as is
5. Co-register
   1. Co-register (register and re-slice)
      1. Reference image image that remains stationary, representative of functional data
      2. Source image moved around, structural image
   2. Double click on the reference image, navigate to functional folder, select mean functional image (should only be one, for first run)
   3. Double click on source image, navigate to anatomical folder, select anatomical scan
   4. Click green go button
6. Segment
   1. Segment anatomical image in order to align the different tissue types when normalized
   2. Click segment button
   3. 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)
   4. Save bias corrected – change from save nothing to save bias corrected
   5. Change deformation fields to forward
   6. Click green go button
7. Normalize
   1. Select Normalize (write)
   2. Click on Data field
   3. Create new subject
   4. Select deformation field you created in last step in anatomical folder
      1. y\_####
   5. images to write – select all of realigned and slice-time corrected images (ar prefix files in functional folder, all volumes)
   6. Check output – click check reg
      1. Select one of functional volumes that has “w” prefix
      2. Navigate to spm/canonical and select any of the avg structural images
8. Smoothing
   1. Click on smooth button
   2. Click on Images to Smooth
   3. Select warped functional images
      1. All volumes
      2. All runs
   4. Leave other defaults ar
   5. Click green go button
   6. Use check reg button to check
9. Batching
   1. Create pre-processing batch for each subject
   2. Click “Batch”
      1. Click BasicIO Fire/Dir Operations File Operations Named File Selector
         1. Input Name – give name appropriate for study
         2. E.G. for a study with 5 runs of functional data, run1run2run3run4run5files
         3. Files sets
         4. 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)
         5. For each file set, click empty <-X button and then specify
         6. Navigate to functional directory for first subject
         7. Select first run .nii file
         8. Fill remaining file sets with remaining run .nii files
      2. Click SPM Spatial Realign Estimate & Reslice
         1. Data
            1. Create enough sessions for study (i.e. same number of sessions as number of runs
            2. Click <-X button and then dependency
            3. Select named file selector: inputname(1) -files
            4. Fill in remaining sessions with remaining files
         2. Check defaults – num passes should be register to mean
      3. Slice Timing Correction – confirm if this needs to be done for your experiment. Mandatory for DCM
         1. SPM Temporal Slice Timing
         2. Add correct number of data sessions
         3. Click <-X button, select dependency
         4. Select input to be output from Realign step – Estimate and Realign, Resliced Images
         5. Set number of slices (experiment specific)
         6. Set TR and TA (experiment specific)
         7. Slice Order and Reference Slice
            1. Input slice times (experiment specific)
            2. Set reference slice to 0 (experiment specific)
      4. Co-register – estimate and reslice
         1. SPM Spatial Coregister Estimate and Reslice
            1. Reference image – dependency, output of Realign: Estimate and Reslice: Mean Image
            2. Source Image = subject’s anatomical scan
      5. Segment
         1. SPM Spatial Segment
            1. Under volumes, select Dependency, and then select the output from CoregisterL Estimate & Reslice, Coregistered Images
            2. Save bias corrected ON
            3. Deformation fields = forward
      6. Normalize: Write
         1. SPM Spatial Normalize Write
            1. Deformation field = dependency, Segment: Forward Deformations
            2. Images to Write

Dependency, Slice Timing: Slice Timing Corr. Images (all outputs)

*
  *
    1. Smooth
       1. SPM Spatial Smooth
          1. Images to write = dependency, Normalize: Write: Normalized Images
  * Save Batch for each subject within Subject’s folder

**First level**

1. Make First Level directories within each subject folder
2. In fMRI SPM, click Specify 1st-Level
   1. Directory = set to first subject’s first level directory
3. Timing Parameters
   1. Units for design = seconds
   2. Interscan interval = TR
   3. Leave other defaults
4. Data and Design
   1. Create sessions for each run of functional data
   2. 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.
   3. Under conditions, add correct number of conditions (experiment specific)
      1. Name each condition
      2. For onsets, copy and paste condition onsets
         1. 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.
      3. Set durations (can be taken from onset document)
      4. Repeat for remaining conditions
   4. 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”.
   5. Leave high-pass filter at default of 128 unless specific experiment requires otherwise
   6. Complete for remaining sessions (runs)
      1. Load in next run scans, create conditions, set timing onsets and durations, load movement parameter file as multiple regressor, etc.
5. Leave defaults at bottom of module as is (unless specified by experiment)
6. Save batch file into first-level directory for each subject.
7. Cluck green Go button
8. Once completed, click Estimate
   1. Select SPM mat file – this will be the .spm file created in the previous step, located within the first-level analysis directory
   2. Write residuals = YES
   3. Hit Green Go button

**Contrasts**

1. 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.
2. 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).
   1. This opens the contrast manager
   2. Click Define New Contrast
   3. Give appropriate name that describes contrast (i.e. Condition1, Condition1-Condition2)
   4. Contrast weights
      1. The condition of interest is positive (i.e. 1)
      2. The condition you want to compare against is negative (-1)
      3. The conditions you aren’t interested in are given 0
      4. Contrast has to sum to 0, except when looking at single condition alone
      5. Value is weighted according to number of sessions (i.e. one session = 1, 5 sessions = 0.2)
      6. Example: Condition1, Condition2, Condition3
         1. Condition1 (alone)
            1. 1 0 0
         2. Condition1-Condition2
            1. 1 -1 0
         3. Condition3-Condition1
            1. -1 0 1
         4. Condition2 -Condition1+Condition3
            1. -1 2 -1
      7. Contrasts for multiple sessions
         1. Repeat contrast setup according to number of sessions
         2. Example: three sessions, three conditions, contrast = Condition1-Condition2
            1. 0.33 -0.33 0 0.33 -0.33 0 0.33 -0.33 0
3. This is SUPER TEDIOUS and prone to human error, better to set it up using the Batch Contrast Manager
4. Batch SPM Stats Contrast Manager
   1. Create contrasts for each subject
   2. Select SPM.mat file created during First Level analysis
   3. Contrast Sessions – create sessions for however many contrasts you want
      1. Select T-Contrast (or F-Contrast if that’s appropriate for your study)
      2. Name each contrast appropriately
      3. Weights Vector
         1. Set up weights like you would in the examples above EXCEPT that contrast weight vectors will be padded with zeros to the correct length
            1. EG for 6 condition experiment, comparing first two only (Con1-Con2)

1 -1 (you don’t need to enter remaining zeros)

*
  *
    1. Replicate over sessions – this replicates contrasts over all sessions (runs) for participant
       1. Select replicate\&scale – this replicates and scales vector weight according to number of sessions (runs)
       2. 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**

1. Create Second Level Directory – one for each group (if you have multiple groups)
   1. Within second level, create subfolders for each contrast
2. In SPM menu, select Specify Second Level
3. Set directory to subfolder for first contrast
4. Design = one-sample t-test (for within group)
5. Scans – select all images for first contrast
   1. Navigate to each subject directory and select con\_0001 file that was created during first-level analysis
6. Hit green Go button
7. Click Estimate
   1. Select SPM.mat file created in Second Level Contrast subfolder
   2. Write residuals = no
8. Click Results in SPM menu
   1. This will open contrast manager
   2. Define new contrast
   3. Name contrast (whatever the contrast was)
   4. Enter vector weight of 1
   5. Click done
      1. This open results menu – advance through options as appropriate (apply masking (usually none), p value adjustment (none), threshold (0.001), extent threshold (0)
      2. These value can be changed depending on experimental considerations
9. To compare groups – i.e. you have two groups and want to make a contrast comparing a condition between your groups
   1. Create subfolder for group comparison and specific contrast subfolders within that subfolder
   2. Select Specify 2nd Level
   3. Set directory to subfolder for first contrast
   4. Design
      1. Select two-sample t-test
   5. 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.
   6. Leave remaining defaults as is
   7. Hit green Go Button
   8. Make group contrast
      1. Click Results
      2. Select SPM.mat file from subfolder created in previous step
      3. Select Define new contrast
      4. Name Contrast (i.e. Group1Cond1-Group2Cond1)
      5. Enter vector weights= 1 -1
      6. Click done
      7. Do same for reverse contrast
         1. Group2Cond1-Group1Cond1)
         2. Vector weights= -1 1
      8. Click done
   9. Double click contrast to see results (go through settings in Results menu as above)
