"""
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
import nibabel

### Load ADHD rest dataset ####################################################
from nisl import datasets
# Here we use a limited number of subjects to get faster-running code. For
# better results, simply increase the number.
from nisl.io import NiftiMultiMasker

dataset = datasets.fetch_adhd()
func_files = dataset.func

### Preprocess ################################################################
from nisl.decomposition.canica import CanICA
from nisl.resampling import resample_img

# This is a multi-subject method, thus we need to use the
# NiftiMultiMasker, rather than the NiftiMasker
# We specify the target_affine to downsample to 3mm isotropic
# resolution

target_affine = np.diag((3, 3, 3))
epi_img = nibabel.load(func_files[0])
mean_epi = epi_img.get_data().mean(axis=-1)
mean_epi_img = nibabel.Nifti1Image(mean_epi, epi_img.get_affine())
mean_epi = resample_img(mean_epi_img, target_affine=target_affine).get_data()

masker = NiftiMultiMasker(smoothing_fwhm=6, target_affine=target_affine,
                          memory="nisl_cache", memory_level=5, verbose=1)

n_components = 20
canica = CanICA(mask=masker, n_components=n_components,
                memory="nisl_cache", memory_level=5,
                threshold=2.,
                verbose=1)
canica.fit(func_files)

### Apply CanICA ##############################################################

components_img = canica.components_img_
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()

@@ -1200,18 +1200,23 @@
 ### 
-Preprocess
+Filter and mask
  ###
@@ -1275,58 +1275,8 @@
 ####
-#####%0Afrom nisl.decomposition.canica import CanICA
 %0Afro
@@ -1907,16 +1907,142 @@
 ose=1)%0A%0A
+### Apply CanICA ##############################################################%0Afrom nisl.decomposition.canica import CanICA%0A%0A
 n_compon
@@ -2245,89 +2245,8 @@
 s)%0A%0A
-### Apply CanICA ##############################################################%0A%0A
 comp
@@ -2251,20 +2251,16 @@
 mponents
-_img
  = canic
@@ -2280,178 +2280,18 @@
 img_
-%0Acomponents = components_img.get_data()%0A%0A# Using a masked array is important to have transparency in the figures%0Acomponents = np.ma.masked_equal(components, 0, copy=False
+.get_data(
 )%0A%0A#
@@ -2450,16 +2450,148 @@
 dimage%0A%0A
+# Using a masked array is important to have transparency in the figures%0Acomponents = np.ma.masked_equal(components, 0, copy=False)%0A%0A
 for i in
@@ -3019,16 +3019,16 @@
 -vmax)%0A%0A
+
 pl.show(
@@ -3028,9 +3028,8 @@
 l.show()
-%0A

Clean canica example