Skip to content

Commit b511c1b

Browse files
committed
DOC describe explainable variance
1 parent 072d2b6 commit b511c1b

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

tutorials/movies_3T/01_plot_explainable_variance.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
Compute the explainable variance
44
================================
55
6+
Before fitting voxelwise models to the fMRI responses, we can compute the
7+
explainable variance on the test set repeats.
8+
9+
The explainable variance is the part of the fMRI responses that can be
10+
explained by voxelwise modeling. It is thus the upper bound of voxelwise
11+
modeling performances.
12+
13+
Indeed, we can decompose the signal into a sum of two components, one
14+
component that is repeated if we repeat the same experiment, and one component
15+
that changes for each repeat. Because voxelwise modeling would use the same
16+
features for each repeat, it can only model the component that is common to
17+
all repeats. This shared component can be estimated by taking the mean over
18+
repeats of the same experiment.
619
"""
720

821
###############################################################################
@@ -14,25 +27,39 @@
1427
subject = "S01"
1528

1629
###############################################################################
30+
# Compute the explainable variance
31+
# --------------------------------
1732
import os
1833
import numpy as np
1934

2035
from voxelwise.io import load_hdf5_array
2136

22-
# Load fMRI responses on the test set
37+
###############################################################################
38+
# First, we load the fMRI responses on the test set, which contains 10 repeats.
2339
file_name = os.path.join(directory, 'responses', f'{subject}_responses.hdf')
2440
Y_test = load_hdf5_array(file_name, "Y_test")
2541

26-
# compute the explainable variance per voxel, based on the test set repeats
42+
###############################################################################
43+
# Then, we compute the explainable variance per voxel.
44+
# The variance of the signal is estimated by taking the average variance over
45+
# repeats (``mean_var``). The variance of the component shared across repeats
46+
# is estimated by taking the variance of the average response (``var_mean``).
47+
# Then, we can compute the explainable variance by dividing the two quantities.
48+
2749
mean_var = np.mean(np.var(Y_test, axis=1), axis=0)
2850
var_mean = np.var(np.mean(Y_test, axis=0), axis=0)
2951
explainable_variance = var_mean / mean_var
3052

3153
###############################################################################
3254
# Map to subject flatmap
55+
# ----------------------
3356
import matplotlib.pyplot as plt
3457
from voxelwise.viz import plot_flatmap_from_mapper
3558

3659
mapper_file = os.path.join(directory, 'mappers', f'{subject}_mappers.hdf')
3760
plot_flatmap_from_mapper(explainable_variance, mapper_file, vmin=0, vmax=0.7)
3861
plt.show()
62+
63+
###############################################################################
64+
# We can see that the explainable variance is mainly located in the visual
65+
# cortex, which was expected since this is a purely visual experiment.

voxelwise/viz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def plot_flatmap_from_mapper(voxels, mapper_file, ax=None, alpha=0.7,
120120
figsize = np.array(flatmap_mask.shape) / 100.
121121
fig = plt.figure(figsize=figsize)
122122
ax = fig.add_axes((0, 0, 1, 1))
123+
ax.axis('off')
123124

124125
# process plotting parameters
125126
if vmin is None:
@@ -268,6 +269,7 @@ def plot_2d_flatmap_from_mapper(voxels_1, voxels_2, mapper_file, ax=None,
268269
figsize = np.array(flatmap_mask.shape) / 100.
269270
fig = plt.figure(figsize=figsize)
270271
ax = fig.add_axes((0, 0, 1, 1))
272+
ax.axis('off')
271273

272274
# process plotting parameters
273275
if vmin is None:

0 commit comments

Comments
 (0)