-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathMeasure.py
139 lines (124 loc) · 5.13 KB
/
pathMeasure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from PythonQt import QtGui, Qt
import KnossosModule
from math import sqrt
import networkx as nx
import time
#KNOSSOS_PLUGIN Version 1
#KNOSSOS_PLUGIN Description Find path between two selected nodes, and calculate the physical length (in nanometers)
class main_class(QtGui.QWidget):
def initGUI(self):
self.twiHeadersList = []
self.twiHash = {}
# General
self.setWindowTitle("Path Measure")
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
nameLayout = QtGui.QHBoxLayout()
layout.addLayout(nameLayout)
nameLayout.addWidget(QtGui.QLabel("Enter Path Name: "))
self.nameEdit = QtGui.QLineEdit()
nameLayout.addWidget(self.nameEdit)
buttonLayout = QtGui.QHBoxLayout()
layout.addLayout(buttonLayout)
pathFindButton = QtGui.QPushButton("Find Path")
pathFindButton.clicked.connect(self.findPathButtonClicked)
buttonLayout.addWidget(pathFindButton)
selectPathButton = QtGui.QPushButton("Select Nodes")
selectPathButton.clicked.connect(self.selectPathButtonClicked)
buttonLayout.addWidget(selectPathButton)
self.clearButton = QtGui.QPushButton("Clear Table")
buttonLayout.addWidget(self.clearButton)
self.clearButton.clicked.connect(self.clearClicked)
self.logTable = QtGui.QTableWidget()
layout.addWidget(self.logTable)
self.setTableHeaders(self.logTable, ["Date/Time", "Name", "Node #1", "Node #2", "Length", "Hops", "Path"])
self.finalizeTable(self.logTable)
# Show
self.setWindowFlags(Qt.Qt.Window)
self.show()
return
def finalizeTable(self, table):
table.horizontalHeader().setStretchLastSection(True)
self.resizeTable(table)
table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
table.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
return
def resizeTable(self, table):
table.resizeColumnsToContents()
table.resizeRowsToContents()
return
def clearTable(self, table):
table.clearContents()
del self.twiHash.setdefault(table,[])[:]
table.setRowCount(0)
return
def setTableHeaders(self, table, columnNames):
columnNum = len(columnNames)
table.setColumnCount(columnNum)
for i in xrange(columnNum):
twi = QtGui.QTableWidgetItem(columnNames[i])
table.setHorizontalHeaderItem(i, twi)
self.twiHeadersList.append(twi)
return
def addTableRow(self, table, columnTexts):
rowIndex = 0
table.insertRow(rowIndex)
for i in xrange(len(columnTexts)):
twi = QtGui.QTableWidgetItem(columnTexts[i])
twi.setFlags(twi.flags() & (~Qt.Qt.ItemIsEditable))
self.twiHash.setdefault(table,[]).append(twi)
table.setItem(rowIndex, i, twi)
self.resizeTable(table)
return
def __init__(self, parent=KnossosModule.knossos_global_mainwindow):
super(main_class, self).__init__(parent)
exec(KnossosModule.scripting.getInstanceInContainerStr(__name__) + " = self")
self.initGUI()
self.paths = []
return
def distance_scaled(self, source, target):
c1 = source.coordinate()
c2 = target.coordinate()
x, y, z = c1.x() - c2.x(), c1.y() - c2.y(), c1.z() - c2.z()
sx, sy, sz = KnossosModule.knossos.getScale()
return sqrt(sum([(dim*dim) for dim in [x*sx, y*sy, z*sz]]))
def getGraph(self):
nxG = nx.Graph()
for tree in KnossosModule.skeleton.trees():
for node in tree.nodes():
nxG.add_node(node)
for segment in node.segments():
s, t = segment.source(), segment.target()
nxG.add_edge(s, t, weight=self.distance_scaled(s,t))
return nxG
def clearClicked(self):
self.clearTable(self.logTable)
self.paths = []
return
def findPathButtonClicked(self):
nxG = self.getGraph()
sn = KnossosModule.skeleton.selectedNodes()
if (len(sn) <> 2):
QtGui.QMessageBox.information(0, "Error", "Select exactly two nodes")
return
n1, n2 = sn
all_distance, all_path = nx.single_source_dijkstra(nxG, n1)
if not (n2 in all_distance):
QtGui.QMessageBox.information(0, "Error", "Nodes are not connected")
return
n1_str, n2_str = str(n1.node_id()), str(n2.node_id())
distance_str = str(all_distance[n2])
n2_path = all_path[n2]
path_str = ",".join([str(n.node_id()) for n in n2_path])
hops_str = str(len(n2_path)-1)
datetime_str = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime())
name_str = self.nameEdit.text
self.addTableRow(self.logTable, [datetime_str, name_str, n1_str, n2_str, distance_str, hops_str, path_str])
self.paths.insert(0,n2_path)
return
def selectPathButtonClicked(self):
si = self.logTable.selectedIndexes()
if len(si) > 0:
KnossosModule.skeleton.selectNodes(self.paths[si[0].row()])
return
pass