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 model:
SWCModel(networkx.Graph) with undirected storage and an internal parent map for original SWC directed tree relations; supportsmake_cycle_connections()to apply# CYCLE_BREAK reconnect i jmerges (union-find)- Shared graph metrics via
_graph_attributes()andprint_attributes()helpers
- Geometry:
Segmentdataclass and frustum meshing utilities (frustum_mesh,batch_frusta)FrustaSet.from_swc_model()to build a batched frusta mesh from anSWCModelPointSetfor low-res spheres at arbitrary xyz points (for overlay markers)
- Visualization:
plot_centroid(model, ...)for skeleton plotting (Scatter3d)plot_frusta(frusta_set, ..., radius_scale=1.0)for volumetric frusta rendering (Mesh3d)plot_frusta_with_centroid(model, 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, andPointSetoverlaysplot_frusta_timeseries(frusta, values, ...)animate time-dependent per-segment scalars V_i(t) with a slider and playback controls- 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.Graphwith parent map)- Nodes keyed by SWC id
n - Node attributes:
x,y,z,r(radius),t(tag), and optional metadata _parentspreserves the original directed parent of each node;roots(),parent_of(),path_to_root()use this mapmake_cycle_connections()merges reconnection pairs and returns a merged model (can include cycles)
- Nodes keyed by SWC id
-
Segmentdataclass andFrustaSet(batched frusta mesh) -
FrustaSetordering utilities:print_segment_order(),reordered(...), andper_segment_face_slices()to help align external per-segment arrays with mesh order -
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
SWCModel,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