Examples¶
Note
In following code samples, the prompt >>>
indicates a python interpreter session
started with the virtualenv activated. That gives access to the neurom
installation.
Analysis with neurom
¶
Here we load a morphology and obtain some information from it:
>>> import neurom as nm
>>> m = nm.load_morphology("tests/data/swc/Neuron.swc")
>>> ap_seg_len = nm.get('segment_lengths', m, neurite_type=nm.APICAL_DENDRITE)
>>> ax_sec_len = nm.get('section_lengths', m, neurite_type=nm.AXON)
Morphology visualization with the neurom.view
module¶
Here we visualize a morphology:
>>> # Initialize m as above
>>> from neurom.view import plot_morph, plot_morph3d, plot_dendrogram
>>> plot_morph(m)
>>> plot_morph3d(m)
>>> plot_dendrogram(m)
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 pathlib import Path
from neurom.core.dataformat import COLS
import neurom as nm
from neurom import geom
from neurom.features import section
from neurom.core import Section
from neurom.core.types import tree_type_checker, NEURITES
from neurom import morphmath as mm
import numpy as np
PACKAGE_DIR = Path(__file__).resolve().parent.parent
def main():
filename = Path(PACKAGE_DIR, "tests/data/swc/Neuron.swc")
# load a neuron from an SWC file
m = nm.load_morphology(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(m)))
# 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(m))
)
# 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(m)))
# 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(m)))
# 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(m)))
# 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(m)]))
# 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 m.sections[1:] for p in s.points]
print("Mean radius of points:", np.mean(pts))
# get mean radius of segments
print(
"Mean radius of segments:", np.mean(list(mm.segment_radius(s) for s in nm.iter_segments(m)))
)
# 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(m, 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),
sep="",
)
# Number of bifurcation points.
print(
"Number of bifurcation points:",
sum(1 for _ in nm.iter_sections(m, iterator_type=Section.ibifurcation_point)),
)
# Number of bifurcation points for apical dendrites
print(
"Number of bifurcation points (apical dendrites):",
sum(
1
for _ in nm.iter_sections(
m,
iterator_type=Section.ibifurcation_point,
neurite_filter=tree_type_checker(nm.APICAL_DENDRITE),
)
),
)
# Maximum branch order
print("Maximum branch order:", max(section.branch_order(s) for s in nm.iter_sections(m)))
# Morphology's bounding box
# Note: does not account for soma radius
print("Bounding box ((min x, y, z), (max x, y, z))", geom.bounding_box(m))
if __name__ == "__main__":
main()
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.