swctools is a Python toolbox for loading, parsing, modeling, analyzing, and visualizing neuronal morphologies stored in the SWC format. It is designed for interactive use in Jupyter notebooks and provides a graph-based core (NetworkX), computational geometry utilities (NumPy), and interactive 3D visualization (Plotly).
Status: pre-alpha, actively evolving. Core parsing, models, geometry, and basic visualization are implemented.
Docs: https://jmrfox.github.io/swctools/
Demo notebooks can be found in the notebooks
directory.
- SWC parser:
parse_swc()
with robust error messages, header reconnection directives, iterable/file/string sources - Data models:
SWCModel
(networkx.DiGraph
) for directed parent➔child topology with node attributes (t, x, y, z, r
)GeneralModel
(networkx.Graph
) for visualization; applies# CYCLE_BREAK reconnect i j
merges (union-find)- Shared graph metrics via
_graph_attributes()
andprint_attributes()
helpers
- Geometry:
Segment
dataclass and frustum meshing utilities (frustum_mesh
,batch_frusta
)FrustaSet.from_general_model()
to build a batched frusta mesh from aGeneralModel
PointSet
for low-res spheres at arbitrary xyz points (for overlay markers)
- Visualization:
plot_centroid(general_model, ...)
for skeleton plotting (Scatter3d
)plot_frusta(frusta_set, ..., radius_scale=1.0)
for volumetric frusta rendering (Mesh3d
)plot_frusta_with_centroid(gm, frusta, ...)
to overlay skeleton and meshplot_frusta_slider(frusta, min_scale, max_scale, steps)
interactive radius scale sliderplot_model(...)
master entry point combining centroid, frusta, slider, andPointSet
overlays- Global config via
set_config(...)
(equal axes enforced by default, width/height, template)
- Roadmap: morphometrics and analyses, I/O conversions, time-varying scalars and animations
-
SWCModel
(networkx.DiGraph
)- Nodes keyed by SWC id
n
- Node attributes:
x
,y
,z
,r
,t
(type), and optional metadata
- Nodes keyed by SWC id
-
GeneralModel
(networkx.Graph
)- Built from
SWCParseResult
with header-based reconnection merges - Node attributes preserved; provenance includes
merged_ids
,lines
- Built from
-
Segment
dataclass andFrustaSet
(batched frusta mesh) -
PointSet
(batched spheres for overlay points) -
Visualization functions in
viz.py
:plot_centroid
,plot_frusta
,plot_frusta_with_centroid
,plot_frusta_slider
,plot_model
-
Use in Jupyter:
- Launch a notebook and import
swctools
- Load or paste an SWC and use
GeneralModel
,FrustaSet
,plot_centroid
,plot_frusta
- Launch a notebook and import
from swctools import set_config
# Enforce equal x/y/z scale globally (default True) and size
set_config(force_equal_axes=True, width=800, height=600)
- Community conventions around SWC and tools in the ecosystem
- NetworkX and Plotly for the backbone of graph analysis and 3D visualization