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

📄 owassociationrulesviewer.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 2 页
字号:
"""
<name>Association Rules Viewer</name>
<description>Association rules filter and viewer.</description>
<icon>icons/AssociationRulesViewer.png</icon>
<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact> 
<priority>200</priority>
"""

import orange, sys
from qt import *
from qtcanvas import *
from OWWidget import *
import OWGUI

class AssociationRulesViewerCanvas(QCanvas):
    def __init__(self, master, widget):
        QCanvas.__init__(self, widget)
        self.master = master
        self.rect = None
        self.unselect()
        self.draw()

    def unselect(self):
        if self.rect:
            self.rect.hide()
            self.rect = None
        
        
    def draw(self):
        master = self.master
        nc, nr, cw, ch, ig = master.numcols, master.numrows, master.cellwidth, master.cellheight, master.ingrid
        scmin, scmax, srmin, srmax = master.sel_colmin, master.sel_colmax, master.sel_rowmin, master.sel_rowmax

        self.resize(nc * cw +1, nr * ch +1)

        for a in self.allItems():
            a.hide()
                
        maxcount = max([max([len(cell) for cell in row]) for row in master.ingrid])
        maxcount = float(max(10, maxcount))

        pens = [QPen(QColor(200,200,200), 1), QPen(QColor(200,200,255), 1)]
        brushes = [QBrush(QColor(255, 255, 255)), QBrush(QColor(250, 250, 255))]
        self.cells = []
        for x in range(nc):
            selx = x >= scmin and x <= scmax
            for y in range(nr):
                sel = selx and y >= srmin and y <= srmax
                cell = QCanvasRectangle(x*cw, y*ch, cw+1, ch+1, self)
                cell.setPen(pens[sel])
                if not ig[y][x]:
                    cell.setBrush(brushes[sel])
                else:
                    if sel:
                        color = 220 - 220 * len(ig[y][x]) / maxcount
                        cell.setBrush(QBrush(QColor(color, color, 255)))
                    else:
                        color = 255 - 235 * len(ig[y][x]) / maxcount
                        cell.setBrush(QBrush(QColor(color-20, color-20, color)))
                cell.show()

        if self.rect:
            self.rect.hide()
        if scmin > -1:
            self.rect = QCanvasRectangle(scmin*cw, srmin*ch, (scmax-scmin+1)*cw, (srmax-srmin+1)*ch, self)
            self.rect.setPen(QPen(QColor(128, 128, 255), 2))
            self.rect.show()
        else:
            self.rect = None

        self.update()
        self.master.shownSupport.setText('%3i%% - %3i%%' % (int(master.supp_min*100), int(master.supp_max*100)))
        self.master.shownConfidence.setText('%3i%% - %3i%%' % (int(master.conf_min*100), int(master.conf_max*100)))
        self.master.shownRules.setText('%3i' % sum([sum([len(cell) for cell in row]) for row in master.ingrid]))


class AssociationRulesViewerView(QCanvasView):
    def __init__(self, master, canvas, widget):
        QCanvasView.__init__(self, canvas, widget)
        self.master = master
        self.canvas = canvas
        self.setFixedSize(365, 365)
        self.selecting = False
        self.update()

    def contentsMousePressEvent(self, ev):
        self.sel_startX = ev.pos().x()
        self.sel_startY = ev.pos().y()
        master = self.master
        self.master.sel_colmin = self.master.sel_colmax = self.sel_startX / self.master.cellwidth
        self.master.sel_rowmin = self.master.sel_rowmax = self.sel_startY / self.master.cellheight
        self.canvas.draw()
        self.master.updateRuleList()

    def contentsMouseMoveEvent(self, ev):
        self.sel_endX = ev.pos().x()
        self.sel_endY = ev.pos().y()
        t = self.sel_startX /self.master.cellwidth, self.sel_endX /self.master.cellwidth
        self.master.sel_colmin, self.master.sel_colmax = min(t), max(t)
        t = self.sel_startY /self.master.cellheight, self.sel_endY /self.master.cellheight
        self.master.sel_rowmin, self.master.sel_rowmax = min(t), max(t)

        self.master.sel_colmin = max(self.master.sel_colmin, 0)
        self.master.sel_rowmin = max(self.master.sel_rowmin, 0)
        self.master.sel_colmax = min(self.master.sel_colmax, self.master.numcols-1)
        self.master.sel_rowmax = min(self.master.sel_rowmax, self.master.numrows-1)

        self.canvas.draw()
        self.master.updateRuleList()

    def contentsMouseReleaseEvent(self, ev):
        self.master.sendIfAuto()


