dimanche 6 février 2022

dictionary not updating itself

currently I am working on generating a window with a check box on it. I want the window to run a function to generate a dictionary by default, change the values of this dictionary when the check button is clicked and get back to the values generated from the default function if is unchecked. The problem is that when I un-check the checkbox the values of the dictionary do not change. I am very new to object-oriented programing and I really do not understand why this is happening

The data I am working on are segmented cell nuclei images that I got from StarDist 2D


path = "C:/Users/xarss/Desktop/GUI-annotation-helper/"
filename_Labels = "s9_DAPI_S2S3_Prediction_stack.tif"
image_Labels = io.imread(path+filename_Labels) #numpy array 70x228x228

path = "C:/Users/xarss/Desktop/GUI-annotation-helper/"
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]) 

class basicWindow(QWidget):
   def __init__(self, window_title = 'CheckBox Example', x= 1400, y=100, width=300, height=300): 
       super().__init__()
       
       self.__grid = QGridLayout()
       self.__grid.setSpacing(30)
       self.setLayout(self.__grid)
       self.setGeometry(x, y, width, height)
       self.setWindowTitle(window_title)
       
       self.checkBoxA = QCheckBox("Select This.")
       self.checkBoxA.stateChanged.connect(self.checkBoxChangedAction)
       
       self.__grid.addWidget(self.checkBoxA, 1, 2, 1, 1)
       
       
   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 
       
       """
       
       print("Norman color palette")  
       
       for label in labels_2d:
           color_dictionary_1[label] = color
           
       for label in labels_3d:
           color_new_label =  np.random.rand(4)
           color_dictionary_1[label] =  color_new_label  
           
       return color_dictionary_1
       
   color_palette =  color_Palette(base_labels_color, color_dic_base, labels, new_labels) #generating the color palette 
   print(color_palette[52769])
   print(color_palette[10])
   def checkBoxChangedAction(self, state):
       """ Function to call functions if the check box is check or un-check
       
       """
       
       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]),
           
           
           print("color blind palette")
    
           
           for label in labels_2d:
               color_dictionary_cb[label] = color
           
           index = None 
           for label in labels_3d:
               index_ = str(random.randint(1, 6))
               color_dictionary_cb[label] =  color_RGB_dic[index_]
            
           return color_dictionary_cb
        
       
       if (QtCore.Qt.Checked == state):
           print("box was checked")
           base_labels_color3 =  np.array([2.0, 2.0, 2.0, 2.0])
           Color_dic_colorblind = colorBlind_Palette(base_labels_color3, color_dic_base, labels, new_labels)
           print(len(Color_dic_colorblind))
           print(Color_dic_colorblind[52769])
           print(Color_dic_colorblind[10])
       else:
           
           print("box was un-checked")
           #base_labels_color2 =  np.array([1.0, 1.0, 1.0, 1.0])
           color_palette = basicWindow().color_palette
           
           print(len(color_palette))
           print(color_palette[52769])
           print(color_palette[10])


if __name__ == '__main__':
    app = QApplication(sys.argv)
    windowExample = basicWindow()
    windowExample.show()
    sys.exit(app.exec_())

 




Aucun commentaire:

Enregistrer un commentaire