Beispiel #1
0
 def updateDimRedPlot(sliderList, radiobuttonVars):
     levelPlotAxis.clear()
     numericParametersForDimensionReduction = ipe.getSliderValues(
         sliderList, ods.dimReductionNumericParameterDict[dimRedType])
     qualitativeParametersForDimensionReduction = ipe.getRadiobuttonValues(
         radiobuttonVars)
     self.currentReductionParameters = numericParametersForDimensionReduction.copy(
     )
     self.currentReductionParameters.update(
         qualitativeParametersForDimensionReduction.copy())
     self.reducedData = ods.reduceDimensions(
         scaledData, dimRedType, [], self.currentReductionParameters)
     plottingDfReduced = self.reducedData.reset_index()
     self.reducedDataWithFeatures = pd.concat(
         [self.reducedData, scaledData], axis=1)
     plottingDfReducedWithFeatures = self.reducedDataWithFeatures.reset_index(
     )
     ipe.updateDropdownControlledPlot(self.canvas,
                                      levelPlotAxis,
                                      plottingDfReducedWithFeatures,
                                      dropdownVarsDict,
                                      'Dimension 1',
                                      'Dimension 2',
                                      legendoffset=offset)
     levelPlotAxis.set_xlim(self.currentxlims)
     levelPlotAxis.set_ylim(self.currentylims)
     self.canvas.draw()
 def collectInputs():
     #Do the clustering with slider value hyperparameters
     parametersForClusteringFunction = ipe.getSliderValues(sliderList,ods.clusterParameterDict[clusteringMethod])
     currentClusteringParameters = parametersForClusteringFunction.copy()
     scaledData = pickle.load(open('outputData/analysisFiles/scaledData/'+dataSelectionFileName,'rb'))
     clusterdf = ods.clusterData(scaledData,clusteringMethod,parametersForClusteringFunction) 
     clusterdf.columns.name = 'Feature'
     ods.savePostProcessedFile(clusterdf,dataSubsetTitle,'cluster',clusteringMethod,currentClusteringParameters)
     #Switch to cluster based downsampling/dimensional reduction page
     if dimRedBool.get():
         clusteringTitle = ods.getFileName(dataSubsetTitle,'cluster',clusteringMethod,currentClusteringParameters)[0].split('.')[0]
         print(clusteringTitle)
         master.switch_frame(ClusterBasedDimRedPage,clusterdf,clusteringTitle)
     else:
         master.switch_frame(backpage,folderName,secondaryhomepage)    
 def updateClusterPlot(sliders):
     clusterPlotAxis.clear()
     parametersForClusteringFunction = ipe.getSliderValues(sliderList,ods.clusterParameterDict[clusteringMethod])
     self.currentClusteringParameters = parametersForClusteringFunction.copy()
     self.clusterdf = ods.clusterData(scaledData,clusteringMethod,parametersForClusteringFunction) 
     self.clusterdf.columns.name = 'Feature'
     reducedDataWithClusters = reducedData.copy()
     reducedDataWithClusters['Cluster'] = list(self.clusterdf.index.get_level_values('Cluster'))
     plottingDfClustered = reducedDataWithClusters.reset_index()
     clusterPalette = sns.color_palette(sns.color_palette(),len(pd.unique(reducedDataWithClusters['Cluster'])))
     g1 = sns.scatterplot(data=plottingDfClustered,x='Dimension 1',y='Dimension 2',s=3,ax=clusterPlotAxis,alpha=0.7,hue='Cluster',palette=clusterPalette)
     clusterPlotAxis.legend_.remove()
     clusterPlotAxis.set_xlim(self.currentxlims)
     clusterPlotAxis.set_ylim(self.currentylims)
     self.canvas.draw()
