-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucketFiller.py
157 lines (139 loc) · 6.52 KB
/
bucketFiller.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from PythonQt import QtGui, Qt
import KnossosModule
import numpy, os, re, string, sys, time, traceback
from scipy import ndimage
from knossos_utils import knossosdataset, KnossosDataset
knossosdataset._set_noprint(True)
#KNOSSOS_PLUGIN Version 1
#KNOSSOS_PLUGIN Description Iteratively bucket fill a segmentation object based on a pre-calculated membrane prediction
class main_class(QtGui.QWidget):
INSTRUCTION_TEXT_STR = """Fill configuration:
- Pick membrane prediction dataset by browsing to directory of knossos.conf
- Base subobject ID for subobjects to be created
- Size of work area, as an x,y,z blank-separated tuple
Work area should not exceed supercube size
Operation:
- For each cell, enter a subObject Id. Then iteratively:
-- Move to a place inside the cell (stay in mag1), and wait for the loader to finish
-- Click Fill to bucket fill
"""
def initGUI(self):
self.twiHeadersList = []
self.twiHash = {}
self.setWindowTitle("Bucket Filler")
layout = QtGui.QVBoxLayout()
self.setLayout(layout)
instructionsButton = QtGui.QPushButton("See Instructions")
instructionsButton.clicked.connect(self.instructionsButtonClicked)
layout.addWidget(instructionsButton)
configGroupBox = QtGui.QGroupBox("Configuration")
layout.addWidget(configGroupBox)
configLayout = QtGui.QVBoxLayout()
configGroupBox.setLayout(configLayout)
layout.addLayout(configLayout)
memPredLayout = QtGui.QHBoxLayout()
configLayout.addLayout(memPredLayout)
memPredLayout.addWidget(QtGui.QLabel("Membrane Prediction"))
self.dirEdit = QtGui.QLineEdit()
memPredLayout.addWidget(self.dirEdit)
self.dirBrowseButton = QtGui.QPushButton("Browse...")
self.dirBrowseButton.clicked.connect(self.dirBrowseButtonClicked)
memPredLayout.addWidget(self.dirBrowseButton)
subObjSizeLayout = QtGui.QHBoxLayout()
configLayout.addLayout(subObjSizeLayout)
subObjSizeLayout.addWidget(QtGui.QLabel("SubObj ID"))
self.subObjIdEdit = QtGui.QLineEdit()
subObjSizeLayout.addWidget(self.subObjIdEdit)
subObjSizeLayout.addWidget(QtGui.QLabel("Size"))
self.workAreaSizeEdit = QtGui.QLineEdit()
subObjSizeLayout.addWidget(self.workAreaSizeEdit)
itersThresholdLayout = QtGui.QHBoxLayout()
configLayout.addLayout(itersThresholdLayout)
itersThresholdLayout.addWidget(QtGui.QLabel("Iterations"))
self.itersEdit = QtGui.QLineEdit()
itersThresholdLayout.addWidget(self.itersEdit)
itersThresholdLayout.addWidget(QtGui.QLabel("Threshold"))
self.thresholdEdit = QtGui.QLineEdit()
itersThresholdLayout.addWidget(self.thresholdEdit)
fillUndoLayout = QtGui.QHBoxLayout()
layout.addLayout(fillUndoLayout)
self.fillButton = QtGui.QPushButton("Fill")
self.fillButton.clicked.connect(self.fillButtonClicked)
fillUndoLayout.addWidget(self.fillButton)
self.UndoButton = QtGui.QPushButton("Undo")
self.UndoButton.clicked.connect(self.UndoButtonClicked)
fillUndoLayout.addWidget(self.UndoButton)
# Show
self.setWindowFlags(Qt.Qt.Window)
self.show()
return
def __init__(self, parent=KnossosModule.knossos_global_mainwindow):
super(main_class, self).__init__(parent)
exec(KnossosModule.scripting.getInstanceInContainerStr(__name__) + " = self")
self.initGUI()
self.initLogic()
return
def initLogic(self):
KnossosModule.signalRelay.Signal_EventModel_handleMouseReleaseMiddle.connect(self.handleMouseReleaseMiddle)
self.pos_arr = numpy.array(KnossosModule.knossos.getPosition())
return
def handleMouseReleaseMiddle(self, eocd, coord, event):
self.pos_arr = numpy.array(coord.vector())
KnossosModule.knossos.setPosition(coord.vector())
return
def instructionsButtonClicked(self):
QtGui.QMessageBox.information(0, "Instructions", self.INSTRUCTION_TEXT_STR)
return
def dirBrowseButtonClicked(self):
browse_dir = QtGui.QFileDialog.getExistingDirectory()
if "" <> browse_dir:
self.dirEdit.setText(browse_dir)
return
def str2tripint(self, s):
tripint = map(int, re.findall(r"[\w']+", s))
assert(len(tripint) == 3)
return tripint
def validateDir(self, s):
if s[-1] <> "/":
s += "/"
assert(os.path.isdir(s))
return s
def npDataPtr(self, matrix):
return matrix.__array_interface__["data"][0]
def writeMatrix(self, matrix):
return KnossosModule.knossos.processRegionByStridedBufProxy(self.begin_arr, self.size_arr, self.npDataPtr(matrix), matrix.strides, True, True)
def readMatrix(self, matrix):
return KnossosModule.knossos.processRegionByStridedBufProxy(self.begin_arr, self.size_arr, self.npDataPtr(matrix), matrix.strides, False, False)
def fillButtonClicked(self):
path = self.validateDir(str(self.dirEdit.text))
self.size_arr = numpy.array(self.str2tripint(str(self.workAreaSizeEdit.text)))
subObjId = int(str(self.subObjIdEdit.text))
iters = int(str(self.itersEdit.text))
threshold = int(str(self.thresholdEdit.text))
pos_off_arr = self.size_arr/2
self.begin_arr = self.pos_arr - pos_off_arr
memPredDataset = KnossosDataset.knossosDataset()
memPredDataset.initialize_from_knossos_path(path)
memPred = memPredDataset.from_cubes_to_matrix(self.size_arr, self.begin_arr, type='raw')
dil = ndimage.morphology.binary_dilation(memPred > threshold, iterations = iters)
del memPred
if dil[tuple(pos_off_arr)]:
QtGui.QMessageBox.information(0, "Error", "Dilation error")
return
all_labels, num = ndimage.measurements.label(numpy.invert(dil))
del dil
requested_label = all_labels[tuple(pos_off_arr)]
seg = ndimage.morphology.binary_dilation((all_labels == requested_label), iterations = iters + 1)
del all_labels
self.inputMatrix = numpy.ndarray(shape=self.size_arr,dtype="uint64")
self.inputMatrix.fill(0)
self.readMatrix(self.inputMatrix)
outputMatrix = numpy.ndarray(shape=self.size_arr,dtype="uint64")
outputMatrix[:,:,:] = self.inputMatrix[:,:,:]
outputMatrix[seg] = subObjId
self.writeMatrix(outputMatrix)
return
def UndoButtonClicked(self):
self.writeMatrix(self.inputMatrix)
return
pass