From 1ca0eef825188ac5e9fc84b232b4783c166933ac Mon Sep 17 00:00:00 2001 From: "Josh Warner (Mac)" Date: Wed, 7 May 2014 23:53:07 -0500 Subject: [PATCH 1/4] FIX: Non-integer indexing deprecation warnings --- skimage/feature/_hog.py | 3 +- skimage/feature/corner.py | 13 ++-- skimage/feature/tests/test_hog.py | 92 +++++++++++++++++++-------- skimage/filter/lpi_filter.py | 2 +- skimage/morphology/greyreconstruct.py | 5 +- skimage/transform/_geometric.py | 4 +- skimage/util/shape.py | 2 +- 7 files changed, 79 insertions(+), 42 deletions(-) diff --git a/skimage/feature/_hog.py b/skimage/feature/_hog.py index 431a5986..67922b92 100644 --- a/skimage/feature/_hog.py +++ b/skimage/feature/_hog.py @@ -112,7 +112,8 @@ def hog(image, orientations=9, pixels_per_cell=(8, 8), # compute orientations integral images orientation_histogram = np.zeros((n_cellsy, n_cellsx, orientations)) - subsample = np.index_exp[cy / 2:cy * n_cellsy:cy, cx / 2:cx * n_cellsx:cx] + subsample = np.index_exp[cy // 2:cy * n_cellsy:cy, + cx // 2:cx * n_cellsx:cx] for i in range(orientations): #create new integral image for this orientation # isolate orientations in this range diff --git a/skimage/feature/corner.py b/skimage/feature/corner.py index e1dd519b..a9b84ec0 100644 --- a/skimage/feature/corner.py +++ b/skimage/feature/corner.py @@ -2,7 +2,6 @@ import numpy as np from scipy import ndimage from scipy import stats -from skimage.color import rgb2grey from skimage.util import img_as_float, pad from skimage.feature import peak_local_max from skimage.feature.util import _prepare_grayscale_input_2D @@ -134,7 +133,7 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0): Examples -------- - >>> from skimage.feature import hessian_matrix, hessian_matrix_eigvals + >>> from skimage.feature import hessian_matrix >>> square = np.zeros((5, 5)) >>> square[2, 2] = 1 >>> Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1) @@ -342,7 +341,7 @@ def corner_harris(image, method='k', k=0.05, eps=1e-6, sigma=1): A = [(imx**2) (imx*imy)] = [Axx Axy] [(imx*imy) (imy**2)] [Axy Ayy] - Where imx and imy are the first derivatives averaged with a gaussian filter. + Where imx and imy are first derivatives, averaged with a gaussian filter. The corner measure is then defined as:: det(A) - k * trace(A)**2 @@ -423,7 +422,7 @@ def corner_shi_tomasi(image, sigma=1): A = [(imx**2) (imx*imy)] = [Axx Axy] [(imx*imy) (imy**2)] [Axy Ayy] - Where imx and imy are the first derivatives averaged with a gaussian filter. + Where imx and imy are first derivatives, averaged with a gaussian filter. The corner measure is then defined as the smaller eigenvalue of A:: ((Axx + Ayy) - sqrt((Axx - Ayy)**2 + 4 * Axy**2)) / 2 @@ -486,7 +485,7 @@ def corner_foerstner(image, sigma=1): A = [(imx**2) (imx*imy)] = [Axx Axy] [(imx*imy) (imy**2)] [Axy Ayy] - Where imx and imy are the first derivatives averaged with a gaussian filter. + Where imx and imy are first derivatives, averaged with a gaussian filter. The corner measure is then defined as:: w = det(A) / trace(A) (size of error ellipse) @@ -755,9 +754,9 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99): # determine corner class (dot or edge) # variance for different models - var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot \ + var_dot = np.sum(winx_winx * ryy_dot - 2 * winx_winy * rxy_dot + winy_winy * rxx_dot) - var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge \ + var_edge = np.sum(winy_winy * ryy_edge + 2 * winx_winy * rxy_edge + winx_winx * rxx_edge) # test value (F-distributed) t = var_edge / var_dot diff --git a/skimage/feature/tests/test_hog.py b/skimage/feature/tests/test_hog.py index b37513a5..8cab52f7 100644 --- a/skimage/feature/tests/test_hog.py +++ b/skimage/feature/tests/test_hog.py @@ -32,9 +32,11 @@ def test_hog_color_image_unsupported_error(): def test_hog_basic_orientations_and_data_types(): # scenario: - # 1) create image (with float values) where upper half is filled by zeros, bottom half by 100 + # 1) create image (with float values) where upper half is filled by + # zeros, bottom half by 100 # 2) create unsigned integer version of this image - # 3) calculate feature.hog() for both images, both with 'normalise' option enabled and disabled + # 3) calculate feature.hog() for both images, both with 'normalise' + # option enabled and disabled # 4) verify that all results are equal where expected # 5) verify that computed feature vector is as expected # 6) repeat the scenario for 90, 180 and 270 degrees rotated images @@ -43,7 +45,7 @@ def test_hog_basic_orientations_and_data_types(): width = height = 35 image0 = np.zeros((height, width), dtype='float') - image0[height / 2:] = 100 + image0[height // 2:] = 100 for rot in range(4): # rotate by 0, 90, 180 and 270 degrees @@ -52,13 +54,17 @@ def test_hog_basic_orientations_and_data_types(): # create uint8 image from image_float image_uint8 = image_float.astype('uint8') - (hog_float, hog_img_float) = feature.hog(image_float, orientations=4, pixels_per_cell=(8, 8), + (hog_float, hog_img_float) = feature.hog( + image_float, orientations=4, pixels_per_cell=(8, 8), cells_per_block=(1, 1), visualise=True, normalise=False) - (hog_uint8, hog_img_uint8) = feature.hog(image_uint8, orientations=4, pixels_per_cell=(8, 8), + (hog_uint8, hog_img_uint8) = feature.hog( + image_uint8, orientations=4, pixels_per_cell=(8, 8), cells_per_block=(1, 1), visualise=True, normalise=False) - (hog_float_norm, hog_img_float_norm) = feature.hog(image_float, orientations=4, pixels_per_cell=(8, 8), + (hog_float_norm, hog_img_float_norm) = feature.hog( + image_float, orientations=4, pixels_per_cell=(8, 8), cells_per_block=(1, 1), visualise=True, normalise=True) - (hog_uint8_norm, hog_img_uint8_norm) = feature.hog(image_uint8, orientations=4, pixels_per_cell=(8, 8), + (hog_uint8_norm, hog_img_uint8_norm) = feature.hog( + image_uint8, orientations=4, pixels_per_cell=(8, 8), cells_per_block=(1, 1), visualise=True, normalise=True) # set to True to enable manual debugging with graphical output, @@ -66,23 +72,42 @@ def test_hog_basic_orientations_and_data_types(): if False: import matplotlib.pyplot as plt plt.figure() - plt.subplot(2, 3, 1); plt.imshow(image_float); plt.colorbar(); plt.title('image') - plt.subplot(2, 3, 2); plt.imshow(hog_img_float); plt.colorbar(); plt.title('HOG result visualisation (float img)') - plt.subplot(2, 3, 5); plt.imshow(hog_img_uint8); plt.colorbar(); plt.title('HOG result visualisation (uint8 img)') - plt.subplot(2, 3, 3); plt.imshow(hog_img_float_norm); plt.colorbar(); plt.title('HOG result (normalise) visualisation (float img)') - plt.subplot(2, 3, 6); plt.imshow(hog_img_uint8_norm); plt.colorbar(); plt.title('HOG result (normalise) visualisation (uint8 img)') + plt.subplot(2, 3, 1) + plt.imshow(image_float) + plt.colorbar() + plt.title('image') + plt.subplot(2, 3, 2) + plt.imshow(hog_img_float) + plt.colorbar() + plt.title('HOG result visualisation (float img)') + plt.subplot(2, 3, 5) + plt.imshow(hog_img_uint8) + plt.colorbar() + plt.title('HOG result visualisation (uint8 img)') + plt.subplot(2, 3, 3) + plt.imshow(hog_img_float_norm) + plt.colorbar() + plt.title('HOG result (normalise) visualisation (float img)') + plt.subplot(2, 3, 6) + plt.imshow(hog_img_uint8_norm) + plt.colorbar() + plt.title('HOG result (normalise) visualisation (uint8 img)') plt.show() - # results (features and visualisation) for float and uint8 images must be almost equal + # results (features and visualisation) for float and uint8 images must + # be almost equal assert_almost_equal(hog_float, hog_uint8) assert_almost_equal(hog_img_float, hog_img_uint8) - # resulting features should be almost equal when 'normalise' is enabled or disabled (for current simple testing image) + # resulting features should be almost equal when 'normalise' is enabled + # or disabled (for current simple testing image) assert_almost_equal(hog_float, hog_float_norm, decimal=4) assert_almost_equal(hog_float, hog_uint8_norm, decimal=4) - # reshape resulting feature vector to matrix with 4 columns (each corresponding to one of 4 directions), - # only one direction should contain nonzero values (this is manually determined for testing image) + # reshape resulting feature vector to matrix with 4 columns (each + # corresponding to one of 4 directions); only one direction should + # contain nonzero values (this is manually determined for testing + # image) actual = np.max(hog_float.reshape(-1, 4), axis=0) if rot in [0, 2]: @@ -101,8 +126,9 @@ def test_hog_orientations_circle(): # scenario: # 1) create image with blurred circle in the middle # 2) calculate feature.hog() - # 3) verify that the resulting feature vector contains uniformly distributed values for all orientations, - # i.e. no orientation is lost or emphasized + # 3) verify that the resulting feature vector contains uniformly + # distributed values for all orientations, i.e. no orientation is + # lost or emphasized # 4) repeat the scenario for other 'orientations' option # size of testing image @@ -114,29 +140,39 @@ def test_hog_orientations_circle(): image = ndimage.gaussian_filter(image, 2) for orientations in range(2, 15): - (hog, hog_img) = feature.hog(image, orientations=orientations, pixels_per_cell=(8, 8), - cells_per_block=(1, 1), visualise=True, normalise=False) + (hog, hog_img) = feature.hog(image, orientations=orientations, + pixels_per_cell=(8, 8), + cells_per_block=(1, 1), visualise=True, + normalise=False) # set to True to enable manual debugging with graphical output, # must be False for automatic testing if False: import matplotlib.pyplot as plt plt.figure() - plt.subplot(1, 2, 1); plt.imshow(image); plt.colorbar(); plt.title('image_float') - plt.subplot(1, 2, 2); plt.imshow(hog_img); plt.colorbar(); plt.title('HOG result visualisation, orientations=%d' % (orientations)) + plt.subplot(1, 2, 1) + plt.imshow(image) + plt.colorbar() + plt.title('image_float') + plt.subplot(1, 2, 2) + plt.imshow(hog_img) + plt.colorbar() + plt.title('HOG result visualisation, ' + 'orientations=%d' % (orientations)) plt.show() - # reshape resulting feature vector to matrix with N columns (each column corresponds to one direction), + # reshape resulting feature vector to matrix with N columns (each + # column corresponds to one direction), hog_matrix = hog.reshape(-1, orientations) - # compute mean values in the resulting feature vector for each direction, - # these values should be almost equal to the global mean value (since the image contains a circle), - # i.e. all directions have same contribution to the result + # compute mean values in the resulting feature vector for each + # direction, these values should be almost equal to the global mean + # value (since the image contains a circle), i.e., all directions have + # same contribution to the result actual = np.mean(hog_matrix, axis=0) desired = np.mean(hog_matrix) assert_almost_equal(actual, desired, decimal=1) if __name__ == '__main__': - from numpy.testing import run_module_suite - run_module_suite() + np.testing.run_module_suite() diff --git a/skimage/filter/lpi_filter.py b/skimage/filter/lpi_filter.py index f8d08f34..ef85c5cf 100644 --- a/skimage/filter/lpi_filter.py +++ b/skimage/filter/lpi_filter.py @@ -18,7 +18,7 @@ def _centre(x, oshape): """Return an array of oshape from the centre of x. """ - start = (np.array(x.shape) - np.array(oshape)) / 2. + 1 + start = (np.array(x.shape) - np.array(oshape)) // 2 + 1 out = x[[slice(s, s + n) for s, n in zip(start, oshape)]] return out diff --git a/skimage/morphology/greyreconstruct.py b/skimage/morphology/greyreconstruct.py index 3fffd28e..9ec0098d 100644 --- a/skimage/morphology/greyreconstruct.py +++ b/skimage/morphology/greyreconstruct.py @@ -158,7 +158,7 @@ def reconstruction(seed, mask, method='dilation', selem=None, offset=None): # Create a list of strides across the array to get the neighbors within # a flattened array - value_stride = np.array(images.strides[1:]) / images.dtype.itemsize + value_stride = np.array(images.strides[1:]) // images.dtype.itemsize image_stride = images.strides[0] // images.dtype.itemsize selem_mgrid = np.mgrid[[slice(-o, d - o) for d, o in zip(selem.shape, offset)]] @@ -187,7 +187,8 @@ def reconstruction(seed, mask, method='dilation', selem=None, offset=None): value_map = -value_map start = index_sorted[0] - reconstruction_loop(value_rank, prev, next, nb_strides, start, image_stride) + reconstruction_loop(value_rank, prev, next, nb_strides, start, + image_stride) # Reshape reconstructed image to original image shape and remove padding. rec_img = value_map[value_rank[:image_stride]] diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index 883aa88d..ca813600 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -729,7 +729,7 @@ class PolynomialTransform(GeometricTransform): for j in range(order + 1): for i in range(j + 1): A[:rows, pidx] = xs ** (j - i) * ys ** i - A[rows:, pidx + u / 2] = xs ** (j - i) * ys ** i + A[rows:, pidx + u // 2] = xs ** (j - i) * ys ** i pidx += 1 A[:rows, -1] = xd @@ -741,7 +741,7 @@ class PolynomialTransform(GeometricTransform): # singular value params = - V[-1, :-1] / V[-1, -1] - self.params = params.reshape((2, u / 2)) + self.params = params.reshape((2, u // 2)) def __call__(self, coords): """Apply forward transformation. diff --git a/skimage/util/shape.py b/skimage/util/shape.py index eed6b952..4633ada6 100644 --- a/skimage/util/shape.py +++ b/skimage/util/shape.py @@ -94,7 +94,7 @@ def view_as_blocks(arr_in, block_shape): arr_in = np.ascontiguousarray(arr_in) - new_shape = tuple(arr_shape / block_shape) + tuple(block_shape) + new_shape = tuple(arr_shape // block_shape) + tuple(block_shape) new_strides = tuple(arr_in.strides * block_shape) + arr_in.strides arr_out = as_strided(arr_in, shape=new_shape, strides=new_strides) From 375e406f646bc5aa1a78322098a1ffe1217aa18d Mon Sep 17 00:00:00 2001 From: "Josh Warner (Mac)" Date: Thu, 8 May 2014 19:56:47 -0500 Subject: [PATCH 2/4] Use safe_as_int for input checking. --- skimage/feature/corner.py | 3 ++- skimage/transform/_geometric.py | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/skimage/feature/corner.py b/skimage/feature/corner.py index a9b84ec0..6e06b779 100644 --- a/skimage/feature/corner.py +++ b/skimage/feature/corner.py @@ -8,6 +8,7 @@ from skimage.feature.util import _prepare_grayscale_input_2D from skimage.feature.corner_cy import _corner_fast from ._hessian_det_appx import _hessian_matrix_det from ..transform import integral_image +from .._shared.utils import safe_as_int def _compute_derivatives(image, mode='constant', cval=0): @@ -675,7 +676,7 @@ def corner_subpix(image, corners, window_size=11, alpha=0.99): image = pad(image, pad_width=wext, mode='constant', constant_values=0) # add pad width, make sure to not modify the input values in-place - corners = corners + wext + corners = safe_as_int(corners + wext) # normal equation arrays N_dot = np.zeros((2, 2), dtype=np.double) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index ca813600..59ca4731 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -7,6 +7,7 @@ from scipy import ndimage, spatial from skimage._shared.utils import get_bound_method_class from skimage.util import img_as_float from ._warps_cy import _warp_fast +from .._shared.utils import safe_as_int class GeometricTransform(object): @@ -722,6 +723,7 @@ class PolynomialTransform(GeometricTransform): rows = src.shape[0] # number of unknown polynomial coefficients + order = safe_as_int(order) u = (order + 1) * (order + 2) A = np.zeros((rows * 2, u + 1)) @@ -958,6 +960,7 @@ def warp_coords(coord_map, shape, dtype=np.float64): >>> warped_image = map_coordinates(image, coords) """ + shape = safe_as_int(shape) rows, cols = shape[0], shape[1] coords_shape = [len(shape), rows, cols] if len(shape) == 3: From 8bcdfb8325e4d4ded663fcbf4d68540a6ab69ab0 Mon Sep 17 00:00:00 2001 From: "Josh Warner (Mac)" Date: Thu, 8 May 2014 20:12:31 -0500 Subject: [PATCH 3/4] Remove unnecessary double import. --- skimage/transform/_geometric.py | 1 - 1 file changed, 1 deletion(-) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index df5d5f26..b61020f5 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -7,7 +7,6 @@ from scipy import ndimage, spatial from skimage._shared.utils import get_bound_method_class, safe_as_int from skimage.util import img_as_float from ._warps_cy import _warp_fast -from .._shared.utils import safe_as_int class GeometricTransform(object): From 8663e23f4a08309f5fdf456ecab890a60b104413 Mon Sep 17 00:00:00 2001 From: "Josh Warner (Mac)" Date: Thu, 8 May 2014 20:45:19 -0500 Subject: [PATCH 4/4] FIX: Use //= instead of /= to fix deprecation warnings in adapthist --- skimage/exposure/_adapthist.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/skimage/exposure/_adapthist.py b/skimage/exposure/_adapthist.py index e364d2f0..699e9acf 100644 --- a/skimage/exposure/_adapthist.py +++ b/skimage/exposure/_adapthist.py @@ -126,8 +126,8 @@ def _clahe(image, ntiles_x, ntiles_y, clip_limit, nbins=128): x_res = image.shape[1] - image.shape[1] % ntiles_x image = image[: y_res, : x_res] - x_size = image.shape[1] / ntiles_x # Actual size of contextual regions - y_size = image.shape[0] / ntiles_y + x_size = image.shape[1] // ntiles_x # Actual size of contextual regions + y_size = image.shape[0] // ntiles_y n_pixels = x_size * y_size if clip_limit > 0.0: # Calculate actual cliplimit @@ -139,7 +139,7 @@ def _clahe(image, ntiles_x, ntiles_y, clip_limit, nbins=128): bin_size = 1 + NR_OF_GREY / nbins aLUT = np.arange(NR_OF_GREY) - aLUT /= bin_size + aLUT //= bin_size img_blocks = view_as_blocks(image, (y_size, x_size)) # Calculate greylevel mappings for each contextual region