Skip to content

Commit

Permalink
cache skeleton_image shape for path_label_image (#210)
Browse files Browse the repository at this point in the history
It may be possible to update prune_paths to use this as well if the
skeleton_image is None (basically create new image and add paths that
are not to be pruned), but for now now that is being left as is.

Closes #208

---------

Co-authored-by: Juan Nunez-Iglesias <[email protected]>
  • Loading branch information
jarodhanko-crafco and jni authored Oct 3, 2023
1 parent 9582720 commit d9ff273
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/skan/csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ def __init__(
self.distances = np.empty(self.n_paths, dtype=float)
self._distances_initialized = False
self.skeleton_image = None
self.skeleton_shape = skeleton_image.shape
self.source_image = None
self.degrees = np.diff(self.graph.indptr)
self.spacing = (
Expand Down Expand Up @@ -618,7 +619,7 @@ def path_label_image(self):
Image of the same shape as self.skeleton_image where each pixel
has the value of its branch id + 1.
"""
image_out = np.zeros(self.skeleton_image.shape, dtype=int)
image_out = np.zeros(self.skeleton_shape, dtype=int)
for i in range(self.n_paths):
coords_to_wipe = self.path_coordinates(i)
coords_idxs = tuple(np.round(coords_to_wipe).astype(int).T)
Expand Down
14 changes: 14 additions & 0 deletions src/skan/test/test_csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def test_fast_graph_center_idx():
i = csr._fast_graph_center_idx(s)
assert i == 1


def test_sholl():
s = csr.Skeleton(skeleton0)
c, r, counts = csr.sholl_analysis(s, shells=np.arange(0, 5, 1.5))
Expand Down Expand Up @@ -241,3 +242,16 @@ def test_zero_degree_nodes():
np.testing.assert_equal(
np.ravel(s.coordinates), [0, 1, 3, 5, 7, 9, 11, 12]
)


def test_skeleton_path_image_no_keep_image():
"""See #208: "Skeleton.path_label_image requires keep_images=True."
Before PR #210, it was an implicit requirement of path_label_image
to create a Skeleton with keep_images=True. However, we only needed
the shape of the image. This makes sure that the method works even
when keep_images=False.
"""
s = csr.Skeleton(skeleton2, keep_images=False)
pli = s.path_label_image()
assert np.max(pli) == s.n_paths

0 comments on commit d9ff273

Please sign in to comment.