本文整理汇总了Python中skimage._shared.testing.assert_函数的典型用法代码示例。如果您正苦于以下问题:Python assert_函数的具体用法?Python assert_怎么用?Python assert_使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mask
def test_mask():
length = 100
ramps = [np.linspace(0, 4 * np.pi, length),
np.linspace(0, 8 * np.pi, length),
np.linspace(0, 6 * np.pi, length)]
image = np.vstack(ramps)
mask_1d = np.ones((length,), dtype=np.bool)
mask_1d[0] = mask_1d[-1] = False
for i in range(len(ramps)):
# mask all ramps but the i'th one
mask = np.zeros(image.shape, dtype=np.bool)
mask |= mask_1d.reshape(1, -1)
mask[i, :] = False # unmask i'th ramp
image_wrapped = np.ma.array(np.angle(np.exp(1j * image)), mask=mask)
image_unwrapped = unwrap_phase(image_wrapped)
image_unwrapped -= image_unwrapped[0, 0] # remove phase shift
# The end of the unwrapped array should have value equal to the
# endpoint of the unmasked ramp
assert_array_almost_equal_nulp(image_unwrapped[:, -1], image[i, -1])
assert_(np.ma.isMaskedArray(image_unwrapped))
# Same tests, but forcing use of the 3D unwrapper by reshaping
with expected_warnings(['length 1 dimension']):
shape = (1,) + image_wrapped.shape
image_wrapped_3d = image_wrapped.reshape(shape)
image_unwrapped_3d = unwrap_phase(image_wrapped_3d)
# remove phase shift
image_unwrapped_3d -= image_unwrapped_3d[0, 0, 0]
assert_array_almost_equal_nulp(image_unwrapped_3d[:, :, -1],
image[i, -1])
示例2: test_wavelet_threshold
def test_wavelet_threshold():
rstate = np.random.RandomState(1234)
img = astro_gray
sigma = 0.1
noisy = img + sigma * rstate.randn(*(img.shape))
noisy = np.clip(noisy, 0, 1)
# employ a single, user-specified threshold instead of BayesShrink sigmas
with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
denoised = _wavelet_threshold(noisy, wavelet='db1', method=None,
threshold=sigma)
psnr_noisy = compare_psnr(img, noisy)
psnr_denoised = compare_psnr(img, denoised)
assert_(psnr_denoised > psnr_noisy)
# either method or threshold must be defined
with testing.raises(ValueError):
_wavelet_threshold(noisy, wavelet='db1', method=None, threshold=None)
# warns if a threshold is provided in a case where it would be ignored
with expected_warnings(["Thresholding method ",
PYWAVELET_ND_INDEXING_WARNING]):
_wavelet_threshold(noisy, wavelet='db1', method='BayesShrink',
threshold=sigma)
示例3: test_wavelet_denoising_levels
def test_wavelet_denoising_levels():
rstate = np.random.RandomState(1234)
ndim = 2
N = 256
wavelet = 'db1'
# Generate a very simple test image
img = 0.2*np.ones((N, )*ndim)
img[[slice(5, 13), ] * ndim] = 0.8
sigma = 0.1
noisy = img + sigma * rstate.randn(*(img.shape))
noisy = np.clip(noisy, 0, 1)
denoised = restoration.denoise_wavelet(noisy, wavelet=wavelet)
denoised_1 = restoration.denoise_wavelet(noisy, wavelet=wavelet,
wavelet_levels=1)
psnr_noisy = compare_psnr(img, noisy)
psnr_denoised = compare_psnr(img, denoised)
psnr_denoised_1 = compare_psnr(img, denoised_1)
# multi-level case should outperform single level case
assert_(psnr_denoised > psnr_denoised_1 > psnr_noisy)
# invalid number of wavelet levels results in a ValueError
max_level = pywt.dwt_max_level(np.min(img.shape),
pywt.Wavelet(wavelet).dec_len)
with testing.raises(ValueError):
restoration.denoise_wavelet(
noisy,
wavelet=wavelet, wavelet_levels=max_level+1)
with testing.raises(ValueError):
restoration.denoise_wavelet(
noisy,
wavelet=wavelet, wavelet_levels=-1)
示例4: test_wavelet_denoising_nd
def test_wavelet_denoising_nd():
rstate = np.random.RandomState(1234)
for method in ['VisuShrink', 'BayesShrink']:
for ndim in range(1, 5):
# Generate a very simple test image
if ndim < 3:
img = 0.2*np.ones((128, )*ndim)
else:
img = 0.2*np.ones((16, )*ndim)
img[(slice(5, 13), ) * ndim] = 0.8
sigma = 0.1
noisy = img + sigma * rstate.randn(*(img.shape))
noisy = np.clip(noisy, 0, 1)
# Mark H. 2018.08:
# The issue arises because when ndim in [1, 2]
# ``waverecn`` calls ``_match_coeff_dims``
# Which includes a numpy 1.15 deprecation.
# for larger number of dimensions _match_coeff_dims isn't called
# for some reason.
anticipated_warnings = (PYWAVELET_ND_INDEXING_WARNING
if ndim < 3 else None)
with expected_warnings([anticipated_warnings]):
# Verify that SNR is improved with internally estimated sigma
denoised = restoration.denoise_wavelet(noisy, method=method)
psnr_noisy = compare_psnr(img, noisy)
psnr_denoised = compare_psnr(img, denoised)
assert_(psnr_denoised > psnr_noisy)
示例5: check_wrap_around
def check_wrap_around(ndim, axis):
# create a ramp, but with the last pixel along axis equalling the first
elements = 100
ramp = np.linspace(0, 12 * np.pi, elements)
ramp[-1] = ramp[0]
image = ramp.reshape(tuple([elements if n == axis else 1
for n in range(ndim)]))
image_wrapped = np.angle(np.exp(1j * image))
index_first = tuple([0] * ndim)
index_last = tuple([-1 if n == axis else 0 for n in range(ndim)])
# unwrap the image without wrap around
with warnings.catch_warnings():
# We do not want warnings about length 1 dimensions
warnings.simplefilter("ignore")
image_unwrap_no_wrap_around = unwrap_phase(image_wrapped, seed=0)
print('endpoints without wrap_around:',
image_unwrap_no_wrap_around[index_first],
image_unwrap_no_wrap_around[index_last])
# without wrap around, the endpoints of the image should differ
assert_(abs(image_unwrap_no_wrap_around[index_first] -
image_unwrap_no_wrap_around[index_last]) > np.pi)
# unwrap the image with wrap around
wrap_around = [n == axis for n in range(ndim)]
with warnings.catch_warnings():
# We do not want warnings about length 1 dimensions
warnings.simplefilter("ignore")
image_unwrap_wrap_around = unwrap_phase(image_wrapped, wrap_around,
seed=0)
print('endpoints with wrap_around:',
image_unwrap_wrap_around[index_first],
image_unwrap_wrap_around[index_last])
# with wrap around, the endpoints of the image should be equal
assert_almost_equal(image_unwrap_wrap_around[index_first],
image_unwrap_wrap_around[index_last])
示例6: test_clear_border_non_binary_3d
def test_clear_border_non_binary_3d():
image3d = np.array(
[[[1, 2, 3, 1, 2],
[3, 3, 3, 4, 2],
[3, 4, 3, 4, 2],
[3, 3, 2, 1, 2]],
[[1, 2, 3, 1, 2],
[3, 3, 5, 4, 2],
[3, 4, 5, 4, 2],
[3, 3, 2, 1, 2]],
[[1, 2, 3, 1, 2],
[3, 3, 3, 4, 2],
[3, 4, 3, 4, 2],
[3, 3, 2, 1, 2]],
])
result = clear_border(image3d)
expected = np.array(
[[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
])
assert_array_equal(result, expected)
assert_(not np.all(image3d == result))
示例7: test_denoise_tv_chambolle_1d
def test_denoise_tv_chambolle_1d():
"""Apply the TV denoising algorithm on a 1D sinusoid."""
x = 125 + 100*np.sin(np.linspace(0, 8*np.pi, 1000))
x += 20 * np.random.rand(x.size)
x = np.clip(x, 0, 255)
res = restoration.denoise_tv_chambolle(x.astype(np.uint8), weight=0.1)
assert_(res.dtype == np.float)
assert_(res.std() * 255 < x.std())
示例8: test_no_denoising_for_small_h
def test_no_denoising_for_small_h():
img = np.zeros((40, 40))
img[10:-10, 10:-10] = 1.
img += 0.3*np.random.randn(*img.shape)
# very small h should result in no averaging with other patches
denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=True,
multichannel=True)
assert_(np.allclose(denoised, img))
denoised = restoration.denoise_nl_means(img, 7, 5, 0.01, fast_mode=False,
multichannel=True)
assert_(np.allclose(denoised, img))
示例9: test_denoise_tv_chambolle_3d
def test_denoise_tv_chambolle_3d():
"""Apply the TV denoising algorithm on a 3D image representing a sphere."""
x, y, z = np.ogrid[0:40, 0:40, 0:40]
mask = (x - 22)**2 + (y - 20)**2 + (z - 17)**2 < 8**2
mask = 100 * mask.astype(np.float)
mask += 60
mask += 20 * np.random.rand(*mask.shape)
mask[mask < 0] = 0
mask[mask > 255] = 255
res = restoration.denoise_tv_chambolle(mask.astype(np.uint8), weight=0.1)
assert_(res.dtype == np.float)
assert_(res.std() * 255 < mask.std())
示例10: test_denoise_tv_bregman_3d
def test_denoise_tv_bregman_3d():
img = checkerboard.copy()
# add some random noise
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_tv_bregman(img, weight=10)
out2 = restoration.denoise_tv_bregman(img, weight=5)
# make sure noise is reduced in the checkerboard cells
assert_(img[30:45, 5:15].std() > out1[30:45, 5:15].std())
assert_(out1[30:45, 5:15].std() > out2[30:45, 5:15].std())
示例11: test_denoise_nl_means_multichannel
def test_denoise_nl_means_multichannel():
# for true 3D data, 3D denoising is better than denoising as 2D+channels
img = np.zeros((13, 10, 8))
img[6, 4:6, 2:-2] = 1.
sigma = 0.3
imgn = img + sigma * np.random.randn(*img.shape)
denoised_wrong_multichannel = restoration.denoise_nl_means(
imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=True)
denoised_ok_multichannel = restoration.denoise_nl_means(
imgn, 3, 4, 0.6 * sigma, fast_mode=True, multichannel=False)
psnr_wrong = compare_psnr(img, denoised_wrong_multichannel)
psnr_ok = compare_psnr(img, denoised_ok_multichannel)
assert_(psnr_ok > psnr_wrong)
示例12: test_range
def test_range():
"""Output of edge detection should be in [0, 1]"""
image = np.random.random((100, 100))
for detector in (filters.sobel, filters.scharr,
filters.prewitt, filters.roberts):
out = detector(image)
assert_(out.min() >= 0,
"Minimum of `{0}` is smaller than zero".format(
detector.__name__)
)
assert_(out.max() <= 1,
"Maximum of `{0}` is larger than 1".format(
detector.__name__)
)
示例13: test_denoise_bilateral_color
def test_denoise_bilateral_color():
img = checkerboard.copy()[:50, :50]
# add some random noise
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_bilateral(img, sigma_color=0.1,
sigma_spatial=10, multichannel=True)
out2 = restoration.denoise_bilateral(img, sigma_color=0.2,
sigma_spatial=20, multichannel=True)
# make sure noise is reduced in the checkerboard cells
assert_(img[30:45, 5:15].std() > out1[30:45, 5:15].std())
assert_(out1[30:45, 5:15].std() > out2[30:45, 5:15].std())
示例14: test_wavelet_denoising
def test_wavelet_denoising():
rstate = np.random.RandomState(1234)
# version with one odd-sized dimension
astro_gray_odd = astro_gray[:, :-1]
astro_odd = astro[:, :-1]
for img, multichannel, convert2ycbcr in [(astro_gray, False, False),
(astro_gray_odd, False, False),
(astro_odd, True, False),
(astro_odd, True, True)]:
sigma = 0.1
noisy = img + sigma * rstate.randn(*(img.shape))
noisy = np.clip(noisy, 0, 1)
# Verify that SNR is improved when true sigma is used
with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
denoised = restoration.denoise_wavelet(noisy, sigma=sigma,
multichannel=multichannel,
convert2ycbcr=convert2ycbcr)
psnr_noisy = compare_psnr(img, noisy)
psnr_denoised = compare_psnr(img, denoised)
assert_(psnr_denoised > psnr_noisy)
# Verify that SNR is improved with internally estimated sigma
with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
denoised = restoration.denoise_wavelet(noisy,
multichannel=multichannel,
convert2ycbcr=convert2ycbcr)
psnr_noisy = compare_psnr(img, noisy)
psnr_denoised = compare_psnr(img, denoised)
assert_(psnr_denoised > psnr_noisy)
# SNR is improved less with 1 wavelet level than with the default.
denoised_1 = restoration.denoise_wavelet(noisy,
multichannel=multichannel,
wavelet_levels=1,
convert2ycbcr=convert2ycbcr)
psnr_denoised_1 = compare_psnr(img, denoised_1)
assert_(psnr_denoised > psnr_denoised_1)
assert_(psnr_denoised_1 > psnr_noisy)
# Test changing noise_std (higher threshold, so less energy in signal)
with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
res1 = restoration.denoise_wavelet(noisy, sigma=2 * sigma,
multichannel=multichannel)
with expected_warnings([PYWAVELET_ND_INDEXING_WARNING]):
res2 = restoration.denoise_wavelet(noisy, sigma=sigma,
multichannel=multichannel)
assert_(np.sum(res1**2) <= np.sum(res2**2))
示例15: test_denoise_nl_means_2d
def test_denoise_nl_means_2d():
img = np.zeros((40, 40))
img[10:-10, 10:-10] = 1.
sigma = 0.3
img += sigma * np.random.randn(*img.shape)
for s in [sigma, 0]:
denoised = restoration.denoise_nl_means(img, 7, 5, 0.2, fast_mode=True,
multichannel=True, sigma=s)
# make sure noise is reduced
assert_(img.std() > denoised.std())
denoised = restoration.denoise_nl_means(img, 7, 5, 0.2,
fast_mode=False,
multichannel=True, sigma=s)
# make sure noise is reduced
assert_(img.std() > denoised.std())