Skip to content

Instantly share code, notes, and snippets.

@fladd
Last active September 23, 2020 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fladd/90abfe7f5a058d9d338e5a7f201673a0 to your computer and use it in GitHub Desktop.
Save fladd/90abfe7f5a058d9d338e5a7f201673a0 to your computer and use it in GitHub Desktop.
Differences in GLM modelling approaches between SPM and FSL

Script demonstrating differences in GLM modelling approaches between SPM and FSL:

import numpy as np
from nipy.modalities.fmri.glm import GeneralLinearModel


# Simulated data (20 'rest' volumes followed by 20 'task' volumes)
Y = np.hstack((np.random.normal(4200, 50, size=20),
               np.random.normal(4300, 50, size=20)))

# SPM-style GLM
X = np.array([[0] * 20 + [1] * 20,         # model 'task'
              [1] * 40]).T                 # + constant
model = GeneralLinearModel(X)
model.fit(Y)                               # fit model to data
con = model.contrast(np.array([1, 0]))     # test for task effect
print(f"SPM: {con.effect[0,0]:.2f} (z = {con.z_score()[0]:.2f})")

# FSL-style GLM
Y -= np.mean(Y)                            # demean signal
X = np.array([[0.0] * 20 + [1.0] * 20]).T  # model 'task' only (no constant)
X -= np.mean(X, axis=0)                    # demean model
model = GeneralLinearModel(X)
model.fit(Y)                               # fit demeaned model to demeaned data
con = model.contrast(np.array([1]))        # test for task effect
print(f"FSL: {con.effect[0,0]:.2f} (z = {con.z_score()[0]:.2f})")

Output:

SPM: 121.71 (z = 6.51)
FSL: 121.71 (z = 6.60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment