Examples

Morphology file data consistency checks

(nrm)$ morph_check some/data/path/morph_file.swc # single file
INFO: ================================
INFO: Check file some/data/path/morph_file.swc...
INFO: Has valid soma? PASS
INFO: Has Apical Dendrite? PASS
INFO: Has Basal Dendrite? PASS
INFO: All neurites have non-zero radius? PASS
INFO: All segments have non-zero length? PASS
INFO: All sections have non-zero length? PASS
INFO: Check result: PASS
INFO: ================================


(nrm)$ morph_check some/data/path # all files in directory
....

Basic eyz.Neuron usage

  • Load a neuron and obtain some information from it:
>>> from neurom import ezy
>>> nrn = ezy.load_neuron('some/data/path/morph_file.swc')
>>> apical_seg_lengths = nrn.get_segment_lengths(ezy.TreeType.apical_dendrite)
>>> axon_sec_lengths = nrn.get_section_lengths(ezy.TreeType.axon)
  • Visualize a neuronal morphology:
>>> # Initialize nrn as above
>>> fig, ax = ezy.view(nrn)
>>> fig.show()

Basic ezy examples script

These basic examples illustrate the type of morphometrics that can be easily obtained from the ezy module, without the need for any other neurom 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.ezy.Neuron
morphometrics functionality.

'''

from __future__ import print_function
from pprint import pprint
from neurom import ezy
import numpy as np


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 = ezy.load_neuron(filename)

    # Get some soma information
    # Soma radius and surface area
    print("Soma radius", nrn.get_soma_radius())
    print("Soma surface area", nrn.get_soma_surface_area())

    # Get information about neurites
    # Most neurite data can be queried for a particular type of neurite.
    # The allowed types are members of the TreeType 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):', nrn.get_n_neurites())
    print('Number of neurites (axons):', nrn.get_n_neurites(ezy.TreeType.axon))
    print('Number of neurites (apical dendrites):',
          nrn.get_n_neurites(ezy.TreeType.apical_dendrite))
    print('Number of neurites (basal dendrites):',
          nrn.get_n_neurites(ezy.TreeType.basal_dendrite))

    # number of sections
    print('Number of sections:', nrn.get_n_sections())
    print('Number of sections (axons):', nrn.get_n_sections(ezy.TreeType.axon))
    print('Number of sections (apical dendrites):',
          nrn.get_n_sections(ezy.TreeType.apical_dendrite))
    print('Number of sections (basal dendrites):',
          nrn.get_n_sections(ezy.TreeType.basal_dendrite))

    # number of sections per neurite
    print('Number of sections per neurite:',
          nrn.get_n_sections_per_neurite())
    print('Number of sections per neurite (axons):',
          nrn.get_n_sections_per_neurite(ezy.TreeType.axon))
    print('Number of sections per neurite (apical dendrites):',
          nrn.get_n_sections_per_neurite(ezy.TreeType.apical_dendrite))
    print('Number of sections per neurite (basal dendrites):',
          nrn.get_n_sections_per_neurite(ezy.TreeType.basal_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 ezy.NEURITE_TYPES:
        sec_len = nrn.get_section_lengths(ttype)
        print('Section lengths (', ttype, '):', sep='')
        pprint_stats(sec_len)

    # Segment lengths for all and different types of neurite
    for ttype in ezy.NEURITE_TYPES:
        seg_len = nrn.get_segment_lengths(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 ezy.NEURITE_TYPES:
        sec_rad_dist = nrn.get_section_radial_distances(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 ezy.NEURITE_TYPES:
        sec_path_dist = nrn.get_section_path_distances(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 ezy.NEURITE_TYPES:
        local_bifangles = nrn.get_local_bifurcation_angles(ttype)
        print('Local bifurcation angles (', ttype, '):', sep='')
        pprint_stats(local_bifangles)

    # Remote bifurcation angles for all and different types of neurite
    for ttype in ezy.NEURITE_TYPES:
        rem_bifangles = nrn.get_remote_bifurcation_angles(ttype)
        print('Local bifurcation angles (', ttype, '):', sep='')
        pprint_stats(rem_bifangles)

    # Neuron's bounding box
    print('Bounding box ((min x, y, z), (max x, y, z))',
          nrn.bounding_box())

Advanced ezy examples script

These slightly more complex examples illustrate what can be done with the ezy module in combination with 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 easy analysis examples

These examples highlight more advanced neurom.ezy.Neuron
morphometrics functionality.

'''

from __future__ import print_function
from neurom import segments as seg
from neurom import sections as sec
from neurom import bifurcations as bif
from neurom import points as pts
from neurom import iter_neurites
from neurom import ezy
from neurom.core.types import tree_type_checker
from neurom.analysis import morphtree as mt
import numpy as np


if __name__ == '__main__':

    filename = 'test_data/swc/Neuron.swc'

    #  load a neuron from an SWC file
    nrn = ezy.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 length of all neurites in cell by iterating over sections,
    # and summing the section lengths
    print('Total neurite length:',
          sum(iter_neurites(nrn, sec.end_point_path_length)))

    # 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:',
          sum(iter_neurites(nrn, seg.length)))

    # get volume of all neurites in cell by summing over segment
    # volumes
    print('Total neurite volume:',
          sum(iter_neurites(nrn, seg.volume)))

    # get area of all neurites in cell by summing over segment
    # areas
    print('Total neurite surface area:',
          sum(iter_neurites(nrn, seg.area)))

    # get total number of points in cell.
    # iter_neurites needs a mapping function, so we pass the identity.
    print('Total number of points:',
          sum(1 for _ in iter_neurites(nrn, pts.identity)))

    # get mean radius of points in cell.
    # p[COLS.R] yields the radius for point p.
    print('Mean radius of points:',
          np.mean([r for r in iter_neurites(nrn, pts.radius)]))

    # get mean radius of segments
    print('Mean radius of segments:',
          np.mean(list(iter_neurites(nrn, seg.radius))))

    # get stats for the segment taper rate, for different types of neurite
    for ttype in ezy.NEURITE_TYPES:
        ttt = ttype
        seg_taper_rate = list(iter_neurites(nrn, seg.taper_rate, 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:', bif.count(nrn))

    # Number of bifurcation points for apical dendrites
    print('Number of bifurcation points (apical dendrites):',
          sum(1 for _ in iter_neurites(nrn, bif.identity,
                                       tree_type_checker(ezy.TreeType.apical_dendrite))))

    # Maximum branch order
    # We create a custom branch_order section_function
    # and use the generalized iteration method
    @sec.section_function(as_tree=True)
    def branch_order(s):
        '''Get the branch order of a section'''
        return mt.branch_order(s)

    print('Maximum branch order:',
          max(bo for bo in iter_neurites(nrn, branch_order)))

    # Neuron's bounding box
    print('Bounding box ((min x, y, z), (max x, y, z))',
          nrn.bounding_box())