Demo notebook for swctools basic functionality¶

I have included some swc files in data/swc to demonstrate the basic functionality of the package.

In [ ]:
import logging
logging.basicConfig(level=logging.INFO)

from pathlib import Path
from swctools import *

swc_filepath = Path("../data/swc/TS2_s50.swc")  # SWC file, describes model geometry
ps_filepath = Path(
    "../data/points/TS2_AZ.txt"
)  # Point set file, describes points (e.g. synapse locations)

First, we have basic functionality to parse a SWC file using the io module.

In [2]:
parse = parse_swc(swc_filepath)
print("parse: ", parse)
print("parse.records: ", parse.records)
print("parse.reconnections: ", parse.reconnections)
print("parse.header: ", parse.header)
INFO:swctools.io:parse_swc start strict=True validate_reconnections=True float_tol=1e-09
INFO:swctools.io:parse_swc done records=16 reconnections=1 header=5
parse:  SWCParseResult(records=16, reconnections=1, header=5)
parse.records:  {1: SWCRecord(n=1, t=3, x=8878.525222, y=2745.038908, z=2262.158844, r=11.653035, parent=-1, line=6), 2: SWCRecord(n=2, t=3, x=8884.305343, y=2735.691509, z=2310.776616, r=19.378935, parent=1, line=7), 3: SWCRecord(n=3, t=3, x=8886.497449, y=2742.173531, z=2359.738246, r=25.623099, parent=2, line=8), 4: SWCRecord(n=4, t=3, x=8879.841132, y=2761.238472, z=2405.241298, r=29.852547, parent=3, line=9), 5: SWCRecord(n=5, t=3, x=8867.051941, y=2790.856973, z=2442.859475, r=37.62364, parent=4, line=10), 6: SWCRecord(n=6, t=3, x=8845.80538, y=2816.782558, z=2479.936126, r=61.436812, parent=5, line=11), 7: SWCRecord(n=7, t=3, x=8834.940118, y=2832.446277, z=2524.050036, r=76.826678, parent=6, line=12), 8: SWCRecord(n=8, t=3, x=8842.115732, y=2832.605614, z=2561.570012, r=60.985865, parent=7, line=13), 9: SWCRecord(n=9, t=3, x=8871.116979, y=2821.242856, z=2593.890695, r=20.372681, parent=8, line=14), 10: SWCRecord(n=10, t=3, x=8851.063224, y=2821.001451, z=2638.232454, r=43.704201, parent=9, line=15), 11: SWCRecord(n=11, t=3, x=8853.39809, y=2816.419776, z=2650.603399, r=47.086212, parent=10, line=16), 12: SWCRecord(n=12, t=3, x=8859.06809, y=2796.68527, z=2695.692392, r=48.979437, parent=11, line=17), 13: SWCRecord(n=13, t=3, x=8849.14785, y=2774.86406, z=2739.398492, r=40.181497, parent=12, line=18), 14: SWCRecord(n=14, t=3, x=8811.60293, y=2844.361472, z=2628.249842, r=26.098151, parent=10, line=19), 15: SWCRecord(n=15, t=3, x=8820.836568, y=2842.212442, z=2580.692383, r=58.80183, parent=14, line=20), 16: SWCRecord(n=16, t=3, x=8842.115732, y=2832.605614, z=2561.570012, r=60.985865, parent=15, line=21)}
parse.reconnections:  [(8, 16)]
parse.header:  ['# generated by mcf2swc SkeletonGraph.to_swc', '# dfs_roots=1', '# nodes=15 extra_edges=1 duplicates=1', '# type_index=3', '# CYCLE_BREAK reconnect 16 8']

We can create a SWCModel from a parse result. This is the primary entry point for most use cases.

In [3]:
swc = SWCModel.from_parse_result(parse)
swc.print_attributes()
INFO:swctools.model:SWCModel.from_parse_result records=16 reconnections=1 header=5
SWCModel: nodes=16, edges=15, components=1, cycles=0, branch_points=1, roots=1, leaves=2, self_loops=0, density=0.1250

Instead of working with io directly, we can create a SWCModel directly from a SWC file.

In [4]:
swc = SWCModel.from_swc_file(swc_filepath)
swc.print_attributes(node_info=True, edge_info=True)
INFO:swctools.io:parse_swc start strict=True validate_reconnections=True float_tol=1e-09
INFO:swctools.io:parse_swc done records=16 reconnections=1 header=5
INFO:swctools.model:SWCModel.from_parse_result records=16 reconnections=1 header=5
INFO:swctools.model:SWCModel.from_swc_file built nodes=16 edges=15 strict=True validate_reconnections=True
SWCModel: nodes=16, edges=15, components=1, cycles=0, branch_points=1, roots=1, leaves=2, self_loops=0, density=0.1250
Nodes:
  1: t=3, x=8878.525222, y=2745.038908, z=2262.158844, r=11.653035, line=6
  2: t=3, x=8884.305343, y=2735.691509, z=2310.776616, r=19.378935, line=7
  3: t=3, x=8886.497449, y=2742.173531, z=2359.738246, r=25.623099, line=8
  4: t=3, x=8879.841132, y=2761.238472, z=2405.241298, r=29.852547, line=9
  5: t=3, x=8867.051941, y=2790.856973, z=2442.859475, r=37.62364, line=10
  6: t=3, x=8845.80538, y=2816.782558, z=2479.936126, r=61.436812, line=11
  7: t=3, x=8834.940118, y=2832.446277, z=2524.050036, r=76.826678, line=12
  8: t=3, x=8842.115732, y=2832.605614, z=2561.570012, r=60.985865, line=13
  9: t=3, x=8871.116979, y=2821.242856, z=2593.890695, r=20.372681, line=14
  10: t=3, x=8851.063224, y=2821.001451, z=2638.232454, r=43.704201, line=15
  11: t=3, x=8853.39809, y=2816.419776, z=2650.603399, r=47.086212, line=16
  12: t=3, x=8859.06809, y=2796.68527, z=2695.692392, r=48.979437, line=17
  13: t=3, x=8849.14785, y=2774.86406, z=2739.398492, r=40.181497, line=18
  14: t=3, x=8811.60293, y=2844.361472, z=2628.249842, r=26.098151, line=19
  15: t=3, x=8820.836568, y=2842.212442, z=2580.692383, r=58.80183, line=20
  16: t=3, x=8842.115732, y=2832.605614, z=2561.570012, r=60.985865, line=21
Edges:
  1 -- 2
  2 -- 3
  3 -- 4
  4 -- 5
  5 -- 6
  6 -- 7
  7 -- 8
  8 -- 9
  9 -- 10
  10 -- 11
  10 -- 14
  11 -- 12
  12 -- 13
  14 -- 15
  15 -- 16

The SWC file is in units of pixels, and each pixel is 5 nanometers. We can scale the model by a factor of 5/1000 to convert to microns, and save that to a new SWC file.

In [5]:
scale_to_um = 5 / 1000
swc_raw = SWCModel.from_swc_file(swc_filepath)
swc = swc_raw.scale(scale_to_um)
swc.print_attributes(node_info=True, edge_info=True)
swc.to_swc_file("../data/swc/TS2_s50_um.swc")
INFO:swctools.io:parse_swc start strict=True validate_reconnections=True float_tol=1e-09
INFO:swctools.io:parse_swc done records=16 reconnections=1 header=5
INFO:swctools.model:SWCModel.from_parse_result records=16 reconnections=1 header=5
INFO:swctools.model:SWCModel.from_swc_file built nodes=16 edges=15 strict=True validate_reconnections=True
INFO:swctools.model:SWCModel.to_swc_file wrote path=../data/swc/TS2_s50_um.swc nodes=16 edges=15 header_lines=5
SWCModel: nodes=16, edges=15, components=1, cycles=0, branch_points=1, roots=1, leaves=2, self_loops=0, density=0.1250
Nodes:
  1: t=3, x=44.39262611, y=13.72519454, z=11.31079422, r=0.058265174999999995, line=6
  2: t=3, x=44.421526715, y=13.678457545, z=11.55388308, r=0.096894675, line=7
  3: t=3, x=44.432487245000004, y=13.710867655, z=11.79869123, r=0.128115495, line=8
  4: t=3, x=44.39920566, y=13.80619236, z=12.02620649, r=0.149262735, line=9
  5: t=3, x=44.335259705, y=13.954284865, z=12.214297375000001, r=0.1881182, line=10
  6: t=3, x=44.2290269, y=14.08391279, z=12.39968063, r=0.30718406000000004, line=11
  7: t=3, x=44.17470059, y=14.162231385, z=12.620250180000001, r=0.38413339, line=12
  8: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=13
  9: t=3, x=44.355584895, y=14.10621428, z=12.969453475, r=0.101863405, line=14
  10: t=3, x=44.255316119999996, y=14.105007255, z=13.19116227, r=0.218521005, line=15
  11: t=3, x=44.26699045, y=14.082098880000002, z=13.253016995000001, r=0.23543106000000003, line=16
  12: t=3, x=44.295340450000005, y=13.98342635, z=13.47846196, r=0.244897185, line=17
  13: t=3, x=44.24573925, y=13.874320299999999, z=13.696992459999999, r=0.200907485, line=18
  14: t=3, x=44.05801465, y=14.22180736, z=13.141249210000002, r=0.130490755, line=19
  15: t=3, x=44.10418284000001, y=14.21106221, z=12.903461915000001, r=0.29400915, line=20
  16: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=21
Edges:
  1 -- 2
  2 -- 3
  3 -- 4
  4 -- 5
  5 -- 6
  6 -- 7
  7 -- 8
  8 -- 9
  9 -- 10
  10 -- 11
  10 -- 14
  11 -- 12
  12 -- 13
  14 -- 15
  15 -- 16

Visualization¶

We can use the plot_centroid function to visualize the skeleton of the model.

In [6]:
fig_centroid = plot_centroid(swc, show_nodes=True)
fig_centroid.show()
INFO:swctools.viz:plot_centroid edges=15 show_nodes=True

SWC models are often constructed as a set of frusta (conic sections) in simulation codes. For this we have the FrustaSet class, which can be created from a SWCModel. A FrustaSet can be visualized using the plot_frusta function.

In [7]:
frusta = FrustaSet.from_swc_model(swc, sides=20, end_caps=True)
fig_frusta = plot_frusta(frusta)
fig_frusta.show()
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.from_swc_model edges=15 sides=20 end_caps=True
INFO:swctools.viz:plot_frusta frusta=15 radius_scale=1.0 flatshading=True

A very useful feature of plot_frusta_slider function is the ability to change the radius of the frusta interactively.

In [8]:
frusta = FrustaSet.from_swc_model(swc, sides=20, end_caps=False)
fig_frusta = plot_frusta_slider(frusta)
fig_frusta.show()
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.from_swc_model edges=15 sides=20 end_caps=False
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.0
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.05
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.1
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.15
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.2
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.25
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.3
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.35
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.4
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.45
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.55
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.6
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.65
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.7
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.75
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.8
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.85
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.9
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.95
INFO:swctools.viz:plot_frusta_slider frusta=15 scales=21 min=0.0 max=1.0

If we want to overlay the frusta with the centroid skeleton, we can use the plot_frusta_with_centroid function.

In [9]:
plot_frusta_with_centroid(swc, frusta, show_nodes=True)
INFO:swctools.viz:plot_frusta_with_centroid edges=15 frusta=15 radius_scale=1.0

From these individual functions, I have built a more general function called plot_model. This allows for a more interactive and dynamic visualization of the combined centroid skeleton and frusta mesh, and also includes the option to plot a set of points as little spheres.

In [10]:
plot_model(swc_model=swc, frusta=frusta, slider=True, title="TS2 SWC Model in 3D")
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.0
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.05
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.1
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.15
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.2
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.25
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.3
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.35
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.4
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.45
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.55
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.6
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.65
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.7
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.75
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.8
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.85
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.9
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.95
INFO:swctools.viz:plot_model slider=True frusta=15 radius_scale_range=[0.0,1.0]

Our geometry module includes a class PointSet for batched spheres at arbitrary xyz points.

In [11]:
ps = PointSet.from_txt_file(ps_filepath)
ps = ps.scale(scale_to_um)

fig = plot_model(
    swc_model=swc,
    frusta=frusta,
    show_frusta=True,
    show_centroid=True,
    slider=True,
    point_set=ps,  # use the PointSet
    point_size=5,
    point_color="crimson",
)
fig.show()
INFO:swctools.geometry:PointSet.from_txt_file path=..\data\points\TS2_AZ.txt n=6
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.from_points n=6 base_radius=1.0 stacks=6 slices=12
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.scale scalar=0.005
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.scaled radius_scale=5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.0
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.05
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.1
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.15
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.2
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.25
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.3
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.35
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.4
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.45
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.55
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.6
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.65
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.7
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.75
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.8
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.85
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.9
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.95
INFO:swctools.viz:plot_model slider=True frusta=15 radius_scale_range=[0.0,1.0]
In [12]:
ps_projected = ps.project_onto_frusta(frusta)

ps = PointSet.from_txt_file(ps_filepath)

fig = plot_model(
    swc_model=swc,
    frusta=frusta,
    show_frusta=True,
    show_centroid=True,
    slider=True,
    point_set=ps_projected,  # use the PointSet
    point_size=5,  # multiplies base_radius (0.05 * 1.5 = 0.075)
    point_color="crimson",
)
fig.show()
INFO:swctools.geometry:project_onto_frusta points=6 frusta=15 use_caps=False
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:project_onto_frusta done moved_points=6
INFO:swctools.geometry:PointSet.from_txt_file path=..\data\points\TS2_AZ.txt n=6
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.from_points n=6 base_radius=1.0 stacks=6 slices=12
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.scaled radius_scale=5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.0
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.05
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.1
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.15
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.2
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.25
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.3
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.35
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.4
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.45
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.55
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.6
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.65
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.7
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.75
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.8
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.85
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.9
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=False verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.95
INFO:swctools.viz:plot_model slider=True frusta=15 radius_scale_range=[0.0,1.0]
In [13]:
# we can then save this PointSet to a new file
ps.to_txt_file("../data/points/TS2_synpts.txt")

We can use SWCModel.set_tag_by_sphere to set the tag of all nodes inside a sphere.

In [14]:
swc.print_attributes(node_info=True)
swc.set_tag_by_sphere(
    center=(44.392626, 13.725195, 11.310794), radius=1.0, new_tag=7, old_tag=3
)
swc.print_attributes(node_info=True)
frusta = FrustaSet.from_swc_model(
    swc, sides=20, end_caps=True, flip_tag_assignment=True
)  # recreate frusta set with new tags