Beispiel #4
0
    def __init__(self, master, scaledData, dataSubsetTitle, dimRedType,
                 plottingReduction):
        tk.Frame.__init__(self, master)

        #Initialize 1x1 canvas for interactive plots
        plotFrame = tk.Frame(self)
        plotFrame.grid(row=0, column=0, columnspan=3)
        fig = plt.figure(figsize=(10, 7))
        gs = fig.add_gridspec(1, 1)
        fig.subplots_adjust(left=0.25)
        offset = -0.1
        levelPlotAxis = fig.add_subplot(gs[0])
        self.canvas = FigureCanvasTkAgg(fig, master=plotFrame)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack()

        #Dimensional reduction plot (can be colored/resized/restyled by different level values in dropdowns)
        #Level parameter controls
        levelPlotWindow = tk.Frame(self)
        levelPlotWindow.grid(row=1, column=0, sticky=tk.N)
        levelParameterList = ['hue', 'style', 'size']
        levelParameterValueDict = {}
        #Dimensional reduction parameter controls
        dimRedNumericParameterWindow = tk.Frame(self)
        dimRedNumericParameterWindow.grid(row=1, column=1, sticky=tk.N)
        sliderList = ipe.createParameterAdjustmentSliders(
            dimRedNumericParameterWindow,
            ods.dimReductionNumericParameterDict[dimRedType],
            ods.dimReductionNumericParameterBounds)
        dimRedQualitativeParameterWindow = tk.Frame(self)
        dimRedQualitativeParameterWindow.grid(row=1, column=2, sticky=tk.N)
        radiobuttonList, radiobuttonVars = ipe.createParameterSelectionRadiobuttons(
            dimRedQualitativeParameterWindow,
            ods.dimReductionQualitativeParameterDict[dimRedType],
            ods.dimReductionQualitativeParameterValues)
        #Plot
        numericParametersForDimensionReduction = ipe.getSliderValues(
            sliderList, ods.dimReductionNumericParameterDict[dimRedType])
        qualitativeParametersForDimensionReduction = ipe.getRadiobuttonValues(
            radiobuttonVars)
        self.currentReductionParameters = numericParametersForDimensionReduction.copy(
        )
        self.currentReductionParameters.update(
            qualitativeParametersForDimensionReduction.copy())
        if len(plottingReduction) == 0:
            self.reducedData = ods.reduceDimensions(
                scaledData, dimRedType, [], self.currentReductionParameters)
        else:
            self.reducedData = plottingReduction.copy()
        self.reducedDataWithFeatures = pd.concat(
            [self.reducedData, scaledData], axis=1)
        levelList = list(self.reducedData.index.names) + ['None']
        featureList = list(scaledData.columns)
        for level in levelParameterList:
            if level == 'hue' or level == 'size':
                levelParameterValueDict[level] = levelList.copy(
                ) + featureList.copy()
            else:
                levelParameterValueDict[level] = levelList.copy()
        plottingDfReduced = self.reducedData.reset_index()
        plottingDfReducedWithFeatures = self.reducedDataWithFeatures.reset_index(
        )
        kwargs, defaultDict = ipe.getDefaultKwargs(plottingDfReduced)
        dropdownList, dropdownVarsDict = ipe.createParameterSelectionDropdowns(
            levelPlotWindow, levelParameterList, levelParameterValueDict,
            defaultDict)
        ipe.updateDropdownControlledPlot(self.canvas,
                                         levelPlotAxis,
                                         plottingDfReducedWithFeatures,
                                         dropdownVarsDict,
                                         'Dimension 1',
                                         'Dimension 2',
                                         legendoffset=offset)

        self.originalxlims = levelPlotAxis.get_xlim()
        self.originalylims = levelPlotAxis.get_ylim()
        self.currentxlims = levelPlotAxis.get_xlim()
        self.currentylims = levelPlotAxis.get_ylim()

        def updateDimRedPlot(sliderList, radiobuttonVars):
            levelPlotAxis.clear()
            numericParametersForDimensionReduction = ipe.getSliderValues(
                sliderList, ods.dimReductionNumericParameterDict[dimRedType])
            qualitativeParametersForDimensionReduction = ipe.getRadiobuttonValues(
                radiobuttonVars)
            self.currentReductionParameters = numericParametersForDimensionReduction.copy(
            )
            self.currentReductionParameters.update(
                qualitativeParametersForDimensionReduction.copy())
            self.reducedData = ods.reduceDimensions(
                scaledData, dimRedType, [], self.currentReductionParameters)
            plottingDfReduced = self.reducedData.reset_index()
            self.reducedDataWithFeatures = pd.concat(
                [self.reducedData, scaledData], axis=1)
            plottingDfReducedWithFeatures = self.reducedDataWithFeatures.reset_index(
            )
            ipe.updateDropdownControlledPlot(self.canvas,
                                             levelPlotAxis,
                                             plottingDfReducedWithFeatures,
                                             dropdownVarsDict,
                                             'Dimension 1',
                                             'Dimension 2',
                                             legendoffset=offset)
            levelPlotAxis.set_xlim(self.currentxlims)
            levelPlotAxis.set_ylim(self.currentylims)
            self.canvas.draw()

        #Click and drag widget
        def line_select_callback(eclick, erelease):
            'eclick and erelease are the press and release events'
            x1, y1 = eclick.xdata, eclick.ydata
            x2, y2 = erelease.xdata, erelease.ydata

        def toggle_selector(event):
            print(' Key pressed.')
            if event.key in ['Q', 'q'] and toggle_selector.RS.active:
                print(' RectangleSelector deactivated.')
                toggle_selector.RS.set_active(False)
            if event.key in ['A', 'a'] and not toggle_selector.RS.active:
                print(' RectangleSelector activated.')
                toggle_selector.RS.set_active(True)

        rectpropsdict = {
            'facecolor': 'grey',
            'alpha': 0.2,
            'edgecolor': 'grey'
        }
        toggle_selector.RS = RectangleSelector(levelPlotAxis,
                                               line_select_callback,
                                               drawtype='box',
                                               useblit=True,
                                               button=[1, 3],
                                               minspanx=5,
                                               minspany=5,
                                               spancoords='pixels',
                                               interactive=True,
                                               rectprops=rectpropsdict)
        self.ts = toggle_selector.RS
        self.canvas.mpl_connect('key_press_event', toggle_selector)

        def zoomIn():
            clusterSelectionBox = toggle_selector.RS.corners
            ll = np.array(
                [clusterSelectionBox[0][0],
                 clusterSelectionBox[1][0]])  # lower-left
            ur = np.array(
                [clusterSelectionBox[0][2],
                 clusterSelectionBox[1][2]])  # upper-right

            inidx = np.all(np.logical_and(ll <= self.reducedData.values,
                                          self.reducedData.values <= ur),
                           axis=1)
            inbox = self.reducedData.loc[inidx]
            bufferval = 0.1
            xlims = [
                min(inbox['Dimension 1']) - bufferval,
                max(inbox['Dimension 1']) + bufferval
            ]
            ylims = [
                min(inbox['Dimension 2']) - bufferval,
                max(inbox['Dimension 2']) + bufferval
            ]
            self.currentxlims = xlims
            self.currentylims = ylims
            levelPlotAxis.set_xlim(xlims)
            levelPlotAxis.set_ylim(ylims)
            self.canvas.draw()

        def zoomOut():
            levelPlotAxis.set_xlim(self.originalxlims)
            levelPlotAxis.set_ylim(self.originalylims)
            self.currentxlims = self.originalxlims
            self.currentylims = self.originalylims
            self.canvas.draw()

        def update():
            ipe.updateDropdownControlledPlot(
                self.canvas, levelPlotAxis,
                pd.concat([self.reducedData, scaledData],
                          axis=1).reset_index(), dropdownVarsDict,
                'Dimension 1', 'Dimension 2')
            levelPlotAxis.set_xlim(self.currentxlims)
            levelPlotAxis.set_ylim(self.currentylims)
            self.canvas.draw()

        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')

        tk.Button(self, text="Update plot styling",
                  command=lambda: update()).grid(row=2, column=0)
        tk.Button(self, text="Zoom in",
                  command=lambda: zoomIn()).grid(row=4, column=0)
        tk.Button(self, text="Zoom out",
                  command=lambda: zoomOut()).grid(row=5, column=0)
        tk.Button(self,
                  text="Update hyperparameters",
                  command=lambda: updateDimRedPlot(sliderList, radiobuttonVars)
                  ).grid(row=2, column=1, columnspan=2)

        tk.Button(
            self,
            text='Save plot',
            command=lambda: fig.savefig('plots/' + ods.getFileName(
                dataSubsetTitle,
                'reduce',
                dimRedType,
                self.currentReductionParameters,
                plotParameterDict=ipe.getDropdownValues(dropdownVarsDict),
                fileExtension='.png')[0],
                                        bbox_inches='tight')).grid(row=3,
                                                                   column=0)
        tk.Button(self,
                  text='Save dimensional reduction',
                  command=lambda: exportDimRed(),
                  font='Helvetica 14 bold').grid(row=3, column=1, columnspan=2)
        plotAllDimReds = tk.BooleanVar()
        plotAllDimRedsButton = tk.Checkbutton(
            self,
            text='Save all feature-colored dimension reduction plots?',
            variable=plotAllDimReds)
        plotAllDimRedsButton.select()
        plotAllDimRedsButton.grid(row=4, column=1, columnspan=2)

        def okCommand():
            exportDimRed()
            master.switch_frame(backpage, folderName, secondaryhomepage)

        #Default save and quit buttons
        buttonWindow = tk.Frame(self)
        buttonWindow.grid(row=6, column=0, columnspan=2)
        try:
            k = backpage
        except NameError:
            backpage, folderName, secondaryhomepage = pickle.load(
                open('misc/dimRedPlottingParamList.pkl', 'rb'))

        tk.Button(buttonWindow,
                  text="OK",
                  command=lambda: master.switch_frame(
                      backpage, folderName, secondaryhomepage)).grid(row=0,
                                                                     column=0)
        tk.Button(buttonWindow,
                  text="Back",
                  command=lambda: master.switch_frame(
                      DimensionReductionHomePage, folderName, backpage,
                      secondaryhomepage)).grid(row=0, column=1)
        tk.Button(buttonWindow, text="Quit",
                  command=lambda: quit()).grid(row=0, column=2)
