Skip to content

Commit

Permalink
doctests fixed for py 3k
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Maetschke committed Jun 6, 2017
1 parent a8efeac commit 5a051f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
8 changes: 4 additions & 4 deletions nutsml/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ def SplitRandom(iterable, ratio=0.7, constraint=None, rand=rnd.Random(0)):
>>> fix=rnd.Random(0) # fixed random numbers for testing
>>> train, val = range(10) >> SplitRandom(rand=fix, ratio=0.7)
>>> sorted(train), sorted(val)
>>> sorted(train), sorted(val) # doctest: +SKIP
([0, 1, 2, 4, 5, 7, 9], [3, 6, 8])
>>> train, val, test = range(10) >> SplitRandom(rand=fix, ratio=(0.6, 0.3, 0.1))
>>> sorted(train), sorted(val), sorted(test)
>>> sorted(train), sorted(val), sorted(test) # doctest: +SKIP
([0, 1, 3, 4, 5, 6], [2, 7, 8], [9])
>>> data = zip('aabbccddee', range(10))
>>> same_letter = lambda (c,i): c
>>> train, val = data >> SplitRandom(rand=fix, ratio=0.6, constraint=same_letter)
>>> sorted(train)
>>> sorted(train) # doctest: +SKIP
[('a', 0), ('a', 1), ('c', 4), ('c', 5), ('e', 8), ('e', 9)]
>>> sorted(val)
>>> sorted(val) # doctest: +SKIP
[('b', 2), ('b', 3), ('d', 6), ('d', 7)]
:param iterable iterable: Iterable over anything. Will be consumed!
Expand Down
10 changes: 4 additions & 6 deletions nutsml/imageutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.. module:: imageutil
:synopsis: Basic image processing utilities
"""
from __future__ import absolute_import
from __future__ import absolute_import, print_function

import numpy as np
import PIL as pil
Expand Down Expand Up @@ -614,7 +614,6 @@ def patch_iter(image, shape=(3, 3), stride=1):
starting in the left upper corner and then row-wise.
Image can be gray-scale (no third channel dim) or color.
>>> from __future__ import print_function
>>> import numpy as np
>>> img = np.reshape(np.arange(12), (3, 4))
>>> for p in patch_iter(img, (2, 2), 2):
Expand Down Expand Up @@ -772,7 +771,6 @@ def sample_pn_patches(image, mask, pshape, npos, nneg, pos=255, neg=0):
image and mask and that a patch a the same position is extracted from
the image and the mask.
>>> from __future__ import print_function
>>> np.random.seed(0) # just to ensure consistent doctest
>>> mask = np.zeros((3, 4), dtype='uint8')
>>> img = np.reshape(np.arange(12, dtype='uint8'), (3, 4))
Expand Down Expand Up @@ -825,14 +823,14 @@ def annotation2coords(image, annotation):
Annotation regions can exceed the image dimensions and will be clipped.
Note that annotation is in x,y order while output is r,c (row, col).
>>> from __future__ import print_function
>>> import numpy as np
>>> img = np.zeros((5, 5), dtype='uint8')
>>> anno = ('point', ((1, 1), (1, 2)))
>>> for rr, cc in annotation2coords(img, anno):
... print(list(rr), list(cc))
([1], [1])
([2], [1])
[1] [1]
[2] [1]
:param ndarray image: Image
:param annotation annotation: Annotation of an image region such as
Expand Down
31 changes: 14 additions & 17 deletions nutsml/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.. module:: transformer
:synopsis: Data and image transformations
"""
from __future__ import print_function