class OWAssociationRulesViewer(OWWidget):
    measures = [("Support",    "Supp", "support"),
                ("Confidence", "Conf", "confidence"),
                ("Lift",       "Lift", "lift"),
                ("Leverage",   "Lev",  "leverage"),
                ("Strength",   "Strg", "strength"),
                ("Coverage",   "Cov",  "coverage")]

    settingsList = ["autoSend"] + [vn[2] for vn in measures]

    def __init__(self, parent=None, signalManager = None):
        OWWidget.__init__(self, parent, signalManager, "AssociationRulesViewer")

        self.inputs = [("Association Rules", orange.AssociationRules, self.arules)]
        self.outputs = [("Association Rules", orange.AssociationRules)]

        self.supp_min, self.supp_max = self.conf_min, self.conf_max = 0., 1.
        self.numcols = self.numrows = 20
        self.cellwidth = self.cellheight = 18

        for m in self.measures:
            setattr(self, m[2], False)
        self.support = self.confidence = True
        self.autoSend = True
        
        self.loadSettings()

        self.rules = None
        self.selectedRules = []
        self.noZoomButton()


        self.mainLayout = QHBoxLayout(self.mainArea)
        self.mainLayout.setAutoAdd(True)
        mainLeft = OWGUI.widgetBox(self.mainArea, "Filter")
        sep = OWGUI.separator(self.mainArea, 16, 0)
        mainRight = OWGUI.widgetBox(self.mainArea, "Rules")
        mainRight.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding))


        info = QWidget(mainLeft)
        infoGrid = QGridLayout(info, 3, 4, 0, 5)
        infoGrid.addWidget(OWGUI.widgetLabel(info, "Shown"), 1, 0)
        infoGrid.addWidget(OWGUI.widgetLabel(info, "Selected"), 2, 0)
        infoGrid.addWidget(OWGUI.widgetLabel(info, "Support (H)"), 0, 1)
        infoGrid.addWidget(OWGUI.widgetLabel(info, "Confidence (V)"), 0, 2)
        infoGrid.addWidget(OWGUI.widgetLabel(info, "# Rules"), 0, 3)
        
        self.shownSupport = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.shownSupport, 1, 1)
        self.shownConfidence = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.shownConfidence, 1, 2)
        self.shownRules = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.shownRules, 1, 3)

        self.selSupport = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.selSupport, 2, 1)
        self.selConfidence = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.selConfidence, 2, 2)
        self.selRules = OWGUI.widgetLabel(info, " ")
        infoGrid.addWidget(self.selRules, 2, 3)
        
        OWGUI.separator(mainLeft, 0, 4)
        self.ruleCanvas = AssociationRulesViewerCanvas(self, mainLeft)
        self.canvasView = AssociationRulesViewerView(self, self.ruleCanvas, mainLeft)

        boxb = OWGUI.widgetBox(mainLeft, box=None, orientation="horizontal")
        OWGUI.button(boxb, self, 'Zoom', callback = self.zoomButton)
        OWGUI.button(boxb, self, 'Show All', callback = self.showAllButton)
        OWGUI.button(boxb, self, 'No Zoom', callback = self.noZoomButton)
        OWGUI.separator(boxb, 16, 8)
        OWGUI.button(boxb, self, 'Unselect', callback = self.unselect)
        

        rightUpRight = QWidget(mainRight)
        self.grid=QGridLayout(rightUpRight,2,3,5,5)
        for i, m in enumerate(self.measures):
            cb = OWGUI.checkBox(rightUpRight, self, m[2], m[0], callback = self.displayRules)

⌨️ 快捷键说明

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