forked from Cephla-Lab/stitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_block_reduce.py
More file actions
67 lines (45 loc) · 2.03 KB
/
test_block_reduce.py
File metadata and controls
67 lines (45 loc) · 2.03 KB
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
"""Unit tests for GPU block_reduce."""
import numpy as np
import pytest
import sys
from skimage.measure import block_reduce as skimage_block_reduce
sys.path.insert(0, "src")
from tilefusion.utils import block_reduce
class TestBlockReduce:
"""Test block_reduce GPU vs CPU equivalence."""
def test_2d_basic(self, rng):
"""Test 2D block reduce matches skimage."""
arr = rng.random((256, 256)).astype(np.float32)
block_size = (4, 4)
result = block_reduce(arr, block_size, np.mean)
expected = skimage_block_reduce(arr, block_size, np.mean)
np.testing.assert_allclose(result, expected, rtol=1e-5)
def test_2d_large(self, rng):
"""Test larger 2D array."""
arr = rng.random((1024, 1024)).astype(np.float32)
block_size = (8, 8)
result = block_reduce(arr, block_size, np.mean)
expected = skimage_block_reduce(arr, block_size, np.mean)
np.testing.assert_allclose(result, expected, rtol=1e-5)
def test_3d_multichannel(self, rng):
"""Test 3D array with channel dimension."""
arr = rng.random((3, 256, 256)).astype(np.float32)
block_size = (1, 4, 4)
result = block_reduce(arr, block_size, np.mean)
expected = skimage_block_reduce(arr, block_size, np.mean)
np.testing.assert_allclose(result, expected, rtol=1e-5)
def test_output_shape(self, rng):
"""Test output shape is correct."""
arr = rng.random((512, 512)).astype(np.float32)
block_size = (4, 4)
result = block_reduce(arr, block_size, np.mean)
assert result.shape == (128, 128)
def test_non_divisible_shape(self, rng):
"""Test block reduce with non-divisible dimensions."""
arr = rng.random((100, 100)).astype(np.float32)
block_size = (8, 8)
result = block_reduce(arr, block_size, np.mean)
expected = skimage_block_reduce(arr, block_size, np.mean)
np.testing.assert_allclose(result, expected, rtol=1e-5)
if __name__ == "__main__":
pytest.main([__file__, "-v"])