From a29274893a683baf3fe2408f745cbd2fba73c6b8 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Fri, 4 Jul 2014 12:04:47 +0200 Subject: [PATCH] BUG Fix crash with close_holes The code did not check correctly for whether the input was 2 dimensional. --- ChangeLog | 3 +++ mahotas/internal.py | 5 ++++- mahotas/morph.py | 7 ++++--- mahotas/tests/test_morph.py | 13 +++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8eddafe2..ddf1ad6b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +Version 1.1.1 2014-07-04 by luispedro + * Fix crash in close_holes() with nD images (for n > 2) + Version 1.1.0 2014-02-12 by luispedro * Better error checking * Fix interpolation of integer images using order 1 diff --git a/mahotas/internal.py b/mahotas/internal.py index 25fc54fa..ca7b98f0 100644 --- a/mahotas/internal.py +++ b/mahotas/internal.py @@ -1,4 +1,4 @@ -# Copyright (C) 2011-2012, Luis Pedro Coelho +# Copyright (C) 2011-2014, Luis Pedro Coelho # vim: set ts=4 sts=4 sw=4 expandtab smartindent: # # License: MIT (see COPYING file) @@ -166,3 +166,6 @@ def _check_3(arr, funcname): if arr.ndim != 3 or arr.shape[2] != 3: raise ValueError('mahotas.%s: this function expects an array of shape (h, w, 3), received an array of shape %s.' % (funcname, arr.shape)) +def _check_2(arr, funcname): + if arr.ndim != 2: + raise ValueError('mahotas.%s: this function can only handle 2D arrays (passed array with shape %s).' % (funcname, arr.shape)) diff --git a/mahotas/morph.py b/mahotas/morph.py index 72c20c5e..e6931e34 100644 --- a/mahotas/morph.py +++ b/mahotas/morph.py @@ -6,7 +6,7 @@ from __future__ import division import numpy as np -from .internal import _get_output, _verify_is_integer_type +from .internal import _get_output, _verify_is_integer_type, _check_2 from . import _morph __all__ = [ @@ -273,10 +273,10 @@ def cwatershed(surface, markers, Bc=None, return_lines=False): W,WL = cwatershed(surface, markers, Bc=None, return_lines=True) Seeded watershed in n-dimensions - + This function computes the watershed transform on the input surface (which may actually be an n-dimensional volume). - + This function requires initial seed points. A traditional way of initializing watershed is to use regional minima:: @@ -477,6 +477,7 @@ def close_holes(ref, Bc=None): closed : ndarray superset of `ref` (i.e. with closed holes) ''' + _check_2(ref, 'close_holes') ref = np.ascontiguousarray(ref, dtype=np.bool_) Bc = get_structuring_elem(ref, Bc) return _morph.close_holes(ref, Bc) diff --git a/mahotas/tests/test_morph.py b/mahotas/tests/test_morph.py index d880de74..dd357b09 100644 --- a/mahotas/tests/test_morph.py +++ b/mahotas/tests/test_morph.py @@ -1,7 +1,9 @@ +import mahotas as mh import numpy as np from mahotas.morph import get_structuring_elem, subm, tophat_open, tophat_close from nose.tools import raises + def test_get_structuring_elem(): A = np.zeros((10,10), np.bool) Bc = np.ones((4,4), dtype=np.bool) @@ -24,7 +26,7 @@ def bad_dims(): get_structuring_elem(A, Bc) bad_dims() - + def test_open(): from mahotas.morph import open @@ -201,7 +203,7 @@ def test_distance_multi(): def test_disk(): from mahotas.morph import disk - D2 = disk(2) + D2 = disk(2) assert D2.shape[0] == D2.shape[1] assert D2.shape == (5,5) assert not D2[0,0] @@ -226,3 +228,10 @@ def test_negative_dim(dim): test_negative_dim(-2) test_negative_dim(-1) test_negative_dim(0) + + +@raises(ValueError) +def test_close_holes_3d(): + 'Close holes should raise exception with 3D inputs' + f = np.random.rand(100,100,3) > .9 + mh.close_holes(f)