I conducted a clustering analysis within each individual ROI for a given atlas. I’m hoping to visualize the atlas such that each ROI is assigned a value according to the number of clusters for that ROI. I can’t seem to figure out how to re-assign the values of an atlas, so that each label is assigned a unique value based on the analysis.
Hi there, I think the code below might help. Otherwise, you can use plot_markers, but the code below will display the new value at all voxels assigned to that ROI, whereas plot_markers uses the ROI coordinates to plot a single value with a spherical marker.
import numpy as np
import nibabel as nib
import nilearn
from nilearn import datasets, plotting
# load your atlas (here just fetching one from the database)
parcellations = datasets.fetch_atlas_basc_multiscale_2015(version='sym')
networks_64 = parcellations['scale064']
atlas_img = networks_64
atlas = nilearn.image.load_img(atlas_img)
atlas_data = atlas.get_fdata()
clusters=np.arange(65,129) # cluster data; px1 array for atlas with p ROIs
nodes = np.unique(atlas_data) # number of unique ROIs
if np.isin(0,nodes): # in many cases 0 is not considered the first ROI; remove it
nodes = np.delete(nodes,0)
data = np.zeros(atlas_data.shape, dtype=np.float32) # create empty matrix the size of the original atlas.
for i,n in enumerate(nodes): # assign new (cluster) values to atlas ROIs
data[atlas_data == n] = clusters[i]
new_img = nib.Nifti1Image(data, atlas.affine, atlas.header) # save as nifti image so we can plot
colormap_min = 65 # set up colorbar max and min to your liking
colormap_max = 129
plotting.plot_roi(new_img, cmap=plotting.cm.bwr_r,vmin=colormap_min, vmax=colormap_max, title='Clusters')
No problem. As a sanity check you may just want to ensure that your atlas ROIs are numerically labeled in the same order as the “clusters” vector (e.g. 1st value in the vector corresponds to the lowest-value ROI, last value in the vector corresponds to the highest-value) as that is assumed with this code.