Skip to content

Instantly share code, notes, and snippets.

@kchawla-pi
Last active July 23, 2018 13:54
Show Gist options
  • Save kchawla-pi/bb97847bec645d3f279d5b9772cd026b to your computer and use it in GitHub Desktop.
Save kchawla-pi/bb97847bec645d3f279d5b9772cd026b to your computer and use it in GitHub Desktop.
Shows the case where nilearn.decoding.SearchLight gives an error if the cv parameter passed is a generator not an iterable. (line# 75 onwards)
"""
From
nilearn/examples/02_decoding/plot_haxby_searchlight.py
Erring code section is after Line # 75.
"""
"""
Searchlight analysis of face vs house recognition
==================================================
Searchlight analysis requires fitting a classifier a large amount of
times. As a result, it is an intrinsically slow method. In order to speed
up computing, in this example, Searchlight is run only on one slice on
the fMRI (see the generated figures).
"""
#########################################################################
# Load Haxby dataset
# -------------------
import pandas as pd
from nilearn import datasets
from nilearn.image import new_img_like, load_img
# We fetch 2nd subject from haxby datasets (which is default)
haxby_dataset = datasets.fetch_haxby()
# print basic information on the dataset
print('Anatomical nifti image (3D) is located at: %s' % haxby_dataset.mask)
print('Functional nifti image (4D) is located at: %s' % haxby_dataset.func[0])
fmri_filename = haxby_dataset.func[0]
labels = pd.read_csv(haxby_dataset.session_target[0], sep=" ")
y = labels['labels']
session = labels['chunks']
#########################################################################
# Restrict to faces and houses
# ------------------------------
from nilearn.image import index_img
condition_mask = y.isin(['face', 'house'])
fmri_img = index_img(fmri_filename, condition_mask)
y, session = y[condition_mask], session[condition_mask]
#########################################################################
# Prepare masks
# --------------
# - mask_img is the original mask
# - process_mask_img is a subset of mask_img, it contains the voxels that
# should be processed (we only keep the slice z = 26 and the back of the
# brain to speed up computation)
import numpy as np
mask_img = load_img(haxby_dataset.mask)
# .astype() makes a copy.
process_mask = mask_img.get_data().astype(np.int)
picked_slice = 29
process_mask[..., (picked_slice + 1):] = 0
process_mask[..., :picked_slice] = 0
process_mask[:, 30:] = 0
process_mask_img = new_img_like(mask_img, process_mask)
#########################################################################
# Searchlight computation
# -------------------------
# Make processing parallel
# /!\ As each thread will print its progress, n_jobs > 1 could mess up the
# information output.
n_jobs = 1
# Define the cross-validation scheme used for validation.
# Here we use a KFold cross-validation on the session, which corresponds to
# splitting the samples in 4 folds and make 4 runs using each fold as a test
# set once and the others as learning sets
'''
------------- COMMON SECTION ENDS -------------------------------
sklearn.cross_validation.KFold section begins
'''
from sklearn.cross_validation import KFold as KFold_cv
cross_validation_cv = KFold_cv(y.size, n_folds=4) # This is an iterable.
import nilearn.decoding
# The radius is the one of the Searchlight sphere that will scan the volume
searchlight = nilearn.decoding.SearchLight(
mask_img,
process_mask_img=process_mask_img,
radius=5.6, n_jobs=n_jobs,
verbose=1, cv=cross_validation_cv) # THIS WORKS.
searchlight.fit(fmri_img, y) # No Error.
'''
sklearn.model_selection.KFold section begins
'''
from sklearn.model_selection import KFold as KFold_ms
model_selection_cv = KFold_ms(n_splits=4) # model_selection_cv.split() gives a generator.
import nilearn.decoding
# The radius is the one of the Searchlight sphere that will scan the volume
searchlight = nilearn.decoding.SearchLight(
mask_img,
process_mask_img=process_mask_img,
radius=5.6, n_jobs=n_jobs,
verbose=1,
# THIS FAILS. It worked in other replacements I made so far, but not with SearchLight.
cv=model_selection_cv.split(X=y),
)
searchlight.fit(fmri_img, y) # The Error Occurs in the fit() function.
searchlight = nilearn.decoding.SearchLight(
mask_img,
process_mask_img=process_mask_img,
radius=5.6, n_jobs=n_jobs,
verbose=1,
# THIS WORKS. It is inconsitent, and new users might not understand why this is being done.
cv=list(model_selection_cv.split(X=y)),
)
searchlight.fit(fmri_img, y) # No Error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment