forked from nilearn/nilearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_old_canica_resting_state.py
87 lines (70 loc) · 3.42 KB
/
plot_old_canica_resting_state.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
"""
Group analysis of resting-state fMRI with ICA: CanICA
=====================================================
An example applying CanICA to resting-state data. This example applies it
to 40 subjects of the ADHD200 datasets.
CanICA is an ICA method for group-level analysis of fMRI data. Compared
to other strategies, it brings a well-controlled group model, as well as a
thresholding algorithm controlling for specificity and sensitivity with
an explicit model of the signal. The reference papers are:
* G. Varoquaux et al. "A group model for stable multi-subject ICA on
fMRI datasets", NeuroImage Vol 51 (2010), p. 288-299
* G. Varoquaux et al. "ICA-based sparse features recovery from fMRI
datasets", IEEE ISBI 2010, p. 1177
Pre-prints for both papers are available on hal
(http://hal.archives-ouvertes.fr)
"""
import numpy as np
### Load ADHD rest dataset ####################################################
from nilearn import datasets
# Here we use a limited number of subjects to get faster-running code. For
# better results, simply increase the number.
dataset = datasets.fetch_adhd()
func_files = dataset.func[:5]
### Preprocess ################################################################
from nilearn import io
# This is a multi-subject method, thus we need to use the
# MultiNiftiMasker, rather than the NiftiMasker
# We specify the target_affine to downsample to 3mm isotropic
# resolution
masker = io.MultiNiftiMasker(smoothing_fwhm=6,
target_affine=np.diag((3, 3, 3)),
memory="nilearn_cache", memory_level=1,
verbose=True)
data_masked = masker.fit_transform(func_files)
mean_epi = masker.inverse_transform(data_masked[0].mean(axis=0)).get_data()
### Apply CanICA ##############################################################
from nilearn.decomposition.old_canica import CanICA
n_components = 20
ica = CanICA(n_components=n_components, random_state=42, memory="nilearn_cache",
maps_only=True)
components_masked = ica.fit(data_masked).components_
# We normalize the estimated components, for thresholding to make sense
# XXX: this should probably be integrated in the CanICA object
components_masked -= components_masked.mean(axis=1)[:, np.newaxis]
components_masked /= components_masked.std(axis=1)[:, np.newaxis]
# Threshold
#threshold = (stats.norm.isf(0.5*threshold_p_value)
# /np.sqrt(components_masked.shape[0]))
threshold = .9
components_masked[np.abs(components_masked) < threshold] = 0
# Now invert the masking operation, to go back to a full 3D
# representation
components_img = masker.inverse_transform(components_masked)
components = components_img.get_data()
# Using a masked array is important to have transparency in the figures
components = np.ma.masked_equal(components, 0, copy=False)
### Visualize the results #####################################################
# Show some interesting components
import pylab as pl
from scipy import ndimage
for i in range(n_components):
pl.figure()
pl.axis('off')
cut_coord = ndimage.maximum_position(np.abs(components[..., i]))[2]
vmax = np.max(np.abs(components[:, :, cut_coord, i]))
pl.imshow(np.rot90(mean_epi[:, :, cut_coord]), interpolation='nearest',
cmap=pl.cm.gray)
pl.imshow(np.rot90(components[:, :, cut_coord, i]),
interpolation='nearest', cmap=pl.cm.jet, vmax=vmax, vmin=-vmax)
pl.show()