'dictionary not updating itself

Currently, I am working on generating a dictionary that will be used as a color palette with Napari. However, when I call the functions to generate different color palettes they end up being the same for the values generated by the second for loop and I do not understand why is this happening. This means:

color_dic_colorvision[label] = np.array([1.0, 0.0, 0.0, 1.0]) 
color_dic_colorblind[label] = np.array([1.0, 1.0, 0.0, 1.0])

and 

color_dic_colorvision[label_3d] = np.array[0.960784, 0.47451, 0.227451]
color_dic_colorblind[label_3d] = np.array [0.960784, 0.47451, 0.227451]

when label = 5000 and label_3d= 1

Am I explaining myself?

import napari
import random
import numpy as np
from skimage import io
from skimage.io import imread
import tifffile #import imread, imsave



path = "C:/Users/Xareni Galindo/Desktop/Annotation Helper Images/"
filename_Labels = "s9_DAPI_S2S3_Prediction_stack.tif"
image_Labels = io.imread(path+filename_Labels) #numpy array 70x228x228

path = "C:/Users/Xareni Galindo/Desktop/Annotation Helper Images/"
filename_Fluo = "s90_01_fluo.tif"
image_Fluo = io.imread(path+filename_Fluo) #numpy array 70x228x228

min_val = np.amin(image_Labels)
image_Labels = np.where(image_Labels > min_val, image_Labels + 20000, 0)  

color_dic_base={0:'black'}
labels = np.delete(np.unique(image_Labels), 0)  #numpy 152x1 
new_labels = np.arange(1, labels.shape[0] + 1000) # numpy 1151x1

base_labels_color =  np.array([1.0, 0.0, 0.0, 1.0]) 


def color_Palette(color, color_dictionary_1, labels_2d, labels_3d): 
    
    """ 
    color =  color base labels
    color dictionary_1 = color_dic base
    labels_ =  2d stardis labes 
    labels_3d = labels to put manually 
    
    """
    
    for label in labels_2d:
        color_dictionary_1[label] = color
        
    for label_3d in labels_3d:
        color_new_label =  np.random.rand(4)
        color_dictionary_1[label_3d] =  color_new_label  
        
    return color_dictionary_1
    
color_dic_colorvision =  color_Palette(base_labels_color, color_dic_base, labels, new_labels) #generating the color palette 
   
def colorBlind_Palette(color, color_dictionary_cb, labels_2d, labels_3d): 
    
    """ 
    color =  color base labels
    color dictionary_cb = color_dic base
    labels_ =  2d stardis labes 
    labels_3d = labels to put manually 
    
    """
    color_RGB_dic = {'1': np.asarray([0.96078431, 0.4745098 , 0.22745098]), '2': np.asarray([0.6627451 , 0.35294118, 0.63137255]), 
                     '3': np.asarray([0.52156863, 0.75294118, 0.97647059]), '4': np.asarray([0.05882353, 0.1254902 , 0.50196078]), 
                     '5': np.asarray([0.74117647, 0.72156863, 0.67843137]), '6': np.asarray([0.26666667, 0.45490196, 0.61568627])}  #'8': np.asarray([0.92156863, 0.90588235, 0.87843137]),

    for label in labels_2d:
        color_dictionary_cb[label] = color
    
    index = None 
    for label_3d in labels_3d:
        index_ = str(random.randint(1, 6))
        color_dictionary_cb[label_3d] =  color_RGB_dic[index_]
     
    return color_dictionary_cb

base_labels_color3 =  np.array([1.0, 1.0, 0.0, 0.0])
color_dic_colorblind = colorBlind_Palette(base_labels_color3, color_dic_base, labels, new_labels)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source