In following code samples, the prompt >>> indicates a python interpreter session started with the virtualenv activated. That gives access to the neurom installation.

Fast analysis with neurom

Here we load a neuron and obtain some information from it:

>>> import neurom as nm
>>> nrn = nm.load_neuron('some/data/path/morph_file.swc')
>>> ap_seg_len = nm.get('segment_lengths', nrn, neurite_type=nm.APICAL_DENDRITE)
>>> ax_sec_len = nm.get('section_lengths', nrn, neurite_type=nm.AXON)

Morphology visualization with the neurom.viewer module

Here we visualize a neuronal morphology:

>>> # Initialize nrn as above
>>> from neurom import viewer
>>> fig, ax = viewer.draw(nrn)
>>> fig.show()
>>> fig, ax = viewer.draw(nrn, mode='3d') # valid modes '2d', '3d', 'dendrogram'
>>> fig.show()

Advanced iterator-based feature extraction example

These slightly more complex examples illustrate what can be done with the neurom module’s various generic iterators and simple morphometric functions.

The idea here is that there is a great deal of flexibility to build new analyses based on some limited number of orthogonal iterator and morphometric components that can be combined in many ways. Users with some knowledge of python and neurom can easily implement code to obtain new morphometrics.

All of the examples in the previous sections can be implemented in a similar way to those presented here.

"""Advanced analysis examples

These examples highlight more advanced neurom
morphometrics functionality using iterators.


from __future__ import print_function
from neurom.core.dataformat import COLS
import neurom as nm
from neurom import geom
from neurom.features import sectionfunc
from neurom.core import Section
from neurom.core.types import tree_type_checker, NEURITES
from neurom import morphmath as mm
import numpy as np

if __name__ == '__main__':

    filename = 'tests/data/swc/Neuron.swc'

    #  load a neuron from an SWC file
    nrn = nm.load_neuron(filename)

    # Some examples of what can be done using iteration
    # instead of pre-packaged functions that return lists.
    # The iterations give us a lot of flexibility: we can map
    # any function that takes a segment or section.

    # Get of all neurites in cell by iterating over sections,
    # and summing the section lengths
    def sec_len(sec):
        """Return the length of a section."""
        return mm.section_length(sec.points)

    print('Total neurite length (sections):',
          sum(sec_len(s) for s in nm.iter_sections(nrn)))

    # Get length of all neurites in cell by iterating over segments,
    # and summing the segment lengths.
    # This should yield the same result as iterating over sections.
    print('Total neurite length (segments):',
          sum(mm.segment_length(s) for s in nm.iter_segments(nrn)))

    # get volume of all neurites in cell by summing over segment
    # volumes
    print('Total neurite volume:',
          sum(mm.segment_volume(s) for s in nm.iter_segments(nrn)))

    # get area of all neurites in cell by summing over segment
    # areas
    print('Total neurite surface area:',
          sum(mm.segment_area(s) for s in nm.iter_segments(nrn)))

    # get total number of neurite points in cell.
    def n_points(sec):
        """number of points in a section."""
        n = len(sec.points)
        # Non-root sections have duplicate first point
        return n if sec.parent is None else n - 1

    print('Total number of points:',
          sum(n_points(s) for s in nm.iter_sections(nrn)))

    # get mean radius of neurite points in cell.
    # p[COLS.R] yields the radius for point p.
    # Note: this includes duplicated points at beginning of
    # non-trunk sections
    print('Mean radius of points:',
          np.mean([s.points[:, COLS.R] for s in nm.iter_sections(nrn)]))

    # get mean radius of neurite points in cell.
    # p[COLS.R] yields the radius for point p.
    # Note: this includes duplicated points at beginning of
    # non-trunk sections
    pts = [p[COLS.R] for s in nrn.sections[1:] for p in s.points]
    print('Mean radius of points:',

    # get mean radius of segments
    print('Mean radius of segments:',
          np.mean(list(mm.segment_radius(s) for s in nm.iter_segments(nrn))))

    # get stats for the segment taper rate, for different types of neurite
    for ttype in NEURITES:
        ttt = ttype
        seg_taper_rate = [mm.segment_taper_rate(s)
                          for s in nm.iter_segments(nrn, neurite_filter=tree_type_checker(ttt))]

        print('Segment taper rate (', ttype,
              '):\n  mean=', np.mean(seg_taper_rate),
              ', std=', np.std(seg_taper_rate),
              ', min=', np.min(seg_taper_rate),
              ', max=', np.max(seg_taper_rate),

    # Number of bifurcation points.
    print('Number of bifurcation points:',
          sum(1 for _ in nm.iter_sections(nrn,

    # Number of bifurcation points for apical dendrites
    print('Number of bifurcation points (apical dendrites):',
          sum(1 for _ in nm.iter_sections(nrn,

    # Maximum branch order
    print('Maximum branch order:',
          max(sectionfunc.branch_order(s) for s in nm.iter_sections(nrn)))

    # Neuron's bounding box
    # Note: does not account for soma radius
    print('Bounding box ((min x, y, z), (max x, y, z))', geom.bounding_box(nrn))

Getting Log Information

neurom emits many logging statements during the course of its functioning. They are emitted in the neurom namespace, and can thus be filtered based on this. An example of setting up a handler is:

>>> import logging
>>> # setup which namespace will be examined, and at what level
>>> # in this case we only want messages from 'neurom' and all messages
>>> # (ie: DEBUG, INFO, etc)
>>> logger = logging.getLogger('neurom')
>>> logger.setLevel(logging.DEBUG)
>>> # setup where the output will be saved, in this case the console
>>> sh = logging.StreamHandler()
>>> logger.addHandler(sh)

For more information on logging, it is recommended to read the official Python logging HOWTOs: Python 3.