📄 owhierarchicalclustering.py
字号:
if self.parent.SelectionMode:
self.cutOffLineDragged=True
self.setCutOffLine(e.pos().x())
return
pos=e.pos()
objList=self.collisions(pos)
if len(objList)==0 and not self.parent.ctrlPressed:
self.clearSelection()
self.update()
return
for e in objList:
if e.__class__==SelectionPoly:
self.removeSelectionItem(e)
self.parent.updateSelection(self.selectionList)
self.update()
return
if objList[0].__class__==MyCanvasRect:
inValid=[]
for el in self.selectionList:
if el.rootCluster.first>=objList[0].cluster.first and \
el.rootCluster.last<=objList[0].cluster.last:
inValid.append(el)
for el in inValid:
self.removeSelectionItem(el)
if not self.parent.ctrlPressed:
self.clearSelection()
self.addSelection(objList[0])
self.parent.updateSelection(self.selectionList)
self.update()
def releaseEvent(self, e):
self.holdoff=False
if not self.rootCluster:
return
if self.parent.SelectionMode and self.cutOffLineDragged:
self.cutOffLineDragged=False
self.bubbleRect.hide()
self.setCutOffLine(e.pos().x())
def mouseMove(self, e):
if not self.rootCluster:
return
if self.parent.SelectionMode==1 and self.cutOffLineDragged:
self.setCutOffLine(e.pos().x())
if not self.parent.DisableBubble:
self.bubbleRect.setText("Cut off height: \n %f" % self.cutOffHeight)
self.bubbleRect.move(e.pos().x(), e.pos().y())
self.bubbleRect.show()
self.update()
return
objList=self.collisions(e.pos())
self.highlight(objList)
if not self.parent.DisableBubble and self.highlighted:
cluster=self.highlighted.cluster
text= "Items: %i \nCluster height: %f" % (len(cluster), cluster.height)
self.bubbleRect.setText(text)
self.bubbleRect.move(e.pos().x(),e.pos().y())
self.bubbleRect.show()
self.update()
else:
self.bubbleRect.hide()
self.update()
if objList and objList[0].__class__==MyCanvasText and not self.parent.DisableBubble:
head="Items: %i" %len(objList[0].cluster)
body=""
if self.parent.Annotation!=0:
bodyItems=[str(a) for a in objList[0].cluster]
if len(bodyItems)>20:
bodyItems=bodyItems[:20]+["..."]
body="\n"+"\n".join(bodyItems)
self.bubbleRect.setText(head+body)
self.bubbleRect.move(e.pos().x(),e.pos().y())
if body!="":
self.bubbleRect.show()
self.update()
def cutOffSelection(self, node, height):
if not node:
return
if node.cluster.height<height:
self.addSelection(node)
else:
self.cutOffSelection(node.left, height)
self.cutOffSelection(node.right, height)
def setCutOffLine(self, x):
if self.parent.SelectionMode==1:
self.cutOffLinePos=x
self.cutOffHeight=self.treeHeight- \
self.treeHeight/self.treeAreaWidth*(x-leftMargin)
self.cutOffLine.move(x,0)
self.footer.moveMarker(x)
self.cutOffLine.show()
self.update()
self.clearSelection()
self.cutOffSelection(self.rootGraphics,self.cutOffHeight)
self.parent.updateSelection(self.selectionList)
else:
self.cutOffLine.hide()
self.cutOffLine.move(x,0)
self.update()
class ScaleCanvas(QCanvasView):
def __init__(self, parent, *args):
apply(QCanvasView.__init__, (self,)+args)
self.parent=parent
self.setFixedHeight(20)
self.setHScrollBarMode(QScrollView.AlwaysOff)
self.setVScrollBarMode(QScrollView.AlwaysOff)
self.canvas().obj=[]
self.canvas().marker=QCanvasRectangle(None)
self.markerDragged=False
self.markerPos=0
def clear(self):
for a in self.canvas().obj:
a.setCanvas(None)
self.canvas().marker.setCanvas(None)
self.canvas().obj=[]
self.canvas().update()
def drawScale(self, treeAreaW, height):
self.clear()
self.canvas().treeAreaW=treeAreaW
self.canvas().treeHeight=height
xPos=leftMargin+treeAreaW
dist=0
distInc=math.floor(height/5)/2
if distInc==0:
distInc=0.25
while xPos>=leftMargin:
text=QCanvasText(str(dist),self.canvas())
text.move(xPos,9)
text.setZ(0)
text.setTextFlags(Qt.AlignCenter)
text.show()
line1=QCanvasLine(self.canvas())
line2=QCanvasLine(self.canvas())
line1.setPoints(xPos,0,xPos,2)
line1.setZ(0)
line1.show()
line2.setPoints(xPos,16,xPos,20)
line2.setZ(0)
line2.show()
self.canvas().obj.append(text)
self.canvas().obj.append(line1)
self.canvas().obj.append(line2)
xPos-=(distInc/height)*treeAreaW
dist+=distInc
self.marker=QCanvasRectangle(self.markerPos-3,0,1,20,self.canvas())
self.marker.setBrush(QBrush(QColor("blue"),2))
self.marker.setZ(1)
self.marker.show()
self.canvas().obj.append(self.marker)
self.canvas().marker=self.marker
self.canvas().update()
def contentsMousePressEvent(self, e):
if e.pos().x()<0 and e.pos().x()>leftMargin+self.canvas().treeAreaW:
return
self.commitStatus=self.parent.CommitOnChange
self.parent.CommitOnChange=False
self.marker=self.canvas().marker
self.markerPos=e.pos().x()+3
self.marker.move(self.markerPos-3,0)
self.markerDragged=True
self.canvas().update()
self.parent.dendogram.setCutOffLine(e.pos().x())
def contentsMouseReleaseEvent(self, e):
self.markerDragged=False
self.parent.CommitOnChange=self.commitStatus
self.parent.dendogram.setCutOffLine(e.pos().x())
def contentsMouseMoveEvent(self, e):
if e.pos().x()<0 or e.pos().x()>leftMargin+self.canvas().treeAreaW:
return
if self.markerDragged and e.pos():
self.markerPos=e.pos().x()+3
self.marker.move(self.markerPos-3,0)
self.canvas().update()
self.parent.dendogram.setCutOffLine(e.pos().x())
def moveMarker(self, x):
self.canvas().marker.move(x,0)
self.canvas().update()
class MyCanvasRect(QCanvasRectangle):
left=None
right=None
cluster=None
def highlight(self, pen):
if self.pen()==pen:
return
if self.left:
self.left.highlight(pen)
if self.right:
self.right.highlight(pen)
self.setPen(pen)
def drawShape(self, painter):
painter.drawLine(self.x(),self.y(),self.x(),self.y()+self.height())
if self.left:
painter.drawLine(self.x(),self.y(),self.left.x(),self.y())
else:
painter.drawLine(self.x(),self.y(),self.x()+self.width(),self.y())
if self.right:
painter.drawLine(self.x(),self.y()+self.height(),self.right.x(),
self.y()+self.height())
else:
painter.drawLine(self.x(),self.y()+self.height(),self.x()+self.width(),
self.y()+self.height())
class MyCanvasText(QCanvasText):
cluster=None
class SelectionPoly(QCanvasPolygon):
rootCluster=None
rootGraphics=None
def __init__(self, *args):
apply(QCanvasPolygon.__init__, (self,)+args)
self.setZ(20)
def clearGraphics(self):
#self.rootGraphics.setBrush(self.canvas().brush)
self.setCanvas(None)
class BubbleRect(QCanvasRectangle):
def __init__(self,*args):
apply(QCanvasRectangle.__init__, (self,)+args)
self.setBrush(QBrush(Qt.white))
self.text=QCanvasText(self.canvas())
self.setZ(30)
self.text.setZ(31)
def setText(self, text):
self.text.setText(text)
self.setSize(self.text.boundingRect().width()+6,self.text.boundingRect().height()+6)
def show(self):
QCanvasRectangle.show(self)
self.text.show()
def hide(self):
QCanvasRectangle.hide(self)
self.text.hide()
def move(self, x, y):
if self.canvas().onCanvas(x+self.width(),y):
QCanvasRectangle.move(self, x+5, y+5)
self.text.move(x+6,y+6)
else:
QCanvasRectangle.move(self, x-self.width()-5, y+5)
self.text.move(x-self.width()-3,y+6)
#if not self.canvas().onCanvas(1,y+self.height()):
# self.move(x,y-self.height())
#if not self.canvas().onCanvas(self.x(),self.y()) and \
# self.canvas().onCanvas(self.x(),self.y()+self.height()):
# while not self.canvas().onCanvas(self.x(),self.y()) and self.y()<self.canvas().height():
# QCanvasRectangle.move(self,self.x(), self.y()+10)
# self.move(self.x(),self.y())
def setCanvas(self, canvas):
QCanvasRectangle.setCanvas(self,canvas)
self.text.setCanvas(canvas)
if __name__=="__main__":
app=QApplication(sys.argv)
w=OWHierarchicalClustering()
app.setMainWidget(w)
w.show()
data=orange.ExampleTable("../../doc/datasets/iris.tab")
id=orange.newmetaid()
data.domain.addmeta(id, orange.FloatVariable("a"))
data.addMetaAttribute(id)
matrix = orange.SymMatrix(len(data))
dist = orange.ExamplesDistanceConstructor_Euclidean(data)
matrix = orange.SymMatrix(len(data))
matrix.setattr('items', data)
for i in range(len(data)):
for j in range(i+1):
matrix[i, j] = dist(data[i], data[j])
w.dataset(matrix)
app.exec_loop()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -