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

📄 owlinprojgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 4 页
字号:
#
#
# OWLinProjGraph.py
#
from OWGraph import *
from copy import copy
import time
from operator import add
from math import *
from OWClusterOptimization import *
from orngScaleLinProjData import *
import orngVisFuncts

# indices in curveData
SYMBOL = 0
PENCOLOR = 1
BRUSHCOLOR = 2

LINE_TOOLTIPS = 0
VISIBLE_ATTRIBUTES = 1
ALL_ATTRIBUTES = 2

TOOLTIPS_SHOW_DATA = 0
TOOLTIPS_SHOW_SPRINGS = 1

###########################################################################################
##### CLASS : OWLINPROJGRAPH
###########################################################################################
class OWLinProjGraph(OWGraph, orngScaleLinProjData):
    def __init__(self, widget, parent = None, name = "None"):
        OWGraph.__init__(self, parent, name)
        orngScaleLinProjData.__init__(self)
        
        self.totalPossibilities = 0 # a variable used in optimization - tells us the total number of different attribute positions
        self.triedPossibilities = 0 # how many possibilities did we already try
        self.startTime = time.time()
        self.p = None
        
        self.dataMap = {}        # each key is of form: "xVal-yVal", where xVal and yVal are discretized continuous values. Value of each key has form: (x,y, HSVValue, [data vals])
        self.tooltipCurveKeys = []
        self.tooltipMarkers   = []
        self.clusterOptimization = None
        self.widget = widget

        # moving anchors manually
        self.shownAttributes = []
        self.selectedAnchorIndex = None

        self.hideRadius = 0
        self.showAnchors = 1
        
        self.onlyOnePerSubset = 1
        self.showLegend = 1
        self.useDifferentSymbols = 0
        self.useDifferentColors = 1
        self.tooltipKind = 0        # index in ["Show line tooltips", "Show visible attributes", "Show all attributes"]
        self.tooltipValue = 0       # index in ["Tooltips show data values", "Tooltips show spring values"]
        self.scaleFactor = 1.0
        self.showClusters = 0
        self.showAttributeNames = 1

        self.showKNN = 0   # widget sets this to 1 or 2 if you want to see correct or wrong classifications
        self.insideColors = None
        self.clusterClosure = None

        self.setAxisScaleDraw(QwtPlot.xBottom, HiddenScaleDraw())
        self.setAxisScaleDraw(QwtPlot.yLeft, HiddenScaleDraw())
        scaleDraw = self.axisScaleDraw(QwtPlot.xBottom)
        scaleDraw.setOptions(0) 
        scaleDraw.setTickLength(0, 0, 0)
        scaleDraw = self.axisScaleDraw(QwtPlot.yLeft)
        scaleDraw.setOptions(0) 
        scaleDraw.setTickLength(0, 0, 0)
        self.setAxisScale(QwtPlot.xBottom, -1.13, 1.13, 1)
        self.setAxisScale(QwtPlot.yLeft, -1.13, 1.13, 1)

        self.showProbabilities = 0
        self.squareGranularity = 3
        self.spaceBetweenCells = 1

    def setData(self, data):
        OWGraph.setData(self, data)
        orngScaleLinProjData.setData(self, data)
        self.anchorData = []

        self.setAxisScale(QwtPlot.yLeft, -1.13, 1.13, 1)
        self.setAxisScale(QwtPlot.xBottom, -1.13, 1.13, 1)

        if data and data.domain.classVar and data.domain.classVar.varType == orange.VarTypes.Continuous:
            self.setAxisScale(QwtPlot.xBottom, -1.13, 1.13 + 0.1, 1)   # if we have a continuous class we need a bit more space on the right to show a color legend

    # ####################################################################
    # update shown data. Set labels, coloring by className ....
    def updateData(self, labels = None, setAnchors = 0, **args):
        self.removeDrawingCurves()  # my function, that doesn't delete selection curves
        #self.removeCurves()
        self.removeMarkers()

        self.__dict__.update(args)
        if not labels: labels = [anchor[2] for anchor in self.anchorData]
        self.shownAttributes = labels
        self.dataMap = {}   # dictionary with keys of form "x_i-y_i" with values (x_i, y_i, color, data)

        if self.scaledData == None or len(labels) < 3: self.updateLayout(); return
        haveSubsetData = 0
        if self.subsetData and self.rawdata and self.subsetData.domain == self.rawdata.domain: haveSubsetData = 1
                
        # store indices to shown attributes
        indices = [self.attributeNameIndex[label] for label in labels]
        if setAnchors or (args.has_key("XAnchors") and args.has_key("YAnchors")):
            self.potentialsBmp = None
            self.setAnchors(args.get("XAnchors"), args.get("YAnchors"), labels)
            #self.anchorData = self.createAnchors(len(labels), labels)    # used for showing tooltips

        # do we want to show anchors and their labels
        if self.showAnchors:
            if self.hideRadius > 0:
                xdata = self.createXAnchors(100)*(self.hideRadius / 10)
                ydata = self.createYAnchors(100)*(self.hideRadius / 10)
                self.addCurve("hidecircle", QColor(200,200,200), QColor(200,200,200), 1, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = xdata.tolist() + [xdata[0]], yData = ydata.tolist() + [ydata[0]])
                
            # draw dots at anchors
            shownAnchorData = filter(lambda p, r=self.hideRadius**2/100: p[0]**2+p[1]**2>r, self.anchorData)
            self.anchorsAsVectors = not self.normalizeExamples # min([x[0]**2+x[1]**2 for x in self.anchorData]) < 0.99
            self.shownLabels = [a[2] for a in shownAnchorData]

            if self.anchorsAsVectors:
                r=self.hideRadius**2/100
                for i,(x,y,a) in enumerate(shownAnchorData):
                    self.addCurve("l%i" % i, QColor(160, 160, 160), QColor(160, 160, 160), 10, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = [0, x], yData = [0, y], showFilledSymbols = 1, lineWidth=2)
                    if self.showAttributeNames:
                        self.addMarker(a, x*1.07, y*1.04, Qt.AlignCenter, bold=1)
            else:
                XAnchors = [a[0] for a in shownAnchorData]
                YAnchors = [a[1] for a in shownAnchorData]
                self.addCurve("dots", QColor(160,160,160), QColor(160,160,160), 10, style = QwtCurve.NoCurve, symbol = QwtSymbol.Ellipse, xData = XAnchors, yData = YAnchors, showFilledSymbols = 1)

                # draw text at anchors
                if self.showAttributeNames:
                    for x, y, a in shownAnchorData:
                        self.addMarker(a, x*1.07, y*1.04, Qt.AlignCenter, bold = 1)

        if self.showAnchors and not self.anchorsAsVectors:
            # draw "circle"
            xdata = self.createXAnchors(100)
            ydata = self.createYAnchors(100)
            self.addCurve("circle", QColor(0,0,0), QColor(0,0,0), 1, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = xdata.tolist() + [xdata[0]], yData = ydata.tolist() + [ydata[0]])

        self.potentialsClassifier = None # remove the classifier so that repaint won't recompute it
        #self.repaint()  # we have to repaint to update scale to get right coordinates for tooltip rectangles
        self.updateLayout()

        classNameIndex = self.attributeNameIndex[self.rawdata.domain.classVar.name]
        if self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:        # if we have a discrete class
            valLen = len(self.rawdata.domain.classVar.values)
            classValueIndices = getVariableValueIndices(self.rawdata, self.rawdata.domain.classVar.name)    # we create a hash table of variable values and their indices            
        else:    # if we have a continuous class
            valLen = 0

        useDifferentSymbols = 0
        if self.useDifferentSymbols and self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete and valLen < len(self.curveSymbols): useDifferentSymbols = 1

        dataSize = len(self.rawdata)
        validData = self.getValidList(indices)
        transProjData = self.createProjectionAsNumericArray(indices, settingsDict = {"validData": validData, "scaleFactor": self.scaleFactor, "normalize": self.normalizeExamples, "jitterSize": -1, "useAnchorData": 1, "removeMissingData": 0})
        projData = Numeric.transpose(transProjData)
        x_positions = projData[0]
        y_positions = projData[1]

        if self.showProbabilities:
            # construct potentialsClassifier from unscaled positions
            domain = orange.Domain([a[2] for a in self.anchorData]+[self.rawdata.domain.classVar], self.rawdata.domain)
            offsets = [self.offsets[i] for i in indices]
            normalizers = [self.normalizers[i] for i in indices]
            averages = [self.averages[i] for i in indices]
            self.potentialsClassifier = orange.P2NN(domain, Numeric.transpose(Numeric.array([self.unscaled_x_positions, self.unscaled_y_positions, [float(ex.getclass()) for ex in self.rawdata]])),
                                                    self.anchorData, offsets, normalizers, averages, self.normalizeExamples, law=1)

           
        # do we have cluster closure information
        if self.showClusters and self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
            data = self.createProjectionAsExampleTable(indices, settingsDict = {"validData": validData, "normalize": self.normalizeExamples, "scaleFactor": self.trueScaleFactor, "jitterSize": 0.001 * self.clusterOptimization.jitterDataBeforeTriangulation, "useAnchorData": 1})
            graph, valueDict, closureDict, polygonVerticesDict, enlargedClosureDict, otherDict = self.clusterOptimization.evaluateClusters(data)
            for key in valueDict.keys():
                if not polygonVerticesDict.has_key(key): continue
                for (i,j) in closureDict[key]:
                    color = classValueIndices[graph.objects[i].getclass().value]
                    self.addCurve("", self.discPalette[color], self.discPalette[color], 1, QwtCurve.Lines, QwtSymbol.None, xData = [data[i][0].value, data[j][0].value], yData = [data[i][1].value, data[j][1].value], lineWidth = 1)

            """
            self.removeMarkers()
            for i in range(graph.nVertices):
                if not validData[i]: continue
                mkey = self.insertMarker(str(i+1))
                self.marker(mkey).setXValue(float(data[i][0]))
                self.marker(mkey).setYValue(float(data[i][1]))
                self.marker(mkey).setLabelAlignment(Qt.AlignCenter + Qt.AlignBottom)
            """

        elif self.clusterClosure: self.showClusterLines(indices, validData)        

        # ##############################################################
        # show model quality
        # ############################################################## 
        if self.insideColors != None or self.showKNN:
            # if we want to show knn classifications of the examples then turn the projection into example table and run knn
            if self.insideColors:
                insideData, stringData = self.insideColors
            else:

⌨️ 快捷键说明

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