📄 owlinproj.py
字号:
text = str(self.vizrank.attributeCountCombo.currentText())
if text == "ALL": maxLen = len(listOfAttributes)
else: maxLen = int(text)
if self.clusterDlg.getOptimizationType() == self.clusterDlg.EXACT_NUMBER_OF_ATTRS: minLen = maxLen
else: minLen = 3
possibilities = 0
for i in range(minLen, maxLen+1): possibilities += orngVisFuncts.combinationsCount(i, len(listOfAttributes))* orngVisFuncts.fact(i-1)/2
self.graph.totalPossibilities = possibilities
self.graph.triedPossibilities = 0
if self.graph.totalPossibilities > 20000:
proj = str(self.graph.totalPossibilities)
l = len(proj)
for i in range(len(proj)-2, 0, -1):
if (l-i)%3 == 0: proj = proj[:i] + "," + proj[i:]
self.printVerbose("OWLinProj: Warning: There are %s possible projections using currently visualized attributes"% (proj))
self.clusterDlg.disableControls()
self.graph.getOptimalClusters(listOfAttributes, minLen, maxLen, self.clusterDlg.addResult)
except:
type, val, traceback = sys.exc_info()
sys.excepthook(type, val, traceback) # print the exception
self.clusterDlg.enableControls()
self.clusterDlg.finishedAddingResults()
self.showSelectedCluster()
# send signals with selected and unselected examples as two datasets
def sendSelections(self):
if not self.data: return
(selected, unselected) = self.graph.getSelectionsAsExampleTables(self.getShownAttributeList(), addProjectedPositions = self.addProjectedPositions)
self.send("Selected Examples",selected)
self.send("Unselected Examples",unselected)
def sendShownAttributes(self):
self.send("Attribute Selection List", [a[0] for a in self.shownAttributes])
# show selected interesting projection
def showSelectedAttributes(self):
val = self.vizrank.getSelectedProjection()
if val:
(accuracy, other_results, tableLen, attrList, tryIndex, generalDict) = val
self.updateGraph(attrList, setAnchors= 1, XAnchors = generalDict.get("XAnchors"), YAnchors = generalDict.get("YAnchors"))
self.graph.removeAllSelections()
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(attr) for attr in attrList])
insideColors = (Numeric.compress(validData, self.clusterDlg.pointStability), "Point inside a cluster in %.2f%%")
else: insideColors = None
self.updateGraph(attrList, 1, insideColors, clusterClosure = (closure, enlargedClosure, classValue))
self.graph.removeAllSelections()
def setShownAttributeList(self, data, shownAttributes = None):
shown = []
hidden = []
if data:
if shownAttributes:
if type(shownAttributes[0]) == tuple:
shown = shownAttributes
else:
domain = self.data.domain
shown = [(domain[a].name, domain[a].varType) for a in shownAttributes]
hidden = filter(lambda x:x not in shown, [(a.name, a.varType) for a in data.domain.attributes])
else:
shown = [(a.name, a.varType) for a in data.domain.attributes]
if not self.showAllAttributes:
hidden = shown[10:]
shown = shown[:10]
if data.domain.classVar and (data.domain.classVar.name, data.domain.classVar.varType) not in shown:
hidden += [(data.domain.classVar.name, data.domain.classVar.varType)]
self.shownAttributes = shown
self.hiddenAttributes = hidden
self.selectedHidden = []
self.selectedShown = []
self.resetAttrManipulation()
self.sendShownAttributes()
def updateGraphAndAnchors(self):
self.updateGraph(setAnchors = 1)
def updateGraph(self, attrList = None, setAnchors = 0, insideColors = None, clusterClosure = None, **args):
if not attrList:
attrList = self.getShownAttributeList()
else:
self.setShownAttributeList(self.data, attrList)
self.graph.showKNN = 0
if (self.vizrank.showKNNCorrectButton.isOn() or self.vizrank.showKNNWrongButton.isOn()) and self.hasDiscreteClass(self.data):
self.graph.showKNN = 1 + self.vizrank.showKNNCorrectButton.isOn()
self.graph.insideColors = insideColors or self.classificationResults or self.outlierValues
self.graph.clusterClosure = clusterClosure
self.graph.updateData(attrList, setAnchors, **args)
self.graph.repaint()
# ###############################################################################################################
# INPUT SIGNALS
# receive new data and update all fields
def cdata(self, data):
if data and data.domain.classVar:
name = getattr(data, "name", "")
data = orange.Preprocessor_dropMissingClasses(data)
# data = data.filterref({data.domain.classVar: [val for val in data.domain.classVar.values]})
data.name = name
if self.data and data and self.data.checksum() == data.checksum(): return # check if the new data set is the same as the old one
self.closeContext()
exData = self.data
self.data = data
self.vizrank.setData(data)
self.clusterDlg.setData(data)
self.freeVizDlg.setData(data)
self.classificationResults = None
self.outlierValues = None
reset = not (data and exData and str(exData.domain.attributes) == str(data.domain.attributes)) # preserve attribute choice if the domain is the same
if reset:
self.setShownAttributeList(self.data, self.attributeSelectionList)
self.openContext("", data)
self.resetAttrManipulation()
self.updateGraph(setAnchors = reset)
self.sendSelections()
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
if update: self.updateGraph()
self.vizrank.setSubsetData(data)
self.clusterDlg.setSubsetData(data)
qApp.processEvents()
# attribute selection signal - info about which attributes to show
def attributeSelection(self, attributeSelectionList):
self.attributeSelectionList = attributeSelectionList
if self.data and self.attributeSelectionList:
for attr in self.attributeSelectionList:
if not self.graph.attributeNameIndex.has_key(attr): # this attribute list belongs to a new dataset that has not come yet
return
self.setShownAttributeList(self.data, self.attributeSelectionList)
self.selectionChanged()
self.updateGraph(setAnchors = 1)
# 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(setAnchors = 1)
# set the learning method to be used in VizRank
def vizRankLearner(self, learner):
self.vizrank.externalLearner = learner
# ###############################################################################################################
# EVENTS
def resetBmpUpdateValues(self):
self.graph.potentialsBmp = None
self.updateGraph()
def setActiveLearner(self):
self.send("Learner", self.learnersArray[self.learnerIndex])
def resetGraphData(self):
orngScaleLinProjData.setData(self.graph, self.data)
#self.graph.setData(self.data)
self.updateGraph()
def setValueScaling(self):
self.graph.insideColors = self.graph.clusterClosure = None
if self.valueScalingType == 0:
self.graph.globalValueScaling = self.graph.scalingByVariance = 0
elif self.valueScalingType == 1:
self.graph.globalValueScaling = 1
self.graph.scalingByVariance = 0
else:
self.graph.globalValueScaling = 0
self.graph.scalingByVariance = 1
#self.graph.setData(self.data)
orngScaleLinProjData.setData(self.graph, self.data)
self.graph.potentialsBmp = None
self.updateGraph()
def selectionChanged(self):
self.zoomSelectToolbar.buttonSendSelections.setEnabled(not self.autoSendSelection)
if self.autoSendSelection: self.sendSelections()
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.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)
box.adjustSize()
c.setColorSchemas(self.colorSettings)
return c
def destroy(self, dw = 1, dsw = 1):
self.clusterDlg.hide()
self.vizrank.hide()
self.freeVizDlg.hide()
OWWidget.destroy(self, dw, dsw)
#test widget appearance
if __name__=="__main__":
a=QApplication(sys.argv)
ow=OWLinProj()
a.setMainWidget(ow)
ow.show()
ow.cdata(orange.ExampleTable("..\\..\\doc\\datasets\\zoo"))
a.exec_loop()
#save settings
ow.saveSettings()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -