jni / skan

Python module to analyse skeleton (thin object) images
https://skeleton-analysis.org
BSD 3-Clause "New" or "Revised" License
117 stars 38 forks source link

Incorrect edge discovery when having two small loops #225

Open jni opened 4 months ago

jni commented 4 months ago

... And which side of the loop is chosen depends on whether the SciPy version used is 1.10 or earlier or 1.11 or later. 😕

Here's a reproducible example:

import napari
import numpy as np
from skimage.draw import random_shapes
from skimage.morphology import skeletonize
from skan import Skeleton, summarize

# Generate a random skeletons, first is a skeleton with a closed loop with side branches
kwargs = {"image_shape": (128, 128),
          "max_shapes": 20,
          "channel_axis": None,
          "shape": None,
          "rng": 1,
          "allow_overlap": True,
          "min_size": 20}

kwargs["rng"] = 13588686514
kwargs["min_size"] = 20
random_images, _ = random_shapes(**kwargs)
mask = np.where(random_images != 255, 1, 0)
skeleton_linear1 = skeletonize(mask)

viewer = napari.Viewer()
layer = viewer.add_labels(skeleton_linear1)

skeleton = Skeleton(skeleton_linear1)
all_paths = [skeleton.path_coordinates(i) for i in range(skeleton.n_paths)]
paths_table = summarize(skeleton, separator='_')
paths_table.reset_index(inplace=True, names='branch-id')

shp_layer = viewer.add_shapes(
        all_paths,
        shape_type='path',
        features=paths_table,
        metadata={'skeleton': skeleton},
        )

shp_layer.edge_color = 'branch-id'
shp_layer.edge_color_cycle = [
        'red', 'green', 'blue', 'cyan', 'magenta', 'yellow'
        ]

napari.run()

This is the output on SciPy 1.10:

Screenshot 2024-04-19 at 6 18 05 PM Screenshot 2024-04-19 at 6 18 22 PM

The same code with SciPy 1.11 produces:

Screenshot 2024-04-19 at 6 19 31 PM

Both are wrong (both loops should be completely closed).

jni commented 4 months ago

SciPy changes to investigate re the change in behaviour, based on the 1.11 release notes: scipy/scipy#16936 / scipy/scipy#16929