def test_3d_fallback_black_tophat(): image = cp.ones((7, 7, 7), dtype=bool) image[2, 2:4, 2:4] = 0 image[3, 2:5, 2:5] = 0 image[4, 3:5, 3:5] = 0 with expected_warnings([r'operator.*deprecated|\A\Z']): new_image = grey.black_tophat(image) footprint = ndi.generate_binary_structure(3, 1) with expected_warnings([r'operator.*deprecated|\A\Z']): image_expected = ndi.black_tophat(image.view(dtype=cp.uint8), footprint=footprint) cp.testing.assert_array_equal(new_image, image_expected)
def test_gray2rgb_alpha(): x = cp.random.random((5, 5, 4)) with expected_warnings(['Pass-through of possibly RGB images']): assert_equal(gray2rgb(x, alpha=None).shape, (5, 5, 4)) with expected_warnings(['Pass-through of possibly RGB images', 'alpha argument is deprecated']): assert_equal(gray2rgb(x, alpha=False).shape, (5, 5, 3)) with expected_warnings(['Pass-through of possibly RGB images', 'alpha argument is deprecated']): assert_equal(gray2rgb(x, alpha=True).shape, (5, 5, 4)) x = cp.random.random((5, 5, 3)) with expected_warnings(['Pass-through of possibly RGB images']): assert_equal(gray2rgb(x, alpha=None).shape, (5, 5, 3)) with expected_warnings(['Pass-through of possibly RGB images', 'alpha argument is deprecated']): assert_equal(gray2rgb(x, alpha=False).shape, (5, 5, 3)) with expected_warnings(['Pass-through of possibly RGB images', 'alpha argument is deprecated']): assert_equal(gray2rgb(x, alpha=True).shape, (5, 5, 4)) with expected_warnings(['alpha argument is deprecated']): assert_array_equal(gray2rgb(cp.asarray([[1, 2], [3, 4.]]), alpha=True)[0, 0, 3], 1) with expected_warnings(['alpha argument is deprecated']): assert_array_equal( gray2rgb(cp.asarray([[1, 2], [3, 4]], dtype=np.uint8), alpha=True)[0, 0, 3], 255)
def test_prob_tol(): np.random.seed(0) a = cp.array(np.random.random((7, 7))) mask = -np.ones(a.shape) # This pixel is an isolated seed mask[1, 1] = 1 # Unlabeled pixels mask[3:, 3:] = 0 # Seeds connected to unlabeled pixels mask[4, 4] = 2 mask[6, 6] = 1 mask = cp.array(mask) with expected_warnings(['The probability range is outside']): res = random_walker(a, mask, return_full_prob=True) # Lower beta, no warning is expected. res = random_walker(a, mask, return_full_prob=True, beta=10) assert res[0, 1, 1] == 1 assert res[1, 1, 1] == 0 # Being more prob_tol tolerant, no warning is expected. res = random_walker(a, mask, return_full_prob=True, prob_tol=1e-1) assert res[0, 1, 1] == 1 assert res[1, 1, 1] == 0 # Reduced tol, no warning is expected. res = random_walker(a, mask, return_full_prob=True, tol=1e-9) assert res[0, 1, 1] == 1 assert res[1, 1, 1] == 0
def test_2d_cg_mg(): lx = 70 ly = 100 data, labels = make_2d_syntheticdata(lx, ly) with expected_warnings(['"cg_mg" not available']): labels_cg_mg = random_walker(data, labels, beta=90, mode='cg_mg') assert (labels_cg_mg[25:45, 40:60] == 2).all() assert data.shape == labels.shape with expected_warnings(['"cg_mg" not available']): full_prob = random_walker(data, labels, beta=90, mode='cg_mg', return_full_prob=True) assert (full_prob[1, 25:45, 40:60] >= full_prob[0, 25:45, 40:60]).all() assert data.shape == labels.shape return data, labels_cg_mg
def test_trivial_case(self): trivial = cp.zeros((25, 25)) peak_indices = peak.peak_local_max(trivial, min_distance=1) assert type(peak_indices) is cp.ndarray assert peak_indices.size == 0 with expected_warnings(["indices argument is deprecated"]): peaks = peak.peak_local_max(trivial, min_distance=1, indices=False) assert (peaks.astype(bool) == trivial).all()
def test_ndarray_indices_false(self): nd_image = cp.zeros((5, 5, 5)) nd_image[2, 2, 2] = 1 with expected_warnings(["indices argument is deprecated"]): peaks = peak.peak_local_max(nd_image, min_distance=1, indices=False) assert (peaks == nd_image.astype(bool)).all()
def test_bool_array_warnings(): img = cp.zeros((10, 10), dtype=bool) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), anti_aliasing=True) with expected_warnings(['Input image dtype is bool']): rescale(img, 0.5, order=1) with expected_warnings(['Input image dtype is bool']): resize(img, (5, 5), order=1) with expected_warnings(['Input image dtype is bool']): warp(img, cp.eye(3), order=1)
def test_lab_full_gamut(self): a, b = cp.meshgrid(cp.arange(-100, 100), cp.arange(-100, 100)) L = cp.ones(a.shape) lab = cp.dstack((L, a, b)) for value in [0, 10, 20]: lab[:, :, 0] = value with expected_warnings(['Color data out of range']): lab2xyz(lab)
def test_structure_tensor_eigvals(): square = cp.zeros((5, 5)) square[2, 2] = 1 A_elems = structure_tensor(square, sigma=0.1, order='rc') with expected_warnings(['structure_tensor_eigvals is deprecated']): eigvals = structure_tensor_eigvals(*A_elems) eigenvalues = structure_tensor_eigenvalues(A_elems) for ev1, ev2 in zip(eigvals, eigenvalues): assert_array_equal(ev1, ev2)
def test_rescale_nan_warning(in_range, out_range): image = cp.arange(12, dtype=float).reshape(3, 4) image[1, 1] = np.nan msg = (r"One or more intensity levels are NaN\." r" Rescaling will broadcast NaN to the full image\.") with expected_warnings([msg]): exposure.rescale_intensity(image, in_range, out_range)
def test_types(): lx = 70 ly = 100 data, labels = make_2d_syntheticdata(lx, ly) data = 255 * (data - data.min()) // (data.max() - data.min()) data = data.astype(np.uint8) with expected_warnings(['"cg_mg" not available']): labels_cg_mg = random_walker(data, labels, beta=90, mode='cg_mg') assert (labels_cg_mg[25:45, 40:60] == 2).all() assert data.shape == labels.shape return data, labels_cg_mg
def test_mssim_mixed_dtype(): mssim = structural_similarity(cam, cam_noisy) with expected_warnings(["Inputs have mismatched dtype"]): mssim_mixed = structural_similarity(cam, cam_noisy.astype(cp.float32)) assert_almost_equal(mssim, mssim_mixed) # no warning when user supplies data_range mssim_mixed = structural_similarity(cam, cam_noisy.astype(cp.float32), data_range=255) assert_almost_equal(mssim, mssim_mixed)
def test_structure_tensor_orders(): square = cp.zeros((5, 5)) square[2, 2] = 1 with expected_warnings(['the default order of the structure']): A_elems_default = structure_tensor(square, sigma=0.1) A_elems_xy = structure_tensor(square, sigma=0.1, order='xy') A_elems_rc = structure_tensor(square, sigma=0.1, order='rc') for elem_xy, elem_def in zip(A_elems_xy, A_elems_default): assert_array_equal(elem_xy, elem_def) for elem_xy, elem_rc in zip(A_elems_xy, A_elems_rc[::-1]): assert_array_equal(elem_xy, elem_rc)
def test_label_warning_holes(): # fmt: off labeled_holes_image = cp.array( [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2]], dtype=int) # fmt: on with expected_warnings(['use a boolean array?']): remove_small_holes(labeled_holes_image, area_threshold=3) remove_small_holes(labeled_holes_image.astype(bool), area_threshold=3)
def test_empty(self): image = cp.zeros((10, 20)) labels = cp.zeros((10, 20), int) with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(image, labels=labels, footprint=cp.ones((3, 3), bool), min_distance=1, threshold_rel=0, indices=False, exclude_border=False) assert cp.all(~result)
def test_isolated_area(): np.random.seed(0) a = cp.array(np.random.random((7, 7))) mask = -np.ones(a.shape) # This pixel is an isolated seed mask[1, 1] = 0 # Unlabeled pixels mask[3:, 3:] = 0 # Seeds connected to unlabeled pixels mask[4, 4] = 2 mask[6, 6] = 1 mask = cp.array(mask) # Test that no error is raised, and that labels of isolated seeds are OK with expected_warnings(['The probability range is outside']): res = random_walker(a, mask) assert res[1, 1] == 0 with expected_warnings(['The probability range is outside']): res = random_walker(a, mask, return_full_prob=True) assert res[0, 1, 1] == 0 assert res[1, 1, 1] == 0
def test_float_conversion_dtype_warns(): """Test that convert issues a warning when called""" x = np.array([-1, 1]) # Test all combinations of dtypes convertions dtype_combin = np.array(np.meshgrid(float_dtype_list, float_dtype_list)).T.reshape(-1, 2) for dtype_in, dtype_out in dtype_combin: x = x.astype(dtype_in) with expected_warnings(["The use of this function is discouraged"]): y = convert(x, dtype_out) assert y.dtype == cp.dtype(dtype_out)
def test_threshold_rel_default(self): image = cp.ones((5, 5)) image[2, 2] = 1 assert len(peak.peak_local_max(image)) == 0 image[2, 2] = 2 assert_array_equal(peak.peak_local_max(image), [[2, 2]]) image[2, 2] = 0 with expected_warnings(["When min_distance < 1"]): assert len(peak.peak_local_max(image, min_distance=0)) == image.size - 1
def test_trivial_cases(): # When all voxels are labeled img = cp.ones((10, 10)) labels = cp.ones((10, 10)) with expected_warnings(["Returning provided labels"]): pass_through = random_walker(img, labels) cp.testing.assert_array_equal(pass_through, labels) # When all voxels are labeled AND return_full_prob is True labels[:, :5] = 3 expected = cp.concatenate( ((labels == 1)[..., cp.newaxis], (labels == 3)[..., cp.newaxis]), axis=2) with expected_warnings(["Returning provided labels"]): test = random_walker(img, labels, return_full_prob=True) cp.testing.assert_array_equal(test, expected) # Unlabeled voxels not connected to seed, so nothing can be done img = cp.full((10, 10), False) object_A = np.array([(6, 7), (6, 8), (7, 7), (7, 8)]) object_B = np.array([(3, 1), (4, 1), (2, 2), (3, 2), (4, 2), (2, 3), (3, 3)]) for x, y in np.vstack((object_A, object_B)): img[y][x] = True markers = cp.zeros((10, 10), dtype=cp.int8) for x, y in object_B: markers[y][x] = 1 markers[img == 0] = -1 with expected_warnings(["All unlabeled pixels are isolated"]): output_labels = random_walker(img, markers) assert cp.all(output_labels[markers == 1] == 1) # Here 0-labeled pixels could not be determined (no connexion to seed) assert cp.all(output_labels[markers == 0] == -1) with expected_warnings(["All unlabeled pixels are isolated"]): test = random_walker(img, markers, return_full_prob=True)
def test_disk(self): """regression test of img-1194, footprint = [1] Test peak.peak_local_max when every point is a local maximum """ image = cp.asarray(np.random.uniform(size=(10, 20))) footprint = cp.asarray([[1]]) with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(image, labels=cp.ones((10, 20), int), footprint=footprint, min_distance=1, threshold_rel=0, threshold_abs=-1, indices=False, exclude_border=False) assert cp.all(result) with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(image, footprint=footprint, threshold_abs=-1, indices=False, exclude_border=False) assert cp.all(result)
def test_adjacent_and_same(self): image = cp.zeros((10, 20)) labels = cp.zeros((10, 20), int) image[5, 5:6] = 1 labels[5, 5:6] = 1 with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(image, labels=labels, footprint=cp.ones((3, 3), bool), min_distance=1, threshold_rel=0, indices=False, exclude_border=False) assert cp.all(result == (labels == 1))
def test_multispectral_2d(): lx, ly = 70, 100 data, labels = make_2d_syntheticdata(lx, ly) data = data[..., cp.newaxis].repeat(2, axis=-1) # Expect identical output with expected_warnings(['The probability range is outside']): multi_labels = random_walker(data, labels, mode='cg', multichannel=True) assert data[..., 0].shape == labels.shape single_labels = random_walker(data[..., 0], labels, mode='cg') assert (multi_labels.reshape(labels.shape)[25:45, 40:60] == 2).all() assert data[..., 0].shape == labels.shape return data, multi_labels, single_labels, labels
def test_ndarray_exclude_border(self): nd_image = cp.zeros((5, 5, 5)) nd_image[[1, 0, 0], [0, 1, 0], [0, 0, 1]] = 1 nd_image[3, 0, 0] = 1 nd_image[2, 2, 2] = 1 expected = cp.zeros_like(nd_image, dtype=bool) expected[2, 2, 2] = True expectedNoBorder = np.zeros_like(nd_image, dtype=bool) expectedNoBorder[2, 2, 2] = True expectedNoBorder[0, 0, 1] = True expectedNoBorder[3, 0, 0] = True expectedNoBorder = cp.asarray(expectedNoBorder) with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(nd_image, min_distance=2, exclude_border=2, indices=False) assert_array_equal(result, expected) # Check that bools work as expected assert_array_equal( peak.peak_local_max(nd_image, min_distance=2, exclude_border=2, indices=False), peak.peak_local_max(nd_image, min_distance=2, exclude_border=True, indices=False)) assert_array_equal( peak.peak_local_max(nd_image, min_distance=2, exclude_border=0, indices=False), peak.peak_local_max(nd_image, min_distance=2, exclude_border=False, indices=False)) # Check both versions with no border assert_array_equal( peak.peak_local_max(nd_image, min_distance=2, exclude_border=0, indices=False), expectedNoBorder, ) assert_array_equal( peak.peak_local_max(nd_image, exclude_border=False, indices=False), nd_image.astype(bool))
def test_nD_gray_conversion(func, shape): img = cp.random.rand(*shape) msg_list = [] if img.ndim == 3 and func == gray2rgb: msg_list.append('Pass-through of possibly RGB images in gray2rgb') elif img.ndim == 2 and func == rgb2gray: msg_list.append('The behavior of rgb2gray will change') with expected_warnings(msg_list): out = func(img) common_ndim = min(out.ndim, len(shape)) assert out.shape[:common_ndim] == shape[:common_ndim]
def test_input_labels_unmodified(self): image = cp.zeros((10, 20)) labels = cp.zeros((10, 20), int) image[5, 5] = 1 labels[5, 5] = 3 labelsin = labels.copy() with expected_warnings(["indices argument is deprecated"]): peak.peak_local_max(image, labels=labels, footprint=cp.ones((3, 3), bool), min_distance=1, threshold_rel=0, indices=False, exclude_border=False) assert cp.all(labels == labelsin)
def test_validate_interpolation_order(dtype, order): if order is None: # Default order assert (_validate_interpolation_order(dtype, None) == 0 if dtype == bool else 1) elif order < 0 or order > 5: # Order not in valid range with pytest.raises(ValueError): _validate_interpolation_order(dtype, order) elif dtype == bool and order != 0: # Deprecated order for bool array with expected_warnings(["Input image dtype is bool"]): assert _validate_interpolation_order(bool, order) == order else: # Valid use case assert _validate_interpolation_order(dtype, order) == order
def test_shape_index(): # software floating point arm doesn't raise a warning on divide by zero # https://github.com/scikit-image/scikit-image/issues/3335 square = cp.zeros((5, 5)) square[2, 2] = 4 with expected_warnings([r"divide by zero|\A\Z", r"invalid value|\A\Z"]): s = shape_index(square, sigma=0.1) # fmt: off assert_array_almost_equal( s, cp.asarray([ [cp.nan, cp.nan, -0.5, cp.nan, cp.nan], # noqa [cp.nan, 0, cp.nan, 0, cp.nan], # noqa [-0.5, cp.nan, -1, cp.nan, -0.5], # noqa [cp.nan, 0, cp.nan, 0, cp.nan], # noqa [cp.nan, cp.nan, -0.5, cp.nan, cp.nan], # noqa ]))
def test_two_objects(self): image = cp.zeros((10, 20)) labels = cp.zeros((10, 20), int) image[5, 5] = 1 image[5, 15] = 0.5 labels[5, 5] = 1 labels[5, 15] = 2 expected = labels > 0 with expected_warnings(["indices argument is deprecated"]): result = peak.peak_local_max(image, labels=labels, footprint=cp.ones((3, 3), bool), min_distance=1, threshold_rel=0, indices=False, exclude_border=False) assert cp.all(result == expected)
def test_PSNR_float(): p_uint8 = peak_signal_noise_ratio(cam, cam_noisy) p_float64 = peak_signal_noise_ratio(cam / 255.0, cam_noisy / 255.0, data_range=1) assert_almost_equal(p_uint8, p_float64, decimal=5) # mixed precision inputs p_mixed = peak_signal_noise_ratio(cam / 255.0, cam_noisy.astype(np.float32) / 255.0, data_range=1) assert_almost_equal(p_mixed, p_float64, decimal=5) # mismatched dtype results in a warning if data_range is unspecified with expected_warnings(["Inputs have mismatched dtype"]): p_mixed = peak_signal_noise_ratio(cam / 255.0, cam_noisy.astype(np.float32) / 255.0) assert_almost_equal(p_mixed, p_float64, decimal=5)
def test_uint_image_holes(): # fmt: off labeled_holes_image = cp.array( [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 0, 0, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 0, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 2, 0, 2], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2]], dtype=cp.uint8) expected = cp.array( [[0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1]], dtype=bool) # fmt: on with expected_warnings(['returned as a boolean array']): observed = remove_small_holes(labeled_holes_image, area_threshold=3) assert_array_equal(observed, expected)