Skip to content

Instantly share code, notes, and snippets.

@fladd
Last active August 16, 2024 13:33
Show Gist options
  • 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