Scikit-image: Key error when using `skimage.measure.find_contours`

Created on 8 Jul 2020  路  3Comments  路  Source: scikit-image/scikit-image

Description

Hi all, a few months back I raised #4497, where certain arrays from my satellite time series data caused a KeyError when skimage.measure.find_contours was applied to them. It seemed like this was solved by #4505, but I've just had the issue re-occur on scikit-image version: 0.17.2.

I've attached a reproducible example below; my full arrays are larger (around 2000 x 2000 pixels).

Way to reproduce

import numpy as np
from skimage.measure import find_contours

# Example array
array = np.array([[ 0.08, -0.03, -0.17,  -0.08,  0.24,  0.06,  0.17, -0.02],
                  [ 0.12,  0.  ,  np.nan, 0.24,  0.  , -0.53,  0.26,  0.16],
                  [ 0.39,  0.  ,  0.  ,   0.  ,  0.  , -0.02, -0.3 ,  0.01],
                  [ 0.28, -0.04, -0.03,   0.16,  0.12,  0.01, -0.87,  0.16],
                  [ 0.26,  0.08,  0.08,   0.08,  0.12,  0.13,  0.11,  0.19],
                  [ 0.27,  0.24,  0.  ,   0.25,  0.32,  0.19,  0.26,  0.22]],
      dtype=np.float32)

# Extract 0 contour
find_contours(array, 0)

```

KeyError Traceback (most recent call last)
in
12
13 # Extract 0 contour
---> 14 find_contours(array, 0)

/g/data/v10/public/modules/dea-env/20200612/lib/python3.6/site-packages/skimage/measure/_find_contours.py in find_contours(array, level, fully_connected, positive_orientation, mask)
135 segments = _get_contour_segments(array.astype(np.double), float(level),
136 fully_connected == 'high', mask=mask)
--> 137 contours = _assemble_contours(segments)
138 if positive_orientation == 'high':
139 contours = [c[::-1] for c in contours]

/g/data/v10/public/modules/dea-env/20200612/lib/python3.6/site-packages/skimage/measure/_find_contours.py in _assemble_contours(segments)
172 head.extend(tail)
173 # remove all traces of tail:
--> 174 ends.pop(tail[-1])
175 contours.pop(tail_num, None)
176 # Update contour starts end ends

KeyError: (2.0, 3)



## Version information
```python
# Paste the output of the following python commands
from __future__ import print_function
import sys; print(sys.version)
import platform; print(platform.platform())
import skimage; print("scikit-image version: {}".format(skimage.__version__))
import numpy; print("numpy version: {}".format(numpy.__version__))
3.6.10 | packaged by conda-forge | (default, Apr 24 2020, 16:44:11) 
[GCC 7.3.0]
Linux-2.6.32-754.18.2.el6.x86_64-x86_64-with-centos-6.10-Final
scikit-image version: 0.17.2
numpy version: 1.18.5

bug

All 3 comments

I've found another example where the default find_contours parameters fail with a KeyError, but fully_connected='high' passes:

import numpy as np
from skimage.measure import find_contours

# Example array
array = np.array([[-0.18,  np.nan, np.nan,  0.22, -0.14, -0.23, -0.2 , -0.17, -0.19, -0.24],
                  [ 0.  ,  np.nan, np.nan, np.nan, -0.1 , -0.24, -0.15, -0.02, -0.09, -0.21],
                  [ 0.43,  0.19,  np.nan, np.nan, -0.01, -0.2 , -0.22, -0.18, -0.16, -0.07],
                  [ 0.23,  0.  ,  np.nan, -0.06, -0.07, -0.21, -0.24, -0.25, -0.23, -0.13],
                  [-0.05, -0.11,  0.  ,  0.1 , -0.19, -0.23, -0.23, -0.18, -0.19,   -0.16],
                  [-0.19, -0.05,  0.13, -0.08, -0.22, -0.23, -0.26, -0.15, -0.12, -0.13],
                  [-0.2 , -0.11, -0.11, -0.24, -0.29, -0.27, -0.35, -0.36, -0.27, -0.13],
                  [-0.28, -0.33, -0.31, -0.36, -0.39, -0.37, -0.38, -0.32, -0.34, -0.2 ],
                  [-0.28, -0.33, -0.39, -0.4 , -0.42, -0.38, -0.35, -0.39, -0.35,  -0.34],
                  [-0.38, -0.35, -0.41, -0.42, -0.39, -0.36, -0.34, -0.36, -0.28,  -0.34]], 
                 dtype=np.float32)

# This passes
find_contours(array, 0, fully_connected='high')

# This fails
find_contours(array, 0)

Thank you @robbibt for reopening this, I will investigate: the example that you provide will help us for sure :wink:

It looks like the assertion saying that "degenerated segments are picked up later" in the processing loop is not always true when there is masked areas!!

I will investigate on this side...

Was this page helpful?
0 / 5 - 0 ratings