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

📄 ownomogramgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 5 页
字号:
class BasicNomogram(QCanvas):
    def __init__(self, parent, constant, *args):
        apply(QCanvas.__init__,(self, parent, ""))
        
        self.parent=parent
        self.items = []
        
        self.attributes = []
        self.constant = constant
        self.minBeta = 0
        self.maxBeta = 0
        self.max_difference = 0
        
        self.fontSize = parent.fontSize

        self.zeroLine = QCanvasLine(self)
        self.zeroLine.setPen(QPen(Qt.DotLine))
        self.zeroLine.setZ(-10)

        self.header = BasicNomogramHeader(self, parent)
        self.footerCanvas = BasicNomogramFooter(self, parent)
        self.parent.header.setCanvas(self.header)
        self.parent.footer.setCanvas(self.footerCanvas)
        
    def addAttribute(self, attr):
        self.attributes.append(attr)
        if attr.minValue < self.minBeta:
            self.minBeta = attr.minValue
        if attr.maxValue > self.maxBeta:
            self.maxBeta = attr.maxValue
        if attr.maxValue-attr.minValue > self.max_difference:
            self.max_difference = attr.maxValue-attr.minValue

    def hideAllMarkers(self):
        for at in self.attributes:
            at.marker.hide()
        self.footerCanvas.footer.marker.hide()
        self.footerCanvas.footerPercent.marker.hide()
        self.footerCanvas.connectedLine.hide()
        self.footerCanvas.hideCI()
        self.update()
        self.footerCanvas.update()

    def showAllMarkers(self):
        for at in self.attributes:
            at.marker.show()
        self.footerCanvas.footer.marker.show()
        self.footerCanvas.footerPercent.marker.show()
        if self.parent.confidence_check:
            self.footerCanvas.showCI()
        if self.footerCanvas.footer.marker.x() == self.footerCanvas.footerPercent.marker.x():
            self.footerCanvas.connectedLine.setPoints(self.footerCanvas.footer.marker.x(), self.footerCanvas.footer.marker.y(), self.footerCanvas.footerPercent.marker.x(), self.footerCanvas.footerPercent.marker.y())
            self.footerCanvas.connectedLine.setCanvas(self.footerCanvas)
            self.footerCanvas.connectedLine.show()
        self.update()
        self.footerCanvas.update()

    def paint(self, rect, mapper):
        self.zeroLine.setPoints(mapper.mapBeta(0, self.header.headerAttrLine), rect.top(), mapper.mapBeta(0, self.header.headerAttrLine), rect.bottom()-self.parent.verticalSpacing/2 + 25)
        if self.parent.showBaseLine:
            self.zeroLine.show()
        else:
            self.zeroLine.hide()
        curr_rect = QRect(rect.left(), rect.top(), rect.width(), 0)
        disc = False
 
        for at in self.attributes:
            if (isinstance(at, AttrLineCont) or isinstance(at, AttrLineOrdered)) and self.parent.contType == 1:   
                if disc:
                    curr_rect = QRect(rect.left(), curr_rect.bottom()+20, rect.width(), at.getHeight(self))
                    disc=False
                else:
                    curr_rect = QRect(rect.left(), curr_rect.bottom(), rect.width(), at.getHeight(self))
                at.paint2d(self, curr_rect, mapper)
            else:
                disc = True
                curr_rect = QRect(rect.left(), curr_rect.bottom(), rect.width(), at.getHeight(self))
                at.paint(self, curr_rect, mapper)
                # if histograms are used, a larger rect is required
                if self.parent.histogram:
                    curr_rect.setHeight(at.getHeight(self)+self.parent.histogram_size)
            

    def setSizes(self, parent):
        def getBottom():
            bottom, lastAt = 0, None
            for at in self.attributes:
                if lastAt and self.parent.contType == 1 and isinstance(at, AttrLineCont) and not isinstance(lastAt, AttrLineCont):
                    bottom += 20
                if (isinstance(at, AttrLineCont) or isinstance(at, AttrLineOrdered)) and self.parent.contType == 1:   
                    bottom += at.getHeight(self)
                else:
                    bottom += at.getHeight(self)
                    if self.parent.histogram:
                        bottom += self.parent.histogram_size                
                lastAt = at
            return bottom;
        
        self.pleft, self.pright, self.ptop, self.pbottom = 0, parent.graph.width() - 20, 0, self.parent.verticalSpacing
        self.pbottom += getBottom()

        #graph sizes
        self.gleft = 0
        for at in self.attributes:
            if not (self.parent.contType == 1 and isinstance(at, AttrLineCont)) and at.label.boundingRect().width()>self.gleft:
                self.gleft = at.label.boundingRect().width()
        if QCanvasText(self.footerCanvas.footerPercentName, self.footerCanvas).boundingRect().width()>self.gleft:
            self.gleft = QCanvasText(self.footerCanvas.footerPercentName, self.footerCanvas).boundingRect().width()
            
        #self.gleft = max(self.gleft, 100) # should really test footer width, and with of other lables
        self.gleft = max(self.gleft, 80)
        self.gleft +=20
        self.gright=self.pright-(self.pright-self.pleft)/10
        self.gtop = self.ptop + 10
        self.gbottom = self.pbottom - 10

        self.gwidth = self.gright-self.gleft
        self.gheight = self.gbottom - self.gtop

        if self.pbottom < parent.graph.height() - 30:
            self.pbottom = parent.graph.height() - 30


    def show(self):
        # set sizes
        [item.hide() for item in self.allItems()]
        
        self.setSizes(self.parent)
        self.setBackgroundColor(Qt.white)
        self.resize(self.pright, self.pbottom)

        curr_point = self.parent.verticalSpacing
        if self.parent.alignType == 0:
            if self.parent.yAxis == 0:
                self.mapper = Mapper_Linear_Left(self.max_difference,  self.gleft, self.gright)
            else:
                self.mapper = Mapper_Linear_Left(self.max_difference,  self.gleft, self.gright, maxLinearValue = self.max_difference)
        else:
            if self.parent.yAxis == 0:
                self.mapper = Mapper_Linear_Center(self.minBeta, self.maxBeta, self.gleft, self.gright)
            else:
                self.mapper = Mapper_Linear_Center(self.minBeta, self.maxBeta, self.gleft, self.gright, maxLinearValue = self.maxBeta, minLinearValue = self.minBeta)
        # draw HEADER and vertical line
        topRect=QRect(self.gleft, self.gtop, self.gwidth, self.parent.verticalSpacing-20)
        self.header.paintHeader(topRect, self.mapper) 
        # draw nomogram
        middleRect=QRect(self.gleft, self.ptop, self.gwidth, self.gheight)
        self.paint(middleRect, self.mapper)
        # draw final line
        bottomRect=QRect(self.gleft, self.gtop, self.gwidth, 2*self.parent.verticalSpacing-30)
        self.footerCanvas.paintFooter(bottomRect, self.parent.alignType, self.parent.yAxis, self.mapper)        
        self.footerCanvas.updateMarkers()
        if self.parent.probability:
            self.showAllMarkers()
        self.update()

    def showBaseLine(self, show):
        if show:
            self.zeroLine.show()
        else:
            self.zeroLine.hide()
        self.update()

    def printOUT(self):
        print "constant:", str(self.constant.betaValue)
        for a in self.attributes:
            print a.toString()

    def findAttribute(self, y):
        for at in self.attributes:
            if y>at.minCanvasY and y<at.maxCanvasY:
                return at
        return None
        
    def updateValues(self, x, y, obj):
        if obj.attribute.updateValueXY(x, y):
            self.footerCanvas.updateMarkers()
            self.update()
            self.header.headerAttrLine.marker.setX(obj.attribute.marker.x())
            self.header.headerAttrLine.marker.show()
            self.header.update()

    def stopDragging(self):
        self.header.headerAttrLine.marker.hide()
        self.header.update()



# #################################################################### 
# CANVAS VIEWERS
# ####################################################################
class OWNomogramHeader(QCanvasView):
    def __init__(self, canvas, mainArea):
        apply(QCanvasView.__init__,(self,)+(canvas,mainArea))
        self.setMouseTracking(True)
        self.viewport().setMouseTracking(True)
        self.mouseOverObject = None        

    # ###################################################################
    # mouse is running around, perhaps Jerry is nearby ##################
    # or technically: user moved mouse ################################## 
    def contentsMouseMoveEvent(self, ev):
        if self.canvas():
            items = filter(lambda ci: ci.z()==50, self.canvas().collisions(ev.pos()))
            if len(items)>0:
                if self.mouseOverObject:
                    self.mouseOverObject.hideSelected()
                self.mouseOverObject = items[0]
                self.mouseOverObject.showSelected()
                self.canvas().update()
            elif self.mouseOverObject:
                self.mouseOverObject.hideSelected()
                self.mouseOverObject = None
                self.canvas().update()
                

class OWNomogramGraph(QCanvasView):
    def __init__(self, canvas, mainArea):
        apply(QCanvasView.__init__,(self,)+(canvas,mainArea))
        self.selectedObject = None
        self.mouseOverObject = None        
        self.setMouseTracking(True)
        self.viewport().setMouseTracking(True)
        self.bDragging = False
        self.resizing = False

    def resizeEvent(self, event):
        apply(QCanvasView.resizeEvent, (self,event))
        if self.canvas():
            self.resizing = True
            self.canvas().show()

    # ###################################################################
    # mouse button was pressed #########################################
    def contentsMousePressEvent(self, ev):
        if self.canvas() and ev.button() == QMouseEvent.LeftButton:
            items = filter(lambda ci: ci.z()==50, self.canvas().collisions(ev.pos()))
            if len(items)>0:
                self.selectedObject = items[0]
                #self.canvas().updateValues(ev.x(), ev.y(), self.selectedObject)
                self.bDragging = True

    # ###################################################################
    # mouse button was released #########################################
    def contentsMouseReleaseEvent(self, ev):
 #       if self.resizing:
 #           self.resizing = False
 #           self.cavnas().show()
        if self.bDragging:
            self.bDragging = False
            self.canvas().stopDragging()

    # ###################################################################
    # mouse is running around, perhaps Jerry is nearby ##################
    # or technically: user moved mouse ################################## 
    def contentsMouseMoveEvent(self, ev):
        if self.bDragging:
            self.canvas().updateValues(ev.x(), ev.y(), self.selectedObject)
        elif self.canvas():
            items = filter(lambda ci: ci.z()==50, self.canvas().collisions(ev.pos()))
            if len(items)>0:
                if self.mouseOverObject:
                    self.mouseOverObject.hideSelected()
                self.mouseOverObject = items[0]
                self.mouseOverObject.showSelected()
                self.canvas().update()
            elif self.mouseOverObject:
                self.mouseOverObject.hideSelected()
                self.mouseOverObject = None
                self.canvas().update()


# ###################################################################################################################
# ------------------------------------------------------------------------------------------------------------------
# MAPPERS            
# ------------------------------------------------------------------------------------------------------------------
# ------------------------------------------------------------------------------------------------------------------
def createSetOfVisibleValues(min, max, dif):
    if dif == 0.0 or max == min:
        return [0.0]
    upper = max-min+1.8*dif
#    add = round((min-d

⌨️ 快捷键说明

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