In [ ]:
%matplotlib inline


# 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).

In [ ]:
import pandas as pd
from nilearn import datasets
from nilearn.image import new_img_like, load_img, get_data

# 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]
y = labels['labels']
session = labels['chunks']


## Restrict to faces and houses¶

In [ ]:
from nilearn.image import index_img



• 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)
In [ ]:
import numpy as np

# .astype() makes a copy.
picked_slice = 29
process_mask[..., (picked_slice + 1):] = 0


## Searchlight computation¶

In [ ]:
# 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
from sklearn.model_selection import KFold
cv = KFold(n_splits=4)

import nilearn.decoding
# The radius is the one of the Searchlight sphere that will scan the volume
searchlight = nilearn.decoding.SearchLight(
verbose=1, cv=cv)
searchlight.fit(fmri_img, y)


## F-scores computation¶

In [ ]:
from nilearn.input_data import NiftiMasker

# For decoding, standardizing is often very important
standardize=True, memory='nilearn_cache',
memory_level=1)

from sklearn.feature_selection import f_classif
p_values = -np.log10(p_values)
p_values[p_values > 10] = 10


## Visualization¶

Use the fmri mean image as a surrogate of anatomical data

In [ ]:
from nilearn import image
mean_fmri = image.mean_img(fmri_img)

from nilearn.plotting import plot_stat_map, plot_img, show
searchlight_img = new_img_like(mean_fmri, searchlight.scores_)

# Because scores are not a zero-center test statistics, we cannot use
# plot_stat_map
plot_img(searchlight_img, bg_img=mean_fmri,
title="Searchlight", display_mode="z", cut_coords=[-9],
vmin=.42, cmap='hot', threshold=.2, black_bg=True)

# F_score results