Skip to content

Note

Click here to download the full example code

The Basics#

This tutorial will introduce some of the basics concepts in NAVis.

This is not supposed to be comprehensive but rather to give you a flavor of how things work. For inspiriation, explore the example gallery and for detailed explanations have a look at the API documentation.

Single Neurons#

NAVis lets you import neurons from a variety of local and remote sources. For demonstration purposes NAVis comes with a bunch of fruit fly neurons from the Janelia hemibrain project:

Note

We will cover loading neurons from different sources in a different tutorial.

import navis

# Load a single neuron
n = navis.example_neurons(n=1, kind='skeleton')
n
type navis.TreeNeuron
name DA1_lPN_R
id 1734350788
n_nodes 4465
n_connectors 2705
n_branches 599
n_leafs 618
cable_length 266476.875
soma 4177
units 8 nanometer
created_at 2024-09-18 12:13:17.130287
id 1734350788
origin /home/runner/work/navis/navis/navis/data/swc/1...
file 1734350788.swc

In above code we loaded one of the example neurons. NAVis represents neurons as navis.TreeNeuron, navis.MeshNeuron, navis.VoxelNeuron or navis.Dotprops.

In the above example we asked for a skeleton, so the neuron returned is a TreeNeuron. This class is essentially a wrapper around the actual neuron data (the SWC table in this case) and has some convenient features.

Node data is stored as pandas.DataFrame:

n.nodes.head()
node_id label x y z radius parent_id type
0 1 0 15784.0 37250.0 28062.0 10.000000 -1 root
1 2 0 15764.0 37230.0 28082.0 18.284300 1 slab
2 3 0 15744.0 37190.0 28122.0 34.721401 2 slab
3 4 0 15744.0 37150.0 28202.0 34.721401 3 slab
4 5 0 15704.0 37130.0 28242.0 34.721401 4 slab

Pandas

pandas is the data science library for Python and will help you analyze and visualize your data. We highly recommend familiarizing yourself with pandas! There are plenty of good tutorials out there but pandas' own 10 Minutes to pandas is a good place to start.

Try typing in "n." and hitting tab: most attributes and functions are accessible via autocompletion. If you don't know what a function does, check out the documentation using help() or via the API documentation:

help(navis.TreeNeuron.root)

help(navis.TreeNeuron.downsample)

You will notice that many NAVis functions that accept neurons have an inplace parameter. This is analogous to pandas:

Downsample a copy, leaving the original unchanged (this is the default for almost all functions)

n_ds = n.downsample(10, inplace=False)

# Downsample original neuron
n.downsample(10, inplace=True)

n
type navis.TreeNeuron
name DA1_lPN_R
id 1734350788
n_nodes 1304
n_connectors 2705
n_branches 599
n_leafs 618
cable_length 247213.921875
soma 4177
units 8 nanometer
created_at 2024-09-18 12:13:17.130287
id 1734350788
origin /home/runner/work/navis/navis/navis/data/swc/1...
file 1734350788.swc

navis.TreeNeuron functions such as .downsample() are shorthands for calling the actual NAVis functions. So above code is equivalent to:

n = navis.example_neurons(n=1, kind='skeleton')
n_ds = navis.downsample_neuron(n, downsampling_factor=10, inplace=False)
n_ds
type navis.TreeNeuron
name DA1_lPN_R
id 1734350788
n_nodes 1304
n_connectors 2705
n_branches 599
n_leafs 618
cable_length 247213.921875
soma 4177
units 8 nanometer
created_at 2024-09-18 12:13:17.229311
id 1734350788
origin /home/runner/work/navis/navis/navis/data/swc/1...
file 1734350788.swc

Lists of Neurons#

For multiple neurons, NAVis uses navis.NeuronList:

nl = navis.example_neurons(n=3, kind='skeleton')
nl
<class 'navis.core.neuronlist.NeuronList'> containing 3 neurons (875.1KiB)
type name id n_nodes n_connectors n_branches n_leafs cable_length soma units created_at origin file
0 navis.TreeNeuron DA1_lPN_R 1734350788 4465 2705 599 618 266476.87500 4177.0 8 nanometer 2024-09-18 12:13:17.281363 /home/runner/work/navis/navis/navis/data/swc/1... 1734350788.swc
1 navis.TreeNeuron DA1_lPN_R 1734350908 4847 3042 735 761 304332.65625 6.0 8 nanometer 2024-09-18 12:13:17.289664 /home/runner/work/navis/navis/navis/data/swc/1... 1734350908.swc
2 navis.TreeNeuron DA1_lPN_R 722817260 4332 3136 633 656 274703.37500 NaN 8 nanometer 2024-09-18 12:13:17.297253 /home/runner/work/navis/navis/navis/data/swc/7... 722817260.swc

navis.NeuronList is a container and behaves like a glorified list:

nl[0]
type navis.TreeNeuron
name DA1_lPN_R
id 1734350788
n_nodes 4465
n_connectors 2705
n_branches 599
n_leafs 618
cable_length 266476.875
soma 4177
units 8 nanometer
created_at 2024-09-18 12:13:17.281363
id 1734350788
origin /home/runner/work/navis/navis/navis/data/swc/1...
file 1734350788.swc

navis.NeuronList lets you run/access all functions (methods) and properties of the neurons it contrains:

nl.cable_length

Out:

array([266476.88, 304332.66, 274703.38], dtype=float32)
nl_ds = nl.downsample(10, inplace=False)

nl_ds
<class 'navis.core.neuronlist.NeuronList'> containing 3 neurons (670.4KiB)
type name id n_nodes n_connectors n_branches n_leafs cable_length soma units created_at origin file
0 navis.TreeNeuron DA1_lPN_R 1734350788 1304 2705 599 618 247213.921875 4177.0 8 nanometer 2024-09-18 12:13:17.281363 /home/runner/work/navis/navis/navis/data/swc/1... 1734350788.swc
1 navis.TreeNeuron DA1_lPN_R 1734350908 1558 3042 735 761 282457.125000 6.0 8 nanometer 2024-09-18 12:13:17.289664 /home/runner/work/navis/navis/navis/data/swc/1... 1734350908.swc
2 navis.TreeNeuron DA1_lPN_R 722817260 1346 3136 633 656 254727.546875 NaN 8 nanometer 2024-09-18 12:13:17.297253 /home/runner/work/navis/navis/navis/data/swc/7... 722817260.swc

Let's finish this primer with some eye candy

nl.plot3d(backend='plotly')

What next?#

Total running time of the script: ( 0 minutes 1.838 seconds)

Download Python source code: plot_00_basic_neurons.py

Download Jupyter notebook: plot_00_basic_neurons.ipynb

Gallery generated by mkdocs-gallery