Map Yan2023_homotopic parcellation regions to 91k grayordinates

Hello All,

I am trying to mimic the output of hcp.utils.mmp.map_all but with different parcellation, in my case, I am using Yan2023_homotopic.

I am using the script below adapted from the hcp-utils script to get 91k array with labeled regions:

yan_23 = nib.load('/om2/scratch/Mon/gelbanna/parcellations/HCP/fsLR32k/kong17/100Parcels_Kong2022_17Networks.dlabel.nii')
# rois of grayordinates in the cortex
rois = yan_23.dataobj[0].astype(int)
parcels = 100

# change ids so that smaller numbers are in the left hemisphere
# for consistency with Cole-Anticevic parcellation
flip_ordering = np.array([0] + list(range(int((parcels/2)+1), parcels+1)) + list(range(1, int((parcels/2)+1))))
rois = flip_ordering[rois]

# extract parcel names and colors

axis0=yan_23.header.get_index_map(0)
nmap=list(axis0.named_maps)[0]

keys = [0]
labels = ['']
rgba = [(0.0, 0.0, 0.0, 0.0)]

# left hemisphere
for i in range(int((parcels/2)+1), parcels+1):
    roi = nmap.label_table[i]
    labels.append(roi.label[11:])
    rgba.append((roi.red, roi.green, roi.blue, roi.alpha))
    keys.append(i - int(parcels/2))

# right hemisphere
for i in range(1, int((parcels/2)+1)):
    roi = nmap.label_table[i]
    labels.append(roi.label[11:])
    rgba.append((roi.red, roi.green, roi.blue, roi.alpha))
    keys.append(i + int(parcels/2))


# extend the cortical parcellation by the standard subcortical structures from CIFTI-2

map_all = np.zeros(91282, dtype=int)

map_all[:59412] = rois

accumbens_left_=slice(59412,59547)
accumbens_right_=slice(59547,59687)
amygdala_left_=slice(59687,60002)
amygdala_right_=slice(60002,60334)
brainStem_=slice(60334,63806)
caudate_left_=slice(63806,64534)
caudate_right_=slice(64534,65289)
cerebellum_left_=slice(65289,73998)
cerebellum_right_=slice(73998,83142)
diencephalon_left_=slice(83142,83848)
diencephalon_right_=slice(83848,84560)
hippocampus_left_=slice(84560,85324)
hippocampus_right_=slice(85324,86119)
pallidum_left_=slice(86119,86416)
pallidum_right_=slice(86416,86676)
putamen_left_=slice(86676,87736)
putamen_right_=slice(87736,88746)
thalamus_left_=slice(88746,90034)
thalamus_right_=slice(90034,None)

structures_subcortical=[accumbens_left_, accumbens_right_, amygdala_left_, amygdala_right_,
                        caudate_left_, caudate_right_, cerebellum_left_, cerebellum_right_, diencephalon_left_, diencephalon_right_,
                        hippocampus_left_, hippocampus_right_, pallidum_left_, pallidum_right_, putamen_left_, putamen_right_,
                        thalamus_left_, thalamus_right_, brainStem_]

names_subcortical=['accumbens_left', 'accumbens_right', 'amygdala_left', 'amygdala_right',
                   'caudate_left', 'caudate_right', 'cerebellum_left', 'cerebellum_right', 'diencephalon_left', 'diencephalon_right',
                   'hippocampus_left', 'hippocampus_right', 'pallidum_left', 'pallidum_right', 'putamen_left', 'putamen_right',
                   'thalamus_left', 'thalamus_right', 'brainStem']

num_subcortical = len(structures_subcortical)

for i in range(num_subcortical):
    struct = structures_subcortical[i]
    name = names_subcortical[i]
    map_all[struct] = int((parcels/2)+1) + i
    keys.append(int((parcels/2)+1) + i)

labels.extend(names_subcortical)

