forked from google/neuroglancer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflood_filling_simulation.py
executable file
·250 lines (212 loc) · 8.1 KB
/
flood_filling_simulation.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
"""Example of display interactive flood-filling "inference" results.
shift+mousedown0 triggers the simulated flood filling to start with an initial
seed at the mouse position. The computed mask values are displayed as an image,
while the seed points chosen are displayed as point annotations.
keyt causes the simulated flood filling to stop.
In this example, the mask values are actually just computed as a distance
transform of the ground truth segmentation, and the seed points are restricted
to the ground truth segment and assign random priorities. In actual use, this
same visualization approach can be used to display the actual mask and seed
points computed by a flood filling TensorFlow model.
The cloudvolume library (https://github.com/seung-lab/cloud-volume) is used to
retrieve patches of the ground truth volume.
The zarr library is used to represent the sparse in-memory array containing the
computed inference results that are displayed in neuroglancer.
"""
import argparse
import random
import threading
import time
import cloudvolume
import neuroglancer
import neuroglancer.cli
import numpy as np
import scipy.ndimage
import zarr
class InteractiveInference:
def __init__(self):
viewer = self.viewer = neuroglancer.Viewer()
self.gt_vol = cloudvolume.CloudVolume(
"https://storage.googleapis.com/neuroglancer-public-data/flyem_fib-25/ground_truth",
mip=0,
bounded=True,
progress=False,
provenance={},
)
viewer.actions.add("start-fill", self._start_fill_action)
viewer.actions.add("stop-fill", self._stop_fill_action)
self.dimensions = neuroglancer.CoordinateSpace(
names=["x", "y", "z"],
units="nm",
scales=[8, 8, 8],
)
with viewer.config_state.txn() as s:
s.input_event_bindings.data_view["shift+mousedown0"] = "start-fill"
s.input_event_bindings.data_view["keyt"] = "stop-fill"
with viewer.txn() as s:
s.layers["image"] = neuroglancer.ImageLayer(
source="precomputed://gs://neuroglancer-public-data/flyem_fib-25/image",
)
s.layers["ground_truth"] = neuroglancer.SegmentationLayer(
source="precomputed://gs://neuroglancer-public-data/flyem_fib-25/ground_truth",
)
s.layers["ground_truth"].visible = False
self.flood_fill_event = None
def _do_flood_fill(self, initial_pos, inf_results, inf_volume, event):
initial_pos = (int(initial_pos[0]), int(initial_pos[1]), int(initial_pos[2]))
gt_vol_zarr = zarr.zeros(
self.gt_vol.bounds.to_list()[3:], chunks=(64, 64, 64), dtype=np.uint64
)
gt_blocks_seen = set()
block_size = np.array((64, 64, 64), np.int64)
def fetch_gt_block(block):
spos = block * block_size
epos = spos + block_size
slice_expr = np.s_[
int(spos[0]) : int(epos[0]),
int(spos[1]) : int(epos[1]),
int(spos[2]) : int(epos[2]),
]
gt_data = self.gt_vol[slice_expr][..., 0]
gt_vol_zarr[slice_expr] = gt_data
def get_patch(spos, epos):
spos = np.array(spos)
epos = np.array(epos)
sblock = spos // block_size
eblock = (epos - 1) // block_size
for blockoff in np.ndindex(tuple(eblock - sblock + 1)):
block = np.array(blockoff) + sblock
block_tuple = tuple(block)
if block_tuple in gt_blocks_seen:
continue
gt_blocks_seen.add(block_tuple)
fetch_gt_block(block)
slice_expr = np.s_[
int(spos[0]) : int(epos[0]),
int(spos[1]) : int(epos[1]),
int(spos[2]) : int(epos[2]),
]
result = gt_vol_zarr[slice_expr]
return result
segment_id = self.gt_vol[initial_pos][0]
patch_size = np.array((33,) * 3, np.int64)
lower_bound = patch_size // 2
upper_bound = (
np.array(self.gt_vol.bounds.to_list()[3:]) - patch_size + patch_size // 2
)
d = 8
seen = set()
q = []
last_invalidate = [time.time()]
invalidate_interval = 3
def enqueue(pos):
if np.any(pos < lower_bound) or np.any(pos >= upper_bound):
return
if pos in seen:
return
seen.add(pos)
q.append(pos)
def update_view():
if event.is_set():
return
cur_time = time.time()
if cur_time < last_invalidate[0] + invalidate_interval:
return
last_invalidate[0] = cur_time
inf_volume.invalidate()
with self.viewer.txn() as s:
s.layers["points"].annotations = [
neuroglancer.PointAnnotation(id=repr(pos), point=pos)
for pos in list(seen)
]
def process_pos(pos):
spos = pos - patch_size // 2
epos = spos + patch_size
slice_expr = np.s_[
int(spos[0]) : int(epos[0]),
int(spos[1]) : int(epos[1]),
int(spos[2]) : int(epos[2]),
]
gt_data = get_patch(spos, epos)
mask = gt_data == segment_id
for offset in (
(0, 0, d),
(0, 0, -d),
(0, d, 0),
(0, -d, 0),
(d, 0, 0),
(-d, 0, 0),
):
if not mask[tuple(patch_size // 2 + offset)[::-1]]:
continue
new_pos = np.array(pos) + np.array(offset)
enqueue(tuple(new_pos))
dist_transform = scipy.ndimage.morphology.distance_transform_edt(~mask)
inf_results[slice_expr] = 1 + np.asarray(
np.minimum(dist_transform, 5) / 5.0 * 254, dtype=np.uint8
)
self.viewer.defer_callback(update_view)
enqueue(initial_pos)
while len(q) > 0 and not event.is_set():
i = random.randint(0, len(q) - 1)
pos = q[i]
q[i] = q[-1]
del q[-1]
process_pos(pos)
self.viewer.defer_callback(update_view)
def _stop_flood_fill(self):
if self.flood_fill_event is not None:
self.flood_fill_event.set()
self.flood_fill_event = None
def _start_flood_fill(self, pos):
self._stop_flood_fill()
inf_results = zarr.zeros(
self.gt_vol.bounds.to_list()[3:], chunks=(64, 64, 64), dtype=np.uint8
)
inf_volume = neuroglancer.LocalVolume(
data=inf_results, dimensions=self.dimensions
)
with self.viewer.txn() as s:
s.layers["points"] = neuroglancer.LocalAnnotationLayer(self.dimensions)
s.layers["inference"] = neuroglancer.ImageLayer(
source=inf_volume,
shader="""
void main() {
float v = toNormalized(getDataValue(0));
vec4 rgba = vec4(0,0,0,0);
if (v != 0.0) {
rgba = vec4(colormapJet(v), 1.0);
}
emitRGBA(rgba);
}
""",
)
self.flood_fill_event = threading.Event()
t = threading.Thread(
target=self._do_flood_fill,
kwargs=dict(
initial_pos=pos,
inf_results=inf_results,
inf_volume=inf_volume,
event=self.flood_fill_event,
),
)
t.daemon = True
t.start()
def _start_fill_action(self, action_state):
pos = action_state.mouse_voxel_coordinates
if pos is None:
return
self._start_flood_fill(pos)
def _stop_fill_action(self, action_state):
self._stop_flood_fill()
if __name__ == "__main__":
ap = argparse.ArgumentParser()
neuroglancer.cli.add_server_arguments(ap)
args = ap.parse_args()
neuroglancer.cli.handle_server_arguments(args)
inf = InteractiveInference()
print(inf.viewer)
while True:
time.sleep(1000)