Decoding with SpaceNet: face vs house object recognitionΒΆ

Here is a simple example of decoding with a SpaceNet prior (i.e Graph-Net, TV-l1, etc.), reproducing the Haxby 2001 study on a face vs house discrimination task.

See also the SpaceNet documentation: Multivariate decoding with SpaceNet.

  • ../../_images/plot_haxby_space_net_001.png
  • ../../_images/plot_haxby_space_net_002.png

Script output:

Time Elapsed: 42.3145 seconds, 0 minutes.
Results
================================================================================
- graph-net ------------------------------------------------------------
Number of train samples : 126
Number of test samples  : 90
Classification accuracy : 94.4444%
________________________________________________________________________________
Time Elapsed: 219.432 seconds, 3 minutes.
Results
================================================================================
- tv-l1 ------------------------------------------------------------
Number of train samples : 126
Number of test samples  : 90
Classification accuracy : 95.5556%
________________________________________________________________________________

Python source code: plot_haxby_space_net.py

### Load Haxby dataset ########################################################
from nilearn.datasets import fetch_haxby
data_files = fetch_haxby()

### Load Target labels ########################################################
import numpy as np
labels = np.recfromcsv(data_files.session_target[0], delimiter=" ")


### Split data into train and test samples ####################################
target = labels['labels']
condition_mask = np.logical_or(target == "face", target == "house")
condition_mask_train = np.logical_and(condition_mask, labels['chunks'] <= 6)
condition_mask_test = np.logical_and(condition_mask, labels['chunks'] > 6)

# make X (design matrix) and y (response variable)
import nibabel
from nilearn.image import index_img
niimgs = nibabel.load(data_files.func[0])
X_train = index_img(niimgs, condition_mask_train)
X_test = index_img(niimgs, condition_mask_test)
y_train = target[condition_mask_train]
y_test = target[condition_mask_test]


### Loop over Graph-Net and TV-L1 penalties ##################################
from nilearn.decoding import SpaceNetClassifier
import matplotlib.pyplot as plt
from nilearn.image import mean_img
from nilearn.plotting import plot_stat_map
background_img = mean_img(data_files.func[0])
for penalty in ['graph-net', 'tv-l1']:
    ### Fit model on train data and predict on test data ######################
    decoder = SpaceNetClassifier(memory="cache", penalty=penalty)
    decoder.fit(X_train, y_train)
    y_pred = decoder.predict(X_test)
    accuracy = (y_pred == y_test).mean() * 100.

    ### Visualization #########################################################
    print("Results")
    print("=" * 80)
    coef_img = decoder.coef_img_
    plot_stat_map(coef_img, background_img,
                  title="%s: accuracy %g%%" % (penalty, accuracy),
                  cut_coords=(-34, -16), display_mode="yz")
    coef_img.to_filename('haxby_%s_weights.nii' % penalty)
    print("- %s %s" % (penalty, '-' * 60))
    print("Number of train samples : %i" % condition_mask_train.sum())
    print("Number of test samples  : %i" % condition_mask_test.sum())
    print("Classification accuracy : %g%%" % accuracy)
    print("_" * 80)

plt.show()

Total running time of the example: 272.12 seconds ( 4 minutes 32.12 seconds)