Beispiel #5
0
        def collectInputs(plotAllVar):
            plotOptions = {}
            for axis, axisIndex in zip(axisDict[plotType],
                                       range(len(axisDict[plotType]))):
                share = radioButtonVarList2[axisIndex].get()
                if share == 'none':
                    share = False
                plotOptions[axis] = {
                    'axisTitle':
                    entryList[axisIndex].get(),
                    'axisScaling':
                    scalingVariableList[axisIndex].get(),
                    'linThreshold':
                    linearRangeScalingList[axisIndex].get(),
                    'numeric':
                    checkButtonVarList[axisIndex].get(),
                    'share':
                    share,
                    'limit': [
                        limitEntryList[axisIndex][0].get(),
                        limitEntryList[axisIndex][1].get()
                    ]
                }

            plotSpecificDict = {}
            if subPlotType == 'kde':
                scaleBool = ipe.getRadiobuttonValues(
                    modeScaleRadiobuttonVarsDict)['scale to mode']
                if scaleBool == 'yes':
                    plotSpecificDict['scaleToMode'] = True
                else:
                    plotSpecificDict['scaleToMode'] = False
                plotSpecificDict['smoothing'] = int(
                    ipe.getSliderValues(smoothingSliderList,
                                        ['smoothing'])['smoothing'])

            useModifiedDf = False
            sName = titleEntry.get()
            subsettedDfList, subsettedDfListTitles, figureLevels, levelValuesPlottedIndividually = fpl.produceSubsettedDataFrames(
                experimentDf.stack().to_frame('temp'),
                fullFigureLevelBooleanList, includeLevelValueList, self.tld)
            fpl.plotFacetedFigures(
                folderName,
                plotType,
                subPlotType,
                dataType,
                subsettedDfList,
                subsettedDfListTitles,
                figureLevels,
                levelValuesPlottedIndividually,
                useModifiedDf,
                experimentDf,
                plotOptions,
                parametersSelected,
                addDistributionPoints,
                originalLevelValueOrders=experimentParameters[
                    'levelLabelDict'],
                subfolderName=sName,
                context=ipe.getRadiobuttonValues(
                    contextRadiobuttonVarsDict)['context'],
                height=float(heightEntry.get()),
                aspect=float(widthEntry.get()),
                titleBool=ipe.getRadiobuttonValues(
                    plotTitleRadiobuttonVarsDict)['plotTitle'],
                colwrap=int(colWrapEntry.get()),
                legendBool=ipe.getRadiobuttonValues(
                    legendRadiobuttonVarsDict)['legend'],
                outlierBool=ipe.getRadiobuttonValues(
                    outlierRadiobuttonVarsDict)['remove outliers'],
                plotAllVar=plotAllVar,
                titleAdjust=titleAdjustEntry.get(),
                plotSpecificDict=plotSpecificDict)