Pattern Recognition 2011.

"""

### Load nyu_rest dataset #####################################################

from nisl import datasets
dataset = datasets.fetch_nyu_rest(n_subjects=1)

### Mask ######################################################################

fmri_data = dataset.func[0]

# Compute a brain mask
from nisl import masking
mask = masking.compute_mask(fmri_data)

# Mask data: go from a 4D dataset to a 2D dataset with only the voxels
# in the mask
fmri_masked = fmri_data[mask]

### Ward ######################################################################

# Compute connectivity matrix: which voxel is connected to which
from sklearn.feature_extraction import image
shape = mask.shape
connectivity = image.grid_to_graph(n_x=shape[0], n_y=shape[1],
                                   n_z=shape[2], mask=mask)

# Computing the ward for the first time, this is long...
from sklearn.cluster import WardAgglomeration
fmri_data = np.concatenate(dataset.func, axis=3)

# Apply a small amount of Gaussian smoothing: in the case of ICA it is
# important as it introduces a spatial model that ICA lacks and greatly
# reduces the high-frequency signal
from scipy import ndimage
for image in fmri_data.T:
    # This works efficiently because image is a view on fmri_data
    image[...] = ndimage.gaussian_filter(image, 1.5)

# Take the mean along axis 3: the direction of time
mean_img = np.mean(fmri_data, axis=3)

# Mask non brain areas
from nisl import masking
mask = masking.compute_mask(mean_img)
data_masked = fmri_data[mask]


### Apply ICA #################################################################

from sklearn.decomposition import FastICA
n_components = 20
ica = FastICA(n_components=n_components, random_state=42)
components_masked = ica.fit(data_masked).transform(data_masked)

# We normalize the estimated components, for thresholding to make sens
components_masked -= components_masked.mean(axis=0)
components_masked /= components_masked.std(axis=0)
# Threshold
components_masked[np.abs(components_masked) < .5] = 0
Learning a brain parcellation from rest fMRI
=============================================
"""

### Load nyu_rest dataset #####################################################

from nisl import datasets
dataset = datasets.fetch_nyu_rest(n_subjects=1)

### Mask ######################################################################

epi_img = dataset.func[0]
# Compute the mask

from nisl import masking
mask = masking.compute_mask(epi_img)
# Mask data
epi_masked = epi_img[mask]

### Ward ######################################################################

# Compute connectivity matrix
from sklearn.feature_extraction import image
shape = mask.shape
connectivity = image.grid_to_graph(n_x=shape[0], n_y=shape[1],
                                   n_z=shape[2], mask=mask)

# Computing the ward for the first time, this is long...
from sklearn.cluster import WardAgglomeration
import time
start = time.time()
pl.title('Sagittal')
pl.imshow(np.rot90(mean_img[15, :, :]), interpolation='nearest',
          cmap=pl.cm.gray)

# Third subplot: axial view
pl.subplot(1, 3, 3)
pl.axis('off')
pl.title('Axial')
pl.imshow(np.rot90(mean_img[:, :, 32]), interpolation='nearest',
          cmap=pl.cm.gray)

# Extracting a brain mask ###################################################

# Simple computation of a mask from the fMRI data
from nisl.masking import compute_mask
mask = compute_mask(mean_img)

# We create a new figure
pl.figure()
# A plot the axial view of the mask to compare with the axial
# view of the raw data displayed previously
pl.imshow(np.rot90(mask[:, :, 32]), interpolation='nearest')

# Applying the mask #########################################################

# Applying the mask is just a simple array manipulation
masked_data = fmri_data[mask]

# masked_data is now a voxel x time matrix. We can plot the first 10
# lines: they correspond to time-series of 10 voxels on the side of the
# brain