Examples¶
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()
Basic feature extraction example¶
These basic examples illustrate the type of morphometrics that can be easily obtained
directly from the neurom
module, without the need for any other neurom
sub-modules or tools.
The idea here is to pre-package the most common analyses so that users can obtain the
morphometrics with a very minimal knowledge of python
and neurom
.
'''Easy analysis examples
These examples highlight most of the pre-packaged neurom.nm.get
morphometrics functionality.
'''
from __future__ import print_function
from pprint import pprint
import numpy as np
import neurom as nm
def stats(data):
'''Dictionary with summary stats for data
Returns:
dicitonary with length, mean, sum, standard deviation,\
min and max of data
'''
return {'len': len(data),
'mean': np.mean(data),
'sum': np.sum(data),
'std': np.std(data),
'min': np.min(data),
'max': np.max(data)}
def pprint_stats(data):
'''Pretty print summary stats for data'''
pprint(stats(data))
if __name__ == '__main__':
filename = 'test_data/swc/Neuron.swc'
# load a neuron from an SWC file
nrn = nm.load_neuron(filename)
# Get some soma information
# Soma radius and surface area
print("Soma radius", nm.get('soma_radii', nrn)[0])
print("Soma surface area", nm.get('soma_surface_areas', nrn)[0])
# Get information about neurites
# Most neurite data can be queried for a particular type of neurite.
# The allowed types are members of the NeuriteType enumeration.
# NEURITE_TYPES is a list of valid neurite types.
# We start by calling methods for different neurite types separately
# to warm up...
# number of neurites
print('Number of neurites (all):', nm.get('number_of_neurites', nrn)[0])
print('Number of neurites (axons):',
nm.get('number_of_neurites', nrn, neurite_type=nm.NeuriteType.axon)[0])
print('Number of neurites (apical dendrites):',
nm.get('number_of_neurites', nrn, neurite_type=nm.NeuriteType.apical_dendrite)[0])
print('Number of neurites (basal dendrites):',
nm.get('number_of_neurites', nrn, neurite_type=nm.NeuriteType.basal_dendrite)[0])
# number of sections
print('Number of sections:',
nm.get('number_of_sections', nrn)[0])
print('Number of sections (axons):',
nm.get('number_of_sections', nrn, neurite_type=nm.NeuriteType.axon)[0])
print('Number of sections (apical dendrites):',
nm.get('number_of_sections', nrn, neurite_type=nm.NeuriteType.apical_dendrite)[0])
print('Number of sections (basal dendrites):',
nm.get('number_of_sections', nrn, neurite_type=nm.NeuriteType.basal_dendrite)[0])
# number of sections per neurite
print('Number of sections per neurite:',
nm.get('number_of_sections_per_neurite', nrn))
print('Number of sections per neurite (axons):',
nm.get('number_of_sections_per_neurite', nrn, neurite_type=nm.NeuriteType.axon))
print('Number of sections per neurite (apical dendrites):',
nm.get('number_of_sections_per_neurite',
nrn, neurite_type=nm.NeuriteType.apical_dendrite))
print('Number of sections per neurite (basal dendrites):',
nm.get('number_of_sections_per_neurite',
nrn, neurite_type=nm.NeuriteType.apical_dendrite))
# OK, this is getting repetitive, so lets loop over valid neurite types.
# The following methods return arrays of measurements. We will gather some
# summary statistics for each and print them.
# Section lengths for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
sec_len = nm.get('section_lengths', nrn, neurite_type=ttype)
print('Section lengths (', ttype, '):', sep='')
pprint_stats(sec_len)
# Segment lengths for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
seg_len = nm.get('segment_lengths', nrn, neurite_type=ttype)
print('Segment lengths (', ttype, '):', sep='')
pprint_stats(seg_len)
# Section radial distances for all and different types of neurite
# Careful! Here we need to pass tree type as a named argument
for ttype in nm.NEURITE_TYPES:
sec_rad_dist = nm.get('section_radial_distances', nrn, neurite_type=ttype)
print('Section radial distance (', ttype, '):', sep='')
pprint_stats(sec_rad_dist)
# Section path distances for all and different types of neurite
# Careful! Here we need to pass tree type as a named argument
for ttype in nm.NEURITE_TYPES:
sec_path_dist = nm.get('section_path_distances', nrn, neurite_type=ttype)
print('Section path distance (', ttype, '):', sep='')
pprint_stats(sec_path_dist)
# Local bifurcation angles for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
local_bifangles = nm.get('local_bifurcation_angles', nrn, neurite_type=ttype)
print('Local bifurcation angles (', ttype, '):', sep='')
pprint_stats(local_bifangles)
# Remote bifurcation angles for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
rem_bifangles = nm.get('remote_bifurcation_angles', nrn, neurite_type=ttype)
print('Local bifurcation angles (', ttype, '):', sep='')
pprint_stats(rem_bifangles)
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.fst import iter_sections, iter_segments, sectionfunc
from neurom.core import Tree
from neurom.core.types import tree_type_checker, NEURITES
from neurom import morphmath as mm
import numpy as np
if __name__ == '__main__':
filename = 'test_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 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 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 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 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 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 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:',
np.mean(pts))
# get mean radius of segments
print('Mean radius of segments:',
np.mean(list(mm.segment_radius(s) for s in 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 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),
sep='')
# Number of bifurcation points.
print('Number of bifurcation points:',
sum(1 for _ in iter_sections(nrn,
iterator_type=Tree.ibifurcation_point)))
# Number of bifurcation points for apical dendrites
print('Number of bifurcation points (apical dendrites):',
sum(1 for _ in iter_sections(nrn,
iterator_type=Tree.ibifurcation_point,
neurite_filter=tree_type_checker(nm.APICAL_DENDRITE))))
# Maximum branch order
print('Maximum branch order:',
max(sectionfunc.branch_order(s) for s in 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))