Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix n_points calculation (handling of 0-deg nodes), with tests #193

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/skan/csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def _build_skeleton_path_graph(graph):
# the number of cycles ahead of time, but it is bounded by one quarter
# of the number of points.
n_points = (
graph.indices.size + np.sum(endpoint_degrees - 1)
graph.indices.size + np.sum(np.maximum(0, endpoint_degrees - 1))
+ buffer_size_offset
)
path_indices = np.zeros(n_points, dtype=int)
Expand Down
23 changes: 23 additions & 0 deletions src/skan/test/test_csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,26 @@ def test_diagonal():
s, center=[1, 1], shells=np.arange(0.09, 5, 1.45)
)
np.testing.assert_equal(counts, [3, 2, 1, 0])


def test_zero_degree_nodes():
"""Test that graphs with 0-degree nodes don't have allocation errors.

In skan commits 7498831 or prior, isolated pixels, which have degree 0,
would count as *negative* values when counting the total number of edges,
resulting in a buffer too small to hold the edge info.

See issue jni/skan#182.
"""
x = np.array([1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1]).astype(bool)

# the try/except causes an early segfault if the buffer overflows
try:
s = csr.Skeleton(x)
except Exception as e:
print(e)

assert s.n_paths == 2
np.testing.assert_equal(
np.ravel(s.coordinates), [0, 1, 3, 5, 7, 9, 11, 12]
)