Ejemplo n.º 1
0
    def plot(self, resultDict=None, show=False):
        if resultDict == None:
            f=open(os.path.join(self.outFolder, self.outFile))
            resultDict= pickle.load(f); f.close()
            
        if type(resultDict.keys()[0])==str:
            #meaning we are dealing with many alg results
            pass
        elif type(resultDict.keys()[0])==tuple:
        #plotting legend plot
            param_set = resultDict.keys()[0]
            try:
                print self.classNames
            except AttributeError:
                self.classNames = resultDict[param_set].keys()
            f=p.figure(figsize=(17,17))
            ax=f.add_subplot(111)
            
            for morpho in resultDict[param_set]:
                r = resultDict[param_set][morpho]
                colors = makeColorRamp(17, hex_output=True)
                ax.errorbar(np.mean(r[1]), np.mean(r[0]), xerr = np.std(r[1]), yerr=np.std(r[0]), 
                            color =colors[self.classNames.index(morpho)], fmt = markers[methods.index(self.algo)],
                           label = '{} {}'.format(self.algo, morpho))
            ax.legend()
            f.suptitle("Algorithm {}".format(self.algo))
            p.savefig(os.path.join(self.outFolder, 'legend {}.png'.format(self.algo)))
            
        #plotting result plot, one for all classes and one for each morphological class
            plots = {morphological_class : p.subplots(2, sharex=True) for morphological_class in self.classNames}
            
            f=p.figure(figsize=(17,17))
            ax=f.add_subplot(121)
            
            for param_set in resultDict:
                param_dict=dict(param_set)
                for morpho in resultDict[param_set]:
                    r = resultDict[param_set][morpho]
                    ax.errorbar(np.mean(r[1]), np.mean(r[0]), xerr = np.std(r[1]), yerr=np.std(r[0]), 
                            color =colors[self.classNames.index(morpho)], fmt = markers[methods.index(self.algo)],
                           label = '{} {}'.format(self.algo, morpho))
                    
                    plots[morpho][1][0].errorbar(np.mean(r[1]), np.mean(r[0]), xerr = np.std(r[1]), yerr=np.std(r[0]), 
                            color =colors[whiten_param.index(param_dict['whiten'])], fmt = markers[pca_param.index(param_dict['pca'])],
                           label = 'W{} PCA {}'.format(param_dict['whiten'],param_dict["pca"]))
                    plots[morpho][1][0].grid(True)
                    plots[morpho][1][0].set_xlim(0,1)
                    plots[morpho][1][0].set_ylim(0,1)
                    plots[morpho][1][0].set_xlabel("Nb of class elements in cluster/cluster size")
                    plots[morpho][1][0].set_ylabel("Nb of class elements in cluster/class size")
                    #plots[morpho][1][0].legend()

            ax.grid(True)
            ax.set_xlim(0,1)
            ax.set_ylim(0,1)
            ax.set_xlabel("Nb of class elements in cluster/cluster size")
            ax.set_ylabel("Nb of class elements in cluster/class size")
            
            ax=f.add_subplot(122)
            for param_set in resultDict:
                param_dict=dict(param_set)
                for morpho in resultDict[param_set]:
                    r = resultDict[param_set][morpho]
                    ax.errorbar(np.mean(r[3]), np.mean(r[2]), xerr = np.std(r[3]), yerr=np.std(r[2]), 
                            color =colors[self.classNames.index(morpho)], fmt = markers[methods.index(self.algo)],
                           label = '{} {}'.format(self.algo, morpho))
                    
                    plots[morpho][1][1].errorbar(np.mean(r[3]), np.mean(r[2]), xerr = np.std(r[3]), yerr=np.std(r[2]), 
                            color =colors[whiten_param.index(param_dict['whiten'])], fmt = markers[pca_param.index(param_dict['pca'])],
                           label = 'W{} PCA {}'.format(param_dict['whiten'],param_dict["pca"]))
                    plots[morpho][1][1].grid(True)
                    plots[morpho][1][1].set_xlim(0,1)
                    plots[morpho][1][1].set_ylim(0,1)
                    plots[morpho][1][1].set_xlabel("Entropy of cluster which is the one with the most elements from this class")
                    plots[morpho][1][1].set_ylabel("Entropy of class with regard to the clustering")
            ax.grid(True)
            ax.set_xlim(0,1)
            ax.set_ylim(0,1)
            ax.set_xlabel("Entropy of cluster which is the one with the most elements from this class")
            ax.set_ylabel("Entropy of class with regard to the clustering")
            

            f.suptitle("Algorithm {}".format(self.algo))
            
            for morpho in plots:
                plots[morpho][0].suptitle("Algorithm {}, class {}".format(self.algo, morpho))
                plots[morpho][0].savefig(os.path.join(self.outFolder, 'figure {} {}.png'.format(morpho, self.algo)))
            
            if show:
                p.show()
            else:
                p.savefig(os.path.join(self.outFolder, 'figure {}.png'.format(self.algo)))
Ejemplo n.º 2
0
def plotMoy(outFolder='../resultData/nucleus', show=False):
    f=p.figure(figsize=(24,17))
    
    for i, algo in enumerate(["gaussianmixture", 'mini-batchk-means', 'kernelk-means','spectralclustering']):
        file_=open(os.path.join(outFolder, outFile.format(algo)), 'r')
        resultDict = pickle.load(file_); file_.close()
    
        perClass = []; perCluster = []
        for parameter_set in resultDict:
            currLperClass=[]
            currLperCluster = []
            for morphoClass in resultDict[parameter_set]:
                currLperClass.append(np.mean(resultDict[parameter_set][morphoClass][0]))
                currLperCluster.append(np.mean(resultDict[parameter_set][morphoClass][1]))
            perClass.append(currLperClass)
            perCluster.append(currLperCluster)
            
        
        ax=f.add_subplot(2,2,i)
        for k in range(len(perClass)):
            ax.errorbar(np.mean(perCluster[k]), np.mean(perClass[k]), xerr = np.std(perCluster[k]), yerr=np.std(perClass[k]), 
                            fmt = markers[methods.index(algo)],
                           label = '{}'.format(algo))
        ax.grid(True)
        ax.set_xlim(0,1)
        ax.set_ylim(0,1)
        ax.set_title("Algorithm {}, on average on all morphological classes".format(algo))
        ax.set_xlabel("Nb of class elements in cluster/cluster size")
        ax.set_ylabel("Nb of class elements in cluster/class size")
    if show:
        p.show()
    else:
        p.savefig(os.path.join(outFolder, 'figure_MOY.png'.format(algo)))
    
    f=p.figure(figsize=(24,17))
    for i, algo in enumerate(["gaussianmixture", 'mini-batchk-means', 'kernelk-means','spectralclustering']):
        file_=open(os.path.join(outFolder, outFile.format(algo)), 'r')
        resultDict = pickle.load(file_); file_.close()
    
        perClass = []; perCluster = []
        for parameter_set in resultDict:
            currLperClass=[]
            currLperCluster = []
            for morphoClass in resultDict[parameter_set]:
                currLperClass.append(np.mean(resultDict[parameter_set][morphoClass][2]))
                currLperCluster.append(np.mean(resultDict[parameter_set][morphoClass][3]))
            perClass.append(currLperClass)
            perCluster.append(currLperCluster)
            
        
        ax=f.add_subplot(2,2,i)
        for k in range(len(perClass)):
            ax.errorbar(np.mean(perCluster[k]), np.mean(perClass[k]), xerr = np.std(perCluster[k]), yerr=np.std(perClass[k]), 
                            fmt = markers[methods.index(algo)],
                           label = '{}'.format(algo))
        ax.grid(True)
        ax.set_xlim(0,1)
        ax.set_ylim(0,1)
        ax.set_title("Algorithm {}, on average on all morphological classes".format(algo))
        ax.set_xlabel("Cluster entropy")
        ax.set_ylabel("Class entropy")
    if show:
        p.show()
    else:
        p.savefig(os.path.join(outFolder, 'figure_ENT_MOY.png'.format(algo)))