def get_localizations (X, y, cv, maskf, presels, sv): mask, hdr, aff = au.get_nii_data(maskf) maskidx = np.array(np.where(mask > 0)) hdr.set_data_dtype(np.dtype(np.float)) my_presels = np.zeros_like(presels[0]) my_svs = np.zeros_like(mask) my_svs_done = False #unmasking method found in: #http://nisl.github.io/auto_examples/plot_ica_resting_state.html from nisl.io import NiftiMasker k = 0 cv.n = X.shape[0] for train,test in cv: X_train, y_train = X[train,:], y[train] preselsvol = np.zeros_like (mask, dtype=np.float) preselsvol[tuple(maskidx)] = presels[k] > 0 preselsnii = au.save_nibabel ('', preselsvol, aff, hdr) my_presels += presels[k] > 0 if len(sv) > 0: try: nifti_masker = NiftiMasker(mask=preselsnii) nifti_masker.fit (X_train[:,presels[k]>0], y_train) niimg = nifti_masker.inverse_transform(sv[k][0]) #X_masked = nifti_masker.fit_transform (X_train[:,presels[k]>0], y_train) #,y_train, target_affine=aff, target_shape=hdr.get_data_shape() #niimg = nifti_masker.inverse_transform(sv[0][0]) #act = np.ma.masked_array(niimg.get_data(), niimg.get_data() == 0) my_svs += niimg.get_data() my_svs_done = True except: pass k += 1 my_presels /= cv.n_folds my_svs /= cv.n_folds prelocs = np.zeros_like (mask, dtype=np.float) prelocs[tuple(maskidx)] = my_presels return prelocs, my_svs, my_svs_done
from sklearn.pipeline import Pipeline anova_svc = Pipeline([('anova', feature_selection), ('svc', clf)]) ### Fit and predict ########################################################### anova_svc.fit(X, y) y_pred = anova_svc.predict(X) ### Visualisation ############################################################# ### Look at the discriminating weights svc = clf.support_vectors_ # reverse feature selection svc = feature_selection.inverse_transform(svc) # reverse masking niimg = nifti_masker.inverse_transform(svc[0]) # We use a masked array so that the voxels at '-1' are displayed # transparently act = np.ma.masked_array(niimg.get_data(), niimg.get_data() == 0) ### Create the figure import pylab as pl pl.axis('off') pl.title('SVM vectors') pl.imshow(np.rot90(mean_img[..., 27]), cmap=pl.cm.gray, interpolation='nearest') pl.imshow(np.rot90(act[..., 27]), cmap=pl.cm.hot, interpolation='nearest') pl.show()
pl.axis('off') pl.imshow(np.rot90(nibabel.load(dataset.func[0]).get_data()[..., 20, 0]), interpolation='nearest', cmap=pl.cm.gray) ma = np.ma.masked_equal(mask, False) pl.imshow(np.rot90(ma[..., 20]), interpolation='nearest', cmap=pl.cm.autumn, alpha=0.5) pl.title("Mask") ### Preprocess data ########################################################### nifti_masker.fit(dataset.func[0]) fmri_masked = nifti_masker.transform(dataset.func[0]) ### Run an algorithm ########################################################## from sklearn.decomposition import FastICA n_components = 20 ica = FastICA(n_components=n_components, random_state=42) components_masked = ica.fit_transform(fmri_masked.T).T ### Reverse masking ########################################################### components = nifti_masker.inverse_transform(components_masked) ### Show results ############################################################## components_data = np.ma.masked_equal(components.get_data(), 0) pl.figure() pl.axis('off') pl.imshow(np.rot90(nibabel.load(dataset.func[0]).get_data()[..., 20, 0]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(components_data[..., 20, 7]), interpolation='nearest', cmap=pl.cm.hot) pl.show()
pl.imshow(np.rot90(ma[..., 20]), interpolation='nearest', cmap=pl.cm.autumn, alpha=0.5) pl.title("Mask") ### Preprocess data ########################################################### nifti_masker.fit(dataset.func[0]) fmri_masked = nifti_masker.transform(dataset.func[0]) ### Run an algorithm ########################################################## from sklearn.decomposition import FastICA n_components = 20 ica = FastICA(n_components=n_components, random_state=42) components_masked = ica.fit_transform(fmri_masked) ### Reverse masking ########################################################### components = nifti_masker.inverse_transform(components_masked) ### Show results ############################################################## components = np.ma.masked_equal(components.get_data(), 0) pl.figure() pl.axis('off') pl.imshow(np.rot90(nibabel.load(dataset.func[0]).get_data()[..., 20, 0]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(components[..., 20, 16]), interpolation='nearest', cmap=pl.cm.hot) ### The same with a pipeline ################################################## from sklearn.pipeline import Pipeline mask_ica = Pipeline([('masking', nifti_masker), ('ica', ica)]) components = nifti_masker.inverse_transform( mask_ica.fit_transform(dataset.func[0]))
from sklearn.pipeline import Pipeline anova_svc = Pipeline([('anova', feature_selection), ('svc', clf)]) ### Fit and predict ########################################################### anova_svc.fit(X, y) y_pred = anova_svc.predict(X) ### Visualisation ############################################################# ### Look at the discriminating weights svc = clf.support_vectors_ # reverse feature selection svc = feature_selection.inverse_transform(svc) # reverse masking niimg = nifti_masker.inverse_transform(svc[0]) # We use a masked array so that the voxels at '-1' are displayed # transparently act = np.ma.masked_array(niimg.get_data(), niimg.get_data() == 0) ### Create the figure import pylab as pl pl.axis('off') pl.title('SVM vectors') pl.imshow(np.rot90(mean_img[..., 27]), cmap=pl.cm.gray, interpolation='nearest') pl.imshow(np.rot90(act[..., 27]), cmap=pl.cm.hot, interpolation='nearest') pl.show()
### Restrict to faces and houses ############################################## condition_mask = np.logical_or(conditions == 'face', conditions == 'house') X = fmri_data[..., condition_mask] y = y[condition_mask] session = session[condition_mask] conditions = conditions[condition_mask] ### Loading step ############################################################## from nisl.io import NiftiMasker from nibabel import Nifti1Image nifti_masker = NiftiMasker(mask=mask, sessions=session, memory='nisl_cache', memory_level=1) niimg = Nifti1Image(X, affine) X_masked = nifti_masker.fit(niimg).transform(niimg) X_preprocessed = nifti_masker.inverse_transform(X_masked).get_data() X_preprocessed = np.rollaxis(X_preprocessed, axis=-1) mask = nifti_masker.mask_img_.get_data().astype(np.bool) ### Prepare the masks ######################################################### # Here we will use several masks : # * mask is the originalmask # * process_mask is a subset of mask, it contains voxels that should be # processed (we only keep the slice z = 26 and the back of the brain to speed # up computation) process_mask = mask.copy() process_mask[..., 38:] = False process_mask[..., :36] = False process_mask[:, 30:] = False ### Searchlight ###############################################################
X = fmri_data[..., condition_mask] y = y[condition_mask] session = session[condition_mask] conditions = conditions[condition_mask] ### Loading step ############################################################## from nisl.io import NiftiMasker from nibabel import Nifti1Image # Detrending is disabled as we are not yet able to do it by session nifti_masker = NiftiMasker(mask=mask, detrend=True, copy=False, sessions=session) niimg = Nifti1Image(X, affine) X_masked = nifti_masker.fit(niimg).transform(niimg) X_detrended = nifti_masker.inverse_transform(X_masked).get_data() ### Prepare the masks ######################################################### # Here we will use several masks : # * mask is the originalmask # * process_mask is a subset of mask, it contains voxels that should be # processed (we only keep the slice z = 26 and the back of the brain to speed # up computation) process_mask = mask.copy() process_mask[..., 38:] = False process_mask[..., :36] = False process_mask[:, 30:] = False ### Searchlight ############################################################### # Make processing parallel
### Restrict to faces and houses ############################################## condition_mask = np.logical_or(conditions == 'face', conditions == 'house') X = fmri_data[..., condition_mask] y = y[condition_mask] session = session[condition_mask] conditions = conditions[condition_mask] ### Loading step ############################################################## from nisl.io import NiftiMasker from nibabel import Nifti1Image # Detrending is disabled as we are not yet able to do it by session nifti_masker = NiftiMasker(mask=mask, detrend=True, copy=False, sessions=session) niimg = Nifti1Image(X, affine) X_masked = nifti_masker.fit(niimg).transform(niimg) X_detrended = nifti_masker.inverse_transform(X_masked).get_data() ### Prepare the masks ######################################################### # Here we will use several masks : # * mask is the originalmask # * process_mask is a subset of mask, it contains voxels that should be # processed (we only keep the slice z = 26 and the back of the brain to speed # up computation) process_mask = mask.copy() process_mask[..., 38:] = False process_mask[..., :36] = False process_mask[:, 30:] = False ### Searchlight ############################################################### # Make processing parallel
# Compute the ward with more clusters, should be faster as we are using # the caching mechanism start = time.time() ward = WardAgglomeration(n_clusters=1000, connectivity=connectivity, memory='nisl_cache') ward.fit(fmri_masked) print "Ward agglomeration 1000 clusters: %.2fs" % (time.time() - start) ### Show result ############################################################### # Unmask data import numpy as np # Avoid 0 label labels = ward.labels_ + 1 labels = nifti_masker.inverse_transform(ward.labels_).get_data() # 0 is the background, putting it to -1 labels = labels - 1 # Display the labels import pylab as pl # Cut at z=20 cut = labels[:, :, 20].astype(np.int) # Assign random colors to each cluster. For this we build a random # RGB look up table associating a color to each cluster, and apply it # below colors = np.random.random(size=(ward.n_clusters + 1, 3)) # Cluster '-1' should be black (it's outside the brain) colors[-1] = 0 pl.figure()
### Visualization ############################################################# import pylab as pl pl.figure(1) # searchlight.scores_ contains per voxel cross validation scores s_scores = np.ma.array(searchlight.scores_, mask=np.logical_not(process_mask)) pl.imshow(np.rot90(mean_img[..., 37]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(s_scores[..., 37]), interpolation='nearest', cmap=pl.cm.hot, vmax=1) pl.axis('off') pl.title('Searchlight') pl.show() ### Show the F_score from sklearn.feature_selection import f_classif pl.figure(2) f_values, p_values = f_classif(X_masked, y) p_values = -np.log10(p_values) p_values[np.isnan(p_values)] = 0 p_values[p_values > 10] = 10 p_unmasked = nifti_masker.inverse_transform(p_values).get_data() p_ma = np.ma.array(p_unmasked, mask=np.logical_not(process_mask)) pl.imshow(np.rot90(mean_img[..., 37]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(p_ma[..., 37]), interpolation='nearest', cmap=pl.cm.hot) pl.title('F-scores') pl.axis('off') pl.show()
pl.figure(1) # searchlight.scores_ contains per voxel cross validation scores s_scores = np.ma.array(searchlight.scores_, mask=np.logical_not(process_mask)) pl.imshow(np.rot90(mean_img[..., 37]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(s_scores[..., 37]), interpolation='nearest', cmap=pl.cm.hot, vmax=1) pl.axis('off') pl.title('Searchlight') pl.show() ### Show the F_score from sklearn.feature_selection import f_classif pl.figure(2) f_values, p_values = f_classif(X_masked, y) p_values = -np.log10(p_values) p_values[np.isnan(p_values)] = 0 p_values[p_values > 10] = 10 p_unmasked = nifti_masker.inverse_transform(p_values).get_data() p_ma = np.ma.array(p_unmasked, mask=np.logical_not(process_mask)) pl.imshow(np.rot90(mean_img[..., 37]), interpolation='nearest', cmap=pl.cm.gray) pl.imshow(np.rot90(p_ma[..., 37]), interpolation='nearest', cmap=pl.cm.hot) pl.title('F-scores') pl.axis('off') pl.show()
# Compute the ward with more clusters, should be faster as we are using # the caching mechanism start = time.time() ward = WardAgglomeration(n_clusters=1000, connectivity=connectivity, memory='nisl_cache') ward.fit(fmri_masked) print "Ward agglomeration 1000 clusters: %.2fs" % (time.time() - start) ### Show result ############################################################### # Unmask data import numpy as np # Avoid 0 label labels = ward.labels_ + 1 labels = nifti_masker.inverse_transform(ward.labels_).get_data() # 0 is the background, putting it to -1 labels = labels - 1 # Display the labels import pylab as pl # Cut at z=20 cut = labels[:, :, 20].astype(np.int) # Assign random colors to each cluster. For this we build a random # RGB look up table associating a color to each cluster, and apply it # below colors = np.random.random(size=(ward.n_clusters + 1, 3)) # Cluster '-1' should be black (it's outside the brain) colors[-1] = 0 pl.figure()