Skip to content

Instantly share code, notes, and snippets.

@danjgale
Last active November 28, 2019 21:29
Show Gist options
  • Save danjgale/3988f424c1a86051b2aa84b429ffd84d to your computer and use it in GitHub Desktop.
Save danjgale/3988f424c1a86051b2aa84b429ffd84d to your computer and use it in GitHub Desktop.
A real-world example of a searchlight analysis with nilearn
from nilearn.decoding import Searchlight
from nilearn.datasets import load_mni152_brain_mask
from nilearn.image import new_img_like
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from skearn.svm import LinearSVC
from sklearn.model_selection import LeaveOneGroupOut
imgs = # 4D NIfTI image or list of 3D NIfTI images
y = # array-like of condition labels for each volume in `imgs`
run_labels = # array-like of run labels for each volume of `imgs`
pipeline = make_pipeline(StandardScaler(), LinearSVC())
brain_mask = load_mni152_brain_mask()
searchlight = Searchlight(mask_img=brain_mask, radius=4, estimator=pipeline,
cv=LeaveOneGroupOut())
searchlight.fit(imgs, y, groups=run_labels)
results = new_img_like(brain_mask, searclight.scores_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment