navis 1.4.0
  • Install
  • Quickstart
  • Tutorials
  • API
  • Changelog
  • Github
  • Ecosystem
  • Site
    • Page
        • navis.write_swc
          • write_swc()

    navis.write_swc¶

    navis.write_swc(x, filepath, header=None, write_meta=True, labels=True, export_connectors=False, return_node_map=False)[source]¶

    Write TreeNeuron(s) to SWC.

    Follows the format specified here.

    Parameters:
    • x (TreeNeuron | Dotprops | NeuronList) – If multiple neurons, will generate a single SWC file for each neuron (see also filepath).

    • filepath (str | pathlib.Path | list thereof) – Destination for the SWC files. See examples for options. If x is multiple neurons, filepath must either be a folder, a “formattable” filename, a filename ending in .zip or a list of filenames (one for each neuron in x). Existing files will be overwritten!

    • header (str | None, optional) – Header for SWC file. If not provided, will use generic header.

    • write_meta (bool | list | dict) –

      If not False, will add meta data as JSON-formatted string to the header:

      True: adds neuron `id`, `name` and `units`
      list: use to set which properties, e.g. ['id', 'units']
      dict: use to set meta data, e.g. {'template': 'JRC2018F'}
      

      This parameter is ignored if custom header is provided.

    • labels (str | dict | bool, optional) –

      Node labels. Can be:

      str : column name in node table
      dict: must be of format {node_id: 'label', ...}.
      bool: if True, will generate automatic labels, if False all nodes have label "0".
      

    • export_connectors (bool, optional) – If True, will label nodes with pre- (“7”) and postsynapse (“8”). Because only one label can be given this might drop synapses (i.e. in case of multiple pre- and/or postsynapses on a single node)! labels must be True for this to have any effect.

    • return_node_map (bool) – If True, will return a dictionary mapping the old node ID to the new reindexed node IDs in the file.

    Returns:

    node_map – Only if return_node_map=True.

    Return type:

    dict

    See also

    navis.read_swc()

    Import skeleton from SWC files.

    Examples

    Save a single neuron to a specific file:

    >>> import navis
    >>> n = navis.example_neurons(1, kind='skeleton')
    >>> navis.write_swc(n, tmp_dir / 'my_neuron.swc')
    

    Save two neurons to specific files:

    >>> import navis
    >>> nl = navis.example_neurons(2, kind='skeleton')
    >>> navis.write_swc(nl, [tmp_dir / 'my_neuron1.swc', tmp_dir / 'my_neuron2.swc'])
    

    Save multiple neurons to a folder (must exist). Filenames will be autogenerated as “{neuron.id}.swc”:

    >>> import navis
    >>> nl = navis.example_neurons(5, kind='skeleton')
    >>> navis.write_swc(nl, tmp_dir)
    

    Save multiple neurons to a folder but modify the pattern for the autogenerated filenames:

    >>> import navis
    >>> nl = navis.example_neurons(5, kind='skeleton')
    >>> navis.write_swc(nl, tmp_dir / 'skel-{neuron.name}.swc')
    

    Save multiple neurons to a zip file:

    >>> import navis
    >>> nl = navis.example_neurons(5, kind='skeleton')
    >>> navis.write_swc(nl, tmp_dir / 'neuronlist.zip')
    

    Save multiple neurons to a zip file but modify the filenames:

    >>> import navis
    >>> nl = navis.example_neurons(5, kind='skeleton')
    >>> navis.write_swc(nl, tmp_dir / 'skel-{neuron.name}.swc@neuronlist.zip')
    

    Back to top

    Source

    © Copyright 2018, Philipp Schlegel.
    Created using Sphinx 5.3.0.