First-level analysis with SPM12

SPM orientation

SPM12 is software written in Matlab. It's one of the most popular packages for analyzing neuroimaging data (the top 3 are SPM, FSL, and AFNI), and the developers at UCL have made it free for everyone to use.
You can do most everything in two ways: By clicking buttons in the graphic interface (GUI), and by coding. SPM functions generally start with spm_<something>. The GUI is a great way to get oriented, but when you run analyses for research--often on many participants--and want to have a reproducible, tracked workflow, coding is essential. We'll start with the GUI for now, but we'll include a few code examples along the way.
Starting SPM brings up the graphics windows:
spm fmri
___ ____ __ __ / __)( _ \( \/ ) \__ \ )___/ ) ( Statistical Parametric Mapping (___/(__) (_/\/\_) SPM12 - SPM12: v6906 16:34:04 - 05/05/2021 ======================================================================== Initialising SPM : ..Item 'Confounds', field 'strtype': Value must be a valid strtype. Item 'Confounds', field 'val': Size mismatch (required [1 100], present [1 1]). Item 'Condition Labels', field 'strtype': Value must be a valid strtype. Item 'Condition Labels', field 'val': Size mismatch (required [1 100], present [1 1]). Duplicate application tag in applications 'SPM' and 'SPM'. Duplicate application tag in applications 'BasicIO' and 'BasicIO'. .
Warning: JAVACOMPONENT will be removed in a future release. For more information see UI Alternatives for MATLAB Apps on
done SPM present working directory: /Users/torwager/Dropbox (Dartmouth College)/COURSES/Courses_Dartmouth/2021_3_Spring_fMRI_Class/Labs_and_Lectures_for_Instructors/Labs_and_assignments/Lab_5/Pinel_data_sample_subject

Prepare data and variables

Setting up a first-level analysis: Ingredients

We're setting up for the first-level analysis. We need five things:
1) Brain data. The functional fMRI data, preprocessed and ready for analysis (usually, realigned, warped to MNI space, and smoothed)
2) Task information. Onset times, durations, and names for each event type (condition) of interest
3) Nuisance covariates. Any custom nuisance covariates we want to add, including
4) Filter. An intended HP filter cutoff in sec
- SPM will add the high-pass filter when it does the analysis
5) Contrast weights for effects (comparisons across conditions) we care about

Find and examine the brain data

Navigate to files

It's helpful to start in a folder with data in it, so you don't have to navigate extensively through the SPM file selection browser.
pwd % where am i now? (in terms of working directory, at least)
ans = '/Users/torwager/Dropbox (Dartmouth College)/COURSES/Courses_Dartmouth/2021_3_Spring_fMRI_Class/Labs_and_Lectures_for_Instructors/Labs_and_assignments/Lab_5/Pinel_data_sample_subject_INSTRUCTOR'
Use cd( ) to go to your sample subject directory. Drag and drop, or copy and paste the path from Terminal.
cd('/Users/torwager/Dropbox (Dartmouth College)/COURSES/Courses_Dartmouth/2021_3_Spring_fMRI_Class/Labs_and_Lectures_for_Instructors/Labs_and_assignments/Lab_5/Pinel_data_sample_subject_INSTRUCTOR')
ls % list files here
SPM_onsets_and_nuisance_regressors.mat c1sub-sid001567_acq-MPRAGE_T1w.nii c2sub-sid001567_acq-MPRAGE_T1w.nii c3sub-sid001567_acq-MPRAGE_T1w.nii c4sub-sid001567_acq-MPRAGE_T1w.nii c5sub-sid001567_acq-MPRAGE_T1w.nii canlab_robust_reg_model1 mwc1sub-sid001567_acq-MPRAGE_T1w.nii mwc2sub-sid001567_acq-MPRAGE_T1w.nii mwc3sub-sid001567_acq-MPRAGE_T1w.nii rp_sub-sid001567_task-pinel_acq-s1p2_run-03_bold.txt rsub-sid001567_task-pinel_acq-s1p2_run-03_bold.mat rsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii rsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii.gz rsub-sid001567_task-pinel_acq-s1p2_run-03_bold_reorient.mat spm_model1 sub-sid001567_acq-MPRAGE_T1w.nii sub-sid001567_acq-MPRAGE_T1w.nii.gz sub-sid001567_acq-MPRAGE_T1w_reorient.mat sub-sid001567_acq-MPRAGE_T1w_seg8.mat sub-sid001567_task-pinel_acq-s1p2_run-03_bold.json sub-sid001567_task-pinel_acq-s1p2_run-03_bold.mat sub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii sub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii.gz sub-sid001567_task-pinel_acq-s1p2_run-03_events.tsv swrsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii wrsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii wrsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii.gz y_sub-sid001567_acq-MPRAGE_T1w.nii

Find the functional run and examine the data

It's always important to look at your data! Check: Do the brains look like brains? is the range of data values what you expected? Are there crazy outliers? Does the image space match the template space? Are the images relatively homogenous, or do some appear to be really different, i.e., on the same scale?
We'll use CANlab object-oriented tools, specifically the fmri_data object, to load the preprocessed data and make a summary plot. Here, we want "swr*" images, which are Smoothed, Warped, and Realigned...and hopefully thus ready for analysis.
preprocessed_image_name = 'swrsub-sid001567_task-pinel_acq-s1p2_run-03_bold.nii';
% Load the images into an fmri_data object
dat = fmri_data(preprocessed_image_name);
Using default mask: /Users/torwager/Documents/GitHub/CanlabCore/CanlabCore/canlab_canonical_brains/Canonical_brains_surfaces/brainmask_canlab.nii loading mask. mapping volumes. checking that dimensions and voxel sizes of volumes are the same. Pre-allocating data array. Needed: 145720800 bytes Loading image number: 150 Elapsed time is 3.490070 seconds. Image names entered, but fullpath attribute is empty. Getting path info. .fullpath should have image name for each image column in .dat Attempting to expand image filenames in case image list is unexpanded 4-D images
% Plot the images. Save potential outliers.
[wh_outlier_uncorr, wh_outlier_corr] = plot(dat);
Calculating mahalanobis distances to identify extreme-valued images ...based on union of corr...Retained 2 components for mahalanobis distance Expected 50% of points within 50% normal ellipsoid, found 22.67% Expected 7.50 outside 95% ellipsoid, found 6 Potential outliers based on mahalanobis distance: Bonferroni corrected: 1 images Cases 1 Uncorrected: 6 images Cases 1 2 3 4 5 6 ...and cov...Retained 2 components for mahalanobis distance Expected 50% of points within 50% normal ellipsoid, found 18.67% Expected 7.50 outside 95% ellipsoid, found 6 Potential outliers based on mahalanobis distance: Bonferroni corrected: 1 images Cases 1 Uncorrected: 6 images Cases 1 2 3 4 5 6 done. Outliers: Outliers after p-value correction: Image numbers: 1 Image numbers, uncorrected: 1 2 3 4 5 6