⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owparallelcoordinates.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 3 页
字号:
    def subsetdata(self, data, update = 1):
        if self.graph.subsetData != None and data != None and self.graph.subsetData.checksum() == data.checksum(): return    # check if the new data set is the same as the old one
        self.graph.setSubsetData(data)
        qApp.processEvents()
        if update: self.updateGraph()
        qApp.processEvents()
    
    # ###### DATA ################################
    # receive new data and update all fields
    def cdata(self, data):
        if data and data.domain.classVar:
            name = getattr(data, "name", "")
            data = data.filterref({data.domain.classVar: [val for val in data.domain.classVar.values]})
            data.name = name
        if self.data != None and data != None and self.data.checksum() == data.checksum(): return    # check if the new data set is the same as the old one
            
        self.projections = None
        self.correlationDict = {}
        
        exData = self.data
        self.data = data
        
        self.graph.setData(self.data)
        self.optimizationDlg.setData(self.data)

        if not (data and exData and str(exData.domain.attributes) == str(data.domain.attributes)): # preserve attribute choice if the domain is the same
            self.shownAttribsLB.clear()
            self.hiddenAttribsLB.clear()

            self.targetValueCombo.clear()
            self.targetValueCombo.insertItem("(None)")

            # update target combo
            if self.data and self.data.domain.classVar and self.data.domain.classVar.varType == orange.VarTypes.Discrete:
                for val in self.data.domain.classVar.values:
                    self.targetValueCombo.insertItem(val)
                self.targetValueCombo.setCurrentItem(0)
            
            attrs = self.attributeSelectionList
            if not attrs and data and data.domain.attributes: attrs = [attr.name for attr in data.domain.attributes[:10]]
            self.setShownAttributeList(self.data, attrs)

        self.resetAttrManipulation()
        self.updateGraph()
        self.sendSelections()
    # ################################################
    
    def attributeSelection(self, attributeSelectionList):
        self.attributeSelectionList = attributeSelectionList
        if self.data and self.attributeSelectionList:
            for attr in self.attributeSelectionList:
                if not self.graph.attributeNameIndex.has_key(attr):  # this attribute list belongs to a new dataset that has not come yet
                    return
            self.setShownAttributeList(self.data, self.attributeSelectionList)
            self.updateGraph()
            self.sendSelections()


    # send signals with selected and unselected examples as two datasets
    def sendSelections(self):
        if not self.data:
            self.send("Selected Examples", None)
            self.send("Unselected Examples", None)
            return

        targetVal = str(self.targetValueCombo.currentText())
        if targetVal == "(None)": targetVal = None
        else: targetVal = self.data.domain.classVar.values.index(targetVal)

        (selected, unselected) = self.graph.getSelectionsAsExampleTables(targetVal)
        
        self.send("Selected Examples", selected)
        self.send("Unselected Examples", unselected)

    def sendAttributeSelection(self, attrs):
        self.send("Attribute selection", attrs)

    #################################################

    def updateValues(self):
        self.isResizing = 0
        self.updateGraph()

    def resizeEvent(self, e):
        OWWidget.resizeEvent(self,e)
        self.isResizing = 1
        # self.updateGraph()  # had to comment, otherwise python throws an exception

    # jittering options
    def setJitteringSize(self):
        self.isResizing = 0
        self.graph.setData(self.data)
        self.updateGraph()

    def setGlobalValueScaling(self):
        self.isResizing = 0
        self.graph.setData(self.data)
        if self.graph.globalValueScaling:
            self.graph.rescaleAttributesGlobaly(self.data, self.getShownAttributeList())        # we need to call this so that attributes are really rescaled in respect to the CURRENTLY SHOWN ATTRIBUTES
        self.updateGraph()

    # update attribute ordering
    def updateShownAttributeList(self):
        self.isResizing = 0
        self.setShownAttributeList(self.data)
        self.updateGraph()

    def setAutoSendSelection(self):
        if self.autoSendSelection:
            self.zoomSelectToolbar.buttonSendSelections.setEnabled(0)
            self.sendSelections()
        else:
            self.zoomSelectToolbar.buttonSendSelections.setEnabled(1)
            

    def setColors(self):
        dlg = self.createColorDialog()
        if dlg.exec_loop():
            self.colorSettings = dlg.getColorSchemas()
            self.graph.contPalette = dlg.getContinuousPalette("contPalette")
            self.graph.discPalette = dlg.getDiscretePalette()
            self.graph.setCanvasBackground(dlg.getColor("Canvas"))
            self.updateGraph()

    def createColorDialog(self):
        c = OWDlgs.ColorPalette(self, "Color Palette")
        c.createDiscretePalette(" Discrete Palette ")
        c.createContinuousPalette("contPalette", " Continuous palette ")
        box = c.createBox("otherColors", " Other Colors ")
        c.createColorButton(box, "Canvas", "Canvas color", Qt.white)
        box.addSpace(5)
        box.adjustSize()
        c.setColorSchemas(self.colorSettings)
        return c

    def destroy(self, dw = 1, dsw = 1):
        self.optimizationDlg.hide()
        OWWidget.destroy(self, dw, dsw)


