Skip to content

Commit

Permalink
warnings filtered and tests for patch iterator added
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Maetschke committed Oct 16, 2017
1 parent 797b71b commit beba889
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nutsml/imageutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def patch_iter(image, shape=(3, 3), stride=1):
"""
# view_as_windows requires contiguous array, which we ensure here
if not image.flags['C_CONTIGUOUS']:
warn('image is not contiguous array')
warn('Image is not contiguous and will be copied!')
image = np.ascontiguousarray(image)

is_gray = image.ndim == 2
Expand Down
21 changes: 20 additions & 1 deletion tests/nutsml/test_imageutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy.testing as nt
import matplotlib.patches as plp

from warnings import warn
from glob import glob
from PIL import ImageEnhance as ie

Expand Down Expand Up @@ -358,7 +359,7 @@ def test_extract_patch():


def test_patch_iter():
img = np.asfortranarray(np.reshape(np.arange(12), (3, 4)))
img = np.reshape(np.arange(12), (3, 4))

patches = list(ni.patch_iter(img, (2, 2), 2))
expected = [np.array([[0, 1], [4, 5]]),
Expand All @@ -379,6 +380,24 @@ def test_patch_iter():
nt.assert_allclose(p, e)


@pytest.mark.filterwarnings('ignore:Image is not contiguous and will be copied!')
def test_patch_iter_notcontiguous():
img = np.asfortranarray(np.reshape(np.arange(12), (3, 4)))

patches = list(ni.patch_iter(img, (2, 2), 2))
expected = [np.array([[0, 1], [4, 5]]),
np.array([[2, 3], [6, 7]])]
for p, e in zip(patches, expected):
nt.assert_allclose(p, e)


def test_patch_iter_warning():
with pytest.warns(UserWarning):
img = np.asfortranarray(np.reshape(np.arange(12), (3, 4)))
list(ni.patch_iter(img, (2, 2), 2))
warn("Image is not contiguous and will be copied!", UserWarning)


def test_patch_iter_3channel():
img = np.reshape(np.arange(12 * 3), (3, 4, 3))

Expand Down

0 comments on commit beba889

Please sign in to comment.