Repository: seung-lab/connected-components-3d (language: python)

Issue:
the segmentation number is 0 when the segmentation array is the same object
```
fragments_chunk, seg_num = cc3d.connected_components(
                    np.ones(3,3,dtype=np.uint32), connectivity=6, return_N=True
                )
```
the seg_num is 0, which should be 1!


Discussion / hints:
Oh dang, that's a great catch. Thank you Jingpeng! I recently introduced some early-exit changes but forgot to account for size_t& N in some of them.

In the mean time, you can use `seg_num = np.max(fragments_chunk)`

Fix patch:
diff --git a/cc3d.hpp b/cc3d.hpp
index e613976..5fb4d74 100644
--- a/cc3d.hpp
+++ b/cc3d.hpp
@@ -254,6 +254,7 @@ OUT* relabel(
   ) {
 
   if (num_labels <= 1) {
+    N = num_labels;
     return out_labels;
   }
 


Test patch:
diff --git a/automated_test.py b/automated_test.py
index 5efe7a2..2430309 100644
--- a/automated_test.py
+++ b/automated_test.py
@@ -467,6 +467,36 @@ def test_compare_scipy_6():
   assert Ncc3d == Nscipy
   assert np.all(cc3d_labels == scipy_labels)
 
+@pytest.mark.parametrize("connectivity", (6,18,26))
+def test_return_N(connectivity):
+  labels = np.zeros((10,10,10), dtype=np.uint8)
+  cc3d_labels, N = cc3d.connected_components(labels, connectivity=connectivity, return_N=True)
+  assert N == 0
+  assert np.max(cc3d_labels) == 0
+
+  labels = np.ones((10,10,10), dtype=np.uint8) + 2
+  cc3d_labels, N = cc3d.connected_components(labels, connectivity=connectivity, return_N=True)
+  assert N == 1
+  assert np.max(cc3d_labels) == 1
+
+  labels = np.ones((512,512,512), dtype=np.uint8, order='F')
+  labels[256:,:256,:256] = 2
+  labels[256:,256:,:256] = 3
+  labels[:256,256:,:256] = 4
+  labels[256:,256:,256:] = 5
+  labels[:256,256:,256:] = 6
+  labels[256:,:256,256:] = 7 
+  labels[:256,:256,256:] = 8
+  labels[128, 128, 128] = 9
+  cc3d_labels, N = cc3d.connected_components(labels, connectivity=connectivity, return_N=True)
+  assert N == 9
+  assert np.max(cc3d_labels) == 9
+
+  labels = np.random.randint(0,2, (128,128,128), dtype=np.bool)
+  cc3d_labels, N = cc3d.connected_components(labels, connectivity=connectivity, return_N=True)
+  assert N == np.max(cc3d_labels)
+
+
 # @pytest.mark.skipif("sys.maxsize <= 2**33")
 # @pytest.mark.xfail(raises=MemoryError, reason="Some build tools don't have enough memory for this.")
 # def test_sixty_four_bit():