Skip to content

skimage.morphology.flood bug with F-ordered Numpy Arrays #4549

Closed

Description

Description

I am trying to create a flood mask with skimage.morphology.flood that encompassed all points connected to a given start point in a numpy array. This works when i create a small window around my start point but doesn't when i run it on the full image. I posted this issue on stack overflow and it was pointed out to me that my array is in F-ordering in this entails a bug since skimage expects a C-ordering array.

Way to reproduce

# I have a large array node_space and a point of interest current_point
In [1]: node_space.shape                                                                         
Out[1]: (267, 321, 206)

In [2]: current_point                                                                            
Out[2]: (127, 171, 51)

# When I take a small cube and flood it from my point of interest flood works fine

In [3]: array = node_space[current_point[0]-1: current_point[0]+2, current_point[1]-1: current_point[1]+2, current_point[2]-1: current_point[2]+2]                                        

In [3]: mask = flood(array, (1,1,1))                                                             

In [4]: mask                                                                                     
Out[4]: 
array([[[False, False, False],
        [False, False,  True],
        [False, False, False]],

       [[False, False, False],
        [False,  True, False],
        [False, False, False]],

       [[False, False, False],
        [False, False, False],
        [False, False, False]]])

# However when I run The same operation on the larger image it fails
In [5]: mask = flood(node_space, current_point)    

In [6]: mask[current_point[0]-1: current_point[0]+2, current_point[1]-1: current_point[1]+2, current_point[2]-1: current_point[2]+2]                                                      
Out[6]: 
array([[[False, False, False],
        [False, False, False],
        [False, False, False]],

       [[False, False, False],
        [False, False, False],
        [False, False, False]],

       [[False, False, False],
        [False, False, False],
        [False, False, False]]])

Version information

# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version):
3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)]

import platform; print(platform.platform()):
Darwin-18.7.0-x86_64-i386-64bit

import skimage; print("scikit-image version: {}".format(skimage.__version__)):
scikit-image version: 0.16.2

import numpy; print("numpy version: {}".format(numpy.__version__))
numpy version: 1.18.2
# your output here

Activity

  1. rfezzani commented on Apr 4, 2020

    @rfezzani
    Member

    Thank you @MarcoPerezCaceres for reporting this.

    it was pointed out to me that my array is in F-ordering

    Can you please confirm this assumption using

    mask = flood(node_space.copy(order='C'), current_point) 
  2. jni commented on Apr 4, 2020

    @jni
    Member

    @rfezzani sorry, I meant to provide some extra context here but didn't get a chance. I've verified the behaviour myself with the original dataset at the SO question.

  3. jni commented on Apr 4, 2020

    @jni
    Member

    node_space is actually a super-sparse array, so you can verify the behaviour by creating it as follows:

    node_space = np.zeros((267, 321, 206), dtype=np.float64, order='F')
    points = (
        array([121, 121, 122, 123, 124, 125, 126, 127]),
        array([173, 174, 173, 172, 171, 171, 171, 171]),
        array([56, 55, 55, 55, 54, 53, 52, 51]),
    )
    node_space[points] = 1
  4. added
    ⚠️ Priority
    on Apr 4, 2020
  5. jni commented on Apr 4, 2020

    @jni
    Member

    CC @JDWarner @lagru

    The quick fix is to always pass the input through np.ascontiguousarray, which we should do, before deciding whether we want to go through a "proper" fix of ensuring that the neighbors code works with F-ordering.

  6. jni commented on Apr 4, 2020

    @jni
    Member

    @MarcoPerezCaceres thank you for reporting! 😊

  7. rfezzani commented on Apr 4, 2020

    @rfezzani
    Member

    OK, excellent, thank you @jni for all these information. The quick fix sound reasonable.

  8. lagru commented on Apr 5, 2020

    @lagru
    Member

    Wew, quite a few things here which might be oversights:

    • _fast_pad always returns a c-contiguous array regardless of the input's order
    • _offsets_to_raveled_neighbors is missing the order parameter
    • order parameter is missing in all calls to ravel() which therefore defaults to order="C"

    I'll make a PR in a moment.

    Thanks for the ping. 😉

  9. lagru commented on Apr 5, 2020

    @lagru
    Member

    See #4556.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⚠️ Priority

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions