.. _local_data_meshes_tut: Meshes ------ ``navis`` lets you import neurons from a variety of local and remote sources. In this tutorial you will learn how to load meshes from local data and - failing that - construct them from scratch. For loading remote data (e.g. the MICrONS, neuromorpho, Virtual Fly Brain or Janelia hemibrain datasets), ``navis`` has dedicated interfaces. See the :ref:`example gallery ` for tutorials on these. ``navis`` knows two types of meshes: :class:`navis.MeshNeuron` for neurons and :class:`navis.Volume` for e.g. neuropil or brain meshes. For reading run-of-the-mill files containing meshes, ``navis`` provides a single function: :func:`navis.read_mesh`. Under the hood, that function uses ``trimesh.load_mesh`` which supports most of the common formats (`.obj`, `.ply`, `.stl`, etc.). .. code:: ipython3 import navis .. code:: ipython3 # Load an example file (here a FlyWire neuron I downloaded and saved locally) mesh = navis.read_mesh('~/Downloads/test_neuron.stl') mesh .. raw:: html
type navis.MeshNeuron
name test_neuron
id 720575940633212055
units 1 dimensionless
n_vertices 40905
n_faces 82126
The interface is similar to ``read_swc`` in that you can point :func:`navis.read_mesh` at single file or at folders with multiple files: .. code:: ipython3 # When reading all files in folder you have to specificy the file extension (e.g. *.stl) meshes = navis.read_mesh('~/Downloads/neurons/*.stl') meshes .. raw:: html <class 'navis.core.neuronlist.NeuronList'> containing 8 neurons (30.1MiB)
type name units n_vertices n_faces
0 navis.MeshNeuron 720575940614739286 1 dimensionless 59628 119270
1 navis.MeshNeuron 720575940632670433 1 dimensionless 60880 121759
... ... ... ... ... ...
6 navis.MeshNeuron 720575940633280107 1 dimensionless 43403 85888
7 navis.MeshNeuron 720575940615856345 1 dimensionless 83919 167793
By default, :func:`navis.read_mesh` will return neurons. Use the ``output`` parameter to get a :class:`navis.Volume` (or a ``trimesh.Trimesh``) instead: .. code:: ipython3 # Load a mesh file into a Volume vol = navis.read_mesh('~/Downloads/test_mesh.stl', output='volume') vol .. parsed-literal:: For saving :class:`~navis.MeshNeuron` or :class:`~navis.Volume` to disk, use :func:`navis.write_mesh`: .. code:: ipython3 # Save single neuron to file m = navis.example_neurons(1, kind='mesh') navis.write_mesh(m, '~/Downloads/neuron.obj') .. code:: ipython3 # Save a bunch of neurons to mesh nl = navis.example_neurons(3, kind='mesh') navis.write_mesh(nl, '~/Downloads/', filetype='obj') Manually constructing meshes ============================ It's super easy to construct :class:`~navis.MeshNeuron` and :class:`~navis.Volume` from scratch - they are just vertices and faces after all. So if e.g. your mesh file format is not covered by :func:`~navis.read_mesh` or you created the mesh yourself using a marching cube algorithm, just create the neuron/volume yourself: .. code:: ipython3 import numpy as np # Create some mock vertices vertices = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) # Make a single triangular face using the vertex indices faces = np.array([[0, 1, 2]]) .. code:: ipython3 # Turn into MeshNeuron m = navis.MeshNeuron((vertices, faces), name='my_mesh', units='microns') m .. raw:: html
type navis.MeshNeuron
name my_mesh
units 1 micrometer
n_vertices 3
n_faces 1
.. code:: ipython3 vol = navis.Volume(vertices, faces, name='my_volume') vol .. parsed-literal:: One thing to keep in mind here is that ``navis`` only works with triangular faces (i.e. no quads or polygons)! Please see the docstring of :class:`navis.MeshNeuron` and :class:`navis.Volume` for details. Also note that all navis neurons can be stored to disk using ``pickle`` - see the :ref:`pickling tutorial `. Hopefully the above has given you some entry points on how to load/save your data. See also the :ref:`I/O API reference api_io>`. Keep in mind that you can also convert one neuron type into another - for example by skeletonizing ``MeshNeurons`` (see :ref:`neuron_conversion`).