Exemplo n.º 1
0
def addLogicleAxes(axis,ticks):
    #Add appropriate xtick values (also ytick values if kde) for each axis in figure
    for ax in ticks:
        tickValues,tickLabels = returnTicks(ticks[ax])
        if ax == 'x':
            axis.set_xticks(tickValues)
            axis.set_xticklabels(tickLabels)
        else:
            axis.set_yticks(tickValues)
            axis.set_yticklabels(tickLabels)
Exemplo n.º 2
0
 def exportDimRed():
     ods.savePostProcessedFile(self.reducedData, dataSubsetTitle,
                               'reduce', dimRedType,
                               self.currentReductionParameters)
     print('Dimensional Reduction Saved!')
     if plotAllDimReds.get():
         if 'Event' in plottingDfReducedWithFeatures.columns or 'event' in plottingDfReducedWithFeatures.columns:
             sizeParam = {'s': 5}
         else:
             sizeParam = {}
         for feature in pd.unique(scaledData.columns):
             g = sns.relplot(data=plottingDfReducedWithFeatures,
                             x='Dimension 1',
                             y='Dimension 2',
                             hue=feature,
                             palette='coolwarm',
                             alpha=0.7,
                             **sizeParam)
             leg = g._legend
             if max(plottingDfReducedWithFeatures[feature]
                    ) > 100 and len(sizeParam) > 0:
                 a, b = returnTicks([-1000, 100, 10000, 100000])
                 for t, l in zip(leg.texts[1:], (b)):
                     t.set_text(l)
             else:
                 for t in leg.texts:
                     # truncate label text to 4 characters
                     if t.get_text() == '1.2000000000000002':
                         t.set_text('1.0')
                     else:
                         if '.' in t.get_text():
                             if t.get_text().replace('.', '',
                                                     1).isdigit():
                                 decimalIndex = t.get_text().find('.')
                                 t.set_text(
                                     round(float(t.get_text()), 2))
             reducedName = ods.getFileName(
                 dataSubsetTitle,
                 'reduce',
                 dimRedType,
                 self.currentReductionParameters,
                 fileExtension='.png')[0]
             subprocess.run([
                 'mkdir',
                 'plots/' + reducedName[:-4] + '-featureColoredPlots'
             ])
             plt.savefig('plots/' + reducedName[:-4] +
                         '-featureColoredPlots/' + str(feature) +
                         '.png',
                         bbox_inches='tight')
             plt.clf()
             print(feature + ' plot saved')
 def exportDataFrames():
     ods.savePostProcessedFile(self.clusterdf,dataSubsetTitle,'cluster',clusteringMethod,self.currentClusteringParameters)
     df2 = self.clusterdf.copy() 
     df3 = df2.groupby(list(df2.index.names)[-1]).mean()
     print(df2)
     if self.clusterdf.copy().values.max() > 100:
         mfiTicks = [-1000,100,1000,10000,100000]
         mfiTickValues,mfiTickLabels = returnTicks(mfiTicks)
         cg = sns.clustermap(df3.T,cbar_kws={'label':'MFI','ticks':mfiTickValues})
         cg.cax.set_yticklabels(mfiTickLabels)
     else:
         cg = sns.clustermap(df3.T,cbar_kws={'label':'Metric'})
     plt.setp(cg.ax_heatmap.xaxis.get_majorticklabels(), rotation=0)
     plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
     clustermapName = ods.getFileName(dataSubsetTitle,'cluster',clusteringMethod,self.currentClusteringParameters,fileExtension = '.png')[0]
     plt.savefig('plots/clustermap-'+clustermapName,bbox_inches='tight')
     plt.clf()
     print('Clustered Data Frame And Phenotype Plot Saved')
Exemplo n.º 4
0
def updateDropdownControlledPlot(frameCanvas,plotAxis,plottingDf,levelVars,xColumn,yColumn,alpha=0.3,legendoffset=-0.1,trueLabelDict = [],levelDropdown = [],levelValueDropdown = [],axisLimits=[]):
    plotAxis.clear()

    parameters = [levelVars['hue'].get(),levelVars['style'].get(),levelVars['size'].get()]
    parameters2 = ['hue','style','size']
    newkwargs = {'x':xColumn,'y':yColumn}
    numLegendElements = 0
    for parameter,parameter2 in zip(parameters,parameters2):
        if parameter != 'None':
            newkwargs[parameter2] = parameter
            val = plottingDf[parameter][0]
            if isinstance(val,(int,float,np.integer)):
                numLegendElements+=4
            else:
                numLegendElements+=len(pd.unique(plottingDf[parameter]))
    
    features = list(plottingDf.columns)[list(plottingDf.columns).index('Dimension 2')+1:-1]
    if 'hue' in newkwargs:
        if newkwargs['hue'] in features and max(plottingDf[features[0]]) > 100:
            featureHueBool = True
        else:
            featureHueBool = False 
    else:
        featureHueBool = False 
    if not isinstance(trueLabelDict,list):
        orderDict = returnOriginalOrders(trueLabelDict,plottingDf,newkwargs,'2d')
    else:
        orderDict = {}
    if 'hue' in newkwargs.keys():
        if not isinstance(plottingDf[newkwargs['hue']][0],str):
            print('COOLWARM')
            palette = 'coolwarm'
            newkwargs['palette'] = palette
        else:
            print('NONE')
            palette = sns.color_palette(sns.color_palette(),len(pd.unique(plottingDf[newkwargs['hue']]))) 
            newkwargs['palette'] = palette
    if 'hue' in newkwargs.keys():
        if newkwargs['hue'] == 'Cluster':
            clusters = list(pd.unique(plottingDf['Cluster']))
            tempDict = {}
            for i in range(len(clusters)):
                cluster = clusters[i]
                numeric = re.findall(r'\d+', str(cluster))
                tempDict[int(numeric[0])] = cluster
            sortedTempDict = OrderedDict(sorted(tempDict.items()))
            orderDict['hue_order'] = list(sortedTempDict.values())
    if featureHueBool:
        hueNormKwargs = {'hue_norm':(0,1000)}
    else:
        hueNormKwargs = {} 
    if type(levelValueDropdown) != list:
        individualLevelValue = levelValueDropdown.get()
        if individualLevelValue != 'All' and individualLevelValue != '':
            subsetValueBool = True
            truePlottingDf = plottingDf.copy()[plottingDf[levelDropdown.get()] == individualLevelValue]
        else:
            subsetValueBool = False
            truePlottingDf = plottingDf.copy()
    else:
        truePlottingDf = plottingDf.copy()
        subsetValueBool = False
        individualLevelValue = 'All'
    if 'Event' in plottingDf.columns or 'event' in plottingDf.columns:
        g3 = sns.scatterplot(data=truePlottingDf,ax=plotAxis,alpha=0.7,s=3,**newkwargs,**orderDict,**hueNormKwargs)
        if subsetValueBool:
            plotAxis.set_xlim(axisLimits[0])
            plotAxis.set_ylim(axisLimits[1])
    else:
        g3 = sns.scatterplot(data=plottingDf,ax=plotAxis,alpha=0.7,**newkwargs,**orderDict,**hueNormKwargs)
    if 'hue' in newkwargs.keys():
        if newkwargs['hue'] == 'Cluster':
            clusterCentroids = get_cluster_centroids(truePlottingDf)
            for i in range(len(clusterCentroids)):
                g3.annotate(clusterCentroids[i][0],xy=clusterCentroids[i][1])
    legendSpillover = 15
    leg = g3.legend(loc='center right', bbox_to_anchor=(legendoffset, 0.5), ncol=math.ceil(numLegendElements/legendSpillover),framealpha=0)
    if featureHueBool:
        a,b = returnTicks([-1000,100,10000,100000])
        for t, l in zip(leg.texts[1:],(b)):
            t.set_text(l)
    
    frameCanvas.draw()
Exemplo n.º 5
0
def updateDropdownControlledCompositionPlot(frameCanvas,plotAxis,plottingDf,trueLabelDict,levelVars,legendoffset=1.7):
    plotAxis.clear()
    if not isinstance(plottingDf,list):
        parameters = [levelVars['x'].get(),levelVars['y'].get(),levelVars['hue'].get()]
        for i,parameter in enumerate(parameters):
            if parameter == 'Group':
                parameters[i] = 'Cluster'
        parameters2 = ['x','y','hue']
        newkwargs = {}
        for parameter,parameter2 in zip(parameters,parameters2):
            if parameter != 'None':
                newkwargs[parameter2] = parameter
        #scatter plot (2d)
        if 'x' in newkwargs and 'y' in newkwargs and newkwargs['y'] not in ['frequency','log-frequency','percent']:
            orderedClusters = list(map(str,sorted(list(map(int,list(pd.unique(plottingDf['Cluster'])))))))
            modifiedNewKwargs = newkwargs.copy()
            featureX = modifiedNewKwargs.pop('x')
            featureY = modifiedNewKwargs.pop('y')
            valuesX = list(plottingDf[plottingDf['Feature'] == featureX]['Metric'])
            valuesY = list(plottingDf[plottingDf['Feature'] == featureY]['Metric'])
            featureHueBool = False
            paletteKwargs = {}
            if 'hue' in newkwargs:
                if newkwargs['hue'] in list(pd.unique(plottingDf['Feature'])):
                    featureHue = modifiedNewKwargs.pop('hue')
                    valuesHue = list(plottingDf[plottingDf['Feature'] == featureHue]['Metric'])
                    featureHueBool = True
                    palette = 'coolwarm'
                else:
                    if newkwargs['hue'] == 'Time':
                        palette = 'coolwarm'
                    else:
                        palette = sns.color_palette(sns.color_palette(),len(pd.unique(plottingDf[newkwargs['hue']])))
                paletteKwargs['palette'] = palette
            orderDict = returnOriginalOrders(trueLabelDict,plottingDf,modifiedNewKwargs,'1.5d')
            plottingDf = plottingDf[plottingDf['Feature'] == plottingDf['Feature'][0]]
            plottingDf[featureX] = valuesX
            plottingDf[featureY] = valuesY
            if featureHueBool:
                plottingDf[featureHue] = valuesHue
                hueNormKwargs = {'hue_norm':(0,1000)}
            else:
                hueNormKwargs = {}
            hue_orderDict = {}
            if newkwargs['hue'] == 'Cluster':
                hue_orderDict['hue_order'] = orderedClusters 
            else:
                if 'hue_order' in orderDict:
                    hue_orderDict['hue_order'] = orderDict['hue_order'] 
            g3 = sns.scatterplot(data=plottingDf,**newkwargs,**hue_orderDict,**paletteKwargs,s=5,alpha=0.7,**hueNormKwargs)
            leg = g3.legend(loc='center right', bbox_to_anchor=(legendoffset, 0.5), ncol=1,framealpha=0)
            
            if featureHueBool:
                a,b = returnTicks([-1000,100,10000,100000])
                for t, l in zip(leg.texts[1:],(b)):
                    t.set_text(l)
            
            if max(plottingDf['Metric']) > 100:
                tickDict = {}
                ticks = [-1000,100,1000,10000,100000]
                tickDict['x'] = ticks
                tickDict['y'] = ticks
                addLogicleAxes(plotAxis,tickDict)
        
            #Rotate index labels if x not cluster
            #If feature plot on x axis, change x axis name
             
        #barplot (1.5d) or kde (1d)
        else:
            if newkwargs['y'] in ['frequency','log-frequency','percent']:
                yaxisFeature = newkwargs.pop('y')
            else:
                yaxisFeature = newkwargs['y']
            orderedClusters = list(map(str,sorted(list(map(int,list(pd.unique(plottingDf['Cluster'])))))))
            if newkwargs['x'] in list(pd.unique(plottingDf['Feature'])):
                modifiedNewKwargs = newkwargs.copy()
                feature = modifiedNewKwargs.pop('x')
                featureBool = True
                newkwargs['x'] = 'Metric'
            else:
                modifiedNewKwargs = newkwargs.copy()
                featureBool = False
            print(modifiedNewKwargs)
            print(newkwargs)
            orderDict = returnOriginalOrders(trueLabelDict,plottingDf,modifiedNewKwargs,'1.5d')
            if not featureBool:
                palette = sns.color_palette(sns.color_palette(),len(pd.unique(plottingDf['Cluster'])))
            else:
                if 'hue' in newkwargs:
                    palette = sns.color_palette(sns.color_palette(),len(pd.unique(plottingDf[newkwargs['hue']])))
                else:
                    palette = sns.color_palette(sns.color_palette(),1)
            kdePlotBool = False
            if yaxisFeature in ['frequency','log-frequency']:
                if is_number(plottingDf[newkwargs['x']][0]) and len(pd.unique(plottingDf[newkwargs['x']])) > 4 and newkwargs['x'] != 'Cluster':
                    if not featureBool:
                        #"Unstack" dataframe
                        plottingDf = plottingDf[plottingDf['Feature'] == plottingDf['Feature'][0]]
                        for i,cluster in enumerate(orderedClusters):
                            g3 = sns.kdeplot(plottingDf[plottingDf['Cluster'] == cluster][newkwargs['x']],color=palette[i],ax=plotAxis,shade=True,label=cluster)
                    else:
                        plottingDf = plottingDf[plottingDf['Feature'] == feature]
                        if newkwargs['hue'] == 'Cluster':
                            hue_order = orderedClusters 
                        else:
                            hue_order = orderDict['hue_order'] 
                        for i,cluster in enumerate(hue_order):
                            g3 = sns.kdeplot(plottingDf[plottingDf[newkwargs['hue']] == cluster][newkwargs['x']],color=palette[i],ax=plotAxis,shade=True,label=cluster)
                        if max(plottingDf['Metric']) > 100:
                            tickDict = {}
                            ticks = [-1000,100,1000,10000,100000]
                            tickDict['x'] = ticks
                            addLogicleAxes(plotAxis,tickDict)
                            if 'hue' in newkwargs:
                                subplotValues = []
                                for hueval in pd.unique(plottingDf[newkwargs['hue']]):
                                    subplotValues.append(plottingDf[plottingDf[newkwargs['hue']] == hueval]['Metric'])
                            else:
                                subplotValues = [plottingDf['Metric']]
                            addCountYAxis(plotAxis,subplotValues)
            
                    kdePlotBool = True
                else:
                    #"Unstack" dataframe
                    plottingDf = plottingDf[plottingDf['Feature'] == plottingDf['Feature'][0]]
                    g3 = sns.countplot(data=plottingDf,ax=plotAxis,**newkwargs,**orderDict,edgecolor='black',linewidth=1)
                    #Log frequency if needed
                    #Log frequency if needed
                    if yaxisFeature == 'log-frequency':
                        plotAxis.set_yscale('log')
            else: 
                #"Unstack" dataframe
                plottingDf = plottingDf[plottingDf['Feature'] == plottingDf['Feature'][0]]
                if 'hue' in newkwargs.keys():
                    x,y = newkwargs['x'],newkwargs['hue']
                    plottingDf = plottingDf.groupby(x)[y].value_counts(normalize=True).mul(100).rename('percent').reset_index()
                else:
                    plottingDf = plottingDf[newkwargs['x']].value_counts(normalize=True).mul(100)
                    plottingDf.index.names = [newkwargs['x']]
                    plottingDf = plottingDf.to_frame('percent').reset_index()
                newkwargs['y'] = yaxisFeature 
                g3 = sns.barplot(data=plottingDf,ax=plotAxis,**newkwargs,**orderDict,edgecolor='black',linewidth=1)
                #Log frequency if needed
                if yaxisFeature == 'log-frequency':
                    plotAxis.set_yscale('log')
        
            #Rotate index labels if x not cluster
            if newkwargs['x'] != 'Cluster':
                #if not kdeplot, rotate index labels
                if not kdePlotBool:
                    plt.setp(plotAxis.xaxis.get_majorticklabels(), rotation=45)
                #If feature plot on x axis, change x axis name
                if featureBool:
                    plotAxis.set(xlabel=feature)
            else:
                #If count plot, color
                if not kdePlotBool:
                    for i,cluster in enumerate(orderedClusters):
                        plotAxis.get_xticklabels()[i].set_color(palette[i])
                plotAxis.set(xlabel='Group')
            #Correct legend for kde plot
            if kdePlotBool:
                if newkwargs['hue'] == 'Cluster':
                    newkwargs['hue'] = 'Group'
                leg = g3.legend(loc='center right', bbox_to_anchor=(legendoffset, 0.5), ncol=1,framealpha=0,title=newkwargs['hue'])
            else:
                leg = g3.legend(loc='center right', bbox_to_anchor=(legendoffset, 0.5), ncol=1,framealpha=0)

    frameCanvas.draw()
 def updateComparisonPlot(sliderList,rbVarList,rbVarList2):
     clusterComparisonAxis.clear()
     featureDfList = []
     featureDfWithClusters = scaledData.copy()
     featureDfWithClusters['Cluster'] = ['0']*scaledData.shape[0]
     for i,clusterComparison in enumerate(clusterComparisonList):
         featureDfList.append(scaledData.iloc[clusterComparison,:])
         featureDfWithClusters.loc[:,'Cluster'].iloc[clusterComparison] = str(i+1)
     clusterLabelList = featureDfWithClusters['Cluster'] 
     comparisonDf = pd.concat(featureDfList,keys=list(map(str,list(range(1,len(clusterComparisonList)+1)))),names=['Cluster'])
     comparisonDf.columns.name = 'Feature'
     comparisonPlottingDf = comparisonDf.stack().to_frame('Metric').reset_index()
     self.comparisonPlottingDf = comparisonPlottingDf.copy()
     #featureDfWithClusters['Cluster'] = clusterLabelList
     #Get unique cluster labels for hue order and pairwise significance comparisons
     clusterLabelListNoZero = list(filter(lambda a: a != '0', clusterLabelList))
     uniqueClusterLabelListNoZero = list(pd.unique(clusterLabelListNoZero))
     orderedClusterLabelListNoZero = []
     for uniqueClusterLabel in uniqueClusterLabelListNoZero:
         intval = int(uniqueClusterLabel)
         orderedClusterLabelListNoZero.append(intval)
     orderedClusterLabelListNoZero = sorted(orderedClusterLabelListNoZero)
     for i,orderedClusterLabel in enumerate(orderedClusterLabelListNoZero):
         orderedClusterLabelListNoZero[i] = str(orderedClusterLabel)
     #Get all pairwise combinations of clusters
     pairwiseClusterCombinations = list(itertools.combinations(orderedClusterLabelListNoZero,2))
     #Conduct significance testing to get features that define cluster differences
     comparisonKwargs = {'confidence':sliderList[0].get(),'foldchange':sliderList[1].get(),'responseCutoff':sliderList[2].get(),'errorCorrection':rbVarList['error correction method'].get()}
     self.comparisonKwargs = comparisonKwargs.copy()
     featureDfWithClusters.columns.name = 'Feature'
     significanceArray = []
     if len(clusterComparisonList) > 1:
         dataMatrix,significanceArray = cpl.significanceTesting(featureDfWithClusters,pairwiseClusterCombinations,**comparisonKwargs)
     else:
         significanceArray = list(scaledData.columns)
         print(significanceArray)
     if len(significanceArray) != 0:
         comparisonPlottingDf = comparisonPlottingDf[comparisonPlottingDf['Feature'].isin(significanceArray)]
         plotType = rbVarList2['comparison plot type'].get()
         self.comparisonKwargs['plotType'] = plotType
         comparisonPlottingDfTemp = comparisonPlottingDf.copy()
         newcols = []
         for col in comparisonPlottingDf['Feature']:
             newcols.append(col.replace(',','\n'))
         comparisonPlottingDfTemp['Feature'] = newcols
         comparisonPlottingDfTemp['Metric'] = comparisonPlottingDfTemp['Metric'].astype(float)
         if plotType == 'violin':
             if len(clusterComparisonList) == 1:
                 g2 = sns.violinplot(data=comparisonPlottingDfTemp,x='Feature',y='Metric',ax=clusterComparisonAxis,scale='width',inner='quartile',legend=False)
             elif len(clusterComparisonList) == 2:
                 g2 = sns.violinplot(data=comparisonPlottingDfTemp,hue='Cluster',hue_order=orderedClusterLabelListNoZero,x='Feature',y='Metric',ax=clusterComparisonAxis,split=True,scale='width',inner='quartile',legend=False)
             else:
                 g2 = sns.violinplot(data=comparisonPlottingDfTemp,hue='Cluster',hue_order=orderedClusterLabelListNoZero,x='Feature',y='Metric',ax=clusterComparisonAxis,dodge=True,scale='width',inner='quartile',legend=False)
         elif plotType == 'box':
             g2 = sns.boxplot(data=comparisonPlottingDfTemp,hue='Cluster',hue_order=orderedClusterLabelListNoZero,x='Feature',y='Metric',ax=clusterComparisonAxis,showfliers=False)
         elif plotType == 'bar':
             g2 = sns.barplot(data=comparisonPlottingDfTemp,hue='Cluster',hue_order=orderedClusterLabelListNoZero,x='Feature',y='Metric',ax=clusterComparisonAxis,ci='sd',errwidth=1,capsize=0.05,edgecolor='k')
         plt.setp(clusterComparisonAxis.xaxis.get_majorticklabels(), rotation=45)
         if len(clusterComparisonList) > 1:
             clusterComparisonAxis.legend_.remove()
         if max(comparisonPlottingDfTemp['Metric']) > 100:
             yticksToUse = [-1000,100,1000,10000,100000]
             ytickValues,ytickLabels = returnTicks(yticksToUse)
             #clusterComparisonAxis.set_ylim([0,1000])
             clusterComparisonAxis.set_yticks(ytickValues)
             clusterComparisonAxis.set_yticklabels(ytickLabels)
             #clusterComparisonAxis.set_ylim([0,1000])
     else:
         clusterComparisonAxis.text(0.5, 0.5,'No significant differences',horizontalalignment='center',verticalalignment='center',transform = clusterComparisonAxis.transAxes)
     clusterComparisonAxis.set_title('Group Comparison')
     self.canvasComparison.draw()
    def __init__(self,master):
        self.root = master.root
        tk.Frame.__init__(self, master)
        
        experimentNameWindow = tk.Frame(self)
        experimentNameWindow.pack(side=tk.TOP,padx=10,pady=10)
        experimentNameLabel = tk.Label(experimentNameWindow,text=folderName+':').pack()
        
        plotFrame = tk.Frame(self)
        plotFrame.pack()
        row = pickle.load(open('inputFiles/rowValIterationRange.pkl','rb'))
        groupedGateList = pickle.load(open('inputFiles/groupedGateList.pkl','rb'))
        if groupVariable == 'Condition-Time':
            currentDf = logicleDataStacked.to_frame('GFI')
            rawDf = rawDataStacked.to_frame('GFI')
        else:
            currentLevelValues = logicleDataUnstacked.iloc[row,:].name
            currentDf = logicleDataStacked.xs(currentLevelValues,level=levelNames).to_frame('GFI')
            rawDf = rawDataStacked.xs(currentLevelValues,level=levelNames).to_frame('GFI')
        plottingDf = currentDf.reset_index()
        if groupVariable == 'Condition-Time' or groupVariable == 'Condition':
            nonEventLevelNames = currentDf.index.names[:-1]
            groupingColumn = []
            for conditionGroupbyTuple in currentDf.groupby(level=nonEventLevelNames,sort=False):
                conditionGroupbyTupleNew = [str(i) for i in conditionGroupbyTuple[0]]
                conditionName = '-'.join(conditionGroupbyTupleNew)
                for event in range(conditionGroupbyTuple[1].shape[0]):
                    groupingColumn.append(conditionName)
            plottingDf[groupVariable] = groupingColumn        
        #Plot facetgrid of histograms of ctv values of all times for current condition
        g = sns.FacetGrid(plottingDf,legend_out=True,hue=groupVariable,height=6,palette=sns.color_palette("Purples", len(pd.unique(plottingDf[groupVariable]))))
        #g.map(sns.distplot,'GFI',kde=False,bins=256)
        g.map(sns.kdeplot,'GFI',bw=15)
        plt.subplots_adjust(top=0.95)
        xtickValues,xtickLabels = returnTicks([-1000,100,1000,10000,100000])
        axis = g.fig.get_axes()[0]
        axis.set_xticks(xtickValues)
        axis.set_xticklabels(xtickLabels)
        
        startingGateLogicle = 725
        startingGateRaw = rawDf.values[find_nearest(currentDf,startingGateLogicle)[1]][0]
        logicleGates = returnGates(currentDf,rawDf,startingGateRaw,numGens)
        maxY = list(plt.gca().get_yticks())[-1]
        X = startingGateLogicle
        Ymin = -1
        Ymax = maxY
        gates = logicleGates[1:]
        numGates = len(logicleGates)-1
        self.canvas = FigureCanvasTkAgg(g.fig,master=plotFrame)
        self.ax = plt.gca() 
        self.canvas.draw()
        self.canvas.get_tk_widget().pack()
        self.X = X
        self.gates= gates
        self.gateVals = gates
        gateLengths = [y-x for x, y in zip(gates[:-1], gates[1:])][1:]
        self.gateLengths = gateLengths 
        self.numGates = numGates
        x = [X, X]
        y = [Ymin, Ymax]
        
        self.line = linesmpl.Line2D(x, y, picker=5,linestyle=':',color='r')
        self.ax.add_line(self.line)
        self.childlines = []
        for gate in range(self.numGates):
            newX = self.gates[gate]
            childline = linesmpl.Line2D([newX,newX], y,linestyle=':',color='k',picker=5)
            self.childlines.append(childline)
        for childline in self.childlines:
            self.ax.add_line(childline)
        self.canvas.draw()
        self.sid = self.canvas.mpl_connect('pick_event', self.clickOnParentLine)
        childsids = []
        for childline,i in zip(self.childlines,range(self.numGates)):
            childsids.append(self.canvas.mpl_connect('pick_event', self.clickOnChildLines))
        self.childsids = childsids
        
        #Add title
        if groupVariable == 'Condition-Time':
            plt.title(titleVariable)
        else:
            if not isinstance(currentLevelValues, (tuple,)):
                currentLevelValues = [str(currentLevelValues)]
            plt.title('-'.join(currentLevelValues)+' ('+titleVariable+' '+str(row+1)+'/'+str(logicleDataUnstacked.shape[0])+')')
        
        def collectInputs():
            for names in range(len(pd.unique(plottingDf[groupVariable]))):
                groupedGateList.append(self.getAllX())
            with open('inputFiles/groupedGateList.pkl','wb') as f:
                pickle.dump(groupedGateList,f)
            row = pickle.load(open('inputFiles/rowValIterationRange.pkl','rb'))
            row+=1
            with open('inputFiles/rowValIterationRange.pkl','wb') as f:
                pickle.dump(row,f)
            if row != pickle.load(open('inputFiles/iterationRange.pkl','rb')):
                master.switch_frame(Unsplit_Proliferation_Gating_GUI)
            else:
                i = 0
                row = 0
                singleCellProliferationDf = pd.DataFrame(np.zeros(logicleDataStacked.shape),logicleDataStacked.index,columns=['Generation'])
                for outerVariable in outerVariableValues:
                    for innerVariable in innerVariableValues:
                        sampleGenerationGates = groupedGateList[i]
                        #sampleGenerationGates = groupedGate.getAllX()
                        if groupVariable == 'Condition':
                            indexingTuple = tuple(list(innerVariable)+[outerVariable,slice(None)])
                        else:
                            indexingTuple = tuple(list(outerVariable)+[innerVariable,slice(None)])
                        ctvValues = logicleDataStacked.loc[indexingTuple]
                        generationValues = np.zeros(ctvValues.shape)
                        for sampleEvent,row in zip(ctvValues,range(ctvValues.shape[0])):
                            for generation in range(len(sampleGenerationGates)-1):
                                upperGate = sampleGenerationGates[generation]
                                lowerGate = sampleGenerationGates[generation+1]
                                if(sampleEvent > lowerGate and sampleEvent <= upperGate):
                                    generationValues[row] = generation
                        singleCellProliferationDf.loc[indexingTuple,:] = generationValues

                print(singleCellProliferationDf)
                with open('semiProcessedData/singleCellDataFrame-proliferation-'+folderName+'.pkl', 'wb') as f:
                    pickle.dump(singleCellProliferationDf,f)
                master.switch_frame(secondaryhomepage,folderName,expNum,ex_data)

        buttonWindow = tk.Frame(self)
        buttonWindow.pack(side=tk.TOP,pady=10)

        tk.Button(buttonWindow, text="OK",command=lambda: collectInputs()).grid(row=5,column=0)
        tk.Button(buttonWindow, text="Quit",command=quit).grid(row=5,column=2)
Exemplo n.º 8
0
def plot(plottingDf, subsettedDf, kwargs, facetKwargs, auxillaryKwargs,
         plotOptions):
    #Will need to update to make sure it pulls from y axis variable
    yvar = kwargs.pop('y')
    if auxillaryKwargs['subPlotType'] == 'histogram':
        fg = sns.FacetGrid(plottingDf,
                           legend_out=True,
                           **facetKwargs,
                           **kwargs,
                           **plotOptions['Y']['figureDimensions'])
        if plotOptions['Y']['axisScaling'] == 'Logarithmic':
            hist_kws = {'hist_kws': {'log': True}}
        else:
            hist_kws = {}
        fg.map(sns.distplot, yvar, bins=256, kde=False, **hist_kws)
    elif auxillaryKwargs['subPlotType'] == 'kde':
        if auxillaryKwargs['dataType'] != 'singlecell':
            fg = sns.FacetGrid(plottingDf, **facetKwargs, **kwargs,
                               **plotOptions['Y']['figureDimensions'])
            fg.map(sns.kdeplot, yvar, shade=False, bw=15)
        else:
            kwargIndices = []
            cols = list(plottingDf.columns)
            for kwarg in ['hue', 'row', 'col']:
                if kwarg in kwargs:
                    kwargIndices.append(cols.index(kwargs[kwarg]))
            kwargIndices = sorted(kwargIndices)
            uniqueKwargCombinations = [
                list(x) for x in set(
                    tuple(x)
                    for x in list(plottingDf.iloc[:, kwargIndices].values))
            ]
            hist = [0]
            indexTuples = []
            for kwargCombo in uniqueKwargCombinations:
                selectDf = plottingDf.copy()
                for kwarg, kwargIndex in zip(kwargCombo, kwargIndices):
                    selectDf = selectDf[selectDf[list(selectDf.columns)
                                                 [kwargIndex]] == kwarg]
                subplotValueList = selectDf['MFI'].values
                newvals = np.append(subplotValueList, [[0, 1023]])
                temphist, _ = np.histogram(newvals, bins=256)
                temphist[0] -= 1
                temphist[-1] -= 1
                if auxillaryKwargs['plotspecifickwargs']['scaleToMode']:
                    temphist = [x / max(temphist) for x in temphist]
                hist += list(temphist)
                for i in range(len(list(temphist))):
                    indexTuples.append(kwargCombo)

            hist = hist[1:]
            numUniquePlots = len(uniqueKwargCombinations)
            histBins = np.tile(list(range(1, 1024, 4)), numUniquePlots)
            smoothedHistBins = savgol_filter(
                hist, auxillaryKwargs['plotspecifickwargs']['smoothing'] - 1,
                2)

            if not auxillaryKwargs['plotspecifickwargs']['scaleToMode']:
                if plotOptions['Y']['axisScaling'] == 'Logarithmic':
                    cutoff = 1
                else:
                    cutoff = 0
            else:
                if plotOptions['Y']['axisScaling'] == 'Logarithmic':
                    cutoff = 0.001
                else:
                    cutoff = 0
            for i, val in enumerate(smoothedHistBins):
                if val < cutoff:
                    smoothedHistBins[i] = cutoff

            mi = pd.MultiIndex.from_tuples(indexTuples,
                                           names=itemgetter(*kwargIndices)(
                                               list(plottingDf.columns)))
            if auxillaryKwargs['plotspecifickwargs']['scaleToMode']:
                hist = [x * 100 for x in hist]
                smoothedHistBins = [x * 100 for x in smoothedHistBins]
                columns = ['MFI', '% Max']
            else:
                columns = ['MFI', 'Count']

            #Construct correct array of data
            data = np.matrix([histBins, smoothedHistBins]).T

            newdf = pd.DataFrame(data, columns=columns, index=mi)
            plottingDf = newdf.reset_index()

            fg = sns.relplot(data=plottingDf,
                             kind='line',
                             x='MFI',
                             y=columns[1],
                             facet_kws=facetKwargs,
                             **kwargs,
                             **plotOptions['Y']['figureDimensions'])

            xtickValues, xtickLabels = returnTicks(
                [-1000, 1000, 10000, 100000])
            maxVal = max(subsettedDf.values)[0]
            minVal = min(subsettedDf.values)[0]
            if xtickValues[0] < minVal:
                minVal = xtickValues[0]
            if xtickValues[-1] > maxVal:
                maxVal = xtickValues[-1]
            for i, axis in enumerate(fg.axes.flat):
                axis.set_xticks(xtickValues)
                axis.set_xticklabels(xtickLabels)
                axis.set_xlim([minVal, maxVal])
                if plotOptions['Y']['axisScaling'] == 'Logarithmic':
                    axis.set_yscale('log')

    #fg.add_legend()
    return fg