update
This commit is contained in:
0
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__init__.py
vendored
Normal file
0
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__init__.py
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/__init__.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/__init__.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_apply_parallel.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_apply_parallel.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_arraycrop.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_arraycrop.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_compare.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_compare.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_dtype.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_dtype.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_invert.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_invert.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_labels.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_labels.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_lookfor.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_lookfor.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_map_array.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_map_array.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_montage.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_montage.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_random_noise.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_random_noise.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_regular_grid.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_regular_grid.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_shape.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_shape.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_slice_along_axes.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_slice_along_axes.cpython-312.pyc
vendored
Normal file
Binary file not shown.
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_unique_rows.cpython-312.pyc
vendored
Normal file
BIN
.CondaPkg/env/Lib/site-packages/skimage/util/tests/__pycache__/test_unique_rows.cpython-312.pyc
vendored
Normal file
Binary file not shown.
173
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_apply_parallel.py
vendored
Normal file
173
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_apply_parallel.py
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
import numpy as np
|
||||
|
||||
from skimage._shared.testing import assert_array_almost_equal, assert_equal
|
||||
from skimage import color, data, img_as_float
|
||||
from skimage.filters import threshold_local, gaussian
|
||||
from skimage.util.apply_parallel import apply_parallel
|
||||
|
||||
import pytest
|
||||
|
||||
da = pytest.importorskip('dask.array')
|
||||
|
||||
|
||||
def test_apply_parallel():
|
||||
# data
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
|
||||
# apply the filter
|
||||
expected1 = threshold_local(a, 3)
|
||||
result1 = apply_parallel(
|
||||
threshold_local,
|
||||
a,
|
||||
chunks=(6, 6),
|
||||
depth=5,
|
||||
extra_arguments=(3,),
|
||||
extra_keywords={'mode': 'reflect'},
|
||||
)
|
||||
|
||||
assert_array_almost_equal(result1, expected1)
|
||||
|
||||
def wrapped_gauss(arr):
|
||||
return gaussian(arr, sigma=1, mode='reflect')
|
||||
|
||||
expected2 = gaussian(a, sigma=1, mode='reflect')
|
||||
result2 = apply_parallel(wrapped_gauss, a, chunks=(6, 6), depth=5)
|
||||
|
||||
assert_array_almost_equal(result2, expected2)
|
||||
|
||||
expected3 = gaussian(a, sigma=1, mode='reflect')
|
||||
result3 = apply_parallel(
|
||||
wrapped_gauss, da.from_array(a, chunks=(6, 6)), depth=5, compute=True
|
||||
)
|
||||
|
||||
assert isinstance(result3, np.ndarray)
|
||||
assert_array_almost_equal(result3, expected3)
|
||||
|
||||
|
||||
def test_apply_parallel_lazy():
|
||||
# data
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
d = da.from_array(a, chunks=(6, 6))
|
||||
|
||||
# apply the filter
|
||||
expected1 = threshold_local(a, 3)
|
||||
result1 = apply_parallel(
|
||||
threshold_local,
|
||||
a,
|
||||
chunks=(6, 6),
|
||||
depth=5,
|
||||
extra_arguments=(3,),
|
||||
extra_keywords={'mode': 'reflect'},
|
||||
compute=False,
|
||||
)
|
||||
|
||||
# apply the filter on a Dask Array
|
||||
result2 = apply_parallel(
|
||||
threshold_local,
|
||||
d,
|
||||
depth=5,
|
||||
extra_arguments=(3,),
|
||||
extra_keywords={'mode': 'reflect'},
|
||||
)
|
||||
|
||||
assert isinstance(result1, da.Array)
|
||||
|
||||
assert_array_almost_equal(result1.compute(), expected1)
|
||||
|
||||
assert isinstance(result2, da.Array)
|
||||
|
||||
assert_array_almost_equal(result2.compute(), expected1)
|
||||
|
||||
|
||||
def test_no_chunks():
|
||||
a = np.ones(1 * 4 * 8 * 9).reshape(1, 4, 8, 9)
|
||||
|
||||
def add_42(arr):
|
||||
return arr + 42
|
||||
|
||||
expected = add_42(a)
|
||||
result = apply_parallel(add_42, a)
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
|
||||
|
||||
def test_apply_parallel_wrap():
|
||||
def wrapped(arr):
|
||||
return gaussian(arr, sigma=1, mode='wrap')
|
||||
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
expected = gaussian(a, sigma=1, mode='wrap')
|
||||
result = apply_parallel(wrapped, a, chunks=(6, 6), depth=5, mode='wrap')
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
|
||||
|
||||
def test_apply_parallel_nearest():
|
||||
def wrapped(arr):
|
||||
return gaussian(arr, sigma=1, mode='nearest')
|
||||
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
expected = gaussian(a, sigma=1, mode='nearest')
|
||||
result = apply_parallel(
|
||||
wrapped, a, chunks=(6, 6), depth={0: 5, 1: 5}, mode='nearest'
|
||||
)
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('dtype', (np.float32, np.float64))
|
||||
@pytest.mark.parametrize('chunks', (None, (128, 128, 3)))
|
||||
@pytest.mark.parametrize('depth', (0, 8, (8, 8, 0)))
|
||||
def test_apply_parallel_rgb(depth, chunks, dtype):
|
||||
cat = data.chelsea().astype(dtype) / 255.0
|
||||
|
||||
func = color.rgb2ycbcr
|
||||
cat_ycbcr_expected = func(cat)
|
||||
cat_ycbcr = apply_parallel(
|
||||
func, cat, chunks=chunks, depth=depth, dtype=dtype, channel_axis=-1
|
||||
)
|
||||
|
||||
assert_equal(cat_ycbcr.dtype, cat.dtype)
|
||||
|
||||
assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('chunks', (None, (128, 256), 'ndim'))
|
||||
@pytest.mark.parametrize('depth', (0, 8, (8, 16), 'ndim'))
|
||||
@pytest.mark.parametrize('channel_axis', (0, 1, 2, -1, -2, -3))
|
||||
def test_apply_parallel_rgb_channel_axis(depth, chunks, channel_axis):
|
||||
"""Test channel_axis combinations.
|
||||
|
||||
For depth and chunks, test in three ways:
|
||||
1.) scalar (to be applied over all axes)
|
||||
2.) tuple of length ``image.ndim - 1`` corresponding to spatial axes
|
||||
3.) tuple of length ``image.ndim`` corresponding to all axes
|
||||
"""
|
||||
cat = img_as_float(data.chelsea())
|
||||
|
||||
func = color.rgb2ycbcr
|
||||
cat_ycbcr_expected = func(cat, channel_axis=-1)
|
||||
|
||||
# move channel axis to another position
|
||||
cat = np.moveaxis(cat, -1, channel_axis)
|
||||
if chunks == 'ndim':
|
||||
# explicitly specify the chunksize for the channel axis
|
||||
chunks = [128, 128]
|
||||
chunks.insert(channel_axis % cat.ndim, cat.shape[channel_axis])
|
||||
if depth == 'ndim':
|
||||
# explicitly specify the depth for the channel axis
|
||||
depth = [8, 8]
|
||||
depth.insert(channel_axis % cat.ndim, 0)
|
||||
cat_ycbcr = apply_parallel(
|
||||
func,
|
||||
cat,
|
||||
chunks=chunks,
|
||||
depth=depth,
|
||||
dtype=cat.dtype,
|
||||
channel_axis=channel_axis,
|
||||
extra_keywords=dict(channel_axis=channel_axis),
|
||||
)
|
||||
# move channels of output back to the last dimension
|
||||
cat_ycbcr = np.moveaxis(cat_ycbcr, channel_axis, -1)
|
||||
|
||||
assert_array_almost_equal(cat_ycbcr_expected, cat_ycbcr)
|
||||
71
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_arraycrop.py
vendored
Normal file
71
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_arraycrop.py
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
import numpy as np
|
||||
from skimage.util import crop
|
||||
from skimage._shared.testing import assert_array_equal, assert_equal
|
||||
|
||||
|
||||
def test_multi_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, ((1, 2), (2, 1)))
|
||||
assert_array_equal(out[0], [7, 8])
|
||||
assert_array_equal(out[-1], [32, 33])
|
||||
assert_equal(out.shape, (6, 2))
|
||||
|
||||
|
||||
def test_pair_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, (1, 2))
|
||||
assert_array_equal(out[0], [6, 7])
|
||||
assert_array_equal(out[-1], [31, 32])
|
||||
assert_equal(out.shape, (6, 2))
|
||||
|
||||
|
||||
def test_pair_tuple_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, ((1, 2),))
|
||||
assert_array_equal(out[0], [6, 7])
|
||||
assert_array_equal(out[-1], [31, 32])
|
||||
assert_equal(out.shape, (6, 2))
|
||||
|
||||
|
||||
def test_int_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, 1)
|
||||
assert_array_equal(out[0], [6, 7, 8])
|
||||
assert_array_equal(out[-1], [36, 37, 38])
|
||||
assert_equal(out.shape, (7, 3))
|
||||
|
||||
|
||||
def test_int_tuple_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, (1,))
|
||||
assert_array_equal(out[0], [6, 7, 8])
|
||||
assert_array_equal(out[-1], [36, 37, 38])
|
||||
assert_equal(out.shape, (7, 3))
|
||||
|
||||
|
||||
def test_copy_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out0 = crop(arr, 1, copy=True)
|
||||
assert out0.flags.c_contiguous
|
||||
out0[0, 0] = 100
|
||||
assert not np.any(arr == 100)
|
||||
assert not np.may_share_memory(arr, out0)
|
||||
|
||||
out1 = crop(arr, 1)
|
||||
out1[0, 0] = 100
|
||||
assert arr[1, 1] == 100
|
||||
assert np.may_share_memory(arr, out1)
|
||||
|
||||
|
||||
def test_zero_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out = crop(arr, 0)
|
||||
assert out.shape == (9, 5)
|
||||
|
||||
|
||||
def test_np_int_crop():
|
||||
arr = np.arange(45).reshape(9, 5)
|
||||
out1 = crop(arr, np.int64(1))
|
||||
out2 = crop(arr, np.int32(1))
|
||||
assert_array_equal(out1, out2)
|
||||
assert out1.shape == (7, 3)
|
||||
105
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_compare.py
vendored
Normal file
105
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_compare.py
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from skimage.util.compare import compare_images
|
||||
from skimage._shared.testing import assert_stacklevel
|
||||
|
||||
|
||||
def test_compare_images_ValueError_shape():
|
||||
img1 = np.zeros((10, 10), dtype=np.uint8)
|
||||
img2 = np.zeros((10, 1), dtype=np.uint8)
|
||||
with pytest.raises(ValueError):
|
||||
compare_images(img1, img2)
|
||||
|
||||
|
||||
def test_compare_images_ValueError_args():
|
||||
a = np.ones((10, 10)) * 3
|
||||
b = np.zeros((10, 10))
|
||||
with pytest.raises(ValueError):
|
||||
compare_images(a, b, method="unknown")
|
||||
|
||||
|
||||
def test_compare_images_diff():
|
||||
img1 = np.zeros((10, 10), dtype=np.uint8)
|
||||
img1[3:8, 3:8] = 255
|
||||
img2 = np.zeros_like(img1)
|
||||
img2[3:8, 0:8] = 255
|
||||
expected_result = np.zeros_like(img1, dtype=np.float64)
|
||||
expected_result[3:8, 0:3] = 1
|
||||
result = compare_images(img1, img2, method='diff')
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
|
||||
def test_compare_images_replaced_param():
|
||||
img1 = np.zeros((10, 10), dtype=np.uint8)
|
||||
img1[3:8, 3:8] = 255
|
||||
img2 = np.zeros_like(img1)
|
||||
img2[3:8, 0:8] = 255
|
||||
expected_result = np.zeros_like(img1, dtype=np.float64)
|
||||
expected_result[3:8, 0:3] = 1
|
||||
|
||||
regex = ".*Please use `image0, image1`.*"
|
||||
with pytest.warns(FutureWarning, match=regex) as record:
|
||||
result = compare_images(image1=img1, image2=img2)
|
||||
assert_stacklevel(record)
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
with pytest.warns(FutureWarning, match=regex) as record:
|
||||
result = compare_images(image0=img1, image2=img2)
|
||||
assert_stacklevel(record)
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
with pytest.warns(FutureWarning, match=regex) as record:
|
||||
result = compare_images(img1, image2=img2)
|
||||
assert_stacklevel(record)
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
# Test making "method" keyword-only here as well
|
||||
# so whole test can be removed in one go
|
||||
regex = ".*Please pass `method=`.*"
|
||||
with pytest.warns(FutureWarning, match=regex) as record:
|
||||
result = compare_images(img1, img2, "diff")
|
||||
assert_stacklevel(record)
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
|
||||
def test_compare_images_blend():
|
||||
img1 = np.zeros((10, 10), dtype=np.uint8)
|
||||
img1[3:8, 3:8] = 255
|
||||
img2 = np.zeros_like(img1)
|
||||
img2[3:8, 0:8] = 255
|
||||
expected_result = np.zeros_like(img1, dtype=np.float64)
|
||||
expected_result[3:8, 3:8] = 1
|
||||
expected_result[3:8, 0:3] = 0.5
|
||||
result = compare_images(img1, img2, method='blend')
|
||||
np.testing.assert_array_equal(result, expected_result)
|
||||
|
||||
|
||||
def test_compare_images_checkerboard_default():
|
||||
img1 = np.zeros((2**4, 2**4), dtype=np.uint8)
|
||||
img2 = np.full(img1.shape, fill_value=255, dtype=np.uint8)
|
||||
res = compare_images(img1, img2, method='checkerboard')
|
||||
# fmt: off
|
||||
exp_row1 = np.array([0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1., 1.])
|
||||
exp_row2 = np.array([1., 1., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0.])
|
||||
# fmt: on
|
||||
for i in (0, 1, 4, 5, 8, 9, 12, 13):
|
||||
np.testing.assert_array_equal(res[i, :], exp_row1)
|
||||
for i in (2, 3, 6, 7, 10, 11, 14, 15):
|
||||
np.testing.assert_array_equal(res[i, :], exp_row2)
|
||||
|
||||
|
||||
def test_compare_images_checkerboard_tuple():
|
||||
img1 = np.zeros((2**4, 2**4), dtype=np.uint8)
|
||||
img2 = np.full(img1.shape, fill_value=255, dtype=np.uint8)
|
||||
res = compare_images(img1, img2, method='checkerboard', n_tiles=(4, 8))
|
||||
exp_row1 = np.array(
|
||||
[0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0]
|
||||
)
|
||||
exp_row2 = np.array(
|
||||
[1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0]
|
||||
)
|
||||
for i in (0, 1, 2, 3, 8, 9, 10, 11):
|
||||
np.testing.assert_array_equal(res[i, :], exp_row1)
|
||||
for i in (4, 5, 6, 7, 12, 13, 14, 15):
|
||||
np.testing.assert_array_equal(res[i, :], exp_row2)
|
||||
241
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_dtype.py
vendored
Normal file
241
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_dtype.py
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
import numpy as np
|
||||
import itertools
|
||||
from skimage import (
|
||||
img_as_float,
|
||||
img_as_float32,
|
||||
img_as_float64,
|
||||
img_as_int,
|
||||
img_as_uint,
|
||||
img_as_ubyte,
|
||||
)
|
||||
from skimage.util.dtype import _convert
|
||||
|
||||
from skimage._shared._warnings import expected_warnings
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_equal, parametrize
|
||||
|
||||
|
||||
dtype_range = {
|
||||
np.uint8: (0, 255),
|
||||
np.uint16: (0, 65535),
|
||||
np.int8: (-128, 127),
|
||||
np.int16: (-32768, 32767),
|
||||
np.float32: (-1.0, 1.0),
|
||||
np.float64: (-1.0, 1.0),
|
||||
}
|
||||
|
||||
|
||||
img_funcs = (img_as_int, img_as_float64, img_as_float32, img_as_uint, img_as_ubyte)
|
||||
dtypes_for_img_funcs = (np.int16, np.float64, np.float32, np.uint16, np.ubyte)
|
||||
img_funcs_and_types = zip(img_funcs, dtypes_for_img_funcs)
|
||||
|
||||
|
||||
def _verify_range(msg, x, vmin, vmax, dtype):
|
||||
assert_equal(x[0], vmin)
|
||||
assert_equal(x[-1], vmax)
|
||||
assert x.dtype == dtype
|
||||
|
||||
|
||||
@parametrize("dtype, f_and_dt", itertools.product(dtype_range, img_funcs_and_types))
|
||||
def test_range(dtype, f_and_dt):
|
||||
imin, imax = dtype_range[dtype]
|
||||
x = np.linspace(imin, imax, 10).astype(dtype)
|
||||
|
||||
f, dt = f_and_dt
|
||||
|
||||
y = f(x)
|
||||
|
||||
omin, omax = dtype_range[dt]
|
||||
|
||||
if imin == 0 or omin == 0:
|
||||
omin = 0
|
||||
imin = 0
|
||||
|
||||
_verify_range(
|
||||
f"From {np.dtype(dtype)} to {np.dtype(dt)}", y, omin, omax, np.dtype(dt)
|
||||
)
|
||||
|
||||
|
||||
# Add non-standard data types that are allowed by the `_convert` function.
|
||||
dtype_range_extra = dtype_range.copy()
|
||||
dtype_range_extra.update(
|
||||
{np.int32: (-2147483648, 2147483647), np.uint32: (0, 4294967295)}
|
||||
)
|
||||
|
||||
dtype_pairs = [
|
||||
(np.uint8, np.uint32),
|
||||
(np.int8, np.uint32),
|
||||
(np.int8, np.int32),
|
||||
(np.int32, np.int8),
|
||||
(np.float64, np.float32),
|
||||
(np.int32, np.float32),
|
||||
]
|
||||
|
||||
|
||||
@parametrize("dtype_in, dt", dtype_pairs)
|
||||
def test_range_extra_dtypes(dtype_in, dt):
|
||||
"""Test code paths that are not skipped by `test_range`"""
|
||||
|
||||
imin, imax = dtype_range_extra[dtype_in]
|
||||
x = np.linspace(imin, imax, 10).astype(dtype_in)
|
||||
|
||||
y = _convert(x, dt)
|
||||
|
||||
omin, omax = dtype_range_extra[dt]
|
||||
_verify_range(
|
||||
f"From {np.dtype(dtype_in)} to {np.dtype(dt)}", y, omin, omax, np.dtype(dt)
|
||||
)
|
||||
|
||||
|
||||
def test_downcast():
|
||||
x = np.arange(10).astype(np.uint64)
|
||||
with expected_warnings(['Downcasting']):
|
||||
y = img_as_int(x)
|
||||
assert np.allclose(y, x.astype(np.int16))
|
||||
assert y.dtype == np.int16, y.dtype
|
||||
|
||||
|
||||
def test_float_out_of_range():
|
||||
too_high = np.array([2], dtype=np.float32)
|
||||
with testing.raises(ValueError):
|
||||
img_as_int(too_high)
|
||||
too_low = np.array([-2], dtype=np.float32)
|
||||
with testing.raises(ValueError):
|
||||
img_as_int(too_low)
|
||||
|
||||
|
||||
def test_float_float_all_ranges():
|
||||
arr_in = np.array([[-10.0, 10.0, 1e20]], dtype=np.float32)
|
||||
np.testing.assert_array_equal(img_as_float(arr_in), arr_in)
|
||||
|
||||
|
||||
def test_copy():
|
||||
x = np.array([1], dtype=np.float64)
|
||||
y = img_as_float(x)
|
||||
z = img_as_float(x, force_copy=True)
|
||||
|
||||
assert y is x
|
||||
assert z is not x
|
||||
|
||||
|
||||
def test_bool():
|
||||
img_ = np.zeros((10, 10), bool)
|
||||
img8 = np.zeros((10, 10), np.bool_)
|
||||
img_[1, 1] = True
|
||||
img8[1, 1] = True
|
||||
for func, dt in [
|
||||
(img_as_int, np.int16),
|
||||
(img_as_float, np.float64),
|
||||
(img_as_uint, np.uint16),
|
||||
(img_as_ubyte, np.ubyte),
|
||||
]:
|
||||
converted_ = func(img_)
|
||||
assert np.sum(converted_) == dtype_range[dt][1]
|
||||
converted8 = func(img8)
|
||||
assert np.sum(converted8) == dtype_range[dt][1]
|
||||
|
||||
|
||||
def test_clobber():
|
||||
# The `img_as_*` functions should never modify input arrays.
|
||||
for func_input_type in img_funcs:
|
||||
for func_output_type in img_funcs:
|
||||
img = np.random.rand(5, 5)
|
||||
|
||||
img_in = func_input_type(img)
|
||||
img_in_before = img_in.copy()
|
||||
func_output_type(img_in)
|
||||
|
||||
assert_equal(img_in, img_in_before)
|
||||
|
||||
|
||||
def test_signed_scaling_float32():
|
||||
x = np.array([-128, 127], dtype=np.int8)
|
||||
y = img_as_float32(x)
|
||||
assert_equal(y.max(), 1)
|
||||
|
||||
|
||||
def test_float32_passthrough():
|
||||
x = np.array([-1, 1], dtype=np.float32)
|
||||
y = img_as_float(x)
|
||||
assert_equal(y.dtype, x.dtype)
|
||||
|
||||
|
||||
float_dtype_list = [
|
||||
float,
|
||||
float,
|
||||
np.float64,
|
||||
np.single,
|
||||
np.float32,
|
||||
np.float64,
|
||||
'float32',
|
||||
'float64',
|
||||
]
|
||||
|
||||
|
||||
def test_float_conversion_dtype():
|
||||
"""Test any conversion from a float dtype to an other."""
|
||||
x = np.array([-1, 1])
|
||||
|
||||
# Test all combinations of dtypes conversions
|
||||
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)
|
||||
y = _convert(x, dtype_out)
|
||||
assert y.dtype == np.dtype(dtype_out)
|
||||
|
||||
|
||||
def test_float_conversion_dtype_warns():
|
||||
"""Test that convert issues a warning when called"""
|
||||
from skimage.util.dtype import convert
|
||||
|
||||
x = np.array([-1, 1])
|
||||
|
||||
# Test all combinations of dtypes conversions
|
||||
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 == np.dtype(dtype_out)
|
||||
|
||||
|
||||
def test_subclass_conversion():
|
||||
"""Check subclass conversion behavior"""
|
||||
x = np.array([-1, 1])
|
||||
|
||||
for dtype in float_dtype_list:
|
||||
x = x.astype(dtype)
|
||||
y = _convert(x, np.floating)
|
||||
assert y.dtype == x.dtype
|
||||
|
||||
|
||||
def test_int_to_float():
|
||||
"""Check Normalization when casting img_as_float from int types to float"""
|
||||
int_list = np.arange(9, dtype=np.int64)
|
||||
converted = img_as_float(int_list)
|
||||
assert np.allclose(converted, int_list * 1e-19, atol=0.0, rtol=0.1)
|
||||
|
||||
ii32 = np.iinfo(np.int32)
|
||||
ii_list = np.array([ii32.min, ii32.max], dtype=np.int32)
|
||||
floats = img_as_float(ii_list)
|
||||
|
||||
assert_equal(floats.max(), 1)
|
||||
assert_equal(floats.min(), -1)
|
||||
|
||||
|
||||
def test_img_as_ubyte_supports_npulonglong():
|
||||
# Pre NumPy <2.0.0, `data_scaled.dtype.type` is `np.ulonglong` instead of
|
||||
# np.uint64 as one might expect. This caused issues with `img_as_ubyte` due
|
||||
# to `np.ulonglong` missing from `skimage.util.dtype._integer_types`.
|
||||
# This doesn't seem to be an issue for NumPy >=2.0.0.
|
||||
# https://github.com/scikit-image/scikit-image/issues/7385
|
||||
data = np.arange(50, dtype=np.uint64)
|
||||
data_scaled = data * 256 ** (data.dtype.itemsize - 1)
|
||||
result = img_as_ubyte(data_scaled)
|
||||
assert result.dtype == np.uint8
|
||||
74
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_invert.py
vendored
Normal file
74
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_invert.py
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
import numpy as np
|
||||
from skimage import dtype_limits
|
||||
from skimage.util.dtype import dtype_range
|
||||
from skimage.util import invert
|
||||
|
||||
from skimage._shared.testing import assert_array_equal
|
||||
|
||||
|
||||
def test_invert_bool():
|
||||
dtype = 'bool'
|
||||
image = np.zeros((3, 3), dtype=dtype)
|
||||
upper_dtype_limit = dtype_limits(image, clip_negative=False)[1]
|
||||
image[1, :] = upper_dtype_limit
|
||||
expected = np.zeros((3, 3), dtype=dtype) + upper_dtype_limit
|
||||
expected[1, :] = 0
|
||||
result = invert(image)
|
||||
assert_array_equal(expected, result)
|
||||
|
||||
|
||||
def test_invert_uint8():
|
||||
dtype = 'uint8'
|
||||
image = np.zeros((3, 3), dtype=dtype)
|
||||
upper_dtype_limit = dtype_limits(image, clip_negative=False)[1]
|
||||
image[1, :] = upper_dtype_limit
|
||||
expected = np.zeros((3, 3), dtype=dtype) + upper_dtype_limit
|
||||
expected[1, :] = 0
|
||||
result = invert(image)
|
||||
assert_array_equal(expected, result)
|
||||
|
||||
|
||||
def test_invert_int8():
|
||||
dtype = 'int8'
|
||||
image = np.zeros((3, 3), dtype=dtype)
|
||||
lower_dtype_limit, upper_dtype_limit = dtype_limits(image, clip_negative=False)
|
||||
image[1, :] = lower_dtype_limit
|
||||
image[2, :] = upper_dtype_limit
|
||||
expected = np.zeros((3, 3), dtype=dtype)
|
||||
expected[2, :] = lower_dtype_limit
|
||||
expected[1, :] = upper_dtype_limit
|
||||
expected[0, :] = -1
|
||||
result = invert(image)
|
||||
assert_array_equal(expected, result)
|
||||
|
||||
|
||||
def test_invert_float64_signed():
|
||||
dtype = 'float64'
|
||||
image = np.zeros((3, 3), dtype=dtype)
|
||||
lower_dtype_limit, upper_dtype_limit = dtype_limits(image, clip_negative=False)
|
||||
image[1, :] = lower_dtype_limit
|
||||
image[2, :] = upper_dtype_limit
|
||||
expected = np.zeros((3, 3), dtype=dtype)
|
||||
expected[2, :] = lower_dtype_limit
|
||||
expected[1, :] = upper_dtype_limit
|
||||
result = invert(image, signed_float=True)
|
||||
assert_array_equal(expected, result)
|
||||
|
||||
|
||||
def test_invert_float64_unsigned():
|
||||
dtype = 'float64'
|
||||
image = np.zeros((3, 3), dtype=dtype)
|
||||
lower_dtype_limit, upper_dtype_limit = dtype_limits(image, clip_negative=True)
|
||||
image[2, :] = upper_dtype_limit
|
||||
expected = np.zeros((3, 3), dtype=dtype)
|
||||
expected[0, :] = upper_dtype_limit
|
||||
expected[1, :] = upper_dtype_limit
|
||||
result = invert(image)
|
||||
assert_array_equal(expected, result)
|
||||
|
||||
|
||||
def test_invert_roundtrip():
|
||||
for t, limits in dtype_range.items():
|
||||
image = np.array(limits, dtype=t)
|
||||
expected = invert(invert(image))
|
||||
assert_array_equal(image, expected)
|
||||
58
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_labels.py
vendored
Normal file
58
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_labels.py
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
import numpy as np
|
||||
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_equal
|
||||
|
||||
from skimage.util._label import label_points
|
||||
|
||||
|
||||
def test_label_points_coords_dimension():
|
||||
coords, output_shape = np.array([[1, 2], [3, 4]]), (5, 5, 2)
|
||||
with testing.raises(ValueError):
|
||||
label_points(coords, output_shape)
|
||||
|
||||
|
||||
def test_label_points_coords_range():
|
||||
coords, output_shape = np.array([[0, 0], [5, 5]]), (5, 5)
|
||||
with testing.raises(IndexError):
|
||||
label_points(coords, output_shape)
|
||||
|
||||
|
||||
def test_label_points_coords_negative():
|
||||
coords, output_shape = np.array([[-1, 0], [5, 5]]), (5, 5)
|
||||
with testing.raises(ValueError):
|
||||
label_points(coords, output_shape)
|
||||
|
||||
|
||||
def test_label_points_two_dimensional_output():
|
||||
coords, output_shape = np.array([[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]]), (5, 5)
|
||||
mask = label_points(coords, output_shape)
|
||||
assert_equal(
|
||||
mask,
|
||||
np.array(
|
||||
[
|
||||
[1, 0, 0, 0, 0],
|
||||
[0, 2, 0, 0, 0],
|
||||
[0, 0, 3, 0, 0],
|
||||
[0, 0, 0, 4, 0],
|
||||
[0, 0, 0, 0, 5],
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_label_points_multi_dimensional_output():
|
||||
coords, output_shape = np.array(
|
||||
[[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 0], [4, 4, 1]]
|
||||
), (5, 5, 3)
|
||||
mask = label_points(coords, output_shape)
|
||||
result = np.array(
|
||||
[
|
||||
[[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
|
||||
[[0, 0, 0], [0, 2, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
|
||||
[[0, 0, 0], [0, 0, 0], [0, 0, 3], [0, 0, 0], [0, 0, 0]],
|
||||
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [4, 0, 0], [0, 0, 0]],
|
||||
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 5, 0]],
|
||||
]
|
||||
)
|
||||
assert_equal(mask, result)
|
||||
10
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_lookfor.py
vendored
Normal file
10
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_lookfor.py
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
import skimage as ski
|
||||
|
||||
|
||||
def test_lookfor_basic(capsys):
|
||||
assert ski.lookfor is ski.util.lookfor
|
||||
|
||||
ski.util.lookfor("regionprops")
|
||||
search_results = capsys.readouterr().out
|
||||
assert "skimage.measure.regionprops" in search_results
|
||||
assert "skimage.measure.regionprops_table" in search_results
|
||||
87
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_map_array.py
vendored
Normal file
87
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_map_array.py
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
from skimage.util._map_array import map_array, ArrayMap
|
||||
|
||||
from skimage._shared import testing
|
||||
|
||||
|
||||
_map_array_dtypes_in = [
|
||||
np.uint8,
|
||||
np.uint16,
|
||||
np.uint32,
|
||||
np.uint64,
|
||||
np.int8,
|
||||
np.int16,
|
||||
np.int32,
|
||||
np.int64,
|
||||
]
|
||||
|
||||
_map_array_dtypes_out = _map_array_dtypes_in + [np.float32, np.float64]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dtype_in", _map_array_dtypes_in)
|
||||
@pytest.mark.parametrize("dtype_out", _map_array_dtypes_out)
|
||||
@pytest.mark.parametrize("out_array", [True, False])
|
||||
def test_map_array_simple(dtype_in, dtype_out, out_array):
|
||||
input_arr = np.array([0, 2, 0, 3, 4, 5, 0], dtype=dtype_in)
|
||||
input_vals = np.array([1, 2, 3, 4, 6], dtype=dtype_in)[::-1]
|
||||
output_vals = np.array([6, 7, 8, 9, 10], dtype=dtype_out)[::-1]
|
||||
desired = np.array([0, 7, 0, 8, 9, 0, 0], dtype=dtype_out)
|
||||
out = None
|
||||
if out_array:
|
||||
out = np.full(desired.shape, 11, dtype=dtype_out)
|
||||
result = map_array(
|
||||
input_arr=input_arr, input_vals=input_vals, output_vals=output_vals, out=out
|
||||
)
|
||||
np.testing.assert_array_equal(result, desired)
|
||||
assert result.dtype == dtype_out
|
||||
if out_array:
|
||||
assert out is result
|
||||
|
||||
|
||||
def test_map_array_incorrect_output_shape():
|
||||
labels = np.random.randint(0, 5, size=(24, 25))
|
||||
out = np.empty((24, 24))
|
||||
in_values = np.unique(labels)
|
||||
out_values = np.random.random(in_values.shape).astype(out.dtype)
|
||||
with testing.raises(ValueError):
|
||||
map_array(labels, in_values, out_values, out=out)
|
||||
|
||||
|
||||
def test_map_array_non_contiguous_output_array():
|
||||
labels = np.random.randint(0, 5, size=(24, 25))
|
||||
out = np.empty((24 * 3, 25 * 2))[::3, ::2]
|
||||
in_values = np.unique(labels)
|
||||
out_values = np.random.random(in_values.shape).astype(out.dtype)
|
||||
with testing.raises(ValueError):
|
||||
map_array(labels, in_values, out_values, out=out)
|
||||
|
||||
|
||||
def test_arraymap_long_str():
|
||||
labels = np.random.randint(0, 40, size=(24, 25))
|
||||
in_values = np.unique(labels)
|
||||
out_values = np.random.random(in_values.shape)
|
||||
m = ArrayMap(in_values, out_values)
|
||||
assert len(str(m).split('\n')) == m._max_str_lines + 2
|
||||
|
||||
|
||||
def test_arraymap_update():
|
||||
in_values = np.unique(np.random.randint(0, 200, size=5))
|
||||
out_values = np.random.random(len(in_values))
|
||||
m = ArrayMap(in_values, out_values)
|
||||
image = np.random.randint(1, len(m), size=(512, 512))
|
||||
assert np.all(m[image] < 1) # missing values map to 0.
|
||||
m[1:] += 1
|
||||
assert np.all(m[image] >= 1)
|
||||
|
||||
|
||||
def test_arraymap_bool_index():
|
||||
in_values = np.unique(np.random.randint(0, 200, size=5))
|
||||
out_values = np.random.random(len(in_values))
|
||||
m = ArrayMap(in_values, out_values)
|
||||
image = np.random.randint(1, len(in_values), size=(512, 512))
|
||||
assert np.all(m[image] < 1) # missing values map to 0.
|
||||
positive = np.ones(len(m), dtype=bool)
|
||||
positive[0] = False
|
||||
m[positive] += 1
|
||||
assert np.all(m[image] >= 1)
|
||||
183
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_montage.py
vendored
Normal file
183
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_montage.py
vendored
Normal file
@@ -0,0 +1,183 @@
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_equal, assert_array_equal
|
||||
|
||||
import numpy as np
|
||||
from skimage.util import montage
|
||||
|
||||
# TODO: when minimum numpy dependency is 1.25 use:
|
||||
# np..exceptions.AxisError instead of AxisError
|
||||
# and remove this try-except
|
||||
try:
|
||||
from numpy import AxisError
|
||||
except ImportError:
|
||||
from numpy.exceptions import AxisError
|
||||
|
||||
|
||||
def test_montage_simple_gray():
|
||||
n_images, n_rows, n_cols = 3, 2, 3
|
||||
arr_in = np.arange(n_images * n_rows * n_cols, dtype=float)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
arr_out = montage(arr_in)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[0.0, 1.0, 2.0, 6.0, 7.0, 8.0],
|
||||
[3.0, 4.0, 5.0, 9.0, 10.0, 11.0],
|
||||
[12.0, 13.0, 14.0, 8.5, 8.5, 8.5],
|
||||
[15.0, 16.0, 17.0, 8.5, 8.5, 8.5],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
def test_montage_simple_rgb():
|
||||
n_images, n_rows, n_cols, n_channels = 2, 2, 2, 2
|
||||
arr_in = np.arange(
|
||||
n_images * n_rows * n_cols * n_channels,
|
||||
dtype=float,
|
||||
)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols, n_channels)
|
||||
|
||||
arr_out = montage(arr_in, channel_axis=-1)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[[0, 1], [2, 3], [8, 9], [10, 11]],
|
||||
[[4, 5], [6, 7], [12, 13], [14, 15]],
|
||||
[[7, 8], [7, 8], [7, 8], [7, 8]],
|
||||
[[7, 8], [7, 8], [7, 8], [7, 8]],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
@testing.parametrize('channel_axis', (0, 1, 2, 3, -1, -2, -3, -4))
|
||||
def test_montage_simple_rgb_channel_axes(channel_axis):
|
||||
n_images, n_rows, n_cols, n_channels = 2, 2, 2, 2
|
||||
arr_in = np.arange(n_images * n_rows * n_cols * n_channels, dtype=float)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols, n_channels)
|
||||
|
||||
# place channels at the desired location
|
||||
arr_in = np.moveaxis(arr_in, -1, channel_axis)
|
||||
|
||||
arr_out = montage(arr_in, channel_axis=channel_axis)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[[0, 1], [2, 3], [8, 9], [10, 11]],
|
||||
[[4, 5], [6, 7], [12, 13], [14, 15]],
|
||||
[[7, 8], [7, 8], [7, 8], [7, 8]],
|
||||
[[7, 8], [7, 8], [7, 8], [7, 8]],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
@testing.parametrize('channel_axis', (4, -5))
|
||||
def test_montage_invalid_channel_axes(channel_axis):
|
||||
arr_in = np.arange(16, dtype=float).reshape(2, 2, 2, 2)
|
||||
with testing.raises(AxisError):
|
||||
montage(arr_in, channel_axis=channel_axis)
|
||||
|
||||
|
||||
def test_montage_fill_gray():
|
||||
n_images, n_rows, n_cols = 3, 2, 3
|
||||
arr_in = np.arange(n_images * n_rows * n_cols, dtype=float)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
arr_out = montage(arr_in, fill=0)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[0.0, 1.0, 2.0, 6.0, 7.0, 8.0],
|
||||
[3.0, 4.0, 5.0, 9.0, 10.0, 11.0],
|
||||
[12.0, 13.0, 14.0, 0.0, 0.0, 0.0],
|
||||
[15.0, 16.0, 17.0, 0.0, 0.0, 0.0],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
def test_montage_grid_default_gray():
|
||||
n_images, n_rows, n_cols = 15, 11, 7
|
||||
arr_in = np.arange(n_images * n_rows * n_cols, dtype=float)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
n_tiles = int(np.ceil(np.sqrt(n_images)))
|
||||
arr_out = montage(arr_in)
|
||||
assert_equal(arr_out.shape, (n_tiles * n_rows, n_tiles * n_cols))
|
||||
|
||||
|
||||
def test_montage_grid_custom_gray():
|
||||
n_images, n_rows, n_cols = 6, 2, 2
|
||||
arr_in = np.arange(n_images * n_rows * n_cols, dtype=np.float32)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
arr_out = montage(arr_in, grid_shape=(3, 2))
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[0.0, 1.0, 4.0, 5.0],
|
||||
[2.0, 3.0, 6.0, 7.0],
|
||||
[8.0, 9.0, 12.0, 13.0],
|
||||
[10.0, 11.0, 14.0, 15.0],
|
||||
[16.0, 17.0, 20.0, 21.0],
|
||||
[18.0, 19.0, 22.0, 23.0],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
def test_montage_rescale_intensity_gray():
|
||||
n_images, n_rows, n_cols = 4, 3, 3
|
||||
arr_in = np.arange(n_images * n_rows * n_cols, dtype=np.float32)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
arr_out = montage(arr_in, rescale_intensity=True)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[0.0, 0.125, 0.25, 0.0, 0.125, 0.25],
|
||||
[0.375, 0.5, 0.625, 0.375, 0.5, 0.625],
|
||||
[0.75, 0.875, 1.0, 0.75, 0.875, 1.0],
|
||||
[0.0, 0.125, 0.25, 0.0, 0.125, 0.25],
|
||||
[0.375, 0.5, 0.625, 0.375, 0.5, 0.625],
|
||||
[0.75, 0.875, 1.0, 0.75, 0.875, 1.0],
|
||||
]
|
||||
)
|
||||
assert_equal(arr_out.min(), 0.0)
|
||||
assert_equal(arr_out.max(), 1.0)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
def test_montage_simple_padding_gray():
|
||||
n_images, n_rows, n_cols = 2, 2, 2
|
||||
arr_in = np.arange(n_images * n_rows * n_cols)
|
||||
arr_in = arr_in.reshape(n_images, n_rows, n_cols)
|
||||
|
||||
arr_out = montage(arr_in, padding_width=1)
|
||||
arr_ref = np.array(
|
||||
[
|
||||
[3, 3, 3, 3, 3, 3, 3],
|
||||
[3, 0, 1, 3, 4, 5, 3],
|
||||
[3, 2, 3, 3, 6, 7, 3],
|
||||
[3, 3, 3, 3, 3, 3, 3],
|
||||
[3, 3, 3, 3, 3, 3, 3],
|
||||
[3, 3, 3, 3, 3, 3, 3],
|
||||
[3, 3, 3, 3, 3, 3, 3],
|
||||
]
|
||||
)
|
||||
assert_array_equal(arr_out, arr_ref)
|
||||
|
||||
|
||||
def test_error_ndim():
|
||||
arr_error = np.random.randn(1, 2)
|
||||
with testing.raises(ValueError):
|
||||
montage(arr_error)
|
||||
|
||||
arr_error = np.random.randn(1, 2, 3, 4)
|
||||
with testing.raises(ValueError):
|
||||
montage(arr_error)
|
||||
|
||||
arr_error = np.random.randn(1, 2, 3)
|
||||
with testing.raises(ValueError):
|
||||
montage(arr_error, channel_axis=-1)
|
||||
|
||||
arr_error = np.random.randn(1, 2, 3, 4, 5)
|
||||
with testing.raises(ValueError):
|
||||
montage(arr_error, channel_axis=-1)
|
||||
208
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_random_noise.py
vendored
Normal file
208
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_random_noise.py
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_array_equal, assert_allclose
|
||||
|
||||
import numpy as np
|
||||
from skimage.data import camera
|
||||
from skimage.util import random_noise, img_as_float
|
||||
|
||||
|
||||
def test_set_seed():
|
||||
cam = camera()
|
||||
test = random_noise(cam, rng=42)
|
||||
assert_array_equal(test, random_noise(cam, rng=42))
|
||||
|
||||
|
||||
def test_salt():
|
||||
cam = img_as_float(camera())
|
||||
amount = 0.15
|
||||
cam_noisy = random_noise(cam, rng=42, mode='salt', amount=amount)
|
||||
saltmask = cam != cam_noisy
|
||||
|
||||
# Ensure all changes are to 1.0
|
||||
assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))
|
||||
|
||||
# Ensure approximately correct amount of noise was added
|
||||
proportion = float(saltmask.sum()) / (cam.shape[0] * cam.shape[1])
|
||||
tolerance = 1e-2
|
||||
assert abs(amount - proportion) <= tolerance
|
||||
|
||||
|
||||
def test_salt_p1():
|
||||
image = np.random.rand(2, 3)
|
||||
noisy = random_noise(image, mode='salt', amount=1)
|
||||
assert_array_equal(noisy, [[1, 1, 1], [1, 1, 1]])
|
||||
|
||||
|
||||
def test_singleton_dim():
|
||||
"""Ensure images where size of a given dimension is 1 work correctly."""
|
||||
image = np.random.rand(1, 1000)
|
||||
noisy = random_noise(image, mode='salt', amount=0.1, rng=42)
|
||||
tolerance = 5e-2
|
||||
assert abs(np.average(noisy == 1) - 0.1) <= tolerance
|
||||
|
||||
|
||||
def test_pepper():
|
||||
cam = img_as_float(camera())
|
||||
data_signed = cam * 2.0 - 1.0 # Same image, on range [-1, 1]
|
||||
|
||||
amount = 0.15
|
||||
cam_noisy = random_noise(cam, rng=42, mode='pepper', amount=amount)
|
||||
peppermask = cam != cam_noisy
|
||||
|
||||
# Ensure all changes are to 1.0
|
||||
assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))
|
||||
|
||||
# Ensure approximately correct amount of noise was added
|
||||
proportion = float(peppermask.sum()) / (cam.shape[0] * cam.shape[1])
|
||||
tolerance = 1e-2
|
||||
assert abs(amount - proportion) <= tolerance
|
||||
|
||||
# Check to make sure pepper gets added properly to signed images
|
||||
orig_zeros = (data_signed == -1).sum()
|
||||
cam_noisy_signed = random_noise(data_signed, rng=42, mode='pepper', amount=0.15)
|
||||
|
||||
proportion = float((cam_noisy_signed == -1).sum() - orig_zeros) / (
|
||||
cam.shape[0] * cam.shape[1]
|
||||
)
|
||||
assert abs(amount - proportion) <= tolerance
|
||||
|
||||
|
||||
def test_salt_and_pepper():
|
||||
cam = img_as_float(camera())
|
||||
amount = 0.15
|
||||
cam_noisy = random_noise(
|
||||
cam, rng=42, mode='s&p', amount=amount, salt_vs_pepper=0.25
|
||||
)
|
||||
saltmask = np.logical_and(cam != cam_noisy, cam_noisy == 1.0)
|
||||
peppermask = np.logical_and(cam != cam_noisy, cam_noisy == 0.0)
|
||||
|
||||
# Ensure all changes are to 0. or 1.
|
||||
assert_allclose(cam_noisy[saltmask], np.ones(saltmask.sum()))
|
||||
assert_allclose(cam_noisy[peppermask], np.zeros(peppermask.sum()))
|
||||
|
||||
# Ensure approximately correct amount of noise was added
|
||||
proportion = float(saltmask.sum() + peppermask.sum()) / (
|
||||
cam.shape[0] * cam.shape[1]
|
||||
)
|
||||
tolerance = 1e-2
|
||||
assert abs(amount - proportion) <= tolerance
|
||||
|
||||
# Verify the relative amount of salt vs. pepper is close to expected
|
||||
assert 0.18 < saltmask.sum() / peppermask.sum() < 0.35
|
||||
|
||||
|
||||
def test_gaussian():
|
||||
data = np.zeros((128, 128)) + 0.5
|
||||
data_gaussian = random_noise(data, rng=42, var=0.01)
|
||||
assert 0.008 < data_gaussian.var() < 0.012
|
||||
|
||||
data_gaussian = random_noise(data, rng=42, mean=0.3, var=0.015)
|
||||
assert 0.28 < data_gaussian.mean() - 0.5 < 0.32
|
||||
assert 0.012 < data_gaussian.var() < 0.018
|
||||
|
||||
|
||||
def test_localvar():
|
||||
seed = 23703
|
||||
data = np.zeros((128, 128)) + 0.5
|
||||
local_vars = np.zeros((128, 128)) + 0.001
|
||||
local_vars[:64, 64:] = 0.1
|
||||
local_vars[64:, :64] = 0.25
|
||||
local_vars[64:, 64:] = 0.45
|
||||
|
||||
data_gaussian = random_noise(
|
||||
data, mode='localvar', rng=seed, local_vars=local_vars, clip=False
|
||||
)
|
||||
assert 0.0 < data_gaussian[:64, :64].var() < 0.002
|
||||
assert 0.095 < data_gaussian[:64, 64:].var() < 0.105
|
||||
assert 0.245 < data_gaussian[64:, :64].var() < 0.255
|
||||
assert 0.445 < data_gaussian[64:, 64:].var() < 0.455
|
||||
|
||||
# Ensure local variance bounds checking works properly
|
||||
bad_local_vars = np.zeros_like(data)
|
||||
with testing.raises(ValueError):
|
||||
random_noise(data, mode='localvar', rng=seed, local_vars=bad_local_vars)
|
||||
bad_local_vars += 0.1
|
||||
bad_local_vars[0, 0] = -1
|
||||
with testing.raises(ValueError):
|
||||
random_noise(data, mode='localvar', rng=seed, local_vars=bad_local_vars)
|
||||
|
||||
|
||||
def test_speckle():
|
||||
seed = 42
|
||||
data = np.zeros((128, 128)) + 0.1
|
||||
|
||||
rng = np.random.default_rng(seed)
|
||||
noise = rng.normal(0.1, 0.02**0.5, (128, 128))
|
||||
expected = np.clip(data + data * noise, 0, 1)
|
||||
|
||||
data_speckle = random_noise(data, mode='speckle', rng=42, mean=0.1, var=0.02)
|
||||
assert_allclose(expected, data_speckle)
|
||||
|
||||
|
||||
def test_poisson():
|
||||
data = camera() # 512x512 grayscale uint8
|
||||
rng = np.random.default_rng(42)
|
||||
|
||||
cam_noisy = random_noise(data, mode='poisson', rng=42)
|
||||
cam_noisy2 = random_noise(data, mode='poisson', rng=42, clip=False)
|
||||
|
||||
expected = rng.poisson(img_as_float(data) * 256) / 256.0
|
||||
assert_allclose(cam_noisy, np.clip(expected, 0.0, 1.0))
|
||||
assert_allclose(cam_noisy2, expected)
|
||||
|
||||
|
||||
def test_clip_poisson():
|
||||
data = camera() # 512x512 grayscale uint8
|
||||
data_signed = img_as_float(data) * 2.0 - 1.0 # Same image, on range [-1, 1]
|
||||
|
||||
# Signed and unsigned, clipped
|
||||
cam_poisson = random_noise(data, mode='poisson', rng=42, clip=True)
|
||||
cam_poisson2 = random_noise(data_signed, mode='poisson', rng=42, clip=True)
|
||||
assert (cam_poisson.max() == 1.0) and (cam_poisson.min() == 0.0)
|
||||
assert (cam_poisson2.max() == 1.0) and (cam_poisson2.min() == -1.0)
|
||||
|
||||
# Signed and unsigned, unclipped
|
||||
cam_poisson = random_noise(data, mode='poisson', rng=42, clip=False)
|
||||
cam_poisson2 = random_noise(data_signed, mode='poisson', rng=42, clip=False)
|
||||
assert (cam_poisson.max() > 1.15) and (cam_poisson.min() == 0.0)
|
||||
assert (cam_poisson2.max() > 1.3) and (cam_poisson2.min() == -1.0)
|
||||
|
||||
|
||||
def test_clip_gaussian():
|
||||
data = camera() # 512x512 grayscale uint8
|
||||
data_signed = img_as_float(data) * 2.0 - 1.0 # Same image, on range [-1, 1]
|
||||
|
||||
# Signed and unsigned, clipped
|
||||
cam_gauss = random_noise(data, mode='gaussian', rng=42, clip=True)
|
||||
cam_gauss2 = random_noise(data_signed, mode='gaussian', rng=42, clip=True)
|
||||
assert (cam_gauss.max() == 1.0) and (cam_gauss.min() == 0.0)
|
||||
assert (cam_gauss2.max() == 1.0) and (cam_gauss2.min() == -1.0)
|
||||
|
||||
# Signed and unsigned, unclipped
|
||||
cam_gauss = random_noise(data, mode='gaussian', rng=42, clip=False)
|
||||
cam_gauss2 = random_noise(data_signed, mode='gaussian', rng=42, clip=False)
|
||||
assert (cam_gauss.max() > 1.22) and (cam_gauss.min() < -0.35)
|
||||
assert (cam_gauss2.max() > 1.219) and (cam_gauss2.min() < -1.219)
|
||||
|
||||
|
||||
def test_clip_speckle():
|
||||
data = camera() # 512x512 grayscale uint8
|
||||
data_signed = img_as_float(data) * 2.0 - 1.0 # Same image, on range [-1, 1]
|
||||
|
||||
# Signed and unsigned, clipped
|
||||
cam_speckle = random_noise(data, mode='speckle', rng=42, clip=True)
|
||||
cam_speckle_sig = random_noise(data_signed, mode='speckle', rng=42, clip=True)
|
||||
assert (cam_speckle.max() == 1.0) and (cam_speckle.min() == 0.0)
|
||||
assert (cam_speckle_sig.max() == 1.0) and (cam_speckle_sig.min() == -1.0)
|
||||
|
||||
# Signed and unsigned, unclipped
|
||||
cam_speckle = random_noise(data, mode='speckle', rng=42, clip=False)
|
||||
cam_speckle_sig = random_noise(data_signed, mode='speckle', rng=42, clip=False)
|
||||
assert (cam_speckle.max() > 1.219) and (cam_speckle.min() == 0.0)
|
||||
assert (cam_speckle_sig.max() > 1.219) and (cam_speckle_sig.min() < -1.219)
|
||||
|
||||
|
||||
def test_bad_mode():
|
||||
data = np.zeros((64, 64))
|
||||
with testing.raises(KeyError):
|
||||
random_noise(data, 'perlin')
|
||||
37
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_regular_grid.py
vendored
Normal file
37
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_regular_grid.py
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
import numpy as np
|
||||
from skimage.util import regular_grid
|
||||
from skimage._shared.testing import assert_equal
|
||||
|
||||
|
||||
def test_regular_grid_full():
|
||||
ar = np.zeros((2, 2))
|
||||
g = regular_grid(ar, 25)
|
||||
assert_equal(g, [slice(None, None, None), slice(None, None, None)])
|
||||
ar[g] = 1
|
||||
assert_equal(ar.size, ar.sum())
|
||||
|
||||
|
||||
def test_regular_grid_2d_8():
|
||||
ar = np.zeros((20, 40))
|
||||
g = regular_grid(ar.shape, 8)
|
||||
assert_equal(g, [slice(5.0, None, 10.0), slice(5.0, None, 10.0)])
|
||||
ar[g] = 1
|
||||
assert_equal(ar.sum(), 8)
|
||||
|
||||
|
||||
def test_regular_grid_2d_32():
|
||||
ar = np.zeros((20, 40))
|
||||
g = regular_grid(ar.shape, 32)
|
||||
assert_equal(g, [slice(2.0, None, 5.0), slice(2.0, None, 5.0)])
|
||||
ar[g] = 1
|
||||
assert_equal(ar.sum(), 32)
|
||||
|
||||
|
||||
def test_regular_grid_3d_8():
|
||||
ar = np.zeros((3, 20, 40))
|
||||
g = regular_grid(ar.shape, 8)
|
||||
assert_equal(
|
||||
g, [slice(1.0, None, 3.0), slice(5.0, None, 10.0), slice(5.0, None, 10.0)]
|
||||
)
|
||||
ar[g] = 1
|
||||
assert_equal(ar.sum(), 8)
|
||||
181
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_shape.py
vendored
Normal file
181
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_shape.py
vendored
Normal file
@@ -0,0 +1,181 @@
|
||||
import numpy as np
|
||||
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_equal
|
||||
from skimage.util.shape import view_as_blocks, view_as_windows
|
||||
|
||||
|
||||
def test_view_as_blocks_block_not_a_tuple():
|
||||
A = np.arange(10)
|
||||
with testing.raises(TypeError):
|
||||
view_as_blocks(A, [5])
|
||||
|
||||
|
||||
def test_view_as_blocks_negative_shape():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_blocks(A, (-2,))
|
||||
|
||||
|
||||
def test_view_as_blocks_block_too_large():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_blocks(A, (11,))
|
||||
|
||||
|
||||
def test_view_as_blocks_wrong_block_dimension():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_blocks(A, (2, 2))
|
||||
|
||||
|
||||
def test_view_as_blocks_1D_array_wrong_block_shape():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_blocks(A, (3,))
|
||||
|
||||
|
||||
def test_view_as_blocks_1D_array():
|
||||
A = np.arange(10)
|
||||
B = view_as_blocks(A, (5,))
|
||||
assert_equal(B, np.array([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]))
|
||||
|
||||
|
||||
def test_view_as_blocks_2D_array():
|
||||
A = np.arange(4 * 4).reshape(4, 4)
|
||||
B = view_as_blocks(A, (2, 2))
|
||||
assert_equal(B[0, 1], np.array([[2, 3], [6, 7]]))
|
||||
assert_equal(B[1, 0, 1, 1], 13)
|
||||
|
||||
|
||||
def test_view_as_blocks_3D_array():
|
||||
A = np.arange(4 * 4 * 6).reshape(4, 4, 6)
|
||||
B = view_as_blocks(A, (1, 2, 2))
|
||||
assert_equal(B.shape, (4, 2, 3, 1, 2, 2))
|
||||
assert_equal(
|
||||
B[2:, 0, 2], np.array([[[[52, 53], [58, 59]]], [[[76, 77], [82, 83]]]])
|
||||
)
|
||||
|
||||
|
||||
def test_view_as_windows_input_not_array():
|
||||
A = [1, 2, 3, 4, 5]
|
||||
with testing.raises(TypeError):
|
||||
view_as_windows(A, (2,))
|
||||
|
||||
|
||||
def test_view_as_windows_wrong_window_dimension():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_windows(A, (2, 2))
|
||||
|
||||
|
||||
def test_view_as_windows_negative_window_length():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_windows(A, (-1,))
|
||||
|
||||
|
||||
def test_view_as_windows_window_too_large():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_windows(A, (11,))
|
||||
|
||||
|
||||
def test_view_as_windows_step_below_one():
|
||||
A = np.arange(10)
|
||||
with testing.raises(ValueError):
|
||||
view_as_windows(A, (11,), step=0.9)
|
||||
|
||||
|
||||
def test_view_as_windows_1D():
|
||||
A = np.arange(10)
|
||||
window_shape = (3,)
|
||||
B = view_as_windows(A, window_shape)
|
||||
assert_equal(
|
||||
B,
|
||||
np.array(
|
||||
[
|
||||
[0, 1, 2],
|
||||
[1, 2, 3],
|
||||
[2, 3, 4],
|
||||
[3, 4, 5],
|
||||
[4, 5, 6],
|
||||
[5, 6, 7],
|
||||
[6, 7, 8],
|
||||
[7, 8, 9],
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_view_as_windows_2D():
|
||||
A = np.arange(5 * 4).reshape(5, 4)
|
||||
window_shape = (4, 3)
|
||||
B = view_as_windows(A, window_shape)
|
||||
assert_equal(B.shape, (2, 2, 4, 3))
|
||||
assert_equal(
|
||||
B,
|
||||
np.array(
|
||||
[
|
||||
[
|
||||
[[0, 1, 2], [4, 5, 6], [8, 9, 10], [12, 13, 14]],
|
||||
[[1, 2, 3], [5, 6, 7], [9, 10, 11], [13, 14, 15]],
|
||||
],
|
||||
[
|
||||
[[4, 5, 6], [8, 9, 10], [12, 13, 14], [16, 17, 18]],
|
||||
[[5, 6, 7], [9, 10, 11], [13, 14, 15], [17, 18, 19]],
|
||||
],
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def test_view_as_windows_with_skip():
|
||||
A = np.arange(20).reshape((5, 4))
|
||||
B = view_as_windows(A, 2, step=2)
|
||||
assert_equal(
|
||||
B,
|
||||
[
|
||||
[[[0, 1], [4, 5]], [[2, 3], [6, 7]]],
|
||||
[[[8, 9], [12, 13]], [[10, 11], [14, 15]]],
|
||||
],
|
||||
)
|
||||
|
||||
C = view_as_windows(A, 2, step=4)
|
||||
assert_equal(C.shape, (1, 1, 2, 2))
|
||||
|
||||
|
||||
def test_views_non_contiguous():
|
||||
A = np.arange(16).reshape((4, 4))
|
||||
A = A[::2, :]
|
||||
|
||||
res_b = view_as_blocks(A, (2, 2))
|
||||
res_w = view_as_windows(A, (2, 2))
|
||||
print(res_b)
|
||||
print(res_w)
|
||||
|
||||
expected_b = [[[[0, 1], [8, 9]], [[2, 3], [10, 11]]]]
|
||||
|
||||
expected_w = [[[[0, 1], [8, 9]], [[1, 2], [9, 10]], [[2, 3], [10, 11]]]]
|
||||
|
||||
assert_equal(res_b, expected_b)
|
||||
assert_equal(res_w, expected_w)
|
||||
|
||||
|
||||
def test_view_as_windows_step_tuple():
|
||||
A = np.arange(24).reshape((6, 4))
|
||||
B = view_as_windows(A, (3, 2), step=3)
|
||||
assert B.shape == (2, 1, 3, 2)
|
||||
assert B.size != A.size
|
||||
|
||||
C = view_as_windows(A, (3, 2), step=(3, 2))
|
||||
assert C.shape == (2, 2, 3, 2)
|
||||
assert C.size == A.size
|
||||
|
||||
assert_equal(
|
||||
C,
|
||||
[
|
||||
[[[0, 1], [4, 5], [8, 9]], [[2, 3], [6, 7], [10, 11]]],
|
||||
[[[12, 13], [16, 17], [20, 21]], [[14, 15], [18, 19], [22, 23]]],
|
||||
],
|
||||
)
|
||||
63
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_slice_along_axes.py
vendored
Normal file
63
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_slice_along_axes.py
vendored
Normal file
@@ -0,0 +1,63 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from skimage.util import slice_along_axes
|
||||
|
||||
|
||||
rng = np.random.default_rng()
|
||||
|
||||
|
||||
def test_2d_crop_0():
|
||||
data = rng.random((50, 50))
|
||||
out = slice_along_axes(data, [(0, 25)])
|
||||
np.testing.assert_array_equal(out, data[:25, :])
|
||||
|
||||
|
||||
def test_2d_crop_1():
|
||||
data = rng.random((50, 50))
|
||||
out = slice_along_axes(data, [(0, 25), (0, 10)])
|
||||
np.testing.assert_array_equal(out, data[:25, :10])
|
||||
|
||||
|
||||
def test_2d_crop_2():
|
||||
data = rng.random((50, 50))
|
||||
out = slice_along_axes(data, [(0, 25), (0, 30)], axes=[1, 0])
|
||||
np.testing.assert_array_equal(out, data[:30, :25])
|
||||
|
||||
|
||||
def test_2d_negative():
|
||||
data = rng.random((50, 50))
|
||||
out = slice_along_axes(data, [(5, -5), (6, -6)])
|
||||
np.testing.assert_array_equal(out, data[5:-5, 6:-6])
|
||||
|
||||
|
||||
def test_copy():
|
||||
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||||
out_without_copy = slice_along_axes(data, [(0, 3)], axes=[1], copy=False)
|
||||
out_copy = slice_along_axes(data, [(0, 3)], axes=[0], copy=True)
|
||||
assert out_without_copy.base is data
|
||||
assert out_copy.base is not data
|
||||
|
||||
|
||||
def test_nd_crop():
|
||||
data = rng.random((50, 50, 50))
|
||||
out = slice_along_axes(data, [(0, 25)], axes=[2])
|
||||
np.testing.assert_array_equal(out, data[:, :, :25])
|
||||
|
||||
|
||||
def test_axes_invalid():
|
||||
data = np.empty((2, 3))
|
||||
with pytest.raises(ValueError):
|
||||
slice_along_axes(data, [(0, 3)], axes=[2])
|
||||
|
||||
|
||||
def test_axes_limit_invalid():
|
||||
data = np.empty((50, 50))
|
||||
with pytest.raises(ValueError):
|
||||
slice_along_axes(data, [(0, 51)], axes=[0])
|
||||
|
||||
|
||||
def test_too_many_axes():
|
||||
data = np.empty((10, 10))
|
||||
with pytest.raises(ValueError):
|
||||
slice_along_axes(data, [(0, 1), (0, 1), (0, 1)])
|
||||
38
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_unique_rows.py
vendored
Normal file
38
.CondaPkg/env/Lib/site-packages/skimage/util/tests/test_unique_rows.py
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
import numpy as np
|
||||
from skimage.util import unique_rows
|
||||
from skimage._shared import testing
|
||||
from skimage._shared.testing import assert_equal
|
||||
|
||||
|
||||
def test_discontiguous_array():
|
||||
ar = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]], np.uint8)
|
||||
ar = ar[::2]
|
||||
ar_out = unique_rows(ar)
|
||||
desired_ar_out = np.array([[1, 0, 1]], np.uint8)
|
||||
assert_equal(ar_out, desired_ar_out)
|
||||
|
||||
|
||||
def test_uint8_array():
|
||||
ar = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]], np.uint8)
|
||||
ar_out = unique_rows(ar)
|
||||
desired_ar_out = np.array([[0, 1, 0], [1, 0, 1]], np.uint8)
|
||||
assert_equal(ar_out, desired_ar_out)
|
||||
|
||||
|
||||
def test_float_array():
|
||||
ar = np.array([[1.1, 0.0, 1.1], [0.0, 1.1, 0.0], [1.1, 0.0, 1.1]], float)
|
||||
ar_out = unique_rows(ar)
|
||||
desired_ar_out = np.array([[0.0, 1.1, 0.0], [1.1, 0.0, 1.1]], float)
|
||||
assert_equal(ar_out, desired_ar_out)
|
||||
|
||||
|
||||
def test_1d_array():
|
||||
ar = np.array([1, 0, 1, 1], np.uint8)
|
||||
with testing.raises(ValueError):
|
||||
unique_rows(ar)
|
||||
|
||||
|
||||
def test_3d_array():
|
||||
ar = np.arange(8).reshape((2, 2, 2))
|
||||
with testing.raises(ValueError):
|
||||
unique_rows(ar)
|
||||
Reference in New Issue
Block a user