API Reference¶
Public API¶
swctools package scaffolding.
Public API is evolving; currently exposes SWC parsing, models, geometry, and visualization utilities.
SWCRecord
dataclass
¶
One SWC row.
Attributes¶
n: int Node id (unique within file) t: int Structure type code x, y, z: float Coordinates (usually micrometers) r: float Radius parent: int Parent id; -1 indicates root line: int 1-based line number in the source file/string
Source code in swctools/io.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
SWCParseResult
dataclass
¶
Parsed SWC content.
Source code in swctools/io.py
72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
SWCModel
¶
Bases: DiGraph
Directed SWC morphology graph.
Nodes are keyed by SWC id n
and store attributes:
- t: int (structure type)
- x, y, z: float (coordinates)
- r: float (radius)
- line: int (line number in source; informational)
Edges are directed parent -> child.
Source code in swctools/model.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
from_parse_result(result)
classmethod
¶
Build a model from a parsed SWC result.
Source code in swctools/model.py
98 99 100 101 |
|
from_records(records)
classmethod
¶
Build a model from SWC records.
Accepts either a mapping of id->record or any iterable of SWCRecord.
Source code in swctools/model.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
from_swc_file(source, *, strict=True, validate_reconnections=True, float_tol=1e-09)
classmethod
¶
Parse an SWC source then build a model.
The source
is passed through to parse_swc
, which supports a path,
a file-like object, a string with the full contents, or an iterable of lines.
Source code in swctools/model.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
roots()
¶
Return nodes with in-degree 0 (forest roots).
Source code in swctools/model.py
163 164 165 |
|
parent_of(n)
¶
Return the parent id of node n, or None if n is a root.
SWC trees should have at most one parent per node; if multiple are found this indicates invalid structure for SWC and an error is raised.
Source code in swctools/model.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
path_to_root(n)
¶
Return the path from node n up to its root, inclusive.
Example: For edges 1->2->3, path_to_root(3)
returns [3, 2, 1]
.
Source code in swctools/model.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
print_attributes(*, node_info=False, edge_info=False)
¶
Print graph attributes and optional node/edge details.
Parameters¶
node_info: bool If True, print per-node attributes (t, x, y, z, r, line where present). edge_info: bool If True, print all edges (u -> v) with edge attributes if any.
Source code in swctools/model.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
GeneralModel
¶
Bases: Graph
Undirected morphology graph with reconnection merges.
- Subclasses
networkx.Graph
. - Nodes correspond to merged SWC points according to header annotations
# CYCLE_BREAK reconnect i j
. - Node attributes include:
x, y, z, r
(identical across merged ids), representativen
, optionalt
, and provenance listsmerged_ids
,lines
. - Edges are undirected between merged nodes; self-loops are skipped if parent/child collapse into the same merged node.
Source code in swctools/model.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
from_parse_result(result, *, validate_reconnections=True, float_tol=1e-09)
classmethod
¶
Build a merged undirected model from a parsed SWC result.
If validate_reconnections
is True, enforce identical (x, y, z, r)
for each reconnect pair before merging (useful when parse_swc
was
called with validation disabled).
Source code in swctools/model.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
from_swc_file(source, *, strict=True, validate_reconnections=True, float_tol=1e-09)
classmethod
¶
Parse an SWC source and build a merged undirected model.
Source code in swctools/model.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
print_attributes(*, node_info=False, edge_info=False)
¶
Print graph attributes and optional node/edge details.
Parameters¶
node_info: bool If True, print per-node attributes (n, x, y, z, r, t, merged_ids, lines where present). edge_info: bool If True, print all edges (u -- v) with edge attributes if any.
Source code in swctools/model.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
|
Segment
dataclass
¶
Oriented frustum segment between endpoints a
and b
.
Attributes¶
a, b: Point3
Endpoints in model/world coordinates.
ra, rb: float
Radii at a
and b
.
Source code in swctools/geometry.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
FrustaSet
dataclass
¶
A batched frusta mesh derived from a GeneralModel
.
Attributes¶
vertices: List[Point3]
Concatenated vertices for all frusta.
faces: List[Face]
Triangular faces indexing into vertices
.
sides: int
Circumferential resolution used per frustum.
end_caps: bool
Whether end caps were included during construction.
segment_count: int
Number of segments used (one per graph edge).
edge_count: int
Alias for segment_count
for clarity.
Source code in swctools/geometry.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
|
from_general_model(gm, *, sides=16, end_caps=False)
classmethod
¶
Build a FrustaSet
by converting each undirected edge into a Segment
.
Expects nodes to have attributes x, y, z, r
.
Source code in swctools/geometry.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 |
|
to_mesh3d_arrays()
¶
Return Plotly Mesh3d arrays: x, y, z, i, j, k.
Source code in swctools/geometry.py
641 642 643 644 645 646 647 648 649 650 651 |
|
scaled(radius_scale)
¶
Return a new FrustaSet with all segment radii scaled by radius_scale
.
This rebuilds vertices/faces from the stored segments
list.
Source code in swctools/geometry.py
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
|
PointSet
dataclass
¶
A batched mesh of small spheres placed at given 3D points.
Source code in swctools/geometry.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
from_txt(source, *, base_radius=1.0, stacks=6, slices=12, allow_extra_columns=True)
classmethod
¶
Load a simple text format with x y z
coordinates per non-empty line.
- Lines beginning with
#
or blank lines are ignored. - If
allow_extra_columns=True
, extra columns after the first three are ignored. - Raises
ValueError
on malformed lines.
Source code in swctools/geometry.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
|
scaled(radius_scale)
¶
Return a new PointSet
with all sphere radii scaled by radius_scale
.
Source code in swctools/geometry.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
|
project_onto_frusta(frusta, include_end_caps=None)
¶
Project each point to the nearest surface of the nearest frustum.
Parameters¶
frusta: FrustaSet
Set of oriented frusta (as Segment
s) to project onto.
include_end_caps: Optional[bool]
If None (default), follow frusta.end_caps
. If True/False, explicitly
include or ignore projections to the circular end caps.
Returns¶
PointSet
A new PointSet
whose points
have been moved onto the closest
surface points of the closest frusta; sphere mesh is rebuilt.
Notes¶
For each input point, the algorithm iterates all frusta segments and evaluates the squared distance to: - The lateral surface: project the point to the segment axis (clamped t in [0,1]), interpolate radius r(t), then move along the radial direction to the mantle. - The end caps (optional): orthogonal distance to each cap plane; if the projected point falls outside the disk, distance to the rim is used. Degenerate segments (zero length) are treated as a sphere of radius max(ra, rb) centered at the endpoint. Complexity is O(N_points × N_segments), implemented in pure Python.
Source code in swctools/geometry.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 |
|
parse_swc(source, *, strict=True, validate_reconnections=True, float_tol=1e-09)
¶
Parse an SWC file or text stream.
Parameters¶
source Path to an SWC file, a file-like object, an iterable of lines, or a string containing SWC content. strict If True, enforce 7-column rows and validate parent references exist. validate_reconnections If True, ensure reconnection node pairs share identical (x, y, z, r). float_tol Tolerance used when comparing floating-point coordinates/radii.
Returns¶
SWCParseResult Parsed records, reconnection pairs, and collected comments.
Raises¶
ValueError If parsing or validation fails. FileNotFoundError If a string path is provided that does not exist.
Source code in swctools/io.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|
frustum_mesh(seg, *, sides=16, end_caps=False)
¶
Generate a frustum mesh for a single Segment
.
Returns¶
(vertices, faces):
- vertices: List[Point3]
- faces: List[Face], each = (i, j, k) indexing into vertices
Source code in swctools/geometry.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
batch_frusta(segments, *, sides=16, end_caps=False)
¶
Batch multiple frusta into a single mesh.
Returns a concatenated list of vertices
and faces
with the proper index offsets.
Source code in swctools/geometry.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
plot_centroid(gm, *, marker_size=2.0, line_width=2.0, show_nodes=True)
¶
Plot centroid skeleton from a GeneralModel.
Edges are drawn as line segments in 3D using Scatter3d.
Source code in swctools/viz.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
plot_frusta(frusta, *, color='lightblue', opacity=0.8, flatshading=True, radius_scale=1.0)
¶
Plot a FrustaSet as a Mesh3d figure.
Parameters¶
frusta: FrustaSet Batched frusta mesh to render. color: str Mesh color. opacity: float Mesh opacity. flatshading: bool Whether to enable flat shading. radius_scale: float Uniform scale applied to all segment radii before meshing (1.0 = no change).
Source code in swctools/viz.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
plot_frusta_with_centroid(gm, frusta, *, color='lightblue', opacity=0.8, flatshading=True, radius_scale=1.0, centroid_color='#1f77b4', centroid_line_width=2.0, show_nodes=False, node_size=2.0)
¶
Overlay frusta mesh with centroid skeleton from a GeneralModel
.
Parameters mirror plot_centroid
and plot_frusta
with an extra radius_scale
.
Source code in swctools/viz.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
plot_frusta_slider(frusta, *, color='lightblue', opacity=0.8, flatshading=True, min_scale=0.0, max_scale=1.0, steps=21)
¶
Interactive slider (0..1 default) controlling uniform radius_scale
.
Precomputes frames at evenly spaced scales between min_scale
and max_scale
.
Source code in swctools/viz.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
plot_model(*, gm=None, frusta=None, show_frusta=True, show_centroid=True, sides=16, end_caps=False, color='lightblue', opacity=0.8, flatshading=True, radius_scale=1.0, slider=False, min_scale=0.0, max_scale=1.0, steps=21, centroid_color='#1f77b4', centroid_line_width=2.0, show_nodes=False, node_size=2.0, point_set=None, point_size=1.0, point_color='#d62728')
¶
Master visualization combining centroid, frusta, slider, and overlay points.
- If
frusta
is not provided andgm
is, aFrustaSet
is built fromgm
. - If
slider=True
andshow_frusta=True
, a Plotly slider controlsradius_scale
. points
overlays arbitrary xyz positions as small markers.
Source code in swctools/viz.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
get_config()
¶
Return the current visualization configuration (live object).
Source code in swctools/config.py
38 39 40 |
|
set_config(**kwargs)
¶
Update global visualization configuration.
Example
set_config(width=800, height=600, scene_aspectmode="cube")
Source code in swctools/config.py
43 44 45 46 47 48 49 50 51 52 |
|
apply_layout(fig, *, title=None)
¶
Apply global layout defaults to a Plotly figure in-place.
Source code in swctools/config.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|