navis.read_swc¶
- navis.read_swc(f, connector_labels={}, soma_label=1, include_subdirs=False, delimiter=' ', parallel='auto', precision=32, fmt='{name}.swc', read_meta=True, limit=None, **kwargs)[source]¶
Create Neuron/List from SWC file.
This import is following format specified here
- Parameters:
f (str | pandas.DataFrame | iterable) – Filename, folder, SWC string, URL or DataFrame. If folder, will import all
.swc
files. If a.zip
,.tar
or.tar.gz
file will read all SWC files in the file. See alsolimit
parameter.connector_labels (dict, optional) – If provided will extract connectors from SWC. Dictionary must map type to label:
{'presynapse': 7, 'postsynapse': 8}
include_subdirs (bool, optional) – If True and
f
is a folder, will also search subdirectories for.swc
files.delimiter (str) – Delimiter to use. Passed to
pandas.read_csv
.parallel ("auto" | bool | int) – Defaults to
auto
which means only use parallel processing if more than 200 SWC are imported. Spawning and joining processes causes overhead and is considerably slower for imports of small numbers of neurons. Integer will be interpreted as the number of cores (otherwise defaults toos.cpu_count() // 2
).precision (int [8, 16, 32, 64] | None) – Precision for data. Defaults to 32 bit integers/floats. If
None
will let pandas infer data types - this typically leads to higher than necessary precision.fmt (str) –
Formatter to specify how filenames are parsed into neuron attributes. Some illustrative examples:
{name}.swc
(default) uses the filename (minus the suffix) as the neuron’s name property{id}.swc
uses the filename as the neuron’s ID property{name,id}.swc
uses the filename as the neuron’s name and ID properties{name}.{id}.swc
splits the filename at a “.” and uses the first part as name and the second as ID{name,id:int}.swc
same as above but converts into integer for the ID{name}_{myproperty}.swc
splits the filename at “_” and uses the first part as name and as a generic “myproperty” property{name}_{}_{id}.swc
splits the filename at “_” and uses the first part as name and the last as ID. The middle part is ignored.
Throws a ValueError if pattern can’t be found in filename. Ignored for DataFrames.
read_meta (bool) – If True and SWC header contains a line with JSON-encoded meta data e.g. (
# Meta: {'id': 123}
), these data will be read as neuron properties. fmt takes precedence. Will try to assign meta data directly as neuron attribute (e.g.neuron.id
). Failing that (can happen for properties intrinsic toTreeNeurons
), will add a.meta
dictionary to the neuron.limit (int, optional) – If reading from a folder you can use this parameter to read only the first
limit
SWC files. Useful if wanting to get a sample from a large library of skeletons.**kwargs – Keyword arguments passed to the construction of
navis.TreeNeuron
. You can use this to e.g. set meta data.
- Returns:
navis.TreeNeuron – Contains SWC file header as
.swc_header
attribute.navis.NeuronList – If import of multiple SWCs will return NeuronList of TreeNeurons.
See also
navis.write_swc()
Export neurons as SWC files.
Examples
Read a single file:
>>> s = navis.read_swc('skeleton.swc')
Read all .swc files in a directory:
>>> s = navis.read_swc('/some/directory/')
Read all .swc files in a zip archive:
>>> s = navis.read_swc('skeletons.zip')
Sample first 100 SWC files a zip archive:
>>> s = navis.read_swc('skeletons.zip', limit=100)