Repository: scikit-image/scikit-image

Issue:
segmentation.random_walker error, masking problem
## Description
The method segmentation.random_walker produces an error when passing labels with negative values. The code below worked with the `segmentation/random_walker.py` file from commit d61625b3950ffa9ee3d59e91295b49357be03b27. It produces an error starting from commit 8f76f0fb48908ba2ca92816d3611bba5cd09b998 (the next one modifying the file). I am attaching below the arrays used for the image and the labels in calling the random_walker method.
[image.txt](https://github.com/scikit-image/scikit-image/files/4720149/image.txt)
[labels.txt](https://github.com/scikit-image/scikit-image/files/4720150/labels.txt)

<!--
(Note: for guidance on how to use `scikit-image`, please post instead on https://forum.image.sc/tag/scikit-image)
-->

## Way to reproduce
The error can be reproduced using the following code:
```
import numpy as np
from skimage import segmentation

labels = np.loadtxt('labels.txt')
img = np.loadtxt('image.txt')

labeled_image = segmentation.random_walker(img, labels, mode='bf', beta=130., return_full_prob=False)
```

## 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__))
```

```python
3.8.2 (default, Apr 23 2020, 14:32:57) 
[GCC 8.3.0]
Linux-4.15.0-101-generic-x86_64-with-glibc2.2.5
scikit-image version: 0.18.dev0
numpy version: 1.18.4
```



Discussion:

Resolving patch:
diff --git a/skimage/segmentation/random_walker_segmentation.py b/skimage/segmentation/random_walker_segmentation.py
--- a/skimage/segmentation/random_walker_segmentation.py
+++ b/skimage/segmentation/random_walker_segmentation.py
@@ -499,9 +499,10 @@ def random_walker(data, labels, beta=130, mode='cg_j', tol=1.e-3, copy=True,
     labels[inds_isolated_seeds] = isolated_values
     labels = labels.reshape(labels_shape)
 
-    if return_full_prob:
-        mask = labels == 0
+    mask = labels == 0
+    mask[inds_isolated_seeds] = False
 
+    if return_full_prob:
         out = np.zeros((nlabels,) + labels_shape)
         for lab, (label_prob, prob) in enumerate(zip(out, X), start=1):
             label_prob[mask] = prob
@@ -509,6 +510,6 @@ def random_walker(data, labels, beta=130, mode='cg_j', tol=1.e-3, copy=True,
     else:
         X = np.argmax(X, axis=0) + 1
         out = labels.astype(labels_dtype)
-        out[labels == 0] = X
+        out[mask] = X
 
     return out


Test patch:
diff --git a/skimage/segmentation/tests/test_random_walker.py b/skimage/segmentation/tests/test_random_walker.py
--- a/skimage/segmentation/tests/test_random_walker.py
+++ b/skimage/segmentation/tests/test_random_walker.py
@@ -442,6 +442,30 @@ def test_isolated_seeds():
     assert res[1, 1, 1] == 0
 
 
+def test_isolated_area():
+    np.random.seed(0)
+    a = np.random.random((7, 7))
+    mask = - np.ones(a.shape)
+    # This pixel is an isolated seed
+    mask[1, 1] = 0
+    # Unlabeled pixels
+    mask[3:, 3:] = 0
+    # Seeds connected to unlabeled pixels
+    mask[4, 4] = 2
+    mask[6, 6] = 1
+
+    # Test that no error is raised, and that labels of isolated seeds are OK
+    with expected_warnings([NUMPY_MATRIX_WARNING,
+                            'The probability range is outside']):
+        res = random_walker(a, mask)
+    assert res[1, 1] == 0
+    with expected_warnings([NUMPY_MATRIX_WARNING,
+                            'The probability range is outside']):
+        res = random_walker(a, mask, return_full_prob=True)
+    assert res[0, 1, 1] == 0
+    assert res[1, 1, 1] == 0
+
+
 def test_prob_tol():
     np.random.seed(0)
     a = np.random.random((7, 7))