Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit ab03c51

Browse files
authored
Merge pull request #524 from juanchopanza/issue-523
Move iter_neurites to neurom.core and neurom modules.
2 parents 030d807 + 545a65a commit ab03c51

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

neurom/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
'''
6060

6161
from .version import VERSION as __version__
62-
from .core import iter_neurites
62+
from .core import iter_neurites, iter_sections
6363
from .fst import load_neuron, load_neurons, NeuriteType, get, NEURITE_TYPES
6464

6565

neurom/core/__init__.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
''' Core functionality and data types of NeuroM '''
3030

31-
from itertools import ifilter, imap
31+
from itertools import ifilter, imap, chain
3232
from .tree import Tree
3333
from .types import NeuriteType
3434
from ._soma import Soma, make_soma, SomaError
@@ -67,3 +67,28 @@ def iter_neurites(obj, mapfun=None, filt=None):
6767

6868
neurite_iter = iter(neurites) if filt is None else ifilter(filt, neurites)
6969
return neurite_iter if mapfun is None else imap(mapfun, neurite_iter)
70+
71+
72+
def iter_sections(neurites, iterator_type=Tree.ipreorder, neurite_filter=None):
73+
'''Iterator to the sections in a neurite, neuron or neuron population.
74+
75+
Parameters:
76+
neurites: neuron, population, neurite, or iterable containing neurite objects
77+
iterator_type: type of the iteration (ipreorder, iupstream, ibifurcation_point)
78+
neurite_filter: optional top level filter on properties of neurite neurite objects.
79+
80+
Examples:
81+
82+
Get the number of points in each section of all the axons in a neuron population
83+
84+
>>> import neurom as nm
85+
>>> from neurom.core import ites_sections
86+
>>> filter = lambda n : n.type == nm.AXON
87+
>>> n_points = [len(s.points) for s in iter_sections(pop, neurite_filter=filter)]
88+
89+
'''
90+
def _mapfun(neurite):
91+
'''Map an iterator type to the root node of a neurite'''
92+
return iterator_type(neurite.root_node)
93+
94+
return chain.from_iterable(imap(_mapfun, iter_neurites(neurites, filt=neurite_filter)))

neurom/core/tests/test_core.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828

2929
import os
3030
from os.path import join as joinp
31+
from itertools import ifilter
3132

3233
from nose import tools as nt
3334
import neurom as nm
3435
from neurom.core.population import Population
3536
from neurom import load_neuron
3637
from neurom import core
38+
from neurom.core import Tree
3739

3840
_path = os.path.dirname(os.path.abspath(__file__))
3941
DATA_PATH = joinp(_path, '../../../test_data')
@@ -74,3 +76,47 @@ def test_iter_neurites_filter_mapping():
7476

7577
ref = [500, 500, 500]
7678
nt.assert_sequence_equal(n, ref)
79+
80+
81+
def test_iter_sections_default():
82+
83+
ref = [s for n in POP.neurites for s in n.iter_sections()]
84+
nt.assert_sequence_equal(ref,
85+
[n for n in core.iter_sections(POP)])
86+
87+
88+
def test_iter_sections_filter():
89+
90+
for ntyp in nm.NEURITE_TYPES:
91+
a = [s for n in ifilter(lambda nn: nn.type == ntyp, POP.neurites)
92+
for s in n.iter_sections()]
93+
b = [n for n in core.iter_sections(POP, neurite_filter=lambda n : n.type == ntyp)]
94+
nt.assert_sequence_equal(a, b)
95+
96+
97+
def test_iter_sections_ipostorder():
98+
99+
ref = [s for n in POP.neurites for s in n.iter_sections(Tree.ipostorder)]
100+
nt.assert_sequence_equal(ref,
101+
[n for n in core.iter_sections(POP, iterator_type=Tree.ipostorder)])
102+
103+
104+
def test_iter_sections_ibifurcation():
105+
106+
ref = [s for n in POP.neurites for s in n.iter_sections(Tree.ibifurcation_point)]
107+
nt.assert_sequence_equal(ref,
108+
[n for n in core.iter_sections(POP, iterator_type=Tree.ibifurcation_point)])
109+
110+
111+
def test_iter_sections_iforking():
112+
113+
ref = [s for n in POP.neurites for s in n.iter_sections(Tree.iforking_point)]
114+
nt.assert_sequence_equal(ref,
115+
[n for n in core.iter_sections(POP, iterator_type=Tree.iforking_point)])
116+
117+
118+
def test_iter_sections_ileaf():
119+
120+
ref = [s for n in POP.neurites for s in n.iter_sections(Tree.ileaf)]
121+
nt.assert_sequence_equal(ref,
122+
[n for n in core.iter_sections(POP, iterator_type=Tree.ileaf)])

neurom/fst/_neuritefunc.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
'''Neurite functions'''
3030

3131
from functools import partial
32-
from itertools import imap, chain, izip
32+
from itertools import chain, izip
3333
import numpy as np
34-
from neurom.core import Tree, iter_neurites
34+
from neurom.core import Tree, iter_neurites, iter_sections
3535
from neurom.core.types import tree_type_checker as is_type
3636
from neurom.core.types import NeuriteType
3737
from neurom.geom import convex_hull
@@ -42,21 +42,6 @@
4242
bifurcation_partition)
4343

4444

45-
def iter_sections(neurites, iterator_type=Tree.ipreorder, neurite_filter=None):
46-
'''Returns an iterator to the nodes in a iterable of neurite objects
47-
48-
Parameters:
49-
neurites: neuron, population, neurite, or iterable containing neurite objects
50-
iterator_type: type of the iteration (ipreorder, iupstream, ibifurcation_point)
51-
neurite_filter: optional top level filter on properties of neurite neurite objects.
52-
'''
53-
def _mapfun(neurite):
54-
'''Map an iterator type to the root node of a neurite'''
55-
return iterator_type(neurite.root_node)
56-
57-
return chain.from_iterable(imap(_mapfun, iter_neurites(neurites, filt=neurite_filter)))
58-
59-
6045
def iter_segments(neurites, neurite_filter=None):
6146
'''Return an iterator to the segments in a collection of neurites
6247

0 commit comments

Comments
 (0)