CORRELATION = 0
VIZRANK = 1
#
# Find attribute subsets that are interesting to visualize using parallel coordinates
class ParallelOptimization(OWBaseWidget):
    resultListList = [50, 100, 200, 500, 1000]
    qualityMeasure =  ["Classification accuracy", "Average correct", "Brier score"]
    testingMethod = ["Leave one out", "10-fold cross validation", "Test on learning set"]
    
    settingsList = ["attributeCount", "fileBuffer", "lastSaveDirName", "optimizationMeasure",
                    "numberOfAttributes", "orderAllAttributes", "optimizationMeasure"]
    
    def __init__(self, parallelWidget, parent=None, signalManager = None):
        OWBaseWidget.__init__(self, parent, signalManager, "Parallel Optimization Dialog", FALSE)

        self.setCaption("Qt Parallel Optimization Dialog")
        self.topLayout = QVBoxLayout( self, 10 ) 
        self.grid=QGridLayout(4,2)
        self.topLayout.addLayout( self.grid, 10 )
        self.parallelWidget = parallelWidget

        self.optimizationMeasure = 0
        self.attributeCount = 5
        self.numberOfAttributes = 6
        self.fileName = ""
        self.lastSaveDirName = os.getcwd() + "/"
        self.fileBuffer = []
        self.projections = []
        self.allResults = []
        self.canOptimize = 0
        self.orderAllAttributes = 1 # do we wish to order all attributes or find just an interesting subset
        self.worstVal = -1  # used in heuristics to stop the search in uninteresting parts of the graph
        self.datasetName = ""

        self.loadSettings()

        self.measureBox = OWGUI.radioButtonsInBox(self, self, "optimizationMeasure", ["Correlation", "VizRank"], box = " Select optimization measure ", callback = self.updateGUI)
        self.vizrankSettingsBox = OWGUI.widgetBox(self, " VizRank settings ")
        self.optimizeBox = OWGUI.widgetBox(self, " Optimize ")
        self.manageBox = OWGUI.widgetBox(self, " Manage results ")
        self.resultsBox = OWGUI.widgetBox(self, " Results ")

        self.grid.addWidget(self.measureBox,0,0)
        self.grid.addWidget(self.vizrankSettingsBox,1,0)
        self.grid.addWidget(self.optimizeBox,2,0)
        self.grid.addWidget(self.manageBox,3,0)
        self.grid.addMultiCellWidget (self.resultsBox,0,3, 1, 1)
        self.grid.setColStretch(0, 0)
        self.grid.setColStretch(1, 100)
        self.grid.setRowStretch(0, 0)
        self.grid.setRowStretch(1, 0)
        self.grid.setRowStretch(2, 0)
        self.grid.setRowStretch(3, 100)
        self.vizrankSettingsBox.setMinimumWidth(200)

        self.resultList = QListBox(self.resultsBox)
        self.resultList.setMinimumSize(200,200)
        self.connect(self.resultList, SIGNAL("selectionChanged()"), self.showSelectedAttributes)
        
              
        # remove non-existing files
        names = []
        for i in range(len(self.fileBuffer)-1, -1, -1):
            (short, longName) = self.fileBuffer[i]
            if not os.path.exists(longName):
                self.fileBuffer.remove((short, longName))
            else: names.append(short)
        names.append("(None)")
        self.fileName = "(None)"
                
        self.hbox1 = OWGUI.widgetBox(self.vizrankSettingsBox, " VizRank projections file ", orientation = "horizontal")
        self.vizrankFileCombo = OWGUI.comboBox(self.hbox1, self, "fileName", items = names, tooltip = "File that contains information about interestingness of scatterplots \ngenerated by VizRank method in scatterplot widget", callback = self.changeProjectionFile, sendSelectedValue = 1, valueType = str)
        self.browseButton = OWGUI.button(self.hbox1, self, "...", callback = self.loadProjections)
        self.browseButton.setMaximumWidth(20)

        self.resultsInfoBox = OWGUI.widgetBox(self.vizrankSettingsBox, " VizRank parameters ")
        self.kNeighborsLabel = OWGUI.widgetLabel(self.resultsInfoBox, "Number of neighbors (k):")
        self.percentDataUsedLabel = OWGUI.widgetLabel(self.resultsInfoBox, "Percent of data used:")
        self.testingMethodLabel = OWGUI.widgetLabel(self.resultsInfoBox, "Testing method used:")
        self.qualityMeasureLabel = OWGUI.widgetLabel(self.resultsInfoBox, "Quality measure used:")

        #self.numberOfAttributesCombo = OWGUI.comboBoxWithCaption(self.optimizeBox, self, "numberOfAttributes", "Number of visualized attributes: ", tooltip = "Projections with this number of attributes will be evaluated", items = [x for x in range(3, 12)], sendSelectedValue = 1, valueType = int)
        self.allAttributesRadio = QRadioButton("Order all attributes", self.optimizeBox)
        self.connect(self.allAttributesRadio, SIGNAL("clicked()"), self.setAllAttributeRadio)
        box = OWGUI.widgetBox(self.optimizeBox, orientation = "horizontal")
        self.subsetAttributeRadio = QRadioButton("find subsets of      ", box)
        self.connect(self.subsetAttributeRadio, SIGNAL("clicked()"), self.setSubsetAttributeRadio)
        self.subsetAttributeEdit = OWGUI.lineEdit(box, self, "numberOfAttributes", valueType = int)
        label  = OWGUI.widgetLabel(box, "   attributes")
        
        self.startOptimizationButton = OWGUI.button(self.optimizeBox, self, " Start optimization ", callback = self.startOptimization)
        f = self.startOptimizationButton.font()
        f.setBold(1)
        self.startOptimizationButton.setFont(f)
        self.stopOptimizationButton = OWGUI.button(self.optimizeBox, self, "Stop evaluation", callback = self.stopOptimizationClick)
        self.stopOptimizationButton.setFont(f)
        self.stopOptimizationButton.hide()
        self.connect(self.stopOptimizationButton , SIGNAL("clicked()"), self.stopOptimizationClick)

        
        self.clearButton = OWGUI.button(self.manageBox, self, "Clear results", self.clearResults)
        self.loadButton  = OWGUI.button(self.manageBox, self, "Load", self.loadResults)
        self.saveButton  = OWGUI.button(self.manageBox, self, "Save", self.saveResults)
        self.closeButton = OWGUI.button(self.manageBox, self, "Close dialog", self.hide)

        self.changeProjectionFile()
        self.updateGUI()
        self.activateLoadedSettings()

    def activateLoadedSettings(self):
        if self.orderAllAttributes: self.setAllAttributeRadio()
        else:                       self.setSubsetAttributeRadio()

    def updateGUI(self):
        self.vizrankSettingsBox.setEnabled(self.optimizationMeasure)

    def destroy(self, dw = 1, dsw = 1):
        self.saveSettings()

    # if user clicks new attribute list in optimization dialog, we update shown attributes
    def showSelectedAttributes(self):
        attrList = self.getSelectedAttributes()
        if not attrList: return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -