def test_invalid_seed(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) with testing.raises(ValueError): reconstruction(seed * 2, mask, method='dilation') with testing.raises(ValueError): reconstruction(seed * 0.5, mask, method='erosion')
def test_invalid_selem(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) assert_raises(ValueError, reconstruction, seed, mask, selem=np.ones((4, 4))) assert_raises(ValueError, reconstruction, seed, mask, selem=np.ones((3, 4))) reconstruction(seed, mask, selem=np.ones((3, 3)))
def test_invalid_selem(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) assert_raises(ValueError, reconstruction, seed, mask, selem=np.ones((4, 4))) assert_raises(ValueError, reconstruction, seed, mask, selem=np.ones((3, 4))) reconstruction(seed, mask, selem=np.ones((3, 3)))
def test_invalid_seed(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) with pytest.raises(ValueError): reconstruction(seed * 2, mask, method='dilation') with pytest.raises(ValueError): reconstruction(seed * 0.5, mask, method='erosion')
def test_invalid_offset_not_none(): """Test reconstruction with invalid not None offset parameter""" image = np.array([[1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 3, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) mask = np.array([[4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4]]) with testing.raises(ValueError): reconstruction(image, mask, method='dilation', selem=np.ones((3, 3)), offset=np.array([3, 0]))
def h_minima(image, h, selem=None): shifted_img = image + h rec_img = greyreconstruct.reconstruction(shifted_img, image, method='erosion', selem=selem) #grey_reconstruction_erosion(shifted_img, image) residue_img = rec_img - image return residue_img > 0
def fill_spots2(img, size=5): med = morph.dilation(img, selem=np.ones([size, size])) fm = img.copy() fm[1:-2, 1:-2] = med[1:-2, 1:-2] res = rec.reconstruction(fm, img, method='erosion') return res
def test_invalid_offset_not_none(): """Test reconstruction with invalid not None offset parameter""" image = np.array([[1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 3, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) mask = np.array([[4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4]]) with testing.raises(ValueError): reconstruction(image, mask, method='dilation', selem=np.ones((3, 3)), offset=np.array([3, 0]))
def test_offset_not_none(): """Test reconstruction with valid offset parameter""" seed = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) mask = np.array([0, 8, 6, 8, 8, 8, 8, 4, 4, 0]) expected = np.array([0, 3, 6, 6, 6, 6, 6, 4, 4, 0]) assert_array_almost_equal( reconstruction(seed, mask, method='dilation', selem=np.ones(3), offset=np.array([0])), expected)
def fill_spots(img, size=5): med = flt.median(img, selem=np.ones([size, 1])) med = flt.median(med, selem=np.ones([1, size])) fm = img.copy() fm[1:-2, 1:-2] = med[1:-2, 1:-2] fm = np.maximum(fm, img) res = rec.reconstruction(fm, img, method='erosion') return res
def test_offset_not_none(): """Test reconstruction with valid offset parameter""" seed = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) mask = np.array([0, 8, 6, 8, 8, 8, 8, 4, 4, 0]) expected = np.array([0, 3, 6, 6, 6, 6, 6, 4, 4, 0]) assert_array_almost_equal( reconstruction(seed, mask, method='dilation', selem=np.ones(3), offset=np.array([0])), expected)
def test_invalid_selem(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) with testing.raises(ValueError): reconstruction(seed, mask, selem=np.ones((4, 4))) with testing.raises(ValueError): reconstruction(seed, mask, selem=np.ones((3, 4))) reconstruction(seed, mask, selem=np.ones((3, 3)))
def test_two_image_peaks(): """Test reconstruction with two peak pixels isolated by the mask""" image = np.array([[1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 3, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) mask = np.array([[4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4]]) expected = np.array([[2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 3, 3, 3], [1, 1, 1, 1, 1, 3, 3, 3], [1, 1, 1, 1, 1, 3, 3, 3]]) assert_array_almost_equal(reconstruction(image, mask), expected)
def test_invalid_selem(): seed = np.ones((5, 5)) mask = np.ones((5, 5)) with pytest.raises(ValueError): reconstruction(seed, mask, selem=np.ones((4, 4))) with pytest.raises(ValueError): reconstruction(seed, mask, selem=np.ones((3, 4))) reconstruction(seed, mask, selem=np.ones((3, 3)))
def extended_minima(img, h, selem=None): if np.issubdtype(img.dtype, 'half'): resolution = 2 * np.finfo(img.dtype).resolution if h < resolution: h = resolution h_corrected = h - resolution / 2.0 shifted_img = img + h else: shifted_img = _add_constant_clip(img, h) h_corrected = h rec_img = greyreconstruct.reconstruction(shifted_img, img, method='erosion', selem=selem) residue_img = rec_img - img h_min = np.zeros(img.shape, dtype=np.uint8) h_min[residue_img > 0] = 1 return h_min
def test_two_image_peaks(): """Test reconstruction with two peak pixels isolated by the mask""" image = np.array([[1, 1, 1, 1, 1, 1, 1, 1], [1, 2, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 3, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) mask = np.array([[4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [4, 4, 4, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4], [1, 1, 1, 1, 1, 4, 4, 4]]) expected = np.array([[2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 2, 1, 1, 1, 1, 1], [2, 2, 2, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 3, 3, 3], [1, 1, 1, 1, 1, 3, 3, 3], [1, 1, 1, 1, 1, 3, 3, 3]]) assert_close(reconstruction(image, mask), expected)
def test_image_equals_mask(): """Test reconstruction where the image and mask are the same""" assert_close(reconstruction(np.ones((7, 5)), np.ones((7, 5))), 1)
def test_zeros(): """Test reconstruction with image and mask of zeros""" assert_close(reconstruction(np.zeros((5, 7)), np.zeros((5, 7))), 0)
def test_zeros(): """Test reconstruction with image and mask of zeros""" assert_array_almost_equal( reconstruction(np.zeros((5, 7)), np.zeros((5, 7))), 0)
def test_invalid_method(): seed = np.array([0, 8, 8, 8, 8, 8, 8, 8, 8, 0]) mask = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) with testing.raises(ValueError): reconstruction(seed, mask, method='foo')
def test_fill_hole(): """Test reconstruction by erosion, which should fill holes in mask.""" seed = np.array([0, 8, 8, 8, 8, 8, 8, 8, 8, 0]) mask = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) result = reconstruction(seed, mask, method='erosion') assert_close(result, np.array([0, 3, 6, 4, 4, 4, 4, 4, 2, 0]))
def test_image_less_than_mask(): """Test reconstruction where the image is uniform and less than mask""" image = np.ones((5, 5)) mask = np.ones((5, 5)) * 2 assert_close(reconstruction(image, mask), 1)
def test_zeros(): """Test reconstruction with image and mask of zeros""" assert_close(reconstruction(np.zeros((5, 7)), np.zeros((5, 7))), 0)
def test_image_less_than_mask(): """Test reconstruction where the image is uniform and less than mask""" image = np.ones((5, 5)) mask = np.ones((5, 5)) * 2 assert_array_almost_equal(reconstruction(image, mask), 1)
def test_image_equals_mask(): """Test reconstruction where the image and mask are the same""" assert_array_almost_equal(reconstruction(np.ones((7, 5)), np.ones((7, 5))), 1)
def test_zeros(): """Test reconstruction with image and mask of zeros""" assert_array_almost_equal( reconstruction(np.zeros((5, 7)), np.zeros((5, 7))), 0)
def test_one_image_peak(): """Test reconstruction with one peak pixel""" image = np.ones((5, 5)) image[2, 2] = 2 mask = np.ones((5, 5)) * 3 assert_close(reconstruction(image, mask), 2)
def test_zero_image_one_mask(): """Test reconstruction with an image of all zeros and a mask that's not""" result = reconstruction(np.zeros((10, 10)), np.ones((10, 10))) assert_array_almost_equal(result, 0)
def test_zero_image_one_mask(): """Test reconstruction with an image of all zeros and a mask that's not""" result = reconstruction(np.zeros((10, 10)), np.ones((10, 10))) assert_close(result, 0)
def test_fill_hole(): """Test reconstruction by erosion, which should fill holes in mask.""" seed = np.array([0, 8, 8, 8, 8, 8, 8, 8, 8, 0]) mask = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) result = reconstruction(seed, mask, method='erosion') assert_array_almost_equal(result, np.array([0, 3, 6, 4, 4, 4, 4, 4, 2, 0]))
def test_one_image_peak(): """Test reconstruction with one peak pixel""" image = np.ones((5, 5)) image[2, 2] = 2 mask = np.ones((5, 5)) * 3 assert_array_almost_equal(reconstruction(image, mask), 2)
def test_invalid_method(): seed = np.array([0, 8, 8, 8, 8, 8, 8, 8, 8, 0]) mask = np.array([0, 3, 6, 2, 1, 1, 1, 4, 2, 0]) with pytest.raises(ValueError): reconstruction(seed, mask, method='foo')