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

📄 owscatterplotgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 3 页
字号:
#
# OWScatterPlotGraph.py
#
from OWGraph import *
import time
from orngCI import FeatureByCartesianProduct
import OWClusterOptimization
import RandomArray
import orngVisFuncts
from orngScaleScatterPlotData import *

DONT_SHOW_TOOLTIPS = 0
VISIBLE_ATTRIBUTES = 1
ALL_ATTRIBUTES = 2

MIN_SHAPE_SIZE = 6


###########################################################################################
##### CLASS : OWSCATTERPLOTGRAPH
###########################################################################################
class OWScatterPlotGraph(OWGraph, orngScaleScatterPlotData):
    def __init__(self, scatterWidget, parent = None, name = "None"):
        "Constructs the graph"
        OWGraph.__init__(self, parent, name)
        orngScaleScatterPlotData.__init__(self)
    
        self.pointWidth = 5
        self.jitterContinuous = 0
        self.jitterSize = 5
        self.showAxisScale = 1
        self.showXaxisTitle= 1
        self.showYLaxisTitle = 1
        self.showLegend = 1
        self.showDistributions = 0        
        self.optimizedDrawing = 1
        self.showClusters = 0
        self.tooltipKind = 1
        self.showFilledSymbols = 1
        self.showProbabilities = 1
        
        self.toolRects = []
        self.tooltipData = []
        self.scatterWidget = scatterWidget
        self.clusterOptimization = None
        self.insideColors = None
        self.clusterClosure = None
        self.shownAttributeIndices = []
        self.shownXAttribute = ""
        self.shownYAttribute = ""
        self.squareGranularity = 3
        self.spaceBetweenCells = 1
        
        self.oldShowColorLegend = -1
        self.oldLegendKeys = {}

    def setData(self, data):
        OWGraph.setData(self, data)
        orngScaleScatterPlotData.setData(self, data)

    #########################################################
    # update shown data. Set labels, coloring by className ....
    def updateData(self, xAttr, yAttr, colorAttr, shapeAttr = "", sizeShapeAttr = "", showColorLegend = 0, labelAttr = None, **args):
        self.removeDrawingCurves(removeLegendItems = 0)  # my function, that doesn't delete selection curves
        self.removeMarkers()
        self.tips.removeAll()
        if not self.showLegend: self.enableLegend(0)
        self.tooltipData = []
        self.potentialsClassifier = None
        self.shownXAttribute = xAttr
        self.shownYAttribute = yAttr
        
        # if we have some subset data then we show the examples in the data set with full symbols, others with empty
        haveSubsetData = (self.subsetData and self.rawdata and self.subsetData.domain == self.rawdata.domain)
            
        if self.scaledData == None or len(self.scaledData) == 0:
            #self.setAxisScale(QwtPlot.xBottom, 0, 1, 1); self.setAxisScale(QwtPlot.yLeft, 0, 1, 1)
            self.setXaxisTitle(""); self.setYLaxisTitle("")
            return
        
        self.__dict__.update(args)      # set value from args dictionary

        (xVarMin, xVarMax) = self.attrValues[xAttr]; xVar = xVarMax - xVarMin
        (yVarMin, yVarMax) = self.attrValues[yAttr]; yVar = yVarMax - yVarMin
        xAttrIndex = self.attributeNameIndex[xAttr]
        yAttrIndex = self.attributeNameIndex[yAttr]
    
        # set axis for x attribute
        attrXIndices = {}
        discreteX = (self.rawdata.domain[xAttrIndex].varType == orange.VarTypes.Discrete)
        if discreteX:
            xVarMax -= 1; xVar -= 1
            xmin = xVarMin - (self.jitterSize + 10.)/100. ; xmax = xVarMax + (self.jitterSize + 10.)/100.
            attrXIndices = getVariableValueIndices(self.rawdata, xAttrIndex)
            if self.showAxisScale or xAttr != self.XaxisTitle:
                self.setXlabels(getVariableValuesSorted(self.rawdata, xAttrIndex))
            self.setAxisScale(QwtPlot.xBottom, xmin, xmax + showColorLegend * xVar * 0.07, 1)
        else:
            off  = (xVarMax - xVarMin) * (self.jitterSize * self.jitterContinuous + 2) / 100.0
            xmin = xVarMin - off; xmax = xVarMax + off
            self.setAxisScale(QwtPlot.xBottom, xmin, xmax + showColorLegend * xVar * 0.07)
        
        # set axis for y attribute
        attrYIndices = {}
        discreteY = (self.rawdata.domain[yAttrIndex].varType == orange.VarTypes.Discrete)
        if discreteY:
            yVarMax -= 1; yVar -= 1
            ymin, ymax = yVarMin - (self.jitterSize + 10.)/100., yVarMax + (self.jitterSize + 10.)/100.
            attrYIndices = getVariableValueIndices(self.rawdata, yAttrIndex)
            if self.showAxisScale or yAttr != self.YLaxisTitle:
                self.setYLlabels(getVariableValuesSorted(self.rawdata, yAttrIndex))
            self.setAxisScale(QwtPlot.yLeft, ymin, ymax, 1)
        else:
            off  = (yVarMax - yVarMin) * (self.jitterSize * self.jitterContinuous + 2) / 100.0
            ymin, ymax = yVarMin - off, yVarMax + off
            self.setAxisScale(QwtPlot.yLeft, ymin, ymax)

        if self.showXaxisTitle: self.setXaxisTitle(xAttr)
        else: self.setXaxisTitle(None)

        if self.showYLaxisTitle: self.setYLaxisTitle(yAttr)
        else: self.setYLaxisTitle(None)

        self.oldShowColorLegend = showColorLegend

        colorIndex = -1
        if colorAttr != "" and colorAttr != "(One color)":
            colorIndex = self.attributeNameIndex[colorAttr]
            if self.rawdata.domain[colorAttr].varType == orange.VarTypes.Discrete: colorIndices = getVariableValueIndices(self.rawdata, colorIndex)
            
        shapeIndex = -1
        shapeIndices = {}
        if shapeAttr != "" and shapeAttr != "(One shape)" and len(self.rawdata.domain[shapeAttr].values) < 11:
            shapeIndex = self.attributeNameIndex[shapeAttr]
            if self.rawdata.domain[shapeIndex].varType == orange.VarTypes.Discrete: shapeIndices = getVariableValueIndices(self.rawdata, shapeIndex)

        sizeShapeIndex = -1
        if sizeShapeAttr != "" and sizeShapeAttr != "(One size)":
            sizeShapeIndex = self.attributeNameIndex[sizeShapeAttr]

        attrIndices = [xAttrIndex, yAttrIndex, colorIndex, shapeIndex, sizeShapeIndex]
        while -1 in attrIndices: attrIndices.remove(-1)
        self.shownAttributeIndices = attrIndices

        # compute x and y positions of the points in the scatterplot        
        xData, yData = self.getXYPositions(xAttr, yAttr)
        validData = self.getValidList([xAttrIndex, yAttrIndex])

        # #######################################################
        # show probabilities
        if self.showProbabilities and colorIndex >= 0:
            domain = orange.Domain([self.rawdata.domain[xAttrIndex], self.rawdata.domain[yAttrIndex], self.rawdata.domain.classVar], self.rawdata.domain)
            xdiff = xmax-xmin
            ydiff = ymax-ymin
            scX = [x/xdiff for x in xData]
            scY = [y/ydiff for y in yData]
            
            self.potentialsClassifier = orange.P2NN(domain, Numeric.transpose(Numeric.array([scX, scY, [float(ex[colorIndex]) for ex in self.rawdata]])), None, None, None, None)
            self.xmin = xmin; self.xmax = xmax
            self.ymin = ymin; self.ymax = ymax
            

        # #######################################################
        # show clusters
        if self.showClusters and self.rawdata.domain.classVar and self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
            data = self.createProjectionAsExampleTable([xAttrIndex, yAttrIndex], settingsDict = {"validData": validData, "jitterSize": 0.001 * self.clusterOptimization.jitterDataBeforeTriangulation})
            graph, valueDict, closureDict, polygonVerticesDict, enlargedClosureDict, otherDict = self.clusterOptimization.evaluateClusters(data)
            
            classIndices = getVariableValueIndices(self.rawdata, self.attributeNameIndex[self.rawdata.domain.classVar.name])
            indices = Numeric.compress(validData, Numeric.array(range(len(self.rawdata))))
            
            for key in valueDict.keys():
                if not polygonVerticesDict.has_key(key): continue
                for (i,j) in closureDict[key]:
                    color = self.discPalette[classIndices[graph.objects[i].getclass().value]]
                    self.addCurve("", color, color, 1, QwtCurve.Lines, QwtSymbol.None, xData = [float(self.rawdata[indices[i]][xAttr]), float(self.rawdata[indices[j]][xAttr])], yData = [float(self.rawdata[indices[i]][yAttr]), float(self.rawdata[indices[j]][yAttr])], lineWidth = 1)

            self.removeMarkers()
            for i in range(graph.nVertices):
                if not validData[i]: continue
                mkey = self.insertMarker(str(i))
                self.marker(mkey).setXValue(float(self.rawdata[i][xAttrIndex]))
                self.marker(mkey).setYValue(float(self.rawdata[i][yAttrIndex]))
                self.marker(mkey).setLabelAlignment(Qt.AlignCenter + Qt.AlignBottom)
            
        elif self.clusterClosure: self.showClusterLines(xAttr, yAttr)

        # ##############################################################
        # show the distributions
        if self.showDistributions == 1 and colorIndex != -1 and self.rawdata.domain[colorIndex].varType == orange.VarTypes.Discrete and self.rawdata.domain[xAttrIndex].varType == orange.VarTypes.Discrete and self.rawdata.domain[yAttrIndex].varType == orange.VarTypes.Discrete and not self.insideColors:
            (cart, profit) = FeatureByCartesianProduct(self.rawdata, [self.rawdata.domain[xAttrIndex], self.rawdata.domain[yAttrIndex]])
            tempData = self.rawdata.select(list(self.rawdata.domain) + [cart])
            contXY = orange.ContingencyAttrClass(cart, tempData)   # distribution of X attribute
            xValues = getVariableValuesSorted(self.rawdata, xAttr)
            yValues = getVariableValuesSorted(self.rawdata, yAttr)
            classValuesSorted = getVariableValuesSorted(self.rawdata, colorIndex)
            classValues = list(self.rawdata.domain[colorIndex].values)
            self.tooltipData = []

            sum = 0
            for table in contXY:
                for val in table: sum += val

            for i in range(len(xValues)):
                for j in range(len(yValues)):

⌨️ 快捷键说明

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