Merge pull request #620 from scikit-image/2013

Python 3 support without 2to3.
This commit is contained in:
Tony S Yu
2013-06-29 12:15:08 -07:00
22 changed files with 507 additions and 62 deletions
+1 -1
View File
@@ -35,4 +35,4 @@ script:
# Change back to repository root directory and run all doc examples
- cd ..
- for f in doc/examples/*.py; do $PYTHON "$f"; if [ $? -ne 0 ]; then exit 1; fi done
- flake8 --exit-zero skimage doc/examples viewer_examples
- flake8 --exit-zero --exclude=test_*,six.py skimage doc/examples viewer_examples
+1 -4
View File
@@ -30,10 +30,7 @@ import sys
import re
import setuptools
from numpy.distutils.core import setup
try:
from distutils.command.build_py import build_py_2to3 as build_py
except ImportError:
from distutils.command.build_py import build_py
from distutils.command.build_py import build_py
def configuration(parent_package='', top_path=None):
+423
View File
@@ -0,0 +1,423 @@
"""Utilities for writing code that runs on Python 2 and 3"""
# Copyright (c) 2010-2013 Benjamin Peterson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import operator
import sys
import types
__author__ = "Benjamin Peterson <benjamin@python.org>"
__version__ = "1.3.0"
# Useful for very coarse version differentiation.
PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
integer_types = int,
class_types = type,
text_type = str
binary_type = bytes
MAXSIZE = sys.maxsize
else:
string_types = basestring,
integer_types = (int, long)
class_types = (type, types.ClassType)
text_type = unicode
binary_type = str
if sys.platform.startswith("java"):
# Jython always uses 32 bits.
MAXSIZE = int((1 << 31) - 1)
else:
# It's possible to have sizeof(long) != sizeof(Py_ssize_t).
class X(object):
def __len__(self):
return 1 << 31
try:
len(X())
except OverflowError:
# 32-bit
MAXSIZE = int((1 << 31) - 1)
else:
# 64-bit
MAXSIZE = int((1 << 63) - 1)
del X
def _add_doc(func, doc):
"""Add documentation to a function."""
func.__doc__ = doc
def _import_module(name):
"""Import module, returning the module after the last dot."""
__import__(name)
return sys.modules[name]
class _LazyDescr(object):
def __init__(self, name):
self.name = name
def __get__(self, obj, tp):
result = self._resolve()
setattr(obj, self.name, result)
# This is a bit ugly, but it avoids running this again.
delattr(tp, self.name)
return result
class MovedModule(_LazyDescr):
def __init__(self, name, old, new=None):
super(MovedModule, self).__init__(name)
if PY3:
if new is None:
new = name
self.mod = new
else:
self.mod = old
def _resolve(self):
return _import_module(self.mod)
class MovedAttribute(_LazyDescr):
def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None):
super(MovedAttribute, self).__init__(name)
if PY3:
if new_mod is None:
new_mod = name
self.mod = new_mod
if new_attr is None:
if old_attr is None:
new_attr = name
else:
new_attr = old_attr
self.attr = new_attr
else:
self.mod = old_mod
if old_attr is None:
old_attr = name
self.attr = old_attr
def _resolve(self):
module = _import_module(self.mod)
return getattr(module, self.attr)
class _MovedItems(types.ModuleType):
"""Lazy loading of moved objects"""
_moved_attributes = [
MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"),
MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"),
MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"),
MovedAttribute("map", "itertools", "builtins", "imap", "map"),
MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"),
MovedAttribute("reload_module", "__builtin__", "imp", "reload"),
MovedAttribute("reduce", "__builtin__", "functools"),
MovedAttribute("StringIO", "StringIO", "io"),
MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"),
MovedAttribute("zip", "itertools", "builtins", "izip", "zip"),
MovedModule("builtins", "__builtin__"),
MovedModule("configparser", "ConfigParser"),
MovedModule("copyreg", "copy_reg"),
MovedModule("http_cookiejar", "cookielib", "http.cookiejar"),
MovedModule("http_cookies", "Cookie", "http.cookies"),
MovedModule("html_entities", "htmlentitydefs", "html.entities"),
MovedModule("html_parser", "HTMLParser", "html.parser"),
MovedModule("http_client", "httplib", "http.client"),
MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"),
MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"),
MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"),
MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"),
MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"),
MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"),
MovedModule("cPickle", "cPickle", "pickle"),
MovedModule("queue", "Queue"),
MovedModule("reprlib", "repr"),
MovedModule("socketserver", "SocketServer"),
MovedModule("tkinter", "Tkinter"),
MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"),
MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"),
MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"),
MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"),
MovedModule("tkinter_tix", "Tix", "tkinter.tix"),
MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"),
MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"),
MovedModule("tkinter_colorchooser", "tkColorChooser",
"tkinter.colorchooser"),
MovedModule("tkinter_commondialog", "tkCommonDialog",
"tkinter.commondialog"),
MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"),
MovedModule("tkinter_font", "tkFont", "tkinter.font"),
MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"),
MovedModule("tkinter_tksimpledialog", "tkSimpleDialog",
"tkinter.simpledialog"),
MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"),
MovedModule("winreg", "_winreg"),
]
for attr in _moved_attributes:
setattr(_MovedItems, attr.name, attr)
del attr
moves = sys.modules[__name__ + ".moves"] = _MovedItems("moves")
def add_move(move):
"""Add an item to six.moves."""
setattr(_MovedItems, move.name, move)
def remove_move(name):
"""Remove item from six.moves."""
try:
delattr(_MovedItems, name)
except AttributeError:
try:
del moves.__dict__[name]
except KeyError:
raise AttributeError("no such move, %r" % (name,))
if PY3:
_meth_func = "__func__"
_meth_self = "__self__"
_func_closure = "__closure__"
_func_code = "__code__"
_func_defaults = "__defaults__"
_func_globals = "__globals__"
_iterkeys = "keys"
_itervalues = "values"
_iteritems = "items"
_iterlists = "lists"
else:
_meth_func = "im_func"
_meth_self = "im_self"
_func_closure = "func_closure"
_func_code = "func_code"
_func_defaults = "func_defaults"
_func_globals = "func_globals"
_iterkeys = "iterkeys"
_itervalues = "itervalues"
_iteritems = "iteritems"
_iterlists = "iterlists"
try:
advance_iterator = next
except NameError:
def advance_iterator(it):
return it.next()
next = advance_iterator
try:
callable = callable
except NameError:
def callable(obj):
return any("__call__" in klass.__dict__ for klass in type(obj).__mro__)
if PY3:
def get_unbound_function(unbound):
return unbound
create_bound_method = types.MethodType
Iterator = object
else:
def get_unbound_function(unbound):
return unbound.im_func
def create_bound_method(func, obj):
return types.MethodType(func, obj, obj.__class__)
class Iterator(object):
def next(self):
return type(self).__next__(self)
callable = callable
_add_doc(get_unbound_function,
"""Get the function out of a possibly unbound function""")
get_method_function = operator.attrgetter(_meth_func)
get_method_self = operator.attrgetter(_meth_self)
get_function_closure = operator.attrgetter(_func_closure)
get_function_code = operator.attrgetter(_func_code)
get_function_defaults = operator.attrgetter(_func_defaults)
get_function_globals = operator.attrgetter(_func_globals)
def iterkeys(d, **kw):
"""Return an iterator over the keys of a dictionary."""
return iter(getattr(d, _iterkeys)(**kw))
def itervalues(d, **kw):
"""Return an iterator over the values of a dictionary."""
return iter(getattr(d, _itervalues)(**kw))
def iteritems(d, **kw):
"""Return an iterator over the (key, value) pairs of a dictionary."""
return iter(getattr(d, _iteritems)(**kw))
def iterlists(d, **kw):
"""Return an iterator over the (key, [values]) pairs of a dictionary."""
return iter(getattr(d, _iterlists)(**kw))
if PY3:
def b(s):
return s.encode("latin-1")
def u(s):
return s
unichr = chr
if sys.version_info[1] <= 1:
def int2byte(i):
return bytes((i,))
else:
# This is about 2x faster than the implementation above on 3.2+
int2byte = operator.methodcaller("to_bytes", 1, "big")
byte2int = operator.itemgetter(0)
indexbytes = operator.getitem
iterbytes = iter
import io
StringIO = io.StringIO
BytesIO = io.BytesIO
else:
def b(s):
return s
def u(s):
return unicode(s, "unicode_escape")
unichr = unichr
int2byte = chr
def byte2int(bs):
return ord(bs[0])
def indexbytes(buf, i):
return ord(buf[i])
def iterbytes(buf):
return (ord(byte) for byte in buf)
import StringIO
StringIO = BytesIO = StringIO.StringIO
_add_doc(b, """Byte literal""")
_add_doc(u, """Text literal""")
if PY3:
import builtins
exec_ = getattr(builtins, "exec")
def reraise(tp, value, tb=None):
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
raise value
print_ = getattr(builtins, "print")
del builtins
else:
def exec_(_code_, _globs_=None, _locs_=None):
"""Execute code in a namespace."""
if _globs_ is None:
frame = sys._getframe(1)
_globs_ = frame.f_globals
if _locs_ is None:
_locs_ = frame.f_locals
del frame
elif _locs_ is None:
_locs_ = _globs_
exec("""exec _code_ in _globs_, _locs_""")
exec_("""def reraise(tp, value, tb=None):
raise tp, value, tb
""")
def print_(*args, **kwargs):
"""The new-style print function."""
fp = kwargs.pop("file", sys.stdout)
if fp is None:
return
def write(data):
if not isinstance(data, basestring):
data = str(data)
fp.write(data)
want_unicode = False
sep = kwargs.pop("sep", None)
if sep is not None:
if isinstance(sep, unicode):
want_unicode = True
elif not isinstance(sep, str):
raise TypeError("sep must be None or a string")
end = kwargs.pop("end", None)
if end is not None:
if isinstance(end, unicode):
want_unicode = True
elif not isinstance(end, str):
raise TypeError("end must be None or a string")
if kwargs:
raise TypeError("invalid keyword arguments to print()")
if not want_unicode:
for arg in args:
if isinstance(arg, unicode):
want_unicode = True
break
if want_unicode:
newline = unicode("\n")
space = unicode(" ")
else:
newline = "\n"
space = " "
if sep is None:
sep = space
if end is None:
end = newline
for i, arg in enumerate(args):
if i:
write(sep)
write(arg)
write(end)
_add_doc(reraise, """Reraise an exception.""")
def with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
return meta("NewBase", bases, {})
+14 -14
View File
@@ -1,19 +1,11 @@
import warnings
import functools
import sys
from . import six
__all__ = ['deprecated', 'is_str']
try:
isinstance("", basestring)
def is_str(s):
"""Return True if `s` is a string. Safe for Python 2 and 3."""
return isinstance(s, basestring)
except NameError:
def is_str(s):
"""Return True if `s` is a string. Safe for Python 2 and 3."""
return isinstance(s, str)
__all__ = ['deprecated', 'get_bound_method_class']
class deprecated(object):
@@ -46,10 +38,11 @@ class deprecated(object):
@functools.wraps(func)
def wrapped(*args, **kwargs):
if self.behavior == 'warn':
func_code = six.get_function_code(func)
warnings.warn_explicit(msg,
category=DeprecationWarning,
filename=func.func_code.co_filename,
lineno=func.func_code.co_firstlineno + 1)
filename=func_code.co_filename,
lineno=func_code.co_firstlineno + 1)
elif self.behavior == 'raise':
raise DeprecationWarning(msg)
return func(*args, **kwargs)
@@ -62,3 +55,10 @@ class deprecated(object):
wrapped.__doc__ = doc + '\n\n ' + wrapped.__doc__
return wrapped
def get_bound_method_class(m):
"""Return the class for a bound method.
"""
return m.im_class if sys.version < '3' else m.__self__.__class__
+4 -3
View File
@@ -4,7 +4,8 @@ import itertools
import numpy as np
from skimage import img_as_float
from skimage._shared.utils import is_str
from skimage._shared import six
from skimage._shared.six.moves import zip
from .colorconv import rgb2gray, gray2rgb
from . import rgb_colors
@@ -30,7 +31,7 @@ def _rgb_vector(color):
color : str or array
Color name in `color_dict` or RGB float values between [0, 1].
"""
if is_str(color):
if isinstance(color, six.string_types):
color = color_dict[color]
# slice to handle RGBA colors
return np.array(color[:3]).reshape(1, 3)
@@ -88,7 +89,7 @@ def label2rgb(label, image=None, colors=None, alpha=0.3,
bg_color = _rgb_vector(bg_color)
color_cycle = itertools.chain(bg_color, color_cycle)
for c, i in itertools.izip(color_cycle, labels):
for c, i in zip(color_cycle, labels):
mask = (label == i)
colorized[mask] = c * alpha + colorized[mask] * (1 - alpha)
+3 -2
View File
@@ -15,6 +15,7 @@ import numpy as np
from skimage.io._plugins import call as call_plugin
from skimage.color import rgb2grey
from skimage._shared import six
# Shared image queue
@@ -25,7 +26,7 @@ URL_REGEX = re.compile(r'http://|https://|ftp://|file://|file:\\')
def is_url(filename):
"""Return True if string is an http or ftp path."""
return (isinstance(filename, basestring) and
return (isinstance(filename, six.string_types) and
URL_REGEX.match(filename) is not None)
@@ -220,7 +221,7 @@ def imshow(arr, plugin=None, **plugin_args):
Passed to the given plugin.
"""
if isinstance(arr, basestring):
if isinstance(arr, six.string_types):
arr = call_plugin('imread', arr, plugin=plugin)
return call_plugin('imshow', arr, plugin=plugin, **plugin_args)
+2 -1
View File
@@ -10,6 +10,7 @@ from copy import copy
import numpy as np
from ._io import imread
from .._shared import six
def concatenate_images(ic):
@@ -296,7 +297,7 @@ class ImageCollection(object):
"""
def __init__(self, load_pattern, conserve_memory=True, load_func=None):
"""Load and manage a collection of images."""
if isinstance(load_pattern, basestring):
if isinstance(load_pattern, six.string_types):
load_pattern = load_pattern.split(':')
self._files = []
for pattern in load_pattern:
+2 -3
View File
@@ -12,6 +12,7 @@ from skimage import data_dir
from skimage.io import ImageCollection, MultiImage
from skimage.io.collection import alphanumeric_key
from skimage.io import Image as ioImage
from skimage._shared import six
try:
@@ -21,8 +22,6 @@ except ImportError:
else:
PIL_available = True
if sys.version_info[0] > 2:
basestring = str
class TestAlphanumericKey():
def setUp(self):
@@ -129,7 +128,7 @@ class TestMultiImage():
@skipif(not PIL_available)
def test_files_property(self):
assert isinstance(self.img.filename, basestring)
assert isinstance(self.img.filename, six.string_types)
def set_filename(f):
self.img.filename = f
+1 -17
View File
@@ -1,8 +1,7 @@
import os
from numpy.testing import *
from numpy.testing import assert_array_equal, raises, run_module_suite
import numpy as np
from StringIO import StringIO
import skimage.io as io
from skimage import data_dir
@@ -29,20 +28,5 @@ def test_imread_url():
assert image.shape == (512, 512)
def test_imsave_filelike():
shape = (2, 2)
image = np.zeros(shape)
s = StringIO()
# save to file-like object
io.imsave(s, image)
# read from file-like object
s.seek(0)
out = io.imread(s)
assert out.shape == shape
np.testing.assert_allclose(out, image)
if __name__ == "__main__":
run_module_suite()
+19
View File
@@ -7,6 +7,8 @@ from tempfile import NamedTemporaryFile
from skimage import data_dir
from skimage.io import imread, imsave, use_plugin, reset_plugins
from skimage._shared.six.moves import StringIO
try:
from PIL import Image
@@ -124,5 +126,22 @@ class TestSave:
x = (x * 255).astype(dtype)
yield self.roundtrip, dtype, x
@skipif(not PIL_available)
def test_imsave_filelike():
shape = (2, 2)
image = np.zeros(shape)
s = StringIO()
# save to file-like object
imsave(s, image)
# read from file-like object
s.seek(0)
out = imread(s)
assert out.shape == shape
assert_allclose(out, image)
if __name__ == "__main__":
run_module_suite()
+2 -2
View File
@@ -126,8 +126,8 @@ def find_contours(array, level,
def _take_2(seq):
iterator = iter(seq)
while(True):
n1 = iterator.next()
n2 = iterator.next()
n1 = next(iterator)
n2 = next(iterator)
yield (n1, n2)
+2 -1
View File
@@ -66,7 +66,8 @@ def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
if out.dtype == bool:
selem = nd.generate_binary_structure(ar.ndim, connectivity)
ccs = nd.label(ar, selem)[0]
ccs = np.zeros_like(ar, dtype=np.int32)
nd.label(ar, selem, output=ccs)
else:
ccs = out
+10 -3
View File
@@ -4,6 +4,9 @@ from scipy import ndimage, spatial
from skimage.util import img_as_float
from ._warps_cy import _warp_fast
from skimage._shared.utils import get_bound_method_class
from skimage._shared import six
class GeometricTransform(object):
"""Perform geometric transformations on a set of coordinates.
@@ -1006,12 +1009,16 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1,
# use fast Cython version for specific interpolation orders
if order in range(4) and not map_args:
matrix = None
if inverse_map in HOMOGRAPHY_TRANSFORMS:
matrix = inverse_map._matrix
elif hasattr(inverse_map, '__name__') \
and inverse_map.__name__ == 'inverse' \
and inverse_map.im_class in HOMOGRAPHY_TRANSFORMS:
matrix = np.linalg.inv(inverse_map.im_self._matrix)
and inverse_map.__name__ == 'inverse' \
and get_bound_method_class(inverse_map) in HOMOGRAPHY_TRANSFORMS:
matrix = np.linalg.inv(six.get_method_self(inverse_map)._matrix)
if matrix is not None:
# transform all bands
dims = []
+4 -2
View File
@@ -4,8 +4,10 @@ of an n-dimensional array.
"""
from __future__ import division, absolute_import, print_function
from skimage._shared.six import integer_types
import numpy as np
try:
# Available on 2.x at base, Py3 requires this compatibility import.
# Later versions of NumPy have this for 2.x as well.
@@ -1036,11 +1038,11 @@ def _normalize_shape(narray, shape):
fmt = "Unable to create correctly shaped tuple from %s"
raise ValueError(fmt % (normshp,))
elif (isinstance(shape, (tuple, list))
and isinstance(shape[0], (int, float, long))
and isinstance(shape[0], integer_types + (float,))
and len(shape) == 1):
normshp = ((shape[0], shape[0]), ) * shapelen
elif (isinstance(shape, (tuple, list))
and isinstance(shape[0], (int, float, long))
and isinstance(shape[0], integer_types + (float,))
and len(shape) == 2):
normshp = (shape, ) * shapelen
if normshp is None:
+4
View File
@@ -79,3 +79,7 @@ def test_rescale_intensity():
def test_error_ndim():
arr_error = np.random.randn(1, 2, 3, 4)
montage2d(arr_error)
if __name__ == '__main__':
np.testing.run_module_suite()
+4
View File
@@ -139,3 +139,7 @@ def test_view_as_windows_2D():
[9, 10, 11],
[13, 14, 15],
[17, 18, 19]]]]))
if __name__ == '__main__':
np.testing.run_module_suite()
+3 -3
View File
@@ -1,3 +1,3 @@
from linetool import LineTool, ThickLineTool
from recttool import RectangleTool
from painttool import PaintTool
from .linetool import LineTool, ThickLineTool
from .recttool import RectangleTool
from .painttool import PaintTool
+1 -1
View File
@@ -5,7 +5,7 @@ try:
except ImportError:
print("Could not import matplotlib -- skimage.viewer not available.")
from base import CanvasToolBase, ToolHandles
from skimage.viewer.canvastools.base import CanvasToolBase, ToolHandles
__all__ = ['LineTool', 'ThickLineTool']
+1 -1
View File
@@ -8,7 +8,7 @@ try:
except ImportError:
print("Could not import matplotlib -- skimage.viewer not available.")
from base import CanvasToolBase
from skimage.viewer.canvastools.base import CanvasToolBase
__all__ = ['PaintTool']
+3 -1
View File
@@ -3,6 +3,7 @@ from warnings import warn
from skimage.util.dtype import dtype_range
from .base import Plugin
from ..utils import ClearColormap, update_axes_image
from skimage._shared import six
__all__ = ['OverlayPlugin']
@@ -77,7 +78,8 @@ class OverlayPlugin(Plugin):
@color.setter
def color(self, index):
# Update colormap whenever color is changed.
if isinstance(index, basestring) and index not in self.color_names:
if isinstance(index, six.string_types) and \
index not in self.color_names:
raise ValueError("%s not defined in OverlayPlugin.colors" % index)
else:
name = self.color_names[index]
+1 -1
View File
@@ -1 +1 @@
from core import *
from .core import *
+2 -2
View File
@@ -1,2 +1,2 @@
from core import *
from history import *
from .core import *
from .history import *