Layout Algorithms¶
Network layout and positioning algorithms.
Overview¶
The layout.algorithms module provides various network layout algorithms:
- Spring: Force-directed layout
- Circular: Circular arrangement
- Kamada-Kawai: Energy minimization
- Spectral: Eigenvalue-based
Modules¶
pypopart.layout.algorithms ¶
Layout algorithms for network visualization in PyPopART.
Provides various layout algorithms for positioning nodes in haplotype networks, including force-directed, hierarchical, spectral, and custom layouts.
Algorithm Selection Guide
For small networks (<50 nodes): - KamadaKawaiLayout: Best quality, slow - ForceDirectedLayout: Good quality, moderate speed
For medium networks (50-500 nodes): - ForceDirectedLayout: Default choice, good balance - SpectralLayout: Faster alternative, good quality - HierarchicalLayout: Very fast, tree-like structure
For large networks (>500 nodes): - SpectralLayout: Fast, maintains structure - HierarchicalLayout: Fastest option - CircularLayout: Simple, very fast
Special purposes: - RadialLayout: Emphasize central node - CircularLayout: Show connectivity patterns
LayoutAlgorithm ¶
Base class for layout algorithms.
Provides interface for computing node positions in network visualizations.
Source code in src/pypopart/layout/algorithms.py
__init__ ¶
Initialize layout algorithm with a network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
HaplotypeNetwork
|
HaplotypeNetwork object. |
required |
compute ¶
Compute node positions.
Returns:
| Type | Description |
|---|---|
Dictionary mapping node IDs to (x, y) positions.
|
|
Source code in src/pypopart/layout/algorithms.py
save_layout ¶
Save layout to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
Dict[str, Tuple[float, float]]
|
Node positions dictionary. |
required |
filename
|
str
|
Output filename. |
required |
Source code in src/pypopart/layout/algorithms.py
load_layout
staticmethod
¶
Load layout from a JSON file.
Returns:
| Type | Description |
|---|---|
Dictionary mapping node IDs to (x, y) positions.
|
|
Source code in src/pypopart/layout/algorithms.py
ForceDirectedLayout ¶
Bases: LayoutAlgorithm
Force-directed layout using spring algorithm.
Simulates physical spring forces between connected nodes to create aesthetically pleasing layouts. Uses the Fruchterman-Reingold algorithm implemented in NetworkX's spring_layout.
Performance
- Time complexity: O(iterations * N^2) where N is number of nodes
- Typical runtime: ~25ms for 100 nodes, 50 iterations
- Best for: Networks with 10-500 nodes
- Quality: Good balance between speed and aesthetic quality
Notes
For very large networks (>500 nodes), consider using: - HierarchicalLayout (fastest, ~0.1ms for 100 nodes) - CircularLayout (very fast, ~0.2ms for 100 nodes) - SpectralLayout (faster alternative to force-directed)
Source code in src/pypopart/layout/algorithms.py
compute ¶
compute(
k: Optional[float] = None,
iterations: int = 50,
seed: Optional[int] = None,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute force-directed layout.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
CircularLayout ¶
Bases: LayoutAlgorithm
Circular layout arranging nodes in a circle.
Places nodes evenly spaced around a circle, useful for showing connectivity patterns.
Source code in src/pypopart/layout/algorithms.py
compute ¶
compute(
scale: float = 1.0,
center: Optional[Tuple[float, float]] = None,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute circular layout.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
RadialLayout ¶
Bases: LayoutAlgorithm
Radial layout with center node and concentric rings.
Places a central node at the origin and arranges other nodes in concentric circles based on distance from center.
Source code in src/pypopart/layout/algorithms.py
compute ¶
compute(
center_node: Optional[str] = None,
scale: float = 1.0,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute radial layout.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
HierarchicalLayout ¶
Bases: LayoutAlgorithm
Hierarchical layout arranging nodes in levels.
Creates a tree-like structure with nodes arranged in horizontal levels based on distance from a root node.
Source code in src/pypopart/layout/algorithms.py
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 | |
compute ¶
compute(
root_node: Optional[str] = None,
vertical: bool = True,
width: float = 2.0,
height: float = 2.0,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute hierarchical layout.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
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 | |
KamadaKawaiLayout ¶
Bases: LayoutAlgorithm
Kamada-Kawai layout algorithm.
Uses energy minimization to position nodes based on graph-theoretic distances. Produces high-quality layouts but is computationally expensive for large networks.
Performance
- Time complexity: O(N^3) where N is number of nodes
- Typical runtime: ~190ms for 100 nodes
- Best for: Small networks (<50 nodes) where layout quality is critical
- Quality: Excellent, minimizes stress based on graph distances
Notes
For large networks, use ForceDirectedLayout or SpectralLayout instead. Kamada-Kawai can be very slow for networks with >100 nodes.
Source code in src/pypopart/layout/algorithms.py
compute ¶
compute(
scale: float = 1.0,
center: Optional[Tuple[float, float]] = None,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute Kamada-Kawai layout.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Warnings
This algorithm can be very slow for large networks (>100 nodes). Consider using ForceDirectedLayout or SpectralLayout as faster alternatives.
Source code in src/pypopart/layout/algorithms.py
SpectralLayout ¶
Bases: LayoutAlgorithm
Spectral layout using graph Laplacian eigenvectors.
Uses the eigenvectors of the graph Laplacian matrix to position nodes. This is a fast alternative to force-directed layouts that works well for large networks.
Performance
- Time complexity: O(N^2) where N is number of nodes
- Typical runtime: ~5-10ms for 100 nodes
- Best for: Large networks (100-1000+ nodes)
- Quality: Good, respects graph structure efficiently
Notes
Spectral layout is much faster than Kamada-Kawai and comparable to force-directed layouts while maintaining good quality. Particularly effective for networks with clear clustering structure.
Source code in src/pypopart/layout/algorithms.py
compute ¶
compute(
scale: float = 1.0,
center: Optional[Tuple[float, float]] = None,
dim: int = 2,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute spectral layout using graph Laplacian.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scale
|
float
|
Scale factor for the layout. |
1.0
|
center
|
Optional[Tuple[float, float]]
|
Center position (x, y). |
None
|
dim
|
int
|
Dimensionality of layout (default 2 for 2D visualization). |
2
|
**kwargs
|
Additional parameters passed to spectral_layout. |
{}
|
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
ManualLayout ¶
Bases: LayoutAlgorithm
Manual layout with user-specified positions.
Allows manual positioning of nodes or adjustment of existing layouts.
Source code in src/pypopart/layout/algorithms.py
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 | |
__init__ ¶
__init__(
network: HaplotypeNetwork,
initial_positions: Optional[
Dict[str, Tuple[float, float]]
] = None,
)
Initialize manual layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
HaplotypeNetwork
|
HaplotypeNetwork object. |
required |
initial_positions
|
Optional[Dict[str, Tuple[float, float]]]
|
Starting positions for nodes. |
None
|
Source code in src/pypopart/layout/algorithms.py
compute ¶
Return current manual positions.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
set_position ¶
Set position for a specific node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
str
|
Node ID. |
required |
position
|
Tuple[float, float]
|
(x, y) coordinates. |
required |
Source code in src/pypopart/layout/algorithms.py
move_node ¶
Move a node by a relative offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
str
|
Node ID. |
required |
dx
|
float
|
X offset. |
required |
dy
|
float
|
Y offset. |
required |
Source code in src/pypopart/layout/algorithms.py
LayoutManager ¶
Manager for network layout computation and persistence.
Provides high-level interface for computing, saving, and loading network layouts with optional caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
HaplotypeNetwork
|
The haplotype network to compute layouts for. |
required |
enable_cache
|
bool
|
If True, cache layout results for repeated calls with same parameters. Default is True. Disable if network structure changes between calls. |
True
|
Examples:
>>> manager = LayoutManager(network)
>>> layout = manager.compute_layout('spring', iterations=50, seed=42)
>>> # Second call with same parameters uses cached result
>>> layout2 = manager.compute_layout('spring', iterations=50, seed=42)
Source code in src/pypopart/layout/algorithms.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 | |
__init__ ¶
Initialize layout manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
HaplotypeNetwork
|
HaplotypeNetwork object. |
required |
enable_cache
|
bool
|
Enable caching of layout computations. Default True. |
True
|
Source code in src/pypopart/layout/algorithms.py
compute_layout ¶
compute_layout(
algorithm: str = "force_directed",
use_cache: bool = True,
**kwargs
) -> Dict[str, Tuple[float, float]]
Compute network layout using specified algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Layout algorithm name. |
'force_directed'
|
use_cache
|
bool
|
If True and caching is enabled, return cached result if available. Default True. |
True
|
**kwargs
|
Algorithm-specific parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Raises:
| Type | Description |
|---|---|
ValueError :
|
If algorithm not recognized. |
Notes
Results are cached based on algorithm name and parameters. To force recomputation, set use_cache=False or clear_cache().
Source code in src/pypopart/layout/algorithms.py
clear_cache ¶
Clear the layout cache.
Use this after the network structure has changed to ensure fresh layout computations.
save_layout ¶
Save layout to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layout
|
Dict[str, Tuple[float, float]]
|
Node positions dictionary. |
required |
filename
|
str
|
Output filename (JSON format). |
required |
Source code in src/pypopart/layout/algorithms.py
load_layout ¶
Load layout from file.
Returns:
| Type | Description |
|---|---|
Node positions dictionary.
|
|
Source code in src/pypopart/layout/algorithms.py
get_available_algorithms ¶
Get list of available layout algorithms.
Returns:
| Type | Description |
|---|---|
List of algorithm names.
|
|