def test_ward_agglomeration():
    """
    Check that we obtain the correct solution in a simplistic case
    """
    rnd = np.random.RandomState(0)
    mask = np.ones([10, 10], dtype=np.bool)
    X = rnd.randn(50, 100)
    connectivity = grid_to_graph(*mask.shape)
    ward = WardAgglomeration(n_clusters=5, connectivity=connectivity)
    ward.fit(X)
    assert_true(np.size(np.unique(ward.labels_)) == 5)

    Xred = ward.transform(X)
    assert_true(Xred.shape[1] == 5)
    Xfull = ward.inverse_transform(Xred)
    assert_true(np.unique(Xfull[0]).size == 5)
def test_ward_agglomeration():
    """
    Check that we obtain the correct solution in a simplistic case
    """
    rnd = np.random.RandomState(0)
    mask = np.ones([10, 10], dtype=np.bool)
    X = rnd.randn(50, 100)
    connectivity = grid_to_graph(*mask.shape)
    ward = WardAgglomeration(n_clusters=5, connectivity=connectivity)
    ward.fit(X)
    assert_true(np.size(np.unique(ward.labels_)) == 5)

    Xred = ward.transform(X)
    assert_true(Xred.shape[1] == 5)
    Xfull = ward.inverse_transform(Xred)
    assert_true(np.unique(Xfull[0]).size == 5)
Beispiel #3
0
first_epi = nifti_masker.inverse_transform(fmri_masked[0]).get_data()
first_epi = np.ma.masked_array(first_epi, first_epi == 0)
# Outside the mask: a uniform value, smaller than inside the mask
first_epi[np.logical_not(mask)] = 0.9 * first_epi[mask].min()
vmax = first_epi[..., 20].max()
vmin = first_epi[..., 20].min()
pl.imshow(np.rot90(first_epi[..., 20]),
          interpolation='nearest', cmap=pl.cm.spectral, vmin=vmin, vmax=vmax)
pl.axis('off')
pl.title('Original (%i voxels)' % fmri_masked.shape[1])

# A reduced data can be create by taking the parcel-level average:
# Note that, as many objects in the scikit-learn, the ward object exposes
# a transform method that modifies input features. Here it reduces their
# dimension
fmri_reduced = ward.transform(fmri_masked)

# Display the corresponding data compressed using the parcellation
fmri_compressed = ward.inverse_transform(fmri_reduced)
compressed = nifti_masker.inverse_transform(
    fmri_compressed[0]).get_data()
compressed = np.ma.masked_equal(compressed, 0)


pl.figure()
pl.imshow(np.rot90(compressed[:, :, 20]),
          interpolation='nearest', cmap=pl.cm.spectral, vmin=vmin, vmax=vmax)
pl.title('Compressed representation (2000 parcels)')
pl.axis('off')
pl.show()
Beispiel #4
0
first_plot = plot_roi(labels_img,
                      mean_func_img,
                      title="Ward parcellation",
                      display_mode='xz')
# labels_img is a Nifti1Image object, it can be saved to file with the
# following code:
labels_img.to_filename('parcellation.nii')

# Display the original data
plot_epi(nifti_masker.inverse_transform(fmri_masked[0]),
         cut_coords=first_plot.cut_coords,
         title='Original (%i voxels)' % fmri_masked.shape[1],
         display_mode='xz')

# A reduced data can be create by taking the parcel-level average:
# Note that, as many objects in the scikit-learn, the ward object exposes
# a transform method that modifies input features. Here it reduces their
# dimension
fmri_reduced = ward.transform(fmri_masked)

# Display the corresponding data compressed using the parcellation
fmri_compressed = ward.inverse_transform(fmri_reduced)
compressed_img = nifti_masker.inverse_transform(fmri_compressed[0])

plot_epi(compressed_img,
         cut_coords=first_plot.cut_coords,
         title='Compressed representation (2000 parcels)',
         display_mode='xz')

plt.show()
Beispiel #5
0
        #   the example_time_idx timepoint.
        plot_epi(nifti_masker.inverse_transform(fmri_masked[example_time_idx]),
                 cut_coords=cut_coords,
                 axes=figure1.add_subplot(2, 2, 1),
                 title='Original (%i voxels)' % fmri_masked.shape[1],
                 display_mode='xz',
                 colorbar=True)

    # A reduced data can be create by taking the parcel-level average:
    #   Note that, as many objects in the scikit-learn, the ward object exposes
    #   a transform method that modifies input features. Here it reduces their
    #   dimension by averaging data within a cluster.
    fmri_reduced = ward.transform(fmri_masked)

    # Plot an image of the data compressed using the parcellation
    fmri_clustered = ward.inverse_transform(fmri_reduced)
    clustered_img = nifti_masker.inverse_transform(fmri_clustered[0])

    if 1 in plots:
        plot_epi(clustered_img,
                 cut_coords=cut_coords,
                 axes=figure1.add_subplot(2, 2, 2),
                 title='Clustered representation (%d clusters)' % n_clusters,
                 display_mode='xz',
                 colorbar=True)

    # To visualize the centroids, compute an image of the distance
    #   from each voxel's associated centroid to that voxel's position.
    # Now, do true compression--reduce the number of samples.
    centroids = compute_centroids(nifti_masker,
                                  labels=ward.labels_,
labels[mask] = ward.labels_

cut = labels[:, :, 20].astype(np.int)
colors = np.random.random(size=(ward.n_clusters + 1, 3))
colors[-1] = 0
pl.axis('off')
pl.imshow(colors[cut], interpolation='nearest')
pl.title('Ward parcellation')

# Display the original data
pl.figure()
first_epi_img = epi_img[..., 0].copy()
first_epi_img[np.logical_not(mask)] = 0
pl.imshow(first_epi_img[..., 20], interpolation='nearest',
           cmap=pl.cm.spectral)
pl.axis('off')
pl.title('Original')

# Display the corresponding data compressed using the parcellation
X_r = ward.transform(epi_masked.T)
X_c = ward.inverse_transform(X_r)
compressed_img = np.zeros(mask.shape)
compressed_img[mask] = X_c[0]

pl.figure()
pl.imshow(compressed_img[:, :, 20], interpolation='nearest',
           cmap=pl.cm.spectral)
pl.title('Compressed representation')
pl.axis('off')
pl.show()