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

Add check for overlapping points in neurites #1115

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion neurom/check/morphology_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import numpy as np
from neurom import NeuriteType
from neurom.check import CheckResult
from neurom.check.morphtree import get_flat_neurites, back_tracking_segments
from neurom.check.morphtree import get_flat_neurites, back_tracking_segments, duplicated_points
from neurom.core.morphology import Section, iter_neurites, iter_sections, iter_segments
from neurom.core.dataformat import COLS
from neurom.exceptions import NeuroMError
Expand Down Expand Up @@ -365,3 +365,13 @@ def has_no_back_tracking(morph):
for i in back_tracking_segments(neurite)
]
return CheckResult(len(bad_ids) == 0, bad_ids)


def has_no_duplicated_point(morph, tolerance=None):
"""Check if the morphology has duplicated points."""
bad_ids = [
(i[:2], np.atleast_2d(i[2]))
for neurite in iter_neurites(morph)
for i in duplicated_points(neurite, tolerance=tolerance)
]
return CheckResult(len(bad_ids) == 0, bad_ids)
68 changes: 68 additions & 0 deletions neurom/check/morphtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"""Python module of NeuroM to check morphology trees."""

import numpy as np
from scipy.spatial import KDTree

from neurom.core.dataformat import COLS
from neurom import morphmath as mm
from neurom.morphmath import principal_direction_extent
Expand Down Expand Up @@ -204,6 +206,60 @@ def is_back_tracking(neurite):
return False


def duplicated_points(neurite, tolerance=None):
"""Return duplicated points of a neurite.

Args:
neurite(Neurite): neurite to operate on
tolerance(float): the tolerance used to find duplicated points

Returns:
A generator of tuples containing the IDs of the two intersecting sections and the
duplicated point.
"""
section_pts = np.vstack(
[
np.insert(neurite.root_node.points[0], 0, neurite.root_node.id),
np.vstack(
[
np.concatenate(
[np.ones((len(sec.points) - 1, 1)) * sec.id, sec.points[1:]],
axis=1,
)
for sec in neurite.iter_sections()
],
),
],
)
eleftherioszisis marked this conversation as resolved.
Show resolved Hide resolved
tree = KDTree(section_pts[:, [1, 2, 3]])
eleftherioszisis marked this conversation as resolved.
Show resolved Hide resolved
if tolerance is None:
tolerance = 0
for pt_id1, pt_id2 in tree.query_pairs(tolerance):
yield (
int(section_pts[pt_id1, 0]),
int(section_pts[pt_id2, 0]),
section_pts[pt_id1, [1, 2, 3]],
)


def has_duplicated_points(neurite, tolerance=None):
"""Check if a neurite has at least one duplicated point.

See duplicated_points() for more details.

Args:
neurite(Neurite): neurite to operate on
tolerance(float): the tolerance used to find duplicated points

Returns:
True if two points of the neurite are duplicated.
"""
for _i in duplicated_points(neurite, tolerance=tolerance):
# If one duplicated point is found then the neurite is duplicating
return True
return False


def get_flat_neurites(morph, tol=0.1, method='ratio'):
"""Check if a morphology has neurites that are flat within a tolerance.

Expand Down Expand Up @@ -246,3 +302,15 @@ def get_back_tracking_neurites(morph):
List of morphologies with backtracks
"""
return [n for n in morph.neurites if is_back_tracking(n)]


def get_duplicated_point_neurites(morph, tolerance=0):
"""Get neurites that have duplicated points.

Args:
morph(Morphology): neurite to operate on

Returns:
List of morphologies with backtracks
"""
return [n for n in morph.neurites if has_duplicated_points(n, tolerance=tolerance)]
30 changes: 30 additions & 0 deletions tests/check/test_morphology_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,33 @@ def test_has_no_back_tracking():
assert_array_equal(info[0][1], [[1, -3, 0]])
assert_array_equal(info[1][0], [2, 1, 1])
assert_array_equal(info[1][1], [[1, -3, 0]])


def test_has_no_duplicated_point():
m = load_morphology("""
((CellBody) (-1 0 0 2) (1 0 0 2))

((Dendrite)
(0 0 0 0.4)
(0 1 0 0.3)
(0 2 0 0.28)
(
(0 2 0 0.28)
(1 3 0 0.3)
(2 4 0 0.22)
|
(0 2 0 0.28)
(1 -3 0 0.3)
(2 -4 0 0.24)
(1 -3 0 0.52)
(0 1 0 0.2)
(4 -6 0 0.2)
))
""", "asc")
result = morphology_checks.has_no_duplicated_point(m)
assert result.status is False
info = result.info
assert_array_equal(info[0][0], [0, 2])
assert_array_equal(info[0][1], [[0, 1, 0]])
assert_array_equal(info[1][0], [2, 2])
assert_array_equal(info[1][1], [[1, -3, 0]])
7 changes: 7 additions & 0 deletions tests/check/test_morphtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,10 @@ def test_get_nonmonotonic_neurites():
def test_get_back_tracking_neurites():
m = load_morphology(Path(SWC_PATH, 'Neuron.swc'))
assert len(mt.get_back_tracking_neurites(m)) == 4


def test_get_duplicated_point_neurites():
m = load_morphology(Path(SWC_PATH, 'Neuron.swc'))
assert len(mt.get_duplicated_point_neurites(m)) == 0
assert len(mt.get_duplicated_point_neurites(m, tolerance=0.09)) == 1
assert len(mt.get_duplicated_point_neurites(m, tolerance=999)) == 4
Loading