-
Notifications
You must be signed in to change notification settings - Fork 14
/
label_test.py
477 lines (392 loc) · 17.4 KB
/
label_test.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
"""Test for File classes"""
import pytest
import numpy as np
from deepcell_label import label
from deepcell_label import models
from deepcell_label.conftest import DummyLoader
# Automatically enable transactions for all tests, without importing any extra fixtures.
@pytest.fixture(autouse=True)
def enable_transactional_tests(app, db_session):
db_session.autoflush = False
# Tests can mock a actions different frames/features/channels
# by setting edit.project.frame/feature/channel within the test
class TestBaseEdit():
def test_del_cell_info_last_frame(self):
labels = np.ones((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell_ids = edit.labels.cell_ids
cell_info = edit.labels.cell_info
feature = 0
cell = 1
frame = 0
# Remove the label completely (only remaining frame)
edit.del_cell_info(cell, frame)
assert cell not in cell_ids[feature]
assert cell not in cell_info[feature]
assert edit.y_changed
assert edit.labels_changed
np.testing.assert_array_equal(cell_ids[feature], np.array([]))
assert edit.labels.cell_info[feature] == {}
def test_def_cell_info_one_frame(self):
labels = np.ones((2, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell_ids = edit.labels.cell_ids
cell_info = edit.labels.cell_info
feature = 0
cell = 1
frame = 1
# Remove from second frame only
edit.del_cell_info(cell, frame)
assert cell in cell_ids[feature]
assert cell in cell_info[feature]
assert frame not in cell_info[feature][frame]['frames']
assert edit.y_changed
assert edit.labels_changed
def test_action_new_single_cell(self, app):
labels = np.ones((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell = 1
feature = 0
expected_new_label = 2
with app.app_context():
edit.action_new_single_cell(cell)
assert expected_new_label in edit.frame[..., feature]
assert cell not in edit.frame[..., feature]
assert edit.y_changed
assert edit.labels_changed
def test_action_delete_mask(self, app):
labels = np.ones((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell = 1
feature = 0
with app.app_context():
edit.action_delete_mask(cell)
assert cell not in edit.frame[..., feature]
assert edit.labels_changed
assert edit.y_changed
def test_action_swap_single_frame(self, app):
# single 2 x 2 frame with one feature; cell 1 in top row, cell 2 in bottom row
labels = np.reshape([1, 1, 2, 2], (1, 2, 2, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell1 = 1
cell2 = 2
feature = 0
expected_swap = np.array([[2, 2], [1, 1]])
with app.app_context():
edit.action_swap_single_frame(cell1, cell2)
np.testing.assert_array_equal(edit.frame[..., feature], expected_swap)
assert edit.y_changed
assert edit.labels_changed
def test_action_swap_single_frame_with_label_not_in_frame(self, app):
"""Tests that swapping with a cell not in frame updates the cell info."""
labels = np.reshape([1], (1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
cell1 = 1
cell2 = 2
expected_labels = np.reshape([2], (1, 1, 1, 1))
with app.app_context():
edit.action_swap_single_frame(cell1, cell2)
np.testing.assert_array_equal(project.label_array, expected_labels)
assert edit.y_changed
assert edit.labels_changed
assert cell2 in edit.tracks
assert cell1 not in edit.tracks
def test_action_replace_single(self, app):
# single 2 x 2 frame with two labels: 1s in top row, 2s in bottom
labels = np.reshape([1, 2], (1, 2, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
expected_labels = np.reshape([1, 1], (1, 2, 1, 1))
cell1 = 1
cell2 = 2
with app.app_context():
edit.action_replace_single(cell1, cell2)
np.testing.assert_array_equal(project.label_array, expected_labels)
assert 2 not in edit.tracks
def test_action_flood_background(self, app):
"""Flooding background does NOT spread to diagonal areas."""
# 3 x 3 frame with label in diamond shape
labels = np.reshape([0, 1, 0, 1, 0, 1, 0, 1, 0], (1, 3, 3, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
flood_label, x_loc, y_loc = 2, 1, 1
feature = 0
expected_flood = np.reshape([0, 1, 0, 1, 2, 1, 0, 1, 0], (3, 3))
with app.app_context():
edit.action_flood(flood_label, x_loc, y_loc)
np.testing.assert_array_equal(edit.frame[..., feature], expected_flood)
assert edit.y_changed
assert edit.labels_changed
def test_action_flood_label(self, app):
"""Flooding a label does spread to diagonal areas."""
# 3 x 3 frame with label in diamond shape
labels = np.reshape([[0, 1, 0], [1, 0, 1], [0, 1, 0]], (1, 3, 3, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
flood_label, x_loc, y_loc = 2, 1, 0
feature = 0
expected_flood = np.reshape([[0, 2, 0], [2, 0, 2], [0, 2, 0]], (3, 3))
with app.app_context():
edit.action_flood(flood_label, x_loc, y_loc)
np.testing.assert_array_equal(edit.frame[..., feature], expected_flood)
assert edit.y_changed
assert edit.labels_changed
def test_action_handle_draw_remove_label(self, app):
"""Erasing a label with by drawing over it."""
labels = np.reshape([1], (1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
feature = 0
project.feature = 0
edit = label.TrackEdit(project)
trace, foreground, background, brush_size = [(0, 0)], 0, 1, 1
expected_draw = np.reshape([0], (1, 1))
with app.app_context():
edit.action_handle_draw(trace, foreground, background, brush_size)
np.testing.assert_array_equal(edit.frame[..., feature], expected_draw)
assert background not in edit.labels.cell_info[feature]
assert background not in edit.labels.cell_ids[feature]
def test_action_handle_draw_add_label(self, app):
"""Adding a label with by drawing it in."""
labels = np.reshape([0], (1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.TrackEdit(project)
trace, foreground, background, brush_size = [(0, 0)], 1, 0, 1
feature = 0
expected_draw = np.reshape([1], (1, 1))
with app.app_context():
edit.action_handle_draw(trace, foreground, background, brush_size)
np.testing.assert_array_equal(edit.frame[..., feature], expected_draw)
assert foreground in edit.labels.cell_info[feature]
assert foreground in edit.labels.cell_ids[feature]
class TestZStackEdit():
def test_zstack_add_cell_info(self):
labels = np.zeros((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell_ids = edit.labels.cell_ids
cell_info = edit.labels.cell_info
cell = 1
frame = 0
feature = 0
# Add new label to first frame
edit.add_cell_info(cell, frame)
assert cell in cell_ids[feature]
assert cell_info[feature][cell] == {'label': 1,
'frames': [frame],
'slices': ''}
assert edit.y_changed
assert edit.labels_changed
def test_add_cell_info_multiple_frames(self):
num_frames = 5
labels = np.zeros((num_frames, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell_ids = edit.labels.cell_ids
cell_info = edit.labels.cell_info
cell = 1
feature = 0
# Add new label to all frames
for frame in range(num_frames):
edit.add_cell_info(cell, frame)
assert cell in cell_ids[feature]
assert cell_info[feature][cell] == {'label': 1,
'frames': list(range(frame + 1)),
'slices': ''}
assert edit.y_changed
assert edit.labels_changed
def test_action_new_cell_stack(self, app):
labels = np.ones((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell = 1
feature = 0
expected_new_cell = 2
with app.app_context():
edit.action_new_cell_stack(cell)
assert cell not in edit.frame[..., feature]
assert expected_new_cell in edit.frame[..., feature]
def test_action_replace_single(self, app):
# single 2 x 2 frame with two labels: 1s in top row, 2s in bottom
labels = np.reshape([1, 1, 2, 2], (1, 2, 2, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
expected_labels = np.array([[[1], [1]],
[[1], [1]]])
cell1 = 1
cell2 = 2
with app.app_context():
edit.action_replace_single(cell1, cell2)
np.testing.assert_array_equal(edit.frame, expected_labels)
def test_action_replace(self, app):
# three 2 x 2 frame with two labels: 1s in top row, 2s in bottom
labels = np.reshape(3 * [1, 1, 2, 2], (3, 2, 2, 1))
project = models.Project.create(DummyLoader(labels=labels))
cell1 = 1
cell2 = 2
frame = 1 # Replace on the middle frame
project.frame = frame
edit = label.ZStackEdit(project)
expected_frame = np.array([[[1], [1]],
[[1], [1]]])
expected_labels = np.array(3 * [expected_frame])
with app.app_context():
edit.action_replace(cell1, cell2)
np.testing.assert_array_equal(edit.project.label_array, expected_labels)
def test_action_active_contour_other_labels_unchanged(self, app):
"""
Tests that other labels not affected by active contouring a label
"""
labels = np.ones((1, 10, 10, 1))
labels[:, 5:, :, :] = 2
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell = 1
other_cell = 2
with app.app_context():
edit.action_active_contour(cell)
np.testing.assert_array_equal(labels[project.frame] == other_cell,
edit.frame == other_cell)
# TODO: active contouring has no effect in this test; find out what correct behavior is
# def test_action_active_contour_label_too_small(self, app):
# """
# Tests that label smaller than the raw object is made larger by active contouring.
# """
# raw = np.zeros((1, 10, 10, 1))
# labels = np.zeros((1, 10, 10, 1))
# labels[0, 4:6, 4:6, 0] = 1
# raw[0, 2:8, 2:8, 0] = 1
# project = models.Project.create(DummyLoader(raw=raw, labels=labels))
# edit = label.ZStackEdit(project)
# cell = 1
# with app.app_context():
# edit.action_active_contour(cell)
# assert int((edit.frame == 1).sum()) > (labels[project.frame] == 1).sum()
def test_action_active_contour_label_too_large(self, app):
"""Tests that a label that is larger than the raw objects is made
smaller by active contouring."""
raw = np.zeros((1, 10, 10, 1))
labels = np.ones((1, 10, 10, 1))
raw[0, 3:6, 3:6, 0] = 1
project = models.Project.create(DummyLoader(raw=raw, labels=labels))
edit = label.ZStackEdit(project)
cell = 1
with app.app_context():
edit.action_active_contour(cell)
assert int((edit.frame == 1).sum()) < (labels[project.frame] == 1).sum()
def test_action_erode_delete_label(self, app):
"""Tests that a label is correctly removed when eroding deletes all of its pixels."""
labels = np.zeros((1, 3, 3, 1))
labels[0, 1, 1, 0] = 1
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell = 1
with app.app_context():
edit.action_erode(cell)
assert cell not in edit.frame
assert cell not in project.labels.cell_ids[project.feature]
assert cell not in project.labels.cell_info[project.feature]
def test_action_erode_other_labels_unchanged(self, app):
"""Tests that other labels not affected by eroding a label."""
labels = np.reshape([1, 1, 2, 2], (1, 2, 2, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell = 1
other_cell = 2
with app.app_context():
edit.action_erode(cell)
np.testing.assert_array_equal(labels[project.frame] == other_cell,
edit.frame == other_cell)
def test_action_dilate_other_labels_unchanged(self, app):
"""Tests that other labels not affected by dilating a label."""
labels = np.reshape([1, 1, 2, 2], (1, 2, 2, 1))
project = models.Project.create(DummyLoader(labels=labels))
edit = label.ZStackEdit(project)
cell = 1
other_cell = 2
with app.app_context():
edit.action_dilate(cell)
np.testing.assert_array_equal(labels[project.frame] == other_cell,
edit.frame == other_cell)
class TestTrackEdit():
def test_track_add_cell_info(self):
labels = np.zeros((1, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels, path='test.trk'))
edit = label.TrackEdit(project)
tracks = edit.tracks
cell = 1
frame = 0
# Add new label to first frame
edit.add_cell_info(cell, frame)
assert tracks[cell] == {
'label': 1,
'frames': [frame],
'daughters': [],
'frame_div': None,
'parent': None,
'capped': False,
}
assert edit.y_changed
assert edit.labels_changed
def test_add_cell_info_multiple_frames(self):
num_frames = 5
labels = np.zeros((num_frames, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels, path='test.trk'))
edit = label.TrackEdit(project)
tracks = edit.tracks
cell = 1
# Add new label to all frames
for frame in range(num_frames):
edit.add_cell_info(cell, frame)
assert tracks[cell] == {
'label': 1,
'frames': list(range(frame + 1)),
'daughters': [],
'frame_div': None,
'parent': None,
'capped': False,
}
assert edit.y_changed
assert edit.labels_changed
def test_action_new_track_first_frame_of_track(self, app):
"""A new track on the first frame a label appears does nothing."""
# two 1x1 frames with one feature; cell starts on second frame
labels = np.reshape([0, 1], (2, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels, path='test.trk'))
cell = 1
frame = 1
feature = 0
project.frame = frame
project.feature = feature
edit = label.TrackEdit(project)
tracks = edit.tracks
prev_track = tracks[cell].copy()
with app.app_context():
edit.action_new_track(cell)
assert cell in edit.frame[..., feature]
assert prev_track == tracks[cell]
def test_action_new_track(self, app):
"""Create a new track on the second frame of a label."""
# two 1x1 frames with one feature; cell appears in both frames
labels = np.reshape([1, 1], (2, 1, 1, 1))
project = models.Project.create(DummyLoader(labels=labels, path='test.trk'))
cell = 1
frame = 1
feature = 0
expected_new_cell = 2
project.frame = frame
edit = label.TrackEdit(project)
tracks = edit.tracks
prev_track = tracks[cell].copy()
with app.app_context():
edit.action_new_track(cell)
assert cell not in edit.frame[..., feature]
assert expected_new_cell in edit.frame[..., feature]
assert expected_new_cell in edit.labels.cell_ids[feature]
assert prev_track['frames'] == (tracks[cell]['frames']
+ tracks[expected_new_cell]['frames'])