Handle warnings in several packages

Start handling warnings in data, exposure, and draw

Add a known_warning decorator and suppress warnings in color pkg

Use the existing all_warnings context manager

Raise warnings in data

Raise warnings in draw

Raise warnings in exposure

Suppress warnings in exposure tests

Add comments about warning suppressions

Raise warnings in feature

Fix warnings in filter package

Add warning handling to graph

Handle warnings in io package
This commit is contained in:
Steven Silvester
2014-12-23 16:47:41 -06:00
parent 874d68ba3f
commit 9e8f91930e
12 changed files with 91 additions and 42 deletions
+21 -11
View File
@@ -9,6 +9,7 @@ from skimage import (
data, io, img_as_uint, img_as_float, img_as_int, img_as_ubyte)
from numpy import testing
import numpy as np
from skimage._shared.utils import all_warnings
SKIP_RE = re.compile("(\s*>>>.*?)(\s*)#\s*skip\s+if\s+(.*)$")
@@ -115,20 +116,25 @@ def color_check(plugin, fmt='png'):
testing.assert_allclose(img2.astype(np.uint8), r2)
img3 = img_as_float(img)
r3 = roundtrip(img3, plugin, fmt)
with all_warnings(): # precision loss
r3 = roundtrip(img3, plugin, fmt)
testing.assert_allclose(r3, img)
img4 = img_as_int(img)
with all_warnings(): # precision loss
img4 = img_as_int(img)
if fmt.lower() in (('tif', 'tiff')):
img4 -= 100
r4 = roundtrip(img4, plugin, fmt)
with all_warnings(): # sign loss
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img4)
else:
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img_as_ubyte(img4))
with all_warnings(): # sign loss
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img_as_ubyte(img4))
img5 = img_as_uint(img)
r5 = roundtrip(img5, plugin, fmt)
with all_warnings(): # precision loss
r5 = roundtrip(img5, plugin, fmt)
testing.assert_allclose(r5, img)
@@ -147,20 +153,24 @@ def mono_check(plugin, fmt='png'):
testing.assert_allclose(img2.astype(np.uint8), r2)
img3 = img_as_float(img)
r3 = roundtrip(img3, plugin, fmt)
with all_warnings(): # precision loss
r3 = roundtrip(img3, plugin, fmt)
if r3.dtype.kind == 'f':
testing.assert_allclose(img3, r3)
else:
testing.assert_allclose(r3, img_as_uint(img))
img4 = img_as_int(img)
with all_warnings(): # precision loss
img4 = img_as_int(img)
if fmt.lower() in (('tif', 'tiff')):
img4 -= 100
r4 = roundtrip(img4, plugin, fmt)
with all_warnings(): # sign loss
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img4)
else:
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img_as_uint(img4))
with all_warnings(): # sign loss
r4 = roundtrip(img4, plugin, fmt)
testing.assert_allclose(r4, img_as_uint(img4))
img5 = img_as_uint(img)
r5 = roundtrip(img5, plugin, fmt)
+2
View File
@@ -0,0 +1,2 @@
import warnings
warnings.simplefilter('error')
+2
View File
@@ -0,0 +1,2 @@
import warnings
warnings.simplefilter('error')
+28 -13
View File
@@ -3,15 +3,22 @@ import numpy as np
from numpy.testing import run_module_suite, assert_equal, assert_raises
import skimage
from skimage import img_as_ubyte, img_as_uint, img_as_float
from skimage import img_as_ubyte, img_as_float
from skimage import data, util, morphology
from skimage.morphology import cmorph, disk
from skimage.filters import rank
from skimage._shared.utils import all_warnings
np.random.seed(0)
def test_all():
with all_warnings(): # precision loss
check_all()
def check_all():
image = np.random.rand(25, 25)
selem = morphology.disk(1)
refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz"))
@@ -151,8 +158,9 @@ def test_bitdepth():
for i in range(5):
image = np.ones((100, 100), dtype=np.uint16) * 255 * 2 ** i
r = rank.mean_percentile(image=image, selem=elem, mask=mask,
out=out, shift_x=0, shift_y=0, p0=.1, p1=.9)
with all_warnings(): # bit depth
rank.mean_percentile(image=image, selem=elem, mask=mask,
out=out, shift_x=0, shift_y=0, p0=.1, p1=.9)
def test_population():
@@ -261,7 +269,8 @@ def test_compare_ubyte_vs_float():
for method in methods:
func = getattr(rank, method)
out_u = func(image_uint, disk(3))
out_f = func(image_float, disk(3))
with all_warnings(): # precision loss
out_f = func(image_float, disk(3))
assert_equal(out_u, out_f)
@@ -273,9 +282,10 @@ def test_compare_8bit_unsigned_vs_signed():
image = img_as_ubyte(data.camera())
image[image > 127] = 0
image_s = image.astype(np.int8)
image_u = img_as_ubyte(image_s)
with all_warnings(): # precision loss
image_u = img_as_ubyte(image_s)
assert_equal(image_u, img_as_ubyte(image_s))
assert_equal(image_u, img_as_ubyte(image_s))
methods = ['autolevel', 'bottomhat', 'equalize', 'gradient', 'maximum',
'mean', 'subtract_mean', 'median', 'minimum', 'modal',
@@ -283,8 +293,10 @@ def test_compare_8bit_unsigned_vs_signed():
for method in methods:
func = getattr(rank, method)
out_u = func(image_u, disk(3))
out_s = func(image_s, disk(3))
with all_warnings(): # sign loss
out_u = func(image_u, disk(3))
out_s = func(image_s, disk(3))
assert_equal(out_u, out_s)
@@ -474,10 +486,12 @@ def test_entropy():
selem = np.ones((64, 64), dtype=np.uint8)
data = np.tile(
np.reshape(np.arange(4096), (64, 64)), (2, 2)).astype(np.uint16)
assert(np.max(rank.entropy(data, selem)) == 12)
with all_warnings(): # bitdepth
assert(np.max(rank.entropy(data, selem)) == 12)
# make sure output is of dtype double
out = rank.entropy(data, np.ones((16, 16), dtype=np.uint8))
with all_warnings(): # bitdepth
out = rank.entropy(data, np.ones((16, 16), dtype=np.uint8))
assert out.dtype == np.double
@@ -508,9 +522,10 @@ def test_16bit():
for bitdepth in range(17):
value = 2 ** bitdepth - 1
image[10, 10] = value
assert rank.minimum(image, selem)[10, 10] == 0
assert rank.maximum(image, selem)[10, 10] == value
assert rank.mean(image, selem)[10, 10] == int(value / selem.size)
with all_warnings(): # bitdepth
assert rank.minimum(image, selem)[10, 10] == 0
assert rank.maximum(image, selem)[10, 10] == value
assert rank.mean(image, selem)[10, 10] == int(value / selem.size)
def test_bilateral():
+2
View File
@@ -0,0 +1,2 @@
import warnings
warnings.simplefilter('error')
+3 -1
View File
@@ -1,5 +1,6 @@
import numpy as np
from skimage.filters._gaussian import gaussian_filter
from skimage._shared.utils import all_warnings
def test_null_sigma():
@@ -25,7 +26,8 @@ def test_multichannel():
assert np.allclose([a[..., i].mean() for i in range(3)],
[gaussian_rgb_a[..., i].mean() for i in range(3)])
# Test multichannel = None
gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect')
with all_warnings(): # multichannel
gaussian_rgb_a = gaussian_filter(a, sigma=1, mode='reflect')
# Check that the mean value is conserved in each channel
# (color channels are not mixed together)
assert np.allclose([a[..., i].mean() for i in range(3)],
+3 -3
View File
@@ -121,7 +121,7 @@ def threshold_otsu(image, nbins=256):
>>> thresh = threshold_otsu(image)
>>> binary = image <= thresh
"""
hist, bin_centers = histogram(image, nbins)
hist, bin_centers = histogram(image.flatten(), nbins)
hist = hist.astype(float)
# class probabilities for all possible thresholds
@@ -176,7 +176,7 @@ def threshold_yen(image, nbins=256):
>>> thresh = threshold_yen(image)
>>> binary = image <= thresh
"""
hist, bin_centers = histogram(image, nbins)
hist, bin_centers = histogram(image.flatten(), nbins)
# On blank images (e.g. filled with 0) with int dtype, `histogram()`
# returns `bin_centers` containing only one value. Speed up with it.
if bin_centers.size == 1:
@@ -246,7 +246,7 @@ def threshold_isodata(image, nbins=256, return_all=False):
>>> binary = image > thresh
"""
hist, bin_centers = histogram(image, nbins)
hist, bin_centers = histogram(image.flatten(), nbins)
# image only contains one unique value
if len(bin_centers) == 1:
+2
View File
@@ -0,0 +1,2 @@
import warnings
warnings.simplefilter('error')
+5 -5
View File
@@ -102,7 +102,7 @@ def pil_to_ndarray(im, dtype=None, img_num=None):
dtype = '>u2' if im.mode.endswith('B') else '<u2'
if 'S' in im.mode:
dtype = dtype.replace('u', 'i')
frame = np.fromstring(frame.tostring(), dtype)
frame = np.fromstring(frame.tobytes(), dtype)
frame.shape = shape[::-1]
else:
@@ -179,15 +179,15 @@ def ndarray_to_pil(arr, format_str=None):
if arr.ndim == 2:
im = Image.new(mode_base, arr.T.shape)
im.fromstring(arr.tostring(), 'raw', mode)
im.frombytes(arr.tobytes(), 'raw', mode)
else:
try:
im = Image.frombytes(mode, (arr.shape[1], arr.shape[0]),
arr.tostring())
arr.tobytes())
except AttributeError:
im = Image.fromstring(mode, (arr.shape[1], arr.shape[0]),
arr.tostring())
im = Image.frombytes(mode, (arr.shape[1], arr.shape[0]),
arr.tobytes())
return im
+2
View File
@@ -0,0 +1,2 @@
import warnings
warnings.simplefilter('error')
+5 -2
View File
@@ -10,6 +10,7 @@ from skimage import data_dir
from skimage.io import (imread, imsave, use_plugin, reset_plugins,
Image as ioImage)
from skimage._shared.testing import mono_check, color_check
from skimage._shared.utils import all_warnings
from six import BytesIO
@@ -143,7 +144,8 @@ def test_imsave_filelike():
s = BytesIO()
# save to file-like object
imsave(s, image)
with all_warnings(): # precision loss
imsave(s, image)
# read from file-like object
s.seek(0)
@@ -155,7 +157,8 @@ def test_imsave_filelike():
def test_imexport_imimport():
shape = (2, 2)
image = np.zeros(shape)
pil_image = ndarray_to_pil(image)
with all_warnings(): # precision loss
pil_image = ndarray_to_pil(image)
out = pil_to_ndarray(pil_image)
assert out.shape == shape
+16 -7
View File
@@ -1,4 +1,5 @@
from skimage.io._plugins.util import prepare_for_display, WindowManager
from skimage._shared.utils import all_warnings
from numpy.testing import *
import numpy as np
@@ -8,31 +9,39 @@ np.random.seed(0)
class TestPrepareForDisplay:
def test_basic(self):
prepare_for_display(np.random.rand(10, 10))
with all_warnings(): # precision loss
prepare_for_display(np.random.rand(10, 10))
def test_dtype(self):
x = prepare_for_display(np.random.rand(10, 15))
with all_warnings(): # precision loss
x = prepare_for_display(np.random.rand(10, 15))
assert x.dtype == np.dtype(np.uint8)
def test_grey(self):
x = prepare_for_display(np.arange(12, dtype=float).reshape((4, 3)) / 11)
with all_warnings(): # precision loss
tmp = np.arange(12, dtype=float).reshape((4, 3)) / 11
x = prepare_for_display(tmp)
assert_array_equal(x[..., 0], x[..., 2])
assert x[0, 0, 0] == 0
assert x[3, 2, 0] == 255
def test_colour(self):
prepare_for_display(np.random.rand(10, 10, 3))
with all_warnings(): # precision loss
prepare_for_display(np.random.rand(10, 10, 3))
def test_alpha(self):
prepare_for_display(np.random.rand(10, 10, 4))
with all_warnings(): # precision loss
prepare_for_display(np.random.rand(10, 10, 4))
@raises(ValueError)
def test_wrong_dimensionality(self):
prepare_for_display(np.random.rand(10, 10, 1, 1))
with all_warnings(): # precision loss
prepare_for_display(np.random.rand(10, 10, 1, 1))
@raises(ValueError)
def test_wrong_depth(self):
prepare_for_display(np.random.rand(10, 10, 5))
with all_warnings(): # precision loss
prepare_for_display(np.random.rand(10, 10, 5))
class TestWindowManager: