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

📄 owmosaicdisplay.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 4 页
字号:
                apriori = [aprioriDist[val]/float(len(self.data)) for val in clsValues]
                if self.showAprioriDistributionBoxes:   # we want to show expected class distribution under independence hypothesis
                    boxCounts = apriori
                else:
                    contingencies = self.optimizationDlg.getContingencys(usedAttrs)
                    boxCounts = []
                    for clsVal in clsValues:
                        # compute: P(c_i) * prod (P(c_i|attr_k) / P(c_i))  for each class value
                        Pci = aprioriDist[clsVal]/float(sum(aprioriDist.values()))
                        tempVal = Pci
                        for i in range(len(usedAttrs)):
                            tempVal *= contingencies[usedAttrs[i]][usedVals[i]][clsVal] / Pci
                        boxCounts.append(tempVal)
                        #boxCounts.append(aprioriDist[val]/float(sum(aprioriDist.values())) * reduce(operator.mul, [contingencies[usedAttrs[i]][usedVals[i]][clsVal]/float(sum(contingencies[usedAttrs[i]][usedVals[i]].values())) for i in range(len(usedAttrs))]))
                        
                total1 = 0; total2 = 0
                if self.useBoxes:
                    if self.horizontalDistribution:  OWCanvasLine(self.canvas, x0, y0+self.boxSize, x1, y0+self.boxSize, z = 30)
                    else:                            OWCanvasLine(self.canvas, x0+self.boxSize, y0, x0+self.boxSize, y1, z = 30)
                    
                for i in range(len(clsValues)):
                    val1 = apriori[i]
                    if self.showAprioriDistributionBoxes: val2 = apriori[i]
                    else:                                 val2 = boxCounts[i]/float(sum(boxCounts))
                    if self.horizontalDistribution:
                        if i == len(clsValues)-1:
                            v1 = x1-x0 - total1
                            v2 = x1-x0 - total2
                        else:
                            v1 = int((x1-x0)* val1)
                            v2 = int((x1-x0)* val2)
                        x,y,w,h, xL1, yL1, xL2, yL2 = x0+total2, y0, v2, self.boxSize, x0+total1+v1, y0, x0+total1+v1, y1
                    else:
                        if i== len(clsValues)-1:
                            v1 = y1-y0 - total1
                            v2 = y1-y0 - total2
                        else:
                            v1 = int((y1-y0)* val1)
                            v2 = int((y1-y0)* val2)
                        x,y,w,h, xL1, yL1, xL2, yL2 = x0, y0+total2, self.boxSize, v2, x0, y0+total1+v1, x1, y0+total1+v1
                        
                    if self.useBoxes:
                        OWCanvasRectangle(self.canvas, x, y, w, h, self.colorPalette[i], self.colorPalette[i], z = 20)
                    if i < len(clsValues)-1 and self.showAprioriDistributionLines:
                        OWCanvasLine(self.canvas, xL1, yL1, xL2, yL2, z = 10)

                    total1 += v1
                    total2 += v2

            # show subset distribution
            if self.conditionalSubsetDict:
                # show a rect around the box if subset examples belong to this box
                if self.conditionalSubsetDict[attrVals]:
                    #counts = [self.conditionalSubsetDict[attrVals + "-" + val] for val in clsValues]
                    #if sum(counts) == 1:    color = self.colorPalette[counts.index(1)]
                    #else:                   color = Qt.black
                    #OWCanvasRectangle(self.canvas, x0-2, y0-2, x1-x0+5, y1-y0+5, color, Qt.white, penWidth = 2, z=-50, penStyle = Qt.DashLine)
                    counts = [self.conditionalSubsetDict[attrVals + "-" + val] for val in clsValues]
                    if sum(counts) == 1:
                        OWCanvasRectangle(self.canvas, x0-2, y0-2, x1-x0+5, y1-y0+5, self.colorPalette[counts.index(1)], Qt.white, penWidth = 2, z=-50, penStyle = Qt.DashLine)

                    if self.showSubsetDataBoxes:     # do we want to show exact distribution in the right edge of each cell
                        if self.horizontalDistribution:  OWCanvasLine(self.canvas, x0, y1-self.boxSize, x1, y1-self.boxSize, z = 30)
                        else:                            OWCanvasLine(self.canvas, x1-self.boxSize, y0, x1-self.boxSize, y1, z = 30)
                        total = 0
                        for i in range(len(aprioriDist)):
                            val = self.conditionalSubsetDict[attrVals + "-" + clsValues[i]]
                            if not self.conditionalSubsetDict[attrVals] or val == 0: continue
                            if self.horizontalDistribution:
                                if i == len(aprioriDist)-1: v = x1-x0 - total
                                else:                       v = int(((x1-x0)* val)/float(self.conditionalSubsetDict[attrVals]))
                                OWCanvasRectangle(self.canvas, x0+total, y1-self.boxSize, v, self.boxSize, self.colorPalette[i], self.colorPalette[i], z = 15)
                            else:
                                if i == len(aprioriDist)-1: v = y1-y0 - total
                                else:                       v = int(((y1-y0)* val)/float(self.conditionalSubsetDict[attrVals]))
                                OWCanvasRectangle(self.canvas, x1-self.boxSize, y0+total, self.boxSize, v, self.colorPalette[i], self.colorPalette[i], z = 15)
                            total += v

        self.addTooltip(x0, y0, x1-x0, y1-y0, condition, aprioriDist, attrVals, pearson, expected)


    # add tooltips
    def addTooltip(self, x, y, w, h, condition, apriori = None, attrVals = None, pearson = None, expected = None):
        tooltipText = "Examples in this area have:<br>" + condition
        
        if apriori:
            clsValues = self.attributeValuesDict.get(self.data.domain.classVar.name, None) or getVariableValuesSorted(self.data, self.data.domain.classVar.name)
            actual = [self.conditionalDict[attrVals + "-" + clsValues[i]] for i in range(len(apriori))]
            if sum(actual) > 0:
                apriori = [apriori[key] for key in clsValues]
                aprioriText = ""; actualText = ""
                text = ""
                for i in range(len(clsValues)):
                    text += 4*"&nbsp;" + "<b>%s</b>: %d / %.1f%% (Expected %.1f / %.1f%%)<br>" % (clsValues[i], actual[i], 100.0*actual[i]/float(sum(actual)), (apriori[i]*sum(actual))/float(sum(apriori)), 100.0*apriori[i]/float(sum(apriori)))
                tooltipText += "Number of examples: " + str(int(sum(actual))) + "<br> Class distribution:<br>" + text[:-4]
        elif pearson and expected:
            tooltipText += "<hr>Expected number of examples: %.1f<br>Actual number of examples: %d<br>Standardized (Pearson) residual: %.1f" % (expected, self.conditionalDict[attrVals], pearson)
        tipRect = QRect(x, y, w, h)
        QToolTip.add(self.canvasView, tipRect, tooltipText)
        self.tooltips.append(tipRect)
   
    def saveToFileCanvas(self):
        sizeDlg = OWDlgs.OWChooseImageSizeDlg(self.canvas)
        sizeDlg.exec_loop()

    def setColors(self):
        dlg = self.createColorDialog()
        if dlg.exec_loop():
            self.colorSettings = dlg.getColorSchemas()
            self.colorPalette = dlg.getDiscretePalette()
            self.updateGraph()

    def createColorDialog(self):
        c = OWDlgs.ColorPalette(self, "Color Palette")
        c.createDiscretePalette(" Discrete Palette ")
        c.setColorSchemas(self.colorSettings)
        return c

    def sendSelectedData(self):
        # send the selected examples
        self.send("Selected Examples", self.selectedData)

    # add a new rectangle. update the graph and see which mosaics does it intersect. add this mosaics to the recentlyAdded list
    def addSelection(self, rect):
        self.selectionRectangle = rect
        self.selectedData = None        # clear previous data so that we will generate a new dataset in the updateGraph()
        self.updateSelectedData = 1 
        self.updateGraph()
        self.updateSelectedData = 0
        self.sendSelectedData()
        
        if getattr(self, "recentlyAdded", []):
            self.selectionConditionsArray.append(self.recentlyAdded)
            self.recentlyAdded = []
            
        self.selectionRectangle = None

    # remove the mosaics that were added with the last selection rectangle
    def removeLastSelection(self):
        if  self.selectionConditionsArray:
            vals = self.selectionConditionsArray.pop()
            for val in vals:
                if self.selectionConditionsDict.has_key(tuple(val)):
                    self.selectionConditionsDict.pop(tuple(val))

        self.updateSelectedData = 1
        self.selectedData = None        # clear previous data so that we will generate a new dataset in the updateGraph()
        self.updateGraph()
        self.updateSelectedData = 0
        self.sendSelectedData()

    def removeAllSelections(self):
        self.selectionConditionsDict = {}
        self.selectionConditionsArray = []
        self.selectedData = None
        self.sendSelectedData()

