neurom.io.utils

Utility functions and for loading neurons.

Functions

get_files_by_path

Get a file or set of files from a file path.

get_morph_files

Get a list of all morphology files in a directory.

load_neuron

Build section trees from a neuron or a h5, swc or asc file.

load_neurons

Create a population object.

Classes

NeuronLoader

Caching morphology loader.

exception neurom.io.utils.NeuroMError[source]

Bases: Exception

Base class for NeuroM errors.

class neurom.io.utils.Neuron(filename, name=None)[source]

Bases: morphio._morphio.mut.Morphology

Class representing a simple neuron.

Neuron constructor.

Parameters
  • filename (str|Path) – a filename

  • name (str) – a option neuron name

transform(trans)[source]

Return a copy of this neuron with a 3D transformation applied.

class neurom.io.utils.NeuronLoader(directory, file_ext=None, cache_size=None)[source]

Bases: object

Caching morphology loader.

Parameters
  • directory – path to directory with morphology files

  • file_ext – file extension to look for (if not set, will pick any of .swc|.h5|.asc)

  • cache_size – size of LRU cache (if not set, no caching done)

Initialize a NeuronLoader object.

get(name)[source]

Get name morphology data.

class neurom.io.utils.Population(files, name='Population', ignored_exceptions=(), cache=False)[source]

Bases: object

Neuron Population Class.

Offers an iterator over neurons within population, neurites of neurons, somas of neurons. It does not store the loaded neuron in memory unless the neuron has been already passed as loaded (instance of Neuron).

Construct a neuron population.

Parameters
  • files (collections.abc.Sequence[str|Path|Neuron]) – collection of neuron files or paths to them or instances of Neuron.

  • name (str) – Optional name for this Population

  • ignored_exceptions (tuple) – NeuroM and MorphIO exceptions that you want to ignore when loading neurons.

  • cache (bool) – whether to cache the loaded neurons in memory. If false then a neuron will be loaded everytime it is accessed within the population. Which is good when population is big. If true then all neurons will be loaded upon the construction and kept in memory.

neurom.io.utils.get_files_by_path(path)[source]

Get a file or set of files from a file path.

Return list of files with path

neurom.io.utils.get_morph_files(directory)[source]

Get a list of all morphology files in a directory.

Returns

list with all files with extensions ‘.swc’ , ‘h5’ or ‘.asc’ (case insensitive)

neurom.io.utils.load_neuron(neuron, reader=None)[source]

Build section trees from a neuron or a h5, swc or asc file.

Parameters
  • neuron (str|Path|Neuron|morphio.Morphology|morphio.mut.Morphology) –

    A neuron representation It can be:

    • a filename with the h5, swc or asc extension

    • a NeuroM Neuron object

    • a morphio mutable or immutable Morphology object

    • a stream that can be put into a io.StreamIO object. In this case, the READER argument must be passed with the corresponding file format (asc, swc and h5)

  • reader (str) – Optional, must be provided if neuron is a stream to specify the file format (asc, swc, h5)

Returns

A Neuron object

Examples:

neuron = neurom.load_neuron('my_neuron_file.h5')

neuron = neurom.load_neuron(morphio.Morphology('my_neuron_file.h5'))

neuron = nm.load_neuron(io.StringIO('''((Dendrite)
                                       (3 -4 0 2)
                                       (3 -6 0 2)
                                       (3 -8 0 2)
                                       (3 -10 0 2)
                                       (
                                         (0 -10 0 2)
                                         (-3 -10 0 2)
                                         |
                                         (6 -10 0 2)
                                         (9 -10 0 2)
                                       )
                                       )'''), reader='asc')
neurom.io.utils.load_neurons(neurons, name=None, ignored_exceptions=(), cache=False)[source]

Create a population object.

From all morphologies in a directory of from morphologies in a list of file names.

Parameters
  • neurons (str|Path|Iterable[Path]) – path to a folder or list of paths to neuron files

  • name (str) – optional name of population. By default ‘Population’ or filepath basename depending on whether neurons is list or directory path respectively.

  • ignored_exceptions (tuple) – NeuroM and MorphIO exceptions that you want to ignore when loading neurons

  • cache (bool) – whether to cache the loaded neurons in memory

Returns

population object

Return type

Population