def test_map_to_labels(source_lab, target_lab, kwds, out, expects): if expects: with pytest.raises(expects): parc.map_to_labels(source_lab, target_lab, **kwds) else: res = parc.map_to_labels(source_lab, target_lab, **kwds) assert np.all((res == out) | (np.isnan(out) & np.isnan(out))) assert res.dtype == out.dtype
import numpy as np from brainspace.gradient import GradientMaps from brainspace.plotting import plot_hemispheres from brainspace.utils.parcellation import map_to_labels kernels = ['pearson', 'spearman', 'normalized_angle'] gradients_kernel = [None] * len(kernels) for i, k in enumerate(kernels): gm = GradientMaps(kernel=k, approach='dm', random_state=0) gm.fit(conn_matrix) gradients_kernel[i] = map_to_labels(gm.gradients_[:, i], labeling, mask=mask, fill=np.nan) label_text = ['Pearson', 'Spearman', 'Normalized\nAngle'] plot_hemispheres(surf_lh, surf_rh, array_name=gradients_kernel, size=(1200, 800), cmap='viridis_r', color_bar=True, label_text=label_text) ############################################################################### # It seems the gradients provided by these kernels are quite similar although # their scaling is quite different. Do note that the gradients are in arbitrary # units, so the smaller/larger axes across kernels do not imply anything.
# Load the Schaefer 400 atlas schaefer_400 = load_parcellation("schaefer", scale=400, join=True) # Run the analysis histology_profiles = read_histology_profile(template="fs_LR_64k") mpc = compute_mpc(histology_profiles, labels=schaefer_400) gradient_map = compute_histology_gradients(mpc) ######################################################################## # Lets plot the first gradient of histology to see what it looks like. # We will use BrainSpace to create our plots. For full details on how # BrainSpace's plotting functionality works, please consult the BrainSpace # ReadTheDocs. (NOTE: Temporarily disabled due to build errors) from brainspace.plotting.surface_plotting import plot_hemispheres from brainspace.utils.parcellation import map_to_labels from brainspace.datasets import load_conte69 left_surface, right_surface = load_conte69() vertexwise_data = [] for i in range(0, 2): vertexwise_data.append( map_to_labels( gradient_map.gradients_[:, i], schaefer_400, mask=schaefer_400 != 0, fill=np.nan, ) ) # plot_hemispheres(left_surface, right_surface, vertexwise_data, embed_nb=True)
# Visualization from brainspace.datasets import load_group_fc, load_parcellation, load_conte69 from brainspace.gradient import GradientMaps from brainspace.plotting import plot_hemispheres from brainspace.utils.parcellation import map_to_labels atlas = np.load("Z:\\hschoi\\backup\\hschoi\\template\\MMP\\MMP.10k_fs_LR.npy") pc_num = 2 ref_PCs = ref_PC[:, pc_num] X = ref_PCs labeling = atlas conn_matrix = X # X # ref_PCs mask = labeling != 0 grad = map_to_labels(conn_matrix, labeling, mask=mask, fill=np.nan) plot_hemispheres( surf_lh, surf_rh, array_name=grad, size=(1300, 200), color_bar=True, cmap='jet', zoom=1.85 ) #'jet' # 'viridis_r', 'Blues', , 'seismic' # color_range = (-0.1,0.16)
g1 = gm.gradients_[:, 0] print(g1.shape) #plt.imshow(g1) #plt.show() #print(g1) print(np.shape(gm.gradients_[:, 1])) mask = labeling != 0 grad = [None] * 4 for i in range(4): # map the gradient to the parcels grad[i] = map_to_labels(gm.gradients_[:, i], labeling, mask=mask, fill=np.nan) # Plot first gradient on the cortical surface. #plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(1200, 600), label_text=['Grad1', 'Grad2', 'Grad3', 'Grad4']) #plt.scatter(range(gm.lambdas_.size), gm.lambdas_) #plt.show() """ img=nib.load('brainspace/datasets/surfaces/mmpL.func.gii') img_data_L = [x.data for x in img.darrays] img=nib.load('brainspace/datasets/surfaces/mmpR.func.gii') img_data_R = [x.data for x in img.darrays]
from brainspace.gradient import GradientMaps gm = GradientMaps(n_components=2, random_state=0) gm.fit(correlation_matrix) ############################################################################### # Visualize results from brainspace.datasets import load_fsa5 from brainspace.plotting import plot_hemispheres from brainspace.utils.parcellation import map_to_labels # Map gradients to original parcels grad = [None] * 2 for i, g in enumerate(gm.gradients_.T): grad[i] = map_to_labels(g, labeling, mask=mask, fill=np.nan) # Load fsaverage5 surfaces surf_lh, surf_rh = load_fsa5() # sphinx_gallery_thumbnail_number = 2 plot_hemispheres(surf_lh, surf_rh, array_name=grad, size=(1200, 400), cmap='viridis_r', color_bar=True, label_text=['Grad1', 'Grad2'], zoom=1.5) ###############################################################################
plt.xlabel("t-statistic", fontdict={"fontsize": 16}) plt.ylabel("WFDC1 expression", fontdict={"fontsize": 16}) plt.plot(np.unique(df.x), np.poly1d(np.polyfit(df.x, df.y, 1))(np.unique(df.x)), "k") plt.text(-1.0, 0.75, f"r={df.x.corr(df.y):.2f}", fontdict={"size": 14}) plt.show() ######################################################################## # Plot WFDC1 gene to the surface. from brainspace.plotting.surface_plotting import plot_hemispheres from brainspace.utils.parcellation import map_to_labels vertexwise_WFDC1 = map_to_labels( expression["WFDC1"].to_numpy(), schaefer_100_fs5, mask=schaefer_100_fs5 != 0, fill=np.nan, ) plot_hemispheres( surfaces[0], surfaces[1], vertexwise_WFDC1, color_bar=True, embed_nb=True, size=(1400, 200), zoom=1.45, nan_color=(0.7, 0.7, 0.7, 1), cb__labelTextProperty={"fontSize": 12}, )
# show plot plt.show() # ## Functional gradients to fsaverage5 surface # In[152]: # Mask of the medial wall on fsaverage 5 mask_fs5 = labels_fs5 != 0 # Map gradients to original parcels grad = [None] * 3 for i, g in enumerate(gm.gradients_.T[0:3,:]): grad[i] = map_to_labels(g, labels_fs5, fill=np.nan, mask=mask_fs5) # Plot Gradients RdYlBu plot_hemispheres(fs5_lh, fs5_rh, array_name=grad, size=(1000, 600), cmap='coolwarm', embed_nb=True, label_text={'left':['Grad1', 'Grad2','Grad3']}, color_bar='left', zoom=1.25, nan_color=(1, 1, 1, 1) ) # ## Functional gradients to conte69 surface # In[153]: # mask of the medial wall mask_c69 = labels_c69 != 0