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

📄 owscatterplotmatrix.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 2 页
字号:
        
        self.shownAttrCount = count-1
        for i in range(count-1, -1, -1):
            for j in range(i):
                graph = OWScatterPlotGraph(self, self.mainArea)
                graph.setMinimumSize(QSize(10,10))
                graph.jitterSize = self.jitterSize
                graph.jitterContinuous = self.jitterContinuous
                
                if self.graphs == []:
                    graph.setData(self.data)
                else:
                    graph.rawdata = self.graphs[0].rawdata
                    graph.domainDataStat = self.graphs[0].domainDataStat
                    graph.scaledData = self.graphs[0].scaledData
                    graph.noJitteringScaledData = self.graphs[0].noJitteringScaledData
                    graph.validDataArray = self.graphs[0].validDataArray
                    graph.attrValues = self.graphs[0].attrValues
                    graph.attributeNames = self.graphs[0].attributeNames
                    graph.attributeNameIndex = self.graphs[0].attributeNameIndex
                    graph.domainDataStat = self.graphs[0].domainDataStat
                    graph.attributeNames = self.graphs[0].attributeNames

                self.setGraphOptions(graph, "")
                self.grid.addWidget(graph, count-i-1, j)
                self.graphs.append(graph)
                self.connect(graph, SIGNAL('plotMouseReleased(const QMouseEvent&)'),self.onMouseReleased)
                params = (list[j], list[i], self.data.domain.classVar.name, "")
                self.graphParameters.append(params)
                graph.show()

        self.updateGraph()

        w = self.mainArea.width()
        h = self.mainArea.height()
        for i in range(len(list)):
            label = QMyLabel(QSize(w/(count+1), h/(count+1)), list[i], self.mainArea)
            self.grid.addWidget(label, len(list)-i-1, i, Qt.AlignCenter)
            label.setAlignment(Qt.AlignCenter)
            label.show()
            self.labels.append(label)


    def saveToFile(self):
        self.sizeDlg = OWDlgs.OWChooseImageSizeDlg(self)
        self.sizeDlg.disconnect(self.sizeDlg.okButton, SIGNAL("clicked()"), self.sizeDlg.accept)
        self.sizeDlg.connect(self.sizeDlg.okButton, SIGNAL("clicked()"), self.saveToFileAccept)
        self.sizeDlg.exec_loop()

    def saveToFileAccept(self):
        qfileName = QFileDialog.getSaveFileName("graph.png","Portable Network Graphics (*.PNG);;Windows Bitmap (*.BMP);;Graphics Interchange Format (*.GIF)", None, "Save to..", "Save to..")
        fileName = str(qfileName)
        if fileName == "": return
        (fil,ext) = os.path.splitext(fileName)
        ext = ext.replace(".","")
        if ext == "":	
        	ext = "PNG"  	# if no format was specified, we choose png
        	fileName = fileName + ".png"
        ext = ext.upper()

        self.saveToFileDirect(fileName, ext, self.sizeDlg.getSize())
        QDialog.accept(self.sizeDlg)

    # saving scatterplot matrix is a bit harder than the rest of visualization widgets. we have to save each scatterplot separately
    def saveToFileDirect(self, fileName, ext, size):
        if self.graphs == []: return
        count = self.shownAttrCount
        attrNameSpace = 30
        dist = 4
        topOffset = 5
        if size.isEmpty():
            size = self.graphs[0].size()
            size = QSize(size.width()*count + attrNameSpace + count*dist + topOffset, size.height()*count + attrNameSpace + count*dist)

        fullBuffer = QPixmap(size)
        fullPainter = QPainter(fullBuffer)
        fullPainter.fillRect(fullBuffer.rect(), QBrush(Qt.white)) # make background same color as the widget's background

        smallSize = QSize((size.width()-attrNameSpace - count*dist - topOffset)/count, (size.height()-attrNameSpace - count*dist)/count)

        # draw scatterplots
        for i in range(len(self.graphs)):
            buffer = QPixmap(smallSize)
            painter = QPainter(buffer)
            painter.fillRect(buffer.rect(), QBrush(Qt.white)) # make background same color as the widget's background
            self.graphs[i].printPlot(painter, buffer.rect())
            painter.end()

            # here we have i-th scatterplot printed in a QPixmap. we have to print this pixmap into the right position in the big pixmap
            (found, y, x) = self.grid.findWidget(self.graphs[i])
            fullPainter.drawPixmap(attrNameSpace + x*(smallSize.width() + dist), y*(smallSize.height() + dist) + topOffset, buffer)
        

        list = []
        for i in range(self.shownAttribsLB.count()): list.append(str(self.shownAttribsLB.text(i)))

        # draw vertical text
        fullPainter.rotate(-90)
        for i in range(len(self.labels)-1):
            y1 = topOffset + i*(smallSize.height() + dist)
            newRect = fullPainter.xFormDev(QRect(0,y1,30, smallSize.height()));
            fullPainter.drawText(newRect, Qt.AlignCenter, str(self.labels[len(self.labels)-i-1].text()))
            
        # draw hortizonatal text
        fullPainter.rotate(90)
        for i in range(len(self.labels)-1):
            x1 = attrNameSpace + i*(smallSize.width() + dist)
            y1 = (len(self.labels)-1-i)*(smallSize.height() + dist)
            rect = QRect(x1, y1, smallSize.width(), 30)
            fullPainter.drawText(rect, Qt.AlignCenter, str(self.labels[i].text()))
            

        fullPainter.end()
        fullBuffer.save(fileName, ext)


    # we catch mouse release event so that we can send the "Attribute selection" signal
    def onMouseReleased(self, e):
        for i in range(len(self.graphs)):
            if self.graphs[i].blankClick == 1:
                (attr1, attr2, className, string) = self.graphParameters[i]
                self.send("Attribute selection", [attr1, attr2])
                self.graphs[i].blankClick = 0

    ####### CDATA ################################
    # receive new data and update all fields
    def cdata(self, data):
        exData = self.data
        
        if data == None:
            self.shownAttribsLB.clear()
            self.hiddenAttribsLB.clear()
            self.removeAllGraphs()
            return
        
        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
        self.data = data

        if self.data and exData and str(exData.domain.attributes) == str(self.data.domain.attributes): # preserve attribute choice if the domain is the same
            if self.graphs != []: self.createGraphs()   # if we had already created graphs, redraw them with new data
            return  

        if not self.selection(self.attributeSelection):
            self.shownAttribsLB.clear()
            self.hiddenAttribsLB.clear()
        
            for attr in self.data.domain.attributes:
                self.shownAttribsLB.insertItem(self.icons[attr.varType], attr.name)

            #self.createGraphs()

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

    def selection(self, attrList):
        self.attributeSelection = attrList

        if not self.data or not attrList: return 0

        domain = [attr.name for attr in self.data.domain]
        for attr in attrList:
            if attr not in domain: return 0  # this attribute list belongs to a new dataset that has not come yet
        
        self.shownAttribsLB.clear()
        self.hiddenAttribsLB.clear()

        for attr in attrList:
            self.shownAttribsLB.insertItem(self.icons[self.data.domain[attr].varType], attr)

        for attr in self.data.domain.attributes:
            if attr.name not in attrList:
                self.hiddenAttribsLB.insertItem(self.icons[attr.varType], attr.name)
                
        self.createGraphs()
        return 1


    ################################################
    # adding and removing interesting attributes
    def addAttribute(self):
        count = self.hiddenAttribsLB.count()
        pos   = self.shownAttribsLB.count()
        for i in range(count-1, -1, -1):
            if self.hiddenAttribsLB.isSelected(i):
                self.shownAttribsLB.insertItem(self.hiddenAttribsLB.pixmap(i), self.hiddenAttribsLB.text(i), pos)
                self.hiddenAttribsLB.removeItem(i)

    def removeAttribute(self):
        count = self.shownAttribsLB.count()
        pos   = self.hiddenAttribsLB.count()
        for i in range(count-1, -1, -1):
            if self.shownAttribsLB.isSelected(i):
                self.hiddenAttribsLB.insertItem(self.shownAttribsLB.pixmap(i), self.shownAttribsLB.text(i), pos)
                self.shownAttribsLB.removeItem(i)

    def moveAttrUP(self):
        for i in range(1, self.shownAttribsLB.count()):
            if self.shownAttribsLB.isSelected(i):
                self.shownAttribsLB.insertItem(self.shownAttribsLB.pixmap(i), self.shownAttribsLB.text(i), i-1)
                self.shownAttribsLB.removeItem(i+1)
                self.shownAttribsLB.setSelected(i-1, TRUE)

    def moveAttrDOWN(self):
        count = self.shownAttribsLB.count()
        for i in range(count-2,-1,-1):
            if self.shownAttribsLB.isSelected(i):
                self.shownAttribsLB.insertItem(self.shownAttribsLB.pixmap(i), self.shownAttribsLB.text(i), i+2)
                self.shownAttribsLB.removeItem(i)
                self.shownAttribsLB.setSelected(i+1, TRUE)
                
    def resizeEvent(self, e):
        OWWidget.resizeEvent(self,e)
        w = self.mainArea.width()
        h = self.mainArea.height()
        size = QSize(w/(len(self.labels)+1), h/(len(self.labels)+1))
        for label in self.labels:
            label.setSize(size)



#test widget appearance
if __name__=="__main__":
    a=QApplication(sys.argv)
    ow=OWScatterPlotMatrix()
    a.setMainWidget(ow)
    ow.show()
    a.exec_loop()

    #save settings 
    ow.saveSettings()

⌨️ 快捷键说明

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