brzig
brzig

Reputation: 37

Unable to make a custom color scheme in Nilearn

I am trying to write code so that I am able to highlight lobes of the brain based on various continuous values. However, I am having trouble correctly accessing the indexes of the lobes I believe when calling the color scheme.

# Data for a reproducible answer
left_stage = {'Left_Temporal': 7.53381268745182, 'Left_Frontal': 3.2069698527141495, 'Left_Parietal': 15.321909355803081, 'Left_Occipital': 15.20597303571137, 'Left_MTL': 8.053567272349891}

# Atlases of interest
destrieux_atlas = datasets.fetch_atlas_surf_destrieux()
fsaverage = datasets.fetch_surf_fsaverage()
labels = destrieux_atlas['labels']

# Define the regions of interest for plotting
Temporal =  [
    b'G_temporal_inf', b'S_temporal_inf', b'G_temp_sup-G_T_transv', b'G_temp_sup-Lateral',
    b'G_temp_sup-Plan_polar', b'G_temp_sup-Plan_tempo', b'S_temporal_sup', b'S_temporal_transverse', b'S_temporal_transverse'
    ]

Frontal = [
    b'G_front_sup', b'S_front_sup', b'G_front_middle', b'G_front_middle',
    b'G_front_inf-Opercular', b'G_front_inf-Triangul', b'G_front_inf-Orbital',
    b'G_orbital', b'G_orbital', b'G_rectus', b'S_orbital_med-olfact', b'S_orbital-H_Shaped',
    b'G_precentral', b'S_precentral-inf-part', b'S_precentral-sup-part', b'G_and_S_paracentral'
]

Parietal =  [
    b'G_postcentral', b'S_postcentral', b'G_pariet_inf-Supramar',
    b'G_parietal_sup', b'S_intrapariet_and_P_trans', b'G_pariet_inf-Angular',
    b'G_precuneus'
]

Occipital = [
    b'G_oc-temp_med-Lingual', b'S_oc-temp_med_and_Lingual', b'S_calcarine', b'G_cuneus',
    b'G_occipital_middle', b'G_occipital_sup', b'S_oc_middle_and_Lunatus', b'S_oc_sup_and_transversal'
]

parcellation_left = destrieux_atlas['map_left']
custom_map_left = np.zeros(parcellation_left.shape[0], dtype=int)

temporal_region_indices = [labels.index(region) for region in Temporal]
temporal_labels = np.concatenate([np.where(parcellation_left == idx)[0] for idx in temporal_region_indices])
custom_map_left[temporal_labels] = 1

frontal_region_indices = [labels.index(region) for region in Frontal]
frontal_labels = np.concatenate([np.where(parcellation_left == idx)[0] for idx in frontal_region_indices])
custom_map_left[frontal_labels] = 2

occipital_region_indices = [labels.index(region) for region in Occipital]
occipital_labels = np.concatenate([np.where(parcellation_left == idx)[0] for idx in occipital_region_indices])
custom_map_left[occipital_labels] = 3

parietal_region_indices = [labels.index(region) for region in Parietal]
parietal_labels = np.concatenate([np.where(parcellation_left == idx)[0] for idx in parietal_region_indices])
custom_map_left[parietal_labels] = 4

mtl_region_indices = [labels.index(region) for region in MTL]
mtl_labels = np.concatenate([np.where(parcellation_left == idx)[0] for idx in mtl_region_indices])
custom_map_left[mtl_labels] = 5

# Define a function to map values to colors
def map_value_to_color(subtype_stage):
    cmap = nilearn_cmaps["black_blue_r"]
    norm = plt.Normalize(vmin=0, vmax=10)
    
    colors = [(1,1,1)]
    
    for value in subtype_stage:
        
        if value < 0:
             colors.append((1, 1, 1))
        elif value >= 0 and value < 10:
            colors.append(cmap(norm(value)))
        else:
            colors.append((0, 0, 0))
    
    return  mcolors.LinearSegmentedColormap.from_list('Custom', colors)


cmap = nilearn_cmaps["black_blue_r"]
norm = plt.Normalize(vmin=0, vmax=10)

colors = [(1,1,1)]

for value in left_stage:

    if value < 0:
         colors.append((1, 1, 1))
    elif value >= 0 and value < 10:
        colors.append(cmap(norm(value)))
    else:
        colors.append((0, 0, 0))
        
cmap_left = mcolors.LinearSegmentedColormap.from_list('Custom', colors)

img = plotting.plot_surf_roi(
    fsaverage['pial_left'],
    roi_map = custom_map_left,
    hemi="left",
    view="lateral",
    cmap=cmap_left,
    darkness=.6,
    bg_map=fsaverage['sulc_left']
)

This code however incorrectly colors the occipital lobe as an example Brain that is produced by the example

Clearly based on the data the occipital lobe has a value of greater than 10 and based if else it should be assigned black ((0,0,0)). However, its the off greenish blue.

The way that I understand how to provide a custom color map is that I create a custom ROI map which then maps the values in that array to the values based on the corresponding indicies in the colormap. However, this is clearly not working.

Any ideas?

Upvotes: 0

Views: 27

Answers (0)

Related Questions