fig = plot_model(
    swc_model=swc,
    frusta=frusta,
    show_frusta=True,
    show_centroid=True,
    slider=True,
    point_set=ps_projected,
    point_size=5,
    point_color="crimson",
    tag_colors={3: "lightblue", 7: "lightpink"},
    title="Centroid + Frusta with tag colors",
)
fig.show()
INFO:swctools.model:set_tag_by_sphere center=(44.392626, 13.725195, 11.310794) radius=1.000000 new_tag=7 old_tag=3 changed=5 copy=False
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.from_swc_model edges=15 sides=20 end_caps=True
INFO:swctools.geometry:batch_spheres count=6 stacks=6 slices=12 verts=372 faces=720
INFO:swctools.geometry:PointSet.scaled radius_scale=5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=600 faces=600
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.0
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.05
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.1
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.15
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.2
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.25
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.3
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.35
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.4
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.45
SWCModel: nodes=16, edges=15, components=1, cycles=0, branch_points=1, roots=1, leaves=2, self_loops=0, density=0.1250
Nodes:
  1: t=3, x=44.39262611, y=13.72519454, z=11.31079422, r=0.058265174999999995, line=6
  2: t=3, x=44.421526715, y=13.678457545, z=11.55388308, r=0.096894675, line=7
  3: t=3, x=44.432487245000004, y=13.710867655, z=11.79869123, r=0.128115495, line=8
  4: t=3, x=44.39920566, y=13.80619236, z=12.02620649, r=0.149262735, line=9
  5: t=3, x=44.335259705, y=13.954284865, z=12.214297375000001, r=0.1881182, line=10
  6: t=3, x=44.2290269, y=14.08391279, z=12.39968063, r=0.30718406000000004, line=11
  7: t=3, x=44.17470059, y=14.162231385, z=12.620250180000001, r=0.38413339, line=12
  8: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=13
  9: t=3, x=44.355584895, y=14.10621428, z=12.969453475, r=0.101863405, line=14
  10: t=3, x=44.255316119999996, y=14.105007255, z=13.19116227, r=0.218521005, line=15
  11: t=3, x=44.26699045, y=14.082098880000002, z=13.253016995000001, r=0.23543106000000003, line=16
  12: t=3, x=44.295340450000005, y=13.98342635, z=13.47846196, r=0.244897185, line=17
  13: t=3, x=44.24573925, y=13.874320299999999, z=13.696992459999999, r=0.200907485, line=18
  14: t=3, x=44.05801465, y=14.22180736, z=13.141249210000002, r=0.130490755, line=19
  15: t=3, x=44.10418284000001, y=14.21106221, z=12.903461915000001, r=0.29400915, line=20
  16: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=21
SWCModel: nodes=16, edges=15, components=1, cycles=0, branch_points=1, roots=1, leaves=2, self_loops=0, density=0.1250
Nodes:
  1: t=7, x=44.39262611, y=13.72519454, z=11.31079422, r=0.058265174999999995, line=6
  2: t=7, x=44.421526715, y=13.678457545, z=11.55388308, r=0.096894675, line=7
  3: t=7, x=44.432487245000004, y=13.710867655, z=11.79869123, r=0.128115495, line=8
  4: t=7, x=44.39920566, y=13.80619236, z=12.02620649, r=0.149262735, line=9
  5: t=7, x=44.335259705, y=13.954284865, z=12.214297375000001, r=0.1881182, line=10
  6: t=3, x=44.2290269, y=14.08391279, z=12.39968063, r=0.30718406000000004, line=11
  7: t=3, x=44.17470059, y=14.162231385, z=12.620250180000001, r=0.38413339, line=12
  8: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=13
  9: t=3, x=44.355584895, y=14.10621428, z=12.969453475, r=0.101863405, line=14
  10: t=3, x=44.255316119999996, y=14.105007255, z=13.19116227, r=0.218521005, line=15
  11: t=3, x=44.26699045, y=14.082098880000002, z=13.253016995000001, r=0.23543106000000003, line=16
  12: t=3, x=44.295340450000005, y=13.98342635, z=13.47846196, r=0.244897185, line=17
  13: t=3, x=44.24573925, y=13.874320299999999, z=13.696992459999999, r=0.200907485, line=18
  14: t=3, x=44.05801465, y=14.22180736, z=13.141249210000002, r=0.130490755, line=19
  15: t=3, x=44.10418284000001, y=14.21106221, z=12.903461915000001, r=0.29400915, line=20
  16: t=3, x=44.21057866, y=14.163028070000001, z=12.807850060000002, r=0.304929325, line=21
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.5
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.55
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.6
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.65
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.7
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.75
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.8
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.85
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.9
INFO:swctools.geometry:batch_frusta count=15 sides=20 end_caps=True verts=630 faces=1200
INFO:swctools.geometry:FrustaSet.scaled radius_scale=0.95
INFO:swctools.viz:plot_model slider=True frusta=15 radius_scale_range=[0.0,1.0]

Save this SWC to file with new tags

In [15]:
swc.to_swc_file("../data/swc/TS2_retagged.swc")
INFO:swctools.model:SWCModel.to_swc_file wrote path=../data/swc/TS2_retagged.swc nodes=16 edges=15 header_lines=5