from matplotlib.lines import Line2D def plot_lines(mask, linewidth=3, color='b'): for i, j in np.ndindex(mask.shape): if i + 1 < mask.shape[0] and mask[i, j] != mask[i + 1, j]: pl.gca().add_line(Line2D([j - .5, j + .5], [i + .5, i + .5], color=color, linewidth=linewidth)) if j + 1 < mask.shape[1] and mask[i, j] != mask[i, j + 1]: pl.gca().add_line(Line2D([j + .5, j + .5], [i - .5, i + .5], color=color, linewidth=linewidth)) sbrain = masking.unmask(np.array(scores).mean(0), dataset.mask) bg = nibabel.load(os.path.join('utils', 'bg.nii.gz')) pl.figure(figsize=(8, 8)) ax1 = pl.axes([0., 0., 1., 1.]) pl.imshow(bg.get_data()[:, :, 10].T, interpolation="nearest", cmap='gray', origin='lower') pl.imshow(np.ma.masked_less(sbrain[:, :, 10].T, 1e-6), interpolation="nearest", cmap='hot', origin="lower") plot_lines(contour[:, :, 10].T) pl.axis('off') ax2 = pl.axes([.08, .5, .05, .47]) cb = pl.colorbar(cax=ax2, ax=ax1) cb.ax.yaxis.set_ticks_position('left') cb.ax.yaxis.set_tick_params(labelcolor='white')
def plot_lines(mask, linewidth=3, color='b'): for i, j in np.ndindex(mask.shape): if i + 1 < mask.shape[0] and mask[i, j] != mask[i + 1, j]: pl.gca().add_line( Line2D([j - .5, j + .5], [i + .5, i + .5], color=color, linewidth=linewidth)) if j + 1 < mask.shape[1] and mask[i, j] != mask[i, j + 1]: pl.gca().add_line( Line2D([j + .5, j + .5], [i - .5, i + .5], color=color, linewidth=linewidth)) sbrain = masking.unmask(np.array(scores).mean(0), dataset.mask) bg = nibabel.load(os.path.join('utils', 'bg.nii.gz')) pl.figure(figsize=(8, 8)) ax1 = pl.axes([0., 0., 1., 1.]) pl.imshow(bg.get_data()[:, :, 10].T, interpolation="nearest", cmap='gray', origin='lower') pl.imshow(np.ma.masked_less(sbrain[:, :, 10].T, 1e-6), interpolation="nearest", cmap='hot', origin="lower") plot_lines(contour[:, :, 10].T) pl.axis('off')
X = X_ ### CanICA #################################################################### if not exists(join(path, 'canica.nii.gz')): try: from nilearn.decomposition.canica import CanICA t0 = time.time() canica = CanICA(n_components=n_components, mask=mask_img, smoothing_fwhm=6., memory="nilearn_cache", memory_level=1, threshold=None, random_state=1, n_jobs=-1) canica.fit(dataset.func) print('Canica: %f' % (time.time() - t0)) canica_components = masking.unmask(canica.components_, mask_img) nibabel.save(nibabel.Nifti1Image(canica_components, mask_img.get_affine()), join(path, 'canica.nii.gz')) except ImportError: import warnings warnings.warn('nilearn must be installed to run CanICA') canica_dmn = nibabel.load(join(path, 'canica.nii.gz')).get_data()[..., 4] ### Melodic ICA ############################################################ # To have MELODIC results, please use my melodic branch of nilearn melodic_dmn = nibabel.load(join(path, 'melodic.nii.gz')).get_data()[..., 3]
for i, j in np.ndindex(mask.shape): if i + 1 < mask.shape[0] and mask[i, j] != mask[i + 1, j]: pl.gca().add_line( Line2D([j - .5, j + .5], [i + .5, i + .5], color=color, linewidth=linewidth)) if j + 1 < mask.shape[1] and mask[i, j] != mask[i, j + 1]: pl.gca().add_line( Line2D([j + .5, j + .5], [i - .5, i + .5], color=color, linewidth=linewidth)) fig = pl.figure(figsize=(8, 8)) ax1 = pl.axes([0., 0., 1., 1.]) sbrain = masking.unmask(lr_coef[0], dataset.mask) bg = nibabel.load(os.path.join('utils', 'bg.nii.gz')) pl.imshow(bg.get_data()[:, :, 10].T, interpolation="nearest", cmap='gray', origin='lower') pl.imshow(np.ma.masked_equal(sbrain[:, :, 10].T, 0.), interpolation="nearest", cmap=cm.bluegreen, origin='lower', vmin=0., vmax=2.6) plot_lines(contour[:, :, 10].T, color='r') pl.axis('off') ax2 = pl.axes([.1, .5, .05, .45]) cb = pl.colorbar(cax=ax2, ax=ax1)
connectivity = image.grid_to_graph(n_x=shape[0], n_y=shape[1], n_z=shape[2], mask=mask) for n_clusters in 100, 1000: # Compute Ward clustering from sklearn.cluster import WardAgglomeration ward = WardAgglomeration(n_clusters=n_clusters, connectivity=connectivity, memory='nilearn_cache', compute_full_tree=True) ward.fit(X) labels = ward.labels_ + 1 labels = masking.unmask(labels, adhd_mask) # 0 is the background, putting it to -1 labels = labels - 1 # Display the labels plot_labels(labels, 8) pl.savefig(join('clustering', 'ward_%i.eps' % n_clusters)) pl.savefig(join('clustering', 'ward_%i.pdf' % n_clusters)) # Compute Kmeans clustering from sklearn.cluster import MiniBatchKMeans kmeans = MiniBatchKMeans(n_clusters=n_clusters, random_state=1) kmeans.fit(X_smoothed.T) labels = kmeans.labels_ + 1
# 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) for n_clusters in 100, 1000: # Compute Ward clustering from sklearn.cluster import WardAgglomeration ward = WardAgglomeration(n_clusters=n_clusters, connectivity=connectivity, memory='nilearn_cache', compute_full_tree=True) ward.fit(X) labels = ward.labels_ + 1 labels = masking.unmask(labels, adhd_mask) # 0 is the background, putting it to -1 labels = labels - 1 # Display the labels plot_labels(labels, 8) pl.savefig(join('clustering', 'ward_%i.eps' % n_clusters)) pl.savefig(join('clustering', 'ward_%i.pdf' % n_clusters)) # Compute Kmeans clustering from sklearn.cluster import MiniBatchKMeans kmeans = MiniBatchKMeans(n_clusters=n_clusters, random_state=1) kmeans.fit(X_smoothed.T) labels = kmeans.labels_ + 1
pixmask[p] = 1 def plot_lines(mask, linewidth=3, color='b'): for i, j in np.ndindex(mask.shape): if i + 1 < mask.shape[0] and mask[i, j] != mask[i + 1, j]: pl.gca().add_line(Line2D([j - .5, j + .5], [i + .5, i + .5], color=color, linewidth=linewidth)) if j + 1 < mask.shape[1] and mask[i, j] != mask[i, j + 1]: pl.gca().add_line(Line2D([j + .5, j + .5], [i - .5, i + .5], color=color, linewidth=linewidth)) fig = pl.figure(figsize=(8, 8)) ax1 = pl.axes([0., 0., 1., 1.]) sbrain = masking.unmask(lr_coef[0], dataset.mask) bg = nibabel.load(os.path.join('utils', 'bg.nii.gz')) pl.imshow(bg.get_data()[:, :, 10].T, interpolation="nearest", cmap='gray', origin='lower') pl.imshow(np.ma.masked_equal(sbrain[:, :, 10].T, 0.), interpolation="nearest", cmap=bluegreen, origin='lower', vmin=0., vmax=2.6) plot_lines(contour[:, :, 10].T, color='r') pl.axis('off') ax2 = pl.axes([.1, .5, .05, .45]) cb = pl.colorbar(cax=ax2, ax=ax1) cb.ax.yaxis.set_ticks_position('left') cb.ax.yaxis.set_tick_params(labelcolor='white') cb.ax.yaxis.set_tick_params(labelsize=32) cb.set_ticks([0., 1.3, 2.6]) pl.savefig(os.path.join('miyawaki', 'pixel_logistic.pdf')) pl.savefig(os.path.join('miyawaki', 'pixel_logistic.png'))
if not exists(join(path, 'canica.nii.gz')): try: from nilearn.decomposition.canica import CanICA t0 = time.time() canica = CanICA(n_components=n_components, mask=mask_img, smoothing_fwhm=6., memory="nilearn_cache", memory_level=1, threshold=None, random_state=1, n_jobs=-1) canica.fit(dataset.func) print('Canica: %f' % (time.time() - t0)) canica_components = masking.unmask(canica.components_, mask_img) nibabel.save( nibabel.Nifti1Image(canica_components, mask_img.get_affine()), join(path, 'canica.nii.gz')) except ImportError: import warnings warnings.warn('nilearn must be installed to run CanICA') canica_dmn = nibabel.load(join(path, 'canica.nii.gz')).get_data()[..., 4] ### Melodic ICA ############################################################ # To have MELODIC results, please use my melodic branch of nilearn melodic_dmn = nibabel.load(join(path, 'melodic.nii.gz')).get_data()[..., 3] ### FastICA ##################################################################
X_img = Nifti1Image(X, affine) X = masking.apply_mask(X_img, mask, smoothing_fwhm=4) X = signal.clean(X, standardize=True, detrend=False) ############################################################################### # # # F-score # # # ############################################################################### from sklearn.feature_selection import f_classif f_values, p_values = f_classif(X, y) p_values = -np.log10(p_values) p_values[np.isnan(p_values)] = 0 p_values[p_values > 10] = 10 p_unmasked = masking.unmask(p_values, mask) plot_haxby(p_unmasked, 'F-score') pl.savefig('haxby/haxby_fscore.pdf') pl.savefig('haxby/haxby_fscore.eps') ############################################################################### # # # SVC # # # ############################################################################### ### Define the estimator from sklearn.svm import SVC clf = SVC(kernel='linear', C=0.01) ### Dimension reduction #######################################################