class SortAttributeValuesDlg(OWBaseWidget):
    def __init__(self, parentWidget = None, attrList = []):
        OWBaseWidget.__init__(self, None, None, "Sort Attribute Values", modal = TRUE)

        self.space = QVBox(self)
        self.layout = QVBoxLayout(self, 4)
        self.layout.addWidget(self.space)
        
        box1 = OWGUI.widgetBox(self.space, 1, orientation = "horizontal")
        
        self.attributeList = QListBox(box1)
        self.attributeList.setSelectionMode(QListBox.Extended)

        vbox = OWGUI.widgetBox(box1, "", orientation = "vertical")
        self.buttonUPAttr   = OWGUI.button(vbox, self, "", callback = self.moveAttrUP, tooltip="Move selected attribute values up")
        self.buttonDOWNAttr = OWGUI.button(vbox, self, "", callback = self.moveAttrDOWN, tooltip="Move selected attribute values down")
        self.buttonUPAttr.setPixmap(QPixmap(os.path.join(self.widgetDir, r"icons\Dlg_up1.png")))
        self.buttonUPAttr.setSizePolicy(QSizePolicy(QSizePolicy.Fixed , QSizePolicy.Expanding))
        self.buttonUPAttr.setMaximumWidth(20)
        self.buttonDOWNAttr.setPixmap(QPixmap(os.path.join(self.widgetDir, r"icons\Dlg_down1.png")))
        self.buttonDOWNAttr.setSizePolicy(QSizePolicy(QSizePolicy.Fixed , QSizePolicy.Expanding))
        self.buttonDOWNAttr.setMaximumWidth(20)
        self.buttonUPAttr.setMaximumWidth(20)

        box2 = OWGUI.widgetBox(self.space, 1, orientation = "horizontal")
        self.okButton =     OWGUI.button(box2, self, "OK", callback = self.accept)
        self.cancelButton = OWGUI.button(box2, self, "Cancel", callback = self.reject)

        for attr in attrList:
            self.attributeList.insertItem(attr)

        self.resize(300, 300)

    # move selected attribute values
    def moveAttrUP(self):
        for i in range(1, self.attributeList.count()):
            if self.attributeList.isSelected(i):
                self.attributeList.insertItem(self.attributeList.text(i), i-1)
                self.attributeList.removeItem(i+1)
                self.attributeList.setSelected(i-1, TRUE)

    def moveAttrDOWN(self):
        for i in range(self.attributeList.count()-2,-1,-1):
            if self.attributeList.isSelected(i):
                self.attributeList.insertItem(self.attributeList.text(i), i+2)
                self.attributeList.removeItem(i)
                self.attributeList.setSelected(i+1, TRUE)


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

⌨️ 快捷键说明

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