📄 owscatterplot.py
字号:
if not (self.data and exData and str(exData.domain.variables) == str(self.data.domain.variables)): # preserve attribute choice if the domain is the same
self.initAttrValues()
self.openContext("", data)
self.updateGraph()
self.sendSelections()
# set an example table with a data subset subset of the data. if called by a visual classifier, the update parameter will be 0
def subsetdata(self, data, update = 1):
if self.graph.subsetData != None and data != None and self.graph.subsetData.checksum() == data.checksum(): return # check if the new data set is the same as the old one
self.graph.subsetData = data
qApp.processEvents() # TODO: find out why scatterplot crashes if we remove this line and send a subset of data that is not in self.rawdata - as in cluster argumentation
if update: self.updateGraph()
self.vizrank.setSubsetData(data)
self.clusterDlg.setSubsetData(data)
# receive information about which attributes we want to show on x and y axis
def attributeSelection(self, list):
if not self.data or not list or len(list) < 2: return
self.attrX = list[0]
self.attrY = list[1]
self.majorUpdateGraph()
# visualize the results of the classification
def test_results(self, results):
self.classificationResults = None
if isinstance(results, orngTest.ExperimentResults) and len(results.results) > 0 and len(results.results[0].probabilities) > 0:
self.classificationResults = [results.results[i].probabilities[0][results.results[i].actualClass] for i in range(len(results.results))]
self.classificationResults = (self.classificationResults, "Probability of correct classificatioin = %.2f%%")
self.updateGraph()
# set the learning method to be used in VizRank
def vizRankLearner(self, learner):
self.vizrank.externalLearner = learner
# send signals with selected and unselected examples as two datasets
def sendSelections(self):
(selected, unselected) = self.graph.getSelectionsAsExampleTables([self.attrX, self.attrY])
self.send("Selected Examples",selected)
self.send("Unselected Examples",unselected)
# ##############################################################################################################################################################
# KNN OPTIMIZATION BUTTON EVENTS
# ##############################################################################################################################################################
def setActiveLearner(self, idx):
self.send("Learner", self.learnersArray[self.learnerIndex])
# ################################################################################################
# find projections that have tight clusters of points that belong to the same class value
def optimizeClusters(self):
if self.data == None: return
self.clusterDlg.clearResults()
self.clusterDlg.clusterStabilityButton.setOn(0)
self.clusterDlg.pointStability = None
self.clusterDlg.disableControls()
try:
attributeNameOrder = self.clusterDlg.getEvaluatedAttributes(self.data)
self.graph.getOptimalClusters(attributeNameOrder, self.clusterDlg.addResult) # evaluate projections
except:
type, val, traceback = sys.exc_info()
sys.excepthook(type, val, traceback) # print the exception
self.clusterDlg.enableControls()
self.clusterDlg.finishedAddingResults()
def showSelectedAttributes(self):
val = self.vizrank.getSelectedProjection()
if not val: return
(accuracy, other_results, tableLen, attrs, tryIndex, generalDict) = val
if self.data.domain.classVar:
self.attrColor = self.data.domain.classVar.name
self.majorUpdateGraph(attrs)
def showSelectedCluster(self):
val = self.clusterDlg.getSelectedCluster()
if not val: return
(value, closure, vertices, attrList, classValue, enlargedClosure, other, strList) = val
if self.clusterDlg.clusterStabilityButton.isOn():
validData = self.graph.getValidList([self.graph.attributeNames.index(self.attrX), self.graph.attributeNames.index(self.attrY)])
insideColors = (Numeric.compress(validData, self.clusterDlg.pointStability), "Point inside a cluster in %.2f%%")
else: insideColors = None
self.majorUpdateGraph(attrList, insideColors, (closure, enlargedClosure, classValue))
# ##############################################################################################################################################################
# ATTRIBUTE SELECTION
# ##############################################################################################################################################################
def getShownAttributeList(self):
return [self.attrX, self.attrY]
def initAttrValues(self):
self.attrXCombo.clear()
self.attrYCombo.clear()
self.attrColorCombo.clear()
self.attrLabelCombo.clear()
self.attrShapeCombo.clear()
self.attrSizeCombo.clear()
if self.data == None: return
self.attrColorCombo.insertItem("(One color)")
self.attrLabelCombo.insertItem("(No labels)")
self.attrShapeCombo.insertItem("(One shape)")
self.attrSizeCombo.insertItem("(One size)")
#labels are usually chosen from meta variables, put them on top
for metavar in [self.data.domain.getmeta(mykey) for mykey in self.data.domain.getmetas().keys()]:
self.attrLabelCombo.insertItem(self.icons[metavar.varType], metavar.name)
contList = []
discList = []
for attr in self.data.domain:
self.attrXCombo.insertItem(self.icons[attr.varType], attr.name)
self.attrYCombo.insertItem(self.icons[attr.varType], attr.name)
self.attrColorCombo.insertItem(self.icons[attr.varType], attr.name)
self.attrSizeCombo.insertItem(self.icons[attr.varType], attr.name)
if attr.varType == orange.VarTypes.Discrete: self.attrShapeCombo.insertItem(self.icons[attr.varType], attr.name)
self.attrLabelCombo.insertItem(self.icons[attr.varType], attr.name)
self.attrX = str(self.attrXCombo.text(0))
if self.attrYCombo.count() > 1: self.attrY = str(self.attrYCombo.text(1))
else: self.attrY = str(self.attrYCombo.text(0))
if self.data.domain.classVar:
self.attrColor = self.data.domain.classVar.name
else:
self.attrColor = ""
self.attrShape = ""
self.attrSize= ""
self.attrLabel = ""
def majorUpdateGraph(self, attrList = None, insideColors = None, clusterClosure = None, **args):
self.graph.removeAllSelections()
self.updateGraph(attrList, insideColors, clusterClosure, **args)
def updateGraph(self, attrList = None, insideColors = None, clusterClosure = None, **args):
self.graph.zoomStack = []
if not self.data:
return
if attrList:
self.attrX = attrList[0]
self.attrY = attrList[1]
if self.vizrank.showKNNCorrectButton.isOn() or self.vizrank.showKNNWrongButton.isOn():
kNNExampleAccuracy, probabilities = self.vizrank.kNNClassifyData(self.graph.createProjectionAsExampleTable([self.graph.attributeNameIndex[self.attrX], self.graph.attributeNameIndex[self.attrY]]))
if self.vizrank.showKNNCorrectButton.isOn(): kNNExampleAccuracy = ([1.0 - val for val in kNNExampleAccuracy], "Probability of wrong classification = %.2f%%")
else: kNNExampleAccuracy = (kNNExampleAccuracy, "Probability of correct classification = %.2f%%")
else:
kNNExampleAccuracy = None
self.graph.insideColors = insideColors or self.classificationResults or kNNExampleAccuracy or self.outlierValues
self.graph.clusterClosure = clusterClosure
self.graph.updateData(self.attrX, self.attrY, self.attrColor, self.attrShape, self.attrSize, self.showColorLegend, self.attrLabel)
self.graph.repaint()
# ##############################################################################################################################################################
# SCATTERPLOT SETTINGS
# ##############################################################################################################################################################
#update status on progress bar - gets called by OWScatterplotGraph
def updateProgress(self, current, total):
self.progressBar.setTotalSteps(total)
self.progressBar.setProgress(current)
def replotCurves(self):
for key in self.graph.curveKeys():
symbol = self.graph.curveSymbol(key)
self.graph.setCurveSymbol(key, QwtSymbol(symbol.style(), symbol.brush(), symbol.pen(), QSize(self.graph.pointWidth, self.graph.pointWidth)))
self.graph.repaint()
def setShowGridlines(self):
self.graph.enableGridXB(self.showGridlines)
self.graph.enableGridYL(self.showGridlines)
def setAutoSendSelection(self):
if self.autoSendSelection:
self.zoomSelectToolbar.buttonSendSelections.setEnabled(0)
self.sendSelections()
else:
self.zoomSelectToolbar.buttonSendSelections.setEnabled(1)
def setColors(self):
dlg = self.createColorDialog()
if dlg.exec_loop():
self.colorSettings = dlg.getColorSchemas()
self.graph.contPalette = dlg.getContinuousPalette("contPalette")
self.graph.discPalette = dlg.getDiscretePalette()
self.graph.setCanvasBackground(dlg.getColor("Canvas"))
self.graph.setGridPen(QPen(dlg.getColor("Grid")))
self.updateGraph()
def createColorDialog(self):
c = OWDlgs.ColorPalette(self, "Color Palette")
c.createDiscretePalette(" Discrete Palette ")
c.createContinuousPalette("contPalette", " Continuous palette ")
box = c.createBox("otherColors", " Other Colors ")
c.createColorButton(box, "Canvas", "Canvas color", Qt.white)
box.addSpace(5)
c.createColorButton(box, "Grid", "Grid color", Qt.black)
box.addSpace(5)
box.adjustSize()
c.setColorSchemas(self.colorSettings)
return c
def destroy(self, dw = 1, dsw = 1):
self.clusterDlg.hide()
self.vizrank.hide()
OWWidget.destroy(self, dw, dsw)
def hasDiscreteClass(self, data = -1):
if data == -1: data = self.data
return data and data.domain.classVar and data.domain.classVar.varType == orange.VarTypes.Discrete
#test widget appearance
if __name__=="__main__":
# a=QApplication(sys.argv)
a=QApplication([])
ow=OWScatterPlot()
a.setMainWidget(ow)
ow.show()
#save settings
ow.saveSettings()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -