From cef22e4234e611e69337531c2dffebf5a78eb19e Mon Sep 17 00:00:00 2001 From: "Gregory R. Lee" Date: Thu, 9 Jul 2015 13:02:37 -0400 Subject: [PATCH] MAINT: PEP8 fixes and _interpolation_test.pyx -> interpolation.pyx rename --- doc/examples/plot_edge_modes.py | 2 +- skimage/_shared/_interpolation_test.pyx | 31 ------------ skimage/_shared/interpolation.pyx | 56 +++++++++++++++++++++ skimage/_shared/setup.py | 4 +- skimage/_shared/tests/test_interpolation.py | 13 +++-- skimage/transform/_warps.py | 4 +- skimage/transform/_warps_cy.pyx | 4 +- skimage/transform/tests/test_warps.py | 2 - 8 files changed, 69 insertions(+), 47 deletions(-) delete mode 100644 skimage/_shared/_interpolation_test.pyx create mode 100644 skimage/_shared/interpolation.pyx diff --git a/doc/examples/plot_edge_modes.py b/doc/examples/plot_edge_modes.py index 3ef4f408..4044f032 100644 --- a/doc/examples/plot_edge_modes.py +++ b/doc/examples/plot_edge_modes.py @@ -34,4 +34,4 @@ for n, mode in enumerate(modes): plt.tight_layout() -plt.show() \ No newline at end of file +plt.show() diff --git a/skimage/_shared/_interpolation_test.pyx b/skimage/_shared/_interpolation_test.pyx deleted file mode 100644 index fa11382f..00000000 --- a/skimage/_shared/_interpolation_test.pyx +++ /dev/null @@ -1,31 +0,0 @@ -from interpolation cimport coord_map as _coord_map -from interpolation cimport get_pixel2d -import numpy as np -cimport numpy as cnp - - -def coord_map(Py_ssize_t dim, long coord, mode): - """ interpolation.coord_map python wrapper """ - cdef char mode_c = ord(mode[0].upper()) - return _coord_map(dim, coord, mode_c) - - -def extend_image(image, pad=10, mode='C', cval=0): - """ can be used to verify proper get_pixel2d behavior. """ - cdef: - Py_ssize_t rows = image.shape[0] - Py_ssize_t cols = image.shape[1] - long ro, co - char mode_c = ord(mode[0].upper()) - - image = np.ascontiguousarray(image.astype(np.float64)) - output_shape = np.asarray(image.shape) + 2*pad - image_out = np.zeros(output_shape, dtype=image.dtype) - for r in range(-pad, rows+pad): - for c in range(-pad, cols+pad): - ro = r + pad - co = c + pad - image_out[ro, co] = get_pixel2d( cnp.PyArray_DATA(image), - rows, cols, r, c, - mode_c, cval) - return image_out diff --git a/skimage/_shared/interpolation.pyx b/skimage/_shared/interpolation.pyx new file mode 100644 index 00000000..b6470651 --- /dev/null +++ b/skimage/_shared/interpolation.pyx @@ -0,0 +1,56 @@ +from interpolation cimport coord_map, get_pixel2d +import numpy as np +cimport numpy as cnp + + +def coord_map_py(Py_ssize_t dim, long coord, mode): + """ interpolation.coord_map python wrapper """ + cdef char mode_c = ord(mode[0].upper()) + return coord_map(dim, coord, mode_c) + + +def extend_image(image, pad=10, mode='constant', cval=0): + """ Pad a 2D image by ``pad`` pixels on each side. + + Parameters + ---------- + image : ndarray + Input image. + pad : int, optional + The number of pixels to pad around the border + mode : {'constant', 'nearest', 'reflect', 'mirror', 'wrap'}, optional + Points outside the boundaries of the input are filled according + to the given mode. + cval : float, optional + Used in conjunction with mode 'constant', the value outside + the image boundaries. + + Returns + ------- + extended : ndarray + The extended version of the input image. + + Note + ---- + For image padding, ``skimage.util.pad`` should be used instead. This + function is intended only for testing get_pixel2d and demonstrating the + coordinate mapping modes implemented in ``coord_map``. + """ + + cdef: + Py_ssize_t rows = image.shape[0] + Py_ssize_t cols = image.shape[1] + long ro, co + char mode_c = ord(mode[0].upper()) + + image = np.ascontiguousarray(image.astype(np.float64)) + output_shape = np.asarray(image.shape) + 2 * pad + extended = np.zeros(output_shape, dtype=image.dtype) + for r in range(-pad, rows + pad): + for c in range(-pad, cols + pad): + ro = r + pad + co = c + pad + extended[ro, co] = get_pixel2d( cnp.PyArray_DATA(image), + rows, cols, r, c, + mode_c, cval) + return extended diff --git a/skimage/_shared/setup.py b/skimage/_shared/setup.py index 6a6baeda..066a856f 100644 --- a/skimage/_shared/setup.py +++ b/skimage/_shared/setup.py @@ -15,12 +15,12 @@ def configuration(parent_package='', top_path=None): cython(['geometry.pyx'], working_path=base_path) cython(['transform.pyx'], working_path=base_path) - cython(['_interpolation_test.pyx'], working_path=base_path) + cython(['interpolation.pyx'], working_path=base_path) config.add_extension('geometry', sources=['geometry.c']) config.add_extension('transform', sources=['transform.c'], include_dirs=[get_numpy_include_dirs()]) - config.add_extension('_interpolation_test', sources=['_interpolation_test.c']) + config.add_extension('interpolation', sources=['interpolation.c']) return config diff --git a/skimage/_shared/tests/test_interpolation.py b/skimage/_shared/tests/test_interpolation.py index 8edfbcc6..8022e0e1 100644 --- a/skimage/_shared/tests/test_interpolation.py +++ b/skimage/_shared/tests/test_interpolation.py @@ -1,24 +1,23 @@ -from skimage._shared._interpolation_test import coord_map +from skimage._shared.interpolation import coord_map_py from numpy.testing import assert_array_equal def test_coord_map(): - - reflect = [coord_map(4, n, 'R') for n in range(-6, 6)] + reflect = [coord_map_py(4, n, 'R') for n in range(-6, 6)] expected_reflect = [2, 3, 3, 2, 1, 0, 0, 1, 2, 3, 3, 2] assert_array_equal(reflect, expected_reflect) - wrap = [coord_map(4, n, 'W') for n in range(-6, 6)] + wrap = [coord_map_py(4, n, 'W') for n in range(-6, 6)] expected_wrap = [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1] assert_array_equal(wrap, expected_wrap) - nearest = [coord_map(4, n, 'N') for n in range(-6, 6)] + nearest = [coord_map_py(4, n, 'N') for n in range(-6, 6)] expected_neareset = [0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3] assert_array_equal(nearest, expected_neareset) - mirror = [coord_map(4, n, 'M') for n in range(-6, 6)] + mirror = [coord_map_py(4, n, 'M') for n in range(-6, 6)] expected_mirror = [0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1] assert_array_equal(mirror, expected_mirror) - other = [coord_map(4, n, 'undefined') for n in range(-6, 6)] + other = [coord_map_py(4, n, 'undefined') for n in range(-6, 6)] assert_array_equal(other, list(range(-6, 6))) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index c87c7adb..8f968777 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -49,8 +49,8 @@ def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of `img_as_float`. - Notes - ----- + Note + ---- Modes 'mirror' and 'reflect' are similar, but differ in whether the edge voxels are duplicated during the reflection. As an example, if an array has values [0, 1, 2] and was padded to the right by four values using diff --git a/skimage/transform/_warps_cy.pyx b/skimage/transform/_warps_cy.pyx index fd802817..caab79fd 100644 --- a/skimage/transform/_warps_cy.pyx +++ b/skimage/transform/_warps_cy.pyx @@ -76,8 +76,8 @@ def _warp_fast(cnp.ndarray image, cnp.ndarray H, output_shape=None, Used in conjunction with mode 'C' (constant), the value outside the image boundaries. - Notes - ----- + Note + ---- Modes 'mirror' and 'reflect' are similar, but differ in whether the edge voxels are duplicated during the reflection. As an example, if an array has values [0, 1, 2] and was padded to the right by four values using diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 12754d5a..a8dfcb87 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -181,7 +181,6 @@ def test_resize3d_resize(): assert_almost_equal(resized, ref) - def test_resize3d_2din_3dout(): # 3D output with 2D input x = np.zeros((5, 5), dtype=np.double) @@ -192,7 +191,6 @@ def test_resize3d_2din_3dout(): assert_almost_equal(resized, ref) - def test_resize3d_bilinear(): # bilinear 3rd dimension x = np.zeros((5, 5, 2), dtype=np.double)