forked from nilearn/nilearn
/
plot_haxby_searchlight.py
112 lines (91 loc) · 4.26 KB
/
plot_haxby_searchlight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
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 numpy as np
import nibabel
from nilearn import datasets
dataset_files = datasets.fetch_haxby_simple()
fmri_img = nibabel.load(dataset_files.func)
y, session = np.loadtxt(dataset_files.session_target).astype("int").T
conditions = np.recfromtxt(dataset_files.conditions_target)['f0']
### Restrict to faces and houses ##############################################
condition_mask = np.logical_or(conditions == 'face', conditions == 'house')
fmri_img = nibabel.Nifti1Image(fmri_img.get_data()[..., condition_mask],
fmri_img.get_affine().copy())
y, session = y[condition_mask], session[condition_mask]
conditions = conditions[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)
mask_img = nibabel.load(dataset_files.mask)
# .astype() makes a copy.
process_mask = mask_img.get_data().astype(np.int)
picked_slice = 27
process_mask[..., (picked_slice + 1):] = 0
process_mask[..., :picked_slice] = 0
process_mask[:, 30:] = 0
process_mask_img = nibabel.Nifti1Image(process_mask, mask_img.get_affine())
### 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
from sklearn.cross_validation import KFold
cv = KFold(y.size, n_folds=4)
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=cv)
searchlight.fit(fmri_img, y)
### F-scores computation ######################################################
from nilearn.input_data import NiftiMasker
# For decoding, standardizing is often very important
nifti_masker = NiftiMasker(mask=mask_img, sessions=session,
standardize=True, memory='nilearn_cache',
memory_level=1)
fmri_masked = nifti_masker.fit_transform(fmri_img)
from sklearn.feature_selection import f_classif
f_values, p_values = f_classif(fmri_masked, y)
p_values = -np.log10(p_values)
p_values[np.isnan(p_values)] = 0
p_values[p_values > 10] = 10
p_unmasked = nifti_masker.inverse_transform(p_values).get_data()
### Visualization #############################################################
import matplotlib.pyplot as plt
# Use the fmri mean image as a surrogate of anatomical data
from nilearn import image
mean_fmri = image.mean_img(fmri_img).get_data()
### Searchlight results
plt.figure(1)
# searchlight.scores_ contains per voxel cross validation scores
s_scores = np.ma.array(searchlight.scores_, mask=np.logical_not(process_mask))
plt.imshow(np.rot90(mean_fmri[..., picked_slice]), interpolation='nearest',
cmap=plt.cm.gray)
plt.imshow(np.rot90(s_scores[..., picked_slice]), interpolation='nearest',
cmap=plt.cm.hot, vmax=1)
plt.axis('off')
plt.title('Searchlight')
### F_score results
plt.figure(2)
p_ma = np.ma.array(p_unmasked, mask=np.logical_not(process_mask))
plt.imshow(np.rot90(mean_fmri[..., picked_slice]), interpolation='nearest',
cmap=plt.cm.gray)
plt.imshow(np.rot90(p_ma[..., picked_slice]), interpolation='nearest',
cmap=plt.cm.hot)
plt.title('F-scores')
plt.axis('off')
plt.show()