📄 owpolyvizgraph.py
字号:
sum_i = Numeric.add.reduce(selectedData)
# test if there are zeros in sum_i
if len(Numeric.nonzero(sum_i)) < len(sum_i):
add = Numeric.where(sum_i == 0, 1.0, 0.0)
sum_i += add
x_positions = Numeric.sum(Numeric.swapaxes(XAnchorPositions * Numeric.swapaxes(selectedData, 0,1), 0,1)) * self.scaleFactor / sum_i
y_positions = Numeric.sum(Numeric.swapaxes(YAnchorPositions * Numeric.swapaxes(selectedData, 0,1), 0,1)) * self.scaleFactor / sum_i
validData = self.getValidList(indices)
for i in range(dataSize):
if validData[i] == 0: continue
curveData[i][XANCHORS] = XAnchorPositions[i]
curveData[i][YANCHORS] = YAnchorPositions[i]
if self.showKNNModel == 1:
# variables and domain for the table
domain = orange.Domain([orange.FloatVariable("xVar"), orange.FloatVariable("yVar"), self.rawdata.domain.classVar])
table = orange.ExampleTable(domain)
# build an example table
for i in range(dataSize):
if validData[i]:
table.append(orange.Example(domain, [x_positions[i], y_positions[i], self.rawdata[i].getclass()]))
kNNValues, probabilities = self.kNNOptimization.kNNClassifyData(table)
accuracy = copy(kNNValues)
measure = self.kNNOptimization.getQualityMeasure()
if self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
if ((measure == CLASS_ACCURACY or measure == AVERAGE_CORRECT) and self.showCorrect) or (measure == BRIER_SCORE and not self.showCorrect):
kNNValues = [1.0 - val for val in kNNValues]
else:
if self.showCorrect: kNNValues = [1.0 - val for val in kNNValues]
# fill and edge color palettes
bwColors = ColorPaletteBW(-1, 55, 255)
if self.rawdata.domain.classVar.varType == orange.VarTypes.Continuous: classColors = self.contPalette
else: classColors = self.discPalette
if table.domain.classVar.varType == orange.VarTypes.Continuous: preText = 'Mean square error : '
else:
if measure == CLASS_ACCURACY: preText = "Classification accuracy : "
elif measure == AVERAGE_CORRECT: preText = "Average correct classification : "
else: preText = "Brier score : "
for i in range(len(table)):
fillColor = bwColors.getColor(kNNValues[i])
edgeColor = classColors.getColor(classValueIndices[table[i].getclass().value])
self.addCurve(str(i), fillColor, edgeColor, self.pointWidth, xData = [table[i][0].value], yData = [table[i][1].value])
self.addAnchorLine(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], fillColor, i, length)
# CONTINUOUS class
elif self.rawdata.domain.classVar.varType == orange.VarTypes.Continuous:
for i in range(dataSize):
if not validData[i]: continue
if self.useDifferentColors: newColor = self.contPalette[self.noJitteringScaledData[classNameIndex][i]]
else: newColor = QColor(0,0,0)
curveData[i][PENCOLOR] = newColor
curveData[i][BRUSHCOLOR] = newColor
self.addCurve(str(i), newColor, newColor, self.pointWidth, xData = [x_positions[i]], yData = [y_positions[i]])
self.addTooltipKey(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], newColor, i)
self.addAnchorLine(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], newColor, i, length)
# DISCRETE class + optimize drawing
elif self.optimizedDrawing:
pos = [[ [] , [], [], [], [] ] for i in range(valLen)]
for i in range(dataSize):
if not validData[i]: continue
pos[classValueIndices[self.rawdata[i].getclass().value]][0].append(x_positions[i])
pos[classValueIndices[self.rawdata[i].getclass().value]][1].append(y_positions[i])
pos[classValueIndices[self.rawdata[i].getclass().value]][2].append(i)
pos[classValueIndices[self.rawdata[i].getclass().value]][3].append(curveData[i][XANCHORS])
pos[classValueIndices[self.rawdata[i].getclass().value]][4].append(curveData[i][YANCHORS])
if self.useDifferentColors: self.addAnchorLine(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], self.discPalette[classValueIndices[self.rawdata[i].getclass().value]], i, length)
else: self.addAnchorLine(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], QColor(0,0,0), i, length)
for i in range(valLen):
if self.useDifferentColors: newColor = self.discPalette[i]
else: newColor = QColor(0,0,0)
if self.useDifferentSymbols: curveSymbol = self.curveSymbols[i]
else: curveSymbol = self.curveSymbols[0]
key = self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = pos[i][0], yData = pos[i][1])
for k in range(len(pos[i][0])):
self.addTooltipKey(pos[i][0][k], pos[i][1][k], pos[i][3][k], pos[i][4][k], newColor, pos[i][2][k])
elif self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
for i in range(dataSize):
if not validData[i]: continue
if self.useDifferentColors: newColor = self.discPalette[classValueIndices[self.rawdata[i].getclass().value]]
else: newColor = QColor(0,0,0)
if self.useDifferentSymbols: curveSymbol = self.curveSymbols[classValueIndices[self.rawdata[i].getclass().value]]
else: curveSymbol = self.curveSymbols[0]
self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = [x_positions[i]], yData = [y_positions[i]])
self.addTooltipKey(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], newColor, i)
self.addAnchorLine(x_positions[i], y_positions[i], curveData[i][XANCHORS], curveData[i][YANCHORS], newColor, i, length)
# ##########
# draw polygon
xdata = [x for x in self.XAnchor]; xdata.append(xdata[0])
ydata = [y for y in self.YAnchor]; ydata.append(ydata[0])
newCurveKey = self.addCurve("polygon", QColor(0,0,0), QColor(0,0,0), 0, QwtCurve.Lines, symbol = QwtSymbol.None, xData = xdata, yData = ydata)
pen = self.curve(newCurveKey).pen(); pen.setWidth(2); self.curve(newCurveKey).setPen(pen)
#################
# draw the legend
if self.showLegend:
# show legend for discrete class
if self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
self.addMarker(self.rawdata.domain.classVar.name, 0.87, 1.06, Qt.AlignLeft)
classVariableValues = getVariableValuesSorted(self.rawdata, self.rawdata.domain.classVar.name)
for index in range(len(classVariableValues)):
if self.useDifferentColors: color = self.discPalette[index]
else: color = QColor(0,0,0)
y = 1.0 - index * 0.05
if not self.useDifferentSymbols: curveSymbol = self.curveSymbols[0]
else: curveSymbol = self.curveSymbols[index]
self.addCurve(str(index), color, color, self.pointWidth, symbol = curveSymbol, xData = [0.95, 0.95], yData = [y, y])
self.addMarker(classVariableValues[index], 0.90, y, Qt.AlignLeft + Qt.AlignVCenter)
# show legend for continuous class
else:
xs = [1.15, 1.20, 1.20, 1.15]
count = 200
height = 2 / float(count)
for i in range(count):
y = -1.0 + i*2.0/float(count)
col = self.contPalette[i/float(count)]
curve = PolygonCurve(self, QPen(col), QBrush(col))
newCurveKey = self.insertCurve(curve)
self.setCurveData(newCurveKey, xs, [y,y, y+height, y+height])
# add markers for min and max value of color attribute
[minVal, maxVal] = self.attrValues[self.rawdata.domain.classVar.name]
self.addMarker("%s = %%.%df" % (self.rawdata.domain.classVar.name, self.rawdata.domain.classVar.numberOfDecimals) % (minVal), xs[0] - 0.02, -1.0 + 0.04, Qt.AlignLeft)
self.addMarker("%s = %%.%df" % (self.rawdata.domain.classVar.name, self.rawdata.domain.classVar.numberOfDecimals) % (maxVal), xs[0] - 0.02, +1.0 - 0.04, Qt.AlignLeft)
def addAnchorLine(self, x, y, xAnchors, yAnchors, color, index, count):
for j in range(count):
dist = EuclDist([x, y], [xAnchors[j] , yAnchors[j]])
if dist == 0: continue
kvoc = float(self.lineLength * 0.05) / dist
lineX1 = x; lineY1 = y
# we don't make extrapolation
if kvoc > 1: lineX2 = lineX1; lineY2 = lineY1
else:
lineX2 = (1.0 - kvoc)*xAnchors[j] + kvoc * lineX1
lineY2 = (1.0 - kvoc)*yAnchors[j] + kvoc * lineY1
self.addCurve('line' + str(index), color, color, 0, QwtCurve.Lines, symbol = QwtSymbol.None, xData = [xAnchors[j], lineX2], yData = [yAnchors[j], lineY2])
# create a dictionary value for the data point
# this will enable to show tooltips faster and to make selection of examples available
def addTooltipKey(self, x, y, xAnchors, yAnchors, color, index):
dictValue = "%.1f-%.1f"%(x, y)
if not self.dataMap.has_key(dictValue): self.dataMap[dictValue] = []
self.dataMap[dictValue].append((x, y, xAnchors, yAnchors, color, index))
# ##############
# draw tooltips
def onMouseMoved(self, e):
redraw = 0
if self.tooltipCurveKeys != [] or self.tooltipMarkers != []: redraw = 1
for key in self.tooltipCurveKeys: self.removeCurve(key)
for marker in self.tooltipMarkers: self.removeMarker(marker)
self.tooltipCurveKeys = []
self.tooltipMarkers = []
# in case we are drawing a rectangle, we don't draw enhanced tooltips
# because it would then fail to draw the rectangle
if self.mouseCurrentlyPressed:
OWGraph.onMouseMoved(self, e)
if redraw: self.replot()
return
xFloat = self.invTransform(QwtPlot.xBottom, e.x())
yFloat = self.invTransform(QwtPlot.yLeft, e.y())
dictValue = "%.1f-%.1f"%(xFloat, yFloat)
if self.dataMap.has_key(dictValue):
points = self.dataMap[dictValue]
bestDist = 100.0
nearestPoint = ()
for (x_i, y_i, xAnchors, yAnchors, color, index) in points:
currDist = sqrt((xFloat-x_i)*(xFloat-x_i) + (yFloat-y_i)*(yFloat-y_i))
if currDist < bestDist:
bestDist = currDist
nearestPoint = (x_i, y_i, xAnchors, yAnchors, color, index)
(x_i, y_i, xAnchors, yAnchors, color, index) = nearestPoint
if self.tooltipKind == LINE_TOOLTIPS and bestDist < 0.05:
for i in range(len(self.shownAttributes)):
# draw lines
key = self.addCurve("Tooltip curve", color, color, 1, style = QwtCurve.Lines, symbol = QwtSymbol.None, xData = [x_i, xAnchors[i]], yData = [y_i, yAnchors[i]])
self.tooltipCurveKeys.append(key)
# draw text
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -