def test_plot_roi_contours(): display = plot_roi(None) data = np.zeros((91, 109, 91)) x, y, z = -52, 10, 22 x_map, y_map, z_map = coord_transform(x, y, z, np.linalg.inv(mni_affine)) data[int(x_map) - 5:int(x_map) + 5, int(y_map) - 3:int(y_map) + 3, int(z_map) - 10:int(z_map) + 10] = 1 img = nibabel.Nifti1Image(data, mni_affine) plot_roi(img, cmap='RdBu', alpha=0.1, view_type='contours', linewidths=2.) plt.close()
def test_plotting_functions_with_cmaps(): img = load_mni152_template() cmaps = ['Paired', 'Set1', 'Set2', 'Set3'] for cmap in cmaps: plot_roi(img, cmap=cmap, colorbar=True) plot_stat_map(img, cmap=cmap, colorbar=True) plot_glass_brain(img, cmap=cmap, colorbar=True) if LooseVersion(matplotlib.__version__) >= LooseVersion('2.0.0'): plot_stat_map(img, cmap='viridis', colorbar=True) plt.close()
def test_plotting_functions_with_cmaps(): img = load_mni152_template() # some cmaps such as 'viridis' (the new default in 2.0), 'magma', 'plasma', # and 'inferno' are not supported for older matplotlib version from < 1.5 cmaps = ['Paired', 'Set1', 'Set2', 'Set3'] for cmap in cmaps: plot_roi(img, cmap=cmap, colorbar=True) plot_stat_map(img, cmap=cmap, colorbar=True) plot_glass_brain(img, cmap=cmap, colorbar=True) if LooseVersion(matplotlib.__version__) >= LooseVersion('2.0.0'): plot_stat_map(img, cmap='viridis', colorbar=True) plt.close()
def test_plotting_functions_with_nans_in_bg_img(): bg_img = _generate_img() bg_data = bg_img.get_data() bg_data[6, 5, 1] = np.nan bg_data[1, 5, 2] = np.nan bg_data[1, 3, 2] = np.nan bg_data[6, 5, 2] = np.inf bg_img = nibabel.Nifti1Image(bg_data, mni_affine) plot_anat(bg_img) # test with plot_roi passing background image which contains nans values # in it roi_img = _generate_img() plot_roi(roi_img=roi_img, bg_img=bg_img) stat_map_img = _generate_img() plot_stat_map(stat_map_img=stat_map_img, bg_img=bg_img) plt.close()
def demo_plot_roi(**kwargs): """ Demo plotting an ROI """ mni_affine = MNI152TEMPLATE.get_affine() data = np.zeros((91, 109, 91)) # Color a asymetric rectangle around Broca area: x, y, z = -52, 10, 22 x_map, y_map, z_map = coord_transform(x, y, z, np.linalg.inv(mni_affine)) data[int(x_map) - 5 : int(x_map) + 5, int(y_map) - 3 : int(y_map) + 3, int(z_map) - 10 : int(z_map) + 10] = 1 img = nibabel.Nifti1Image(data, mni_affine) return plot_roi(img, title="Broca's area", **kwargs)
def demo_plot_roi(**kwargs): """ Demo plotting an ROI """ mni_affine = MNI152TEMPLATE.get_affine() data = np.zeros((91, 109, 91)) # Color a asymetric rectangle around Broca area: x, y, z = -52, 10, 22 x_map, y_map, z_map = coord_transform(x, y, z, np.linalg.inv(mni_affine)) data[int(x_map) - 5:int(x_map) + 5, int(y_map) - 3:int(y_map) + 3, int(z_map) - 10:int(z_map) + 10] = 1 img = nibabel.Nifti1Image(data, mni_affine) return plot_roi(img, title="Broca's area", **kwargs)
### Visualization ############################################################# import matplotlib.pyplot as plt # Compute the mean EPI: we do the mean along the axis 3, which is time mean_haxby = mean_img(haxby_files.func) plot_epi(mean_haxby) ### Extracting a brain mask ################################################### # Simple computation of a mask from the fMRI data from nilearn.masking import compute_epi_mask mask_img = compute_epi_mask(haxby_files.func[0]) plot_roi(mask_img, mean_haxby) ### Applying the mask ######################################################### from nilearn.masking import apply_mask masked_data = apply_mask(haxby_files.func[0], mask_img) # masked_data shape is (timepoints, voxels). We can plot the first 150 # timepoints from two voxels plt.figure(figsize=(7, 5)) plt.plot(masked_data[:2, :150].T) plt.xlabel('Time [TRs]', fontsize=16) plt.ylabel('Intensity', fontsize=16) plt.xlim(0, 150) plt.subplots_adjust(bottom=.12, top=.95, right=.95, left=.12)
### Show result ############################################################### # Unmask data # Avoid 0 label labels = ward.labels_ + 1 labels_img = nifti_masker.inverse_transform(labels) from nilearn.image import mean_img import matplotlib.pyplot as plt mean_func_img = mean_img(func_filename) # common cut coordinates for all plots 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
# print basic information on the dataset print('First functional nifti image (4D) is located at: %s' % miyawaki_dataset.func[0]) # 4D data miyawaki_filename = miyawaki_dataset.func[0] miyawaki_mean_img = image.mean_img(miyawaki_filename) # This time, we can use the NiftiMasker without changing the default mask # strategy, as the data has already been masked, and thus lies on a # homogeneous background masker = NiftiMasker() masker.fit(miyawaki_filename) plot_roi(masker.mask_img_, miyawaki_mean_img, title="Mask from already masked data") ############################################################################### # From raw EPI data # Load NYU resting-state dataset nyu_dataset = datasets.fetch_nyu_rest(n_subjects=1) nyu_filename = nyu_dataset.func[0] nyu_img = nibabel.load(nyu_filename) # Restrict nyu to 100 frames to speed up computation from nilearn.image import index_img nyu_img = index_img(nyu_img, slice(0, 100)) # To display the background
print "Ward agglomeration 2000 clusters: %.2fs" % (time.time() - start) ### Show result ############################################################### # Unmask data # Avoid 0 label labels = ward.labels_ + 1 labels_img = nifti_masker.inverse_transform(labels) from nilearn.image import mean_img import matplotlib.pyplot as plt mean_func_img = mean_img(dataset.func[0]) # common cut coordinates for all plots 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
import matplotlib.pyplot as plt # Compute the mean EPI: we do the mean along the axis 3, which is time func_filename = haxby_dataset.func[0] mean_haxby = mean_img(func_filename) plot_epi(mean_haxby) ### Extracting a brain mask ################################################### # Simple computation of a mask from the fMRI data from nilearn.masking import compute_epi_mask mask_img = compute_epi_mask(func_filename) plot_roi(mask_img, mean_haxby) ### Applying the mask ######################################################### from nilearn.masking import apply_mask masked_data = apply_mask(func_filename, mask_img) # masked_data shape is (timepoints, voxels). We can plot the first 150 # timepoints from two voxels plt.figure(figsize=(7, 5)) plt.plot(masked_data[:2, :150].T) plt.xlabel('Time [TRs]', fontsize=16) plt.ylabel('Intensity', fontsize=16) plt.xlim(0, 150) plt.subplots_adjust(bottom=.12, top=.95, right=.95, left=.12)