# random colors for subcortical parts
np.random.seed(555)
colors2 = np.random.uniform(size=((num_subcortical-1)//2, 4))
colors1 = 0.8 * colors2

rgba_sc = np.zeros((num_subcortical, 4))
rgba_sc[:-1][::2] = colors1
rgba_sc[1::2] = colors2
rgba_sc[:,3] = 1.0 


labels = np.array(labels)
rgba = np.vstack((np.array(rgba), rgba_sc))
keys = np.array(keys)


np.savez_compressed('yan23_kong17_100parcels.npz', map_all=map_all, labels=labels, rgba=rgba, ids=keys)

The issue is that the rois array has 64k vertices and after removing the medial wall regions (indices with zero value) it gives 58611 vertices instead of 59412. I believe that the parcellation file was resampled from fsaverage6 space to fsLR32k making the medial wall not the same as the cifti file.

How can I align the dlabel file with the cortex grayordinates (59412)?

I solved the issue by using the a medial wall mask for HCP here to extract the 59412 cortical vertices from the parcellation file. Then, reinserting the subcortical regions, following CIFTI-2 structure, to get a 91k parcellation array similar to the ones preloaded in the hcp-utils package.

Here’s the updated code:

import numpy as np
import scipy.io
import nibabel as nib

parcels = 1000
yan_23 = nib.load(f'../parcellations/HCP/fsLR32k/kong17/{parcels}Parcels_Kong2022_17Networks.dlabel.nii')
# rois of grayordinates in the cortex
rois = yan_23.dataobj[0].astype(int)

mat = scipy.io.loadmat('../fs_LR_32k_medial_mask.mat')
mask = mat['medial_mask']
rois = rois[np.where(mask!=0)[0]]

# extract parcel names and colors
axis0=yan_23.header.get_index_map(0)
nmap=list(axis0.named_maps)[0]

keys = [0]
labels = ['']
rgba = [(0.0, 0.0, 0.0, 0.0)]

for i in range(1, parcels+1):
    roi = nmap.label_table[i]
    labels.append(roi.label[11:])
    rgba.append((roi.red, roi.green, roi.blue, roi.alpha))
    keys.append(i)

# extend the cortical parcellation by the standard subcortical structures from CIFTI-2
map_all = np.zeros(91282, dtype=int)
map_all[:59412] = rois

accumbens_left_=slice(59412,59547)
accumbens_right_=slice(59547,59687)
amygdala_left_=slice(59687,60002)
amygdala_right_=slice(60002,60334)
brainStem_=slice(60334,63806)
caudate_left_=slice(63806,64534)
caudate_right_=slice(64534,65289)
cerebellum_left_=slice(65289,73998)
cerebellum_right_=slice(73998,83142)
diencephalon_left_=slice(83142,83848)
diencephalon_right_=slice(83848,84560)
hippocampus_left_=slice(84560,85324)
hippocampus_right_=slice(85324,86119)
pallidum_left_=slice(86119,86416)
pallidum_right_=slice(86416,86676)
putamen_left_=slice(86676,87736)
putamen_right_=slice(87736,88746)
thalamus_left_=slice(88746,90034)
thalamus_right_=slice(90034,None)

structures_subcortical=[accumbens_left_, accumbens_right_, amygdala_left_, amygdala_right_,
                        caudate_left_, caudate_right_, cerebellum_left_, cerebellum_right_, diencephalon_left_, diencephalon_right_,
                        hippocampus_left_, hippocampus_right_, pallidum_left_, pallidum_right_, putamen_left_, putamen_right_,
                        thalamus_left_, thalamus_right_, brainStem_]

names_subcortical=['accumbens_left', 'accumbens_right', 'amygdala_left', 'amygdala_right',
                   'caudate_left', 'caudate_right', 'cerebellum_left', 'cerebellum_right', 'diencephalon_left', 'diencephalon_right',
                   'hippocampus_left', 'hippocampus_right', 'pallidum_left', 'pallidum_right', 'putamen_left', 'putamen_right',
                   'thalamus_left', 'thalamus_right', 'brainStem']

num_subcortical = len(structures_subcortical)

for i in range(num_subcortical):
    struct = structures_subcortical[i]
    name = names_subcortical[i]
    map_all[struct] = int(parcels+1) + i
    keys.append(int(parcels+1) + i)

labels.extend(names_subcortical)

# random colors for subcortical parts
np.random.seed(555)
colors2 = np.random.uniform(size=((num_subcortical-1)//2, 4))
colors1 = 0.8 * colors2
rgba_sc = np.zeros((num_subcortical, 4))

labels = np.array(labels)
rgba = np.vstack((np.array(rgba), rgba_sc))
keys = np.array(keys)

np.savez_compressed(f'yan23_kong17_{parcels}parcels.npz', map_all=map_all, labels=labels, rgba=rgba, ids=keys)
2 Likes