import numpy as np
import os.path as path
Expand Down Expand Up @@ -245,7 +246,6 @@ def RegularImagePatches(iterable, imagecols, pshape, stride):
Extract patches in a regular grid from images.
>>> from __future__ import print_function
>>> import numpy as np
>>> img = np.reshape(np.arange(12), (3, 4))
>>> samples = [(img, 0)]
Expand Down Expand Up @@ -285,17 +285,16 @@ def RandomImagePatches(iterable, imagecols, pshape, npatches):
Extract patches at random locations from images.
>>> from __future__ import print_function
>>> import numpy as np
>>> np.random.seed(0) # just to ensure stable doctest
>>> img = np.reshape(np.arange(30), (5, 6))
>>> samples = [(img, 0)]
>>> getpatches = RandomImagePatches(0, (2, 3), 3)
>>> for (p, l) in samples >> getpatches:
... print(p.tolist(), l)
([[7, 8, 9], [13, 14, 15]], 0)
([[8, 9, 10], [14, 15, 16]], 0)
([[8, 9, 10], [14, 15, 16]], 0)
[[7, 8, 9], [13, 14, 15]] 0
[[8, 9, 10], [14, 15, 16]] 0
[[8, 9, 10], [14, 15, 16]] 0
:param iterable iterable: Samples with images
:param int|tuple imagecols: Indices of sample columns that contain
Expand Down Expand Up @@ -332,7 +331,7 @@ def ImagePatchesByMask(iterable, imagecol, maskcol, pshape, npos,
mask (corresponding to the input image) and is negative for value 'neg'
The mask must be of same size as image.
>>> from __future__ import print_function
>>>
>>> import numpy as np
>>> np.random.seed(0) # just to ensure stable doctest
>>> img = np.reshape(np.arange(25), (5, 5))
Expand All @@ -342,17 +341,17 @@ def ImagePatchesByMask(iterable, imagecol, maskcol, pshape, npos,
>>> getpatches = ImagePatchesByMask(0, 1, (3, 3), 2, 1)
>>> for (p, l) in samples >> getpatches:
... print(p.tolist(), l)
([[10, 11, 12], [15, 16, 17], [20, 21, 22]], 0)
([[12, 13, 14], [17, 18, 19], [22, 23, 24]], 1)
([[6, 7, 8], [11, 12, 13], [16, 17, 18]], 1)
[[10, 11, 12], [15, 16, 17], [20, 21, 22]] 0
[[12, 13, 14], [17, 18, 19], [22, 23, 24]] 1
[[6, 7, 8], [11, 12, 13], [16, 17, 18]] 1
>>> np.random.seed(0) # just to ensure stable doctest
>>> patches = ImagePatchesByMask(0, 1, (3, 3), 1, 1, retlabel=False)
>>> for (p, m) in samples >> getpatches:
... print(p.tolist(), l)
([[10, 11, 12], [15, 16, 17], [20, 21, 22]], 1)
([[12, 13, 14], [17, 18, 19], [22, 23, 24]], 1)
([[6, 7, 8], [11, 12, 13], [16, 17, 18]], 1)
[[10, 11, 12], [15, 16, 17], [20, 21, 22]] 1
[[12, 13, 14], [17, 18, 19], [22, 23, 24]] 1
[[6, 7, 8], [11, 12, 13], [16, 17, 18]] 1
:param iterable iterable: Samples with images
:param int imagecol: Index of sample column that contain image
Expand Down Expand Up @@ -394,7 +393,6 @@ def ImagePatchesByAnnotation(iterable, imagecol, annocol, pshape, npos,
A patch is positive if its center point is within the annotated region
and is negative otherwise
>>> from __future__ import print_function
>>> import numpy as np
>>> np.random.seed(0) # just to ensure stable doctest
>>> img = np.reshape(np.arange(25), (5, 5))
Expand All @@ -404,9 +402,9 @@ def ImagePatchesByAnnotation(iterable, imagecol, annocol, pshape, npos,
>>> getpatches = ImagePatchesByAnnotation(0, 1, (3, 3), 1, 1)
>>> for (p, l) in samples >> getpatches:
... print(p.tolist(), l)
([[12, 13, 14], [17, 18, 19], [22, 23, 24]], 0)
([[11, 12, 13], [16, 17, 18], [21, 22, 23]], 1)
([[7, 8, 9], [12, 13, 14], [17, 18, 19]], 1)
[[12, 13, 14], [17, 18, 19], [22, 23, 24]] 0
[[11, 12, 13], [16, 17, 18], [21, 22, 23]] 1
[[7, 8, 9], [12, 13, 14], [17, 18, 19]] 1
:param iterable iterable: Samples with images
:param int imagecol: Index of sample column that contain image
Expand Down Expand Up @@ -446,7 +444,6 @@ def ImageAnnotationToMask(iterable, imagecol, annocol):
('rect', ((x, y, w, h), ...))
('polyline', (((x, y), (x, y), ...), ...))
>>> from __future__ import print_function
>>> import numpy as np
>>> from nutsflow import Collect
Expand Down

0 comments on commit 5a051f6

Please sign in to comment.