-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_wsi_registration.py
648 lines (554 loc) · 19.7 KB
/
test_wsi_registration.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
"""Test WSI Registration."""
from pathlib import Path
import cv2
import numpy as np
import pytest
import torch
from tests.conftest import timed
from tiatoolbox import logger, rcParam
from tiatoolbox.tools.registration.wsi_registration import (
AffineWSITransformer,
DFBRegister,
apply_affine_transformation,
apply_bspline_transform,
estimate_bspline_transform,
match_histograms,
prealignment,
)
from tiatoolbox.utils import imread
from tiatoolbox.utils.metrics import dice
from tiatoolbox.wsicore.wsireader import WSIReader
RNG = np.random.default_rng() # Numpy Random Generator
def test_extract_features(dfbr_features: Path) -> None:
"""Test for CNN based feature extraction function."""
# dfbr (deep feature based registration).
dfbr = DFBRegister()
fixed_img = np.repeat(
np.expand_dims(
np.repeat(
np.expand_dims(np.arange(0, 64, 1, dtype=np.uint8), axis=1),
64,
axis=1,
),
axis=2,
),
3,
axis=2,
)
output = dfbr.extract_features(fixed_img, fixed_img)
pool3_feat = output["block3_pool"][0, :].detach().numpy()
pool4_feat = output["block4_pool"][0, :].detach().numpy()
pool5_feat = output["block5_pool"][0, :].detach().numpy()
_pool3_feat, _pool4_feat, _pool5_feat = np.load(
str(dfbr_features),
allow_pickle=True,
)
assert np.mean(np.abs(pool3_feat - _pool3_feat)) < 1.0e-4
assert np.mean(np.abs(pool4_feat - _pool4_feat)) < 1.0e-4
assert np.mean(np.abs(pool5_feat - _pool5_feat)) < 1.0e-4
def test_feature_mapping(fixed_image: Path, moving_image: Path) -> None:
"""Test for CNN based feature matching function."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
pre_transform = np.array([[-1, 0, 337.8], [0, -1, 767.7], [0, 0, 1]])
moving_img = cv2.warpAffine(
moving_img,
pre_transform[0:-1][:],
fixed_img.shape[:2][::-1],
)
dfbr = DFBRegister()
features = dfbr.extract_features(fixed_img, moving_img)
fixed_matched_points, moving_matched_points, _ = dfbr.feature_mapping(features)
output = dfbr.estimate_affine_transform(fixed_matched_points, moving_matched_points)
expected = np.array(
[[0.98843, 0.00184, 1.75437], [-0.00472, 0.96973, 5.38854], [0, 0, 1]],
)
assert np.mean(output - expected) < 1.0e-6
def test_dfbr_features() -> None:
"""Test for feature input to feature_mapping function."""
dfbr = DFBRegister()
fixed_img = np.repeat(
np.expand_dims(
np.repeat(
np.expand_dims(np.arange(0, 64, 1, dtype=np.uint8), axis=1),
64,
axis=1,
),
axis=2,
),
3,
axis=2,
)
features = dfbr.extract_features(fixed_img, fixed_img)
del features["block5_pool"]
with pytest.raises(
ValueError,
match=r".*The feature mapping step expects 3 blocks of features.*",
):
_, _, _ = dfbr.feature_mapping(features)
def test_prealignment_mask() -> None:
"""Test for mask inputs to prealignment function."""
fixed_img = RNG.random((10, 10))
moving_img = RNG.random((10, 10))
no_fixed_mask = np.zeros(shape=fixed_img.shape, dtype=int)
no_moving_mask = np.zeros(shape=moving_img.shape, dtype=int)
with pytest.raises(ValueError, match=r".*The foreground is missing in the mask.*"):
_ = prealignment(fixed_img, moving_img, no_fixed_mask, no_moving_mask)
def test_prealignment_input_shape() -> None:
"""Test for inputs to prealignment function."""
fixed_img = RNG.random((10, 10))
moving_img = RNG.random((15, 10))
fixed_mask = RNG.choice([0, 1], size=(15, 10))
moving_mask = RNG.choice([0, 1], size=(10, 10))
with pytest.raises(
ValueError,
match=r".*Mismatch of shape between image and its corresponding mask.*",
):
_ = prealignment(fixed_img, moving_img, fixed_mask, moving_mask)
def test_prealignment_rotation_step() -> None:
"""Test for rotation step input to prealignment function."""
fixed_img = RNG.random((10, 10))
moving_img = RNG.random((10, 10))
fixed_mask = RNG.choice([0, 1], size=(10, 10))
moving_mask = RNG.choice([0, 1], size=(10, 10))
with pytest.raises(
ValueError,
match=r".*Please select the rotation step in between 10 and 20.*",
):
_ = prealignment(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
rotation_step=9,
)
with pytest.raises(
ValueError,
match=r".*Please select the rotation step in between 10 and 20.*",
):
_ = prealignment(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
rotation_step=21,
)
def test_prealignment_output(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
) -> None:
"""Test for prealignment of an image pair."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
fixed_mask = imread(fixed_mask)
moving_mask = imread(moving_mask)
expected = np.array([[-1, 0, 337.8], [0, -1, 767.7], [0, 0, 1]])
output, _, _, _ = prealignment(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
dice_overlap=0.5,
rotation_step=10,
)
assert np.linalg.norm(expected[:2, :2] - output[:2, :2]) < 0.1
assert np.linalg.norm(expected[:2, 2] - output[:2, 2]) < 10
fixed_img, moving_img = fixed_img[:, :, 0], moving_img[:, :, 0]
output, _, _, _ = prealignment(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
dice_overlap=0.5,
rotation_step=10,
)
assert np.linalg.norm(expected - output) < 0.2
def test_dice_overlap_range() -> None:
"""Test if the value of dice_overlap is within the range."""
fixed_img = RNG.integers(20, size=(256, 256))
moving_img = RNG.integers(20, size=(256, 256))
fixed_mask = RNG.integers(2, size=(256, 256))
moving_mask = RNG.integers(2, size=(256, 256))
with pytest.raises(
ValueError,
match=r".*The dice_overlap should be in between 0 and 1.0.*",
):
_ = prealignment(fixed_img, moving_img, fixed_mask, moving_mask, dice_overlap=2)
with pytest.raises(
ValueError,
match=r".*The dice_overlap should be in between 0 and 1.0.*",
):
_ = prealignment(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
dice_overlap=-1,
)
def test_warning(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test for displaying warning in prealignment function."""
fixed_img = imread(Path(fixed_image))
moving_img = imread(Path(moving_image))
fixed_mask = imread(Path(fixed_mask))
moving_mask = imread(Path(moving_mask))
fixed_img, moving_img = fixed_img[:, :, 0], moving_img[:, :, 0]
_ = prealignment(fixed_img, moving_img, fixed_mask, moving_mask, dice_overlap=0.9)
assert "Not able to find the best transformation" in caplog.text
def test_match_histogram_inputs() -> None:
"""Test for inputs to match_histogram function."""
image_a = RNG.integers(256, size=(256, 256, 3))
image_b = RNG.integers(256, size=(256, 256, 3))
with pytest.raises(
ValueError,
match=r".*The input images should be grayscale images.*",
):
_, _ = match_histograms(image_a, image_b)
def test_match_histograms() -> None:
"""Test for preprocessing/normalization of an image pair."""
image_a = RNG.integers(256, size=(256, 256))
image_b = np.zeros(shape=(256, 256), dtype=int)
out_a, out_b = match_histograms(image_a, image_b, 3)
assert np.all(out_a == image_a)
assert np.all(out_b == 255)
out_a, out_b = match_histograms(image_b, image_a, 3)
assert np.all(out_a == 255)
assert np.all(out_b == image_a)
image_a = RNG.integers(256, size=(256, 256, 1))
image_b = RNG.integers(256, size=(256, 256, 1))
_, _ = match_histograms(image_a, image_b)
image_a = np.array(
[
[129, 134, 195, 241, 168],
[231, 91, 145, 91, 0],
[64, 87, 194, 112, 99],
[138, 111, 99, 124, 86],
[164, 127, 167, 222, 100],
],
dtype=np.uint8,
)
image_b = np.array(
[
[25, 91, 177, 212, 114],
[62, 86, 83, 31, 17],
[13, 16, 191, 19, 149],
[58, 127, 22, 111, 255],
[164, 7, 110, 76, 222],
],
dtype=np.uint8,
)
expected_output = np.array(
[
[91, 110, 191, 255, 164],
[222, 22, 114, 22, 7],
[13, 17, 177, 76, 31],
[111, 62, 31, 83, 16],
[127, 86, 149, 212, 58],
],
)
norm_image_a, norm_image_b = match_histograms(image_a, image_b)
assert np.all(norm_image_a == expected_output)
assert np.all(norm_image_b == image_b)
def test_filtering_duplicate_matching_points() -> None:
"""Test filtering_matching_points function with duplicate matching points."""
fixed_mask = np.zeros((50, 50))
fixed_mask[20:40, 20:40] = 255
moving_mask = np.zeros((50, 50))
moving_mask[20:40, 20:40] = 255
fixed_points = np.array(
[[25, 25], [25, 25], [25, 25], [30, 25], [25, 30], [30, 35], [21, 37]],
)
moving_points = np.array(
[[30, 25], [32, 36], [31, 20], [30, 35], [30, 35], [30, 35], [26, 27]],
)
quality = np.ones((7, 1))
dfbr = DFBRegister()
_ = dfbr.filtering_matching_points(
fixed_mask,
moving_mask,
fixed_points,
moving_points,
quality,
)
def test_filtering_no_duplicate_matching_points() -> None:
"""Test filtering_matching_points function with no duplicate matching points."""
fixed_mask = np.zeros((50, 50))
fixed_mask[20:40, 20:40] = 255
moving_mask = np.zeros((50, 50))
moving_mask[20:40, 20:40] = 255
fixed_points = np.array(
[[25, 25], [25, 28], [15, 25], [30, 25], [25, 30], [30, 35], [21, 37]],
)
moving_points = np.array(
[[30, 25], [32, 36], [31, 20], [20, 35], [30, 15], [34, 35], [26, 27]],
)
quality = np.ones((7, 1))
dfbr = DFBRegister()
_ = dfbr.filtering_matching_points(
fixed_mask,
moving_mask,
fixed_points,
moving_points,
quality,
)
def test_register_input() -> None:
"""Test for inputs to register function."""
fixed_img = RNG.random((32, 32))
moving_img = RNG.random((32, 32))
fixed_mask = RNG.choice([0, 1], size=(32, 32))
moving_mask = RNG.choice([0, 1], size=(32, 32))
dfbr = DFBRegister()
with pytest.raises(
ValueError,
match=r".*The required shape for fixed and moving images is n x m x 3.*",
):
_ = dfbr.register(fixed_img, moving_img, fixed_mask, moving_mask)
def test_register_input_channels() -> None:
"""Test for checking inputs' number of channels for register function."""
fixed_img = RNG.random((32, 32, 1))
moving_img = RNG.random((32, 32, 1))
fixed_mask = RNG.choice([0, 1], size=(32, 32))
moving_mask = RNG.choice([0, 1], size=(32, 32))
dfbr = DFBRegister()
with pytest.raises(
ValueError,
match=r".*The input images are expected to have 3 channels.*",
):
_ = dfbr.register(
fixed_img[:, :, :1],
moving_img[:, :, :1],
fixed_mask,
moving_mask,
)
def test_register_output_with_initializer(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
) -> None:
"""Test for register function with initializer."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
fixed_msk = imread(fixed_mask)
moving_msk = imread(moving_mask)
dfbr = DFBRegister()
pre_transform = np.array([[-1, 0, 337.8], [0, -1, 767.7], [0, 0, 1]])
expected = np.array(
[[-0.98454, -0.00708, 336.9562], [-0.01024, -0.99751, 769.81131], [0, 0, 1]],
)
output = dfbr.register(
fixed_img,
moving_img,
fixed_msk,
moving_msk,
transform_initializer=pre_transform,
)
assert np.linalg.norm(expected[:2, :2] - output[:2, :2]) < 0.1
assert np.linalg.norm(expected[:2, 2] - output[:2, 2]) < 10
def test_register_output_without_initializer(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
) -> None:
"""Test for register function without initializer."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
fixed_msk = imread(fixed_mask)
moving_msk = imread(moving_mask)
dfbr = DFBRegister()
expected = np.array(
[[-0.99863, 0.00189, 336.79039], [0.00691, -0.99810, 765.98081], [0, 0, 1]],
)
output = dfbr.register(
fixed_img,
moving_img,
fixed_msk,
moving_msk,
)
assert np.linalg.norm(expected[:2, :2] - output[:2, :2]) < 0.1
assert np.linalg.norm(expected[:2, 2] - output[:2, 2]) < 10
_ = dfbr.register(
fixed_img,
moving_img,
fixed_msk[:, :, 0],
moving_msk[:, :, 0],
)
def test_register_tissue_transform(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
) -> None:
"""Test for the estimated tissue and block-wise transform in register function."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
fixed_msk = imread(fixed_mask)
moving_msk = imread(moving_mask)
dfbr = DFBRegister()
pre_transform = np.eye(3)
_ = dfbr.register(
fixed_img,
moving_img,
fixed_msk,
moving_msk,
transform_initializer=pre_transform,
)
def test_estimate_bspline_transform_inputs() -> None:
"""Test input dimensions for estimate_bspline_transform function."""
fixed_img = RNG.random((32, 32, 32, 3))
moving_img = RNG.random((32, 32, 32, 3))
fixed_mask = RNG.choice([0, 1], size=(32, 32))
moving_mask = RNG.choice([0, 1], size=(32, 32))
with pytest.raises(
ValueError,
match=r".*The input images can only be grayscale or RGB images.*",
):
_, _ = estimate_bspline_transform(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
)
def test_estimate_bspline_transform_rgb_input() -> None:
"""Test inputs' number of channels for estimate_bspline_transform function."""
fixed_img = RNG.random((32, 32, 32))
moving_img = RNG.random((32, 32, 32))
fixed_mask = RNG.choice([0, 1], size=(32, 32))
moving_mask = RNG.choice([0, 1], size=(32, 32))
with pytest.raises(
ValueError,
match=r".*The input images can only have 3 channels.*",
):
_, _ = estimate_bspline_transform(
fixed_img,
moving_img,
fixed_mask,
moving_mask,
)
def test_bspline_transform(
fixed_image: Path,
moving_image: Path,
fixed_mask: Path,
moving_mask: Path,
) -> None:
"""Test for estimate_bspline_transform function."""
fixed_img = imread(fixed_image)
moving_img = imread(moving_image)
fixed_mask_ = imread(fixed_mask)
moving_mask_ = imread(moving_mask)
rigid_transform = np.array(
[[-0.99683, -0.00333, 338.69983], [-0.03201, -0.98420, 770.22941], [0, 0, 1]],
)
moving_img = apply_affine_transformation(fixed_img, moving_img, rigid_transform)
moving_mask_ = apply_affine_transformation(fixed_img, moving_mask_, rigid_transform)
# Grayscale images as input
transform = estimate_bspline_transform(
fixed_img[:, :, 0],
moving_img[:, :, 0],
fixed_mask_[:, :, 0],
moving_mask_[:, :, 0],
)
_ = apply_bspline_transform(fixed_img[:, :, 0], moving_img[:, :, 0], transform)
# RGB images as input
transform = estimate_bspline_transform(
fixed_img,
moving_img,
fixed_mask_,
moving_mask_,
)
_ = apply_bspline_transform(fixed_img, moving_img, transform)
registered_msk = apply_bspline_transform(fixed_mask_, moving_mask_, transform)
mask_overlap = dice(fixed_mask_, registered_msk)
assert mask_overlap > 0.75
def test_affine_wsi_transformer(sample_ome_tiff: Path) -> None:
"""Test Affine WSI transformer."""
test_locations = [(1001, 600), (1000, 500), (800, 701)] # at base level 0
resolution = 0
size = (100, 100)
for location in test_locations:
wsi_reader = WSIReader.open(input_img=sample_ome_tiff)
expected = wsi_reader.read_rect(
location,
size,
resolution=resolution,
units="level",
)
transform_level0 = np.array(
[
[0, -1, location[0] + location[1] + size[1]],
[1, 0, location[1] - location[0]],
[0, 0, 1],
],
)
tfm = AffineWSITransformer(wsi_reader, transform_level0)
output = tfm.read_rect(location, size, resolution=resolution, units="level")
expected = cv2.rotate(expected, cv2.ROTATE_90_CLOCKWISE)
assert np.sum(expected - output) == 0
def test_dfbr_feature_extractor_torch_compile(dfbr_features: Path) -> None:
"""Test DFBRFeatureExtractor with torch.compile functionality.
Args:
dfbr_features (Path): Path to the expected features.
"""
def _extract_features() -> tuple:
dfbr = DFBRegister()
fixed_img = np.repeat(
np.expand_dims(
np.repeat(
np.expand_dims(np.arange(0, 64, 1, dtype=np.uint8), axis=1),
64,
axis=1,
),
axis=2,
),
3,
axis=2,
)
output = dfbr.extract_features(fixed_img, fixed_img)
pool3_feat = output["block3_pool"][0, :].detach().numpy()
pool4_feat = output["block4_pool"][0, :].detach().numpy()
pool5_feat = output["block5_pool"][0, :].detach().numpy()
return pool3_feat, pool4_feat, pool5_feat
torch_compile_mode = rcParam["torch_compile_mode"]
torch._dynamo.reset()
rcParam["torch_compile_mode"] = "default"
(pool3_feat, pool4_feat, pool5_feat), compile_time = timed(_extract_features)
_pool3_feat, _pool4_feat, _pool5_feat = np.load(
str(dfbr_features),
allow_pickle=True,
)
assert np.mean(np.abs(pool3_feat - _pool3_feat)) < 1.0e-4
assert np.mean(np.abs(pool4_feat - _pool4_feat)) < 1.0e-4
assert np.mean(np.abs(pool5_feat - _pool5_feat)) < 1.0e-4
logger.info("torch.compile default mode: %s", compile_time)
torch._dynamo.reset()
rcParam["torch_compile_mode"] = "reduce-overhead"
(pool3_feat, pool4_feat, pool5_feat), compile_time = timed(_extract_features)
_pool3_feat, _pool4_feat, _pool5_feat = np.load(
str(dfbr_features),
allow_pickle=True,
)
assert np.mean(np.abs(pool3_feat - _pool3_feat)) < 1.0e-4
assert np.mean(np.abs(pool4_feat - _pool4_feat)) < 1.0e-4
assert np.mean(np.abs(pool5_feat - _pool5_feat)) < 1.0e-4
logger.info("torch.compile reduce-overhead mode: %s", compile_time)
torch._dynamo.reset()
rcParam["torch_compile_mode"] = "max-autotune"
(pool3_feat, pool4_feat, pool5_feat), compile_time = timed(_extract_features)
_pool3_feat, _pool4_feat, _pool5_feat = np.load(
str(dfbr_features),
allow_pickle=True,
)
assert np.mean(np.abs(pool3_feat - _pool3_feat)) < 1.0e-4
assert np.mean(np.abs(pool4_feat - _pool4_feat)) < 1.0e-4
assert np.mean(np.abs(pool5_feat - _pool5_feat)) < 1.0e-4
logger.info("torch.compile max-autotune mode: %s", compile_time)
torch._dynamo.reset()
rcParam["torch_compile_mode"] = torch_compile_mode