1""" 2Tests for Morphological footprints 3(skimage.morphology.footprint) 4 5Author: Damian Eads 6""" 7import numpy as np 8from numpy.testing import assert_equal 9 10from skimage._shared.testing import fetch 11from skimage.morphology import footprints 12 13 14class TestSElem(): 15 16 def test_square_footprint(self): 17 """Test square footprints""" 18 for k in range(0, 5): 19 actual_mask = footprints.square(k) 20 expected_mask = np.ones((k, k), dtype='uint8') 21 assert_equal(expected_mask, actual_mask) 22 23 def test_rectangle_footprint(self): 24 """Test rectangle footprints""" 25 for i in range(0, 5): 26 for j in range(0, 5): 27 actual_mask = footprints.rectangle(i, j) 28 expected_mask = np.ones((i, j), dtype='uint8') 29 assert_equal(expected_mask, actual_mask) 30 31 def test_cube_footprint(self): 32 """Test cube footprints""" 33 for k in range(0, 5): 34 actual_mask = footprints.cube(k) 35 expected_mask = np.ones((k, k, k), dtype='uint8') 36 assert_equal(expected_mask, actual_mask) 37 38 def strel_worker(self, fn, func): 39 matlab_masks = np.load(fetch(fn)) 40 k = 0 41 for arrname in sorted(matlab_masks): 42 expected_mask = matlab_masks[arrname] 43 actual_mask = func(k) 44 if expected_mask.shape == (1,): 45 expected_mask = expected_mask[:, np.newaxis] 46 assert_equal(expected_mask, actual_mask) 47 k = k + 1 48 49 def strel_worker_3d(self, fn, func): 50 matlab_masks = np.load(fetch(fn)) 51 k = 0 52 for arrname in sorted(matlab_masks): 53 expected_mask = matlab_masks[arrname] 54 actual_mask = func(k) 55 if expected_mask.shape == (1,): 56 expected_mask = expected_mask[:, np.newaxis] 57 # Test center slice for each dimension. This gives a good 58 # indication of validity without the need for a 3D reference 59 # mask. 60 c = int(expected_mask.shape[0]/2) 61 assert_equal(expected_mask, actual_mask[c, :, :]) 62 assert_equal(expected_mask, actual_mask[:, c, :]) 63 assert_equal(expected_mask, actual_mask[:, :, c]) 64 k = k + 1 65 66 def test_footprint_disk(self): 67 """Test disk footprints""" 68 self.strel_worker("data/disk-matlab-output.npz", footprints.disk) 69 70 def test_footprint_diamond(self): 71 """Test diamond footprints""" 72 self.strel_worker("data/diamond-matlab-output.npz", footprints.diamond) 73 74 def test_footprint_ball(self): 75 """Test ball footprints""" 76 self.strel_worker_3d("data/disk-matlab-output.npz", footprints.ball) 77 78 def test_footprint_octahedron(self): 79 """Test octahedron footprints""" 80 self.strel_worker_3d("data/diamond-matlab-output.npz", 81 footprints.octahedron) 82 83 def test_footprint_octagon(self): 84 """Test octagon footprints""" 85 expected_mask1 = np.array([[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0], 86 [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], 87 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], 88 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 89 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 90 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 91 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 92 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 93 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], 94 [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], 95 [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0]], 96 dtype=np.uint8) 97 actual_mask1 = footprints.octagon(5, 3) 98 expected_mask2 = np.array([[0, 1, 0], 99 [1, 1, 1], 100 [0, 1, 0]], dtype=np.uint8) 101 actual_mask2 = footprints.octagon(1, 1) 102 assert_equal(expected_mask1, actual_mask1) 103 assert_equal(expected_mask2, actual_mask2) 104 105 def test_footprint_ellipse(self): 106 """Test ellipse footprints""" 107 expected_mask1 = np.array([[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0], 108 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 109 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 110 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 111 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 112 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 113 [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0]], dtype=np.uint8) 114 actual_mask1 = footprints.ellipse(5, 3) 115 expected_mask2 = np.array([[1, 1, 1], 116 [1, 1, 1], 117 [1, 1, 1]], dtype=np.uint8) 118 actual_mask2 = footprints.ellipse(1, 1) 119 assert_equal(expected_mask1, actual_mask1) 120 assert_equal(expected_mask2, actual_mask2) 121 assert_equal(expected_mask1, footprints.ellipse(3, 5).T) 122 assert_equal(expected_mask2, footprints.ellipse(1, 1).T) 123 124 def test_footprint_star(self): 125 """Test star footprints""" 126 expected_mask1 = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 127 [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], 128 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 129 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 130 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 131 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], 132 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 133 [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], 134 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 135 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 136 [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], 137 [0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0], 138 [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]], 139 dtype=np.uint8) 140 actual_mask1 = footprints.star(4) 141 expected_mask2 = np.array([[1, 1, 1], 142 [1, 1, 1], 143 [1, 1, 1]], dtype=np.uint8) 144 actual_mask2 = footprints.star(1) 145 assert_equal(expected_mask1, actual_mask1) 146 assert_equal(expected_mask2, actual_mask2) 147