Skip to content

Commit

Permalink
Use numpy.isclose in spatial point_to_section_segment.
Browse files Browse the repository at this point in the history
Add new keyword arguments ``rtol``,``atol`` to ``point_to_section_segment``.
  • Loading branch information
arnaudon authored Jul 16, 2021
1 parent 7f11061 commit 6fa973a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

Version 2.8.0
-------------
- Use numpy.isclose in spatial ``point_to_section_segment``. Add new keyword arguments ``rtol``,``atol`` to ``point_to_section_segment``. (#84)
- Add functionality to plot morphologies with synapses. Move all plot functionality under
``plot`` package. (#82)

Expand Down
11 changes: 5 additions & 6 deletions morph_tool/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

from neurom import COLS

X, Y, Z = 0, 1, 2


def point_to_section_segment(neuron, point):
def point_to_section_segment(neuron, point, rtol=1e-05, atol=1e-08):
'''Find section and segment that matches the point.
Only the first point found with the *exact* same coordinates as the point argument is considered
Args:
neuron (morphio.Morphology): neuron object
point (point): value of the point to find in the h5 file
rtol, atol (floats): precission of np.isclose
Returns:
Tuple: (NeuroM/MorphIO section ID, point ID) of the point the matches the input coordinates.
Expand All @@ -22,9 +21,9 @@ def point_to_section_segment(neuron, point):

for section in neuron.iter():
points = section.points
offset = np.where((points[:, X] == point[COLS.X]) &
(points[:, Y] == point[COLS.Y]) &
(points[:, Z] == point[COLS.Z]))
offset = np.where(
np.isclose(points[:, COLS.XYZ], point[COLS.XYZ], rtol=rtol, atol=atol).all(axis=1)
)
if offset[0].size:
return section.id, offset[0][0]

Expand Down
14 changes: 13 additions & 1 deletion tests/test_spatial.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os
from nose.tools import eq_, assert_raises
from pathlib import Path
from morphio import Morphology

from morph_tool import spatial


DATA = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data')
DATA = Path(__file__).absolute().parent / "data"


def test_point_to_section_segment():
neuron = Morphology(os.path.join(DATA, 'apical_test.h5'))
Expand All @@ -15,3 +17,13 @@ def test_point_to_section_segment():
eq_(segment, 1)

assert_raises(ValueError, spatial.point_to_section_segment, neuron, [24, 0, 0])

section, segment = spatial.point_to_section_segment(
neuron, [0., 25.1, 0.], rtol=1e-1, atol=1e-1
)
eq_(section, 1)
eq_(segment, 1)

section, segment = spatial.point_to_section_segment(neuron, [0., 25.0001, 0.])
eq_(section, 1)
eq_(segment, 1)

0 comments on commit 6fa973a

Please sign in to comment.