layout
Network layout algorithms for node positioning.
Provides comprehensive layout computation for network visualization using various algorithms from NetworkX and custom implementations. Supports both weighted and unweighted networks with flexible parameter configuration.
Key Features
- NetworkX integration for proven algorithms
- Custom grid layout for regular structures
- Weighted layout support for better positioning
- Automatic algorithm selection and validation
Available Algorithms
- All layouts that are implemented in
networkx- Random layout
- Circular layout
- Shell layout
- Spectral layout
- Kamada-Kawai layout
- Fruchterman-Reingold force-directed algorithm
- ForceAtlas2 layout algorithm
- Grid layout
Examples:
Compute a spring layout for a simple graph:
>>> from pathpyG import Graph
>>> from pathpyG.visualisations import layout
>>>
>>> g = Graph.from_edge_list([('a', 'b'), ('b', 'c')])
>>> positions = layout(g, layout='spring', k=0.5)
>>> print(positions)
{'a': array([ 0.61899711, -1. ]), 'b': array([-0.00132282, 0.00213747]), 'c': array([-0.61767429, 0.99786253])}
Layout
¶
Bases: object
Layout computation engine for network node positioning.
Core class that handles algorithm selection, parameter management, and coordinate generation. Integrates with NetworkX for proven algorithms while providing custom implementations for specialized cases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list
|
List of unique node identifiers |
required |
edge_index
|
typing.Optional[torch.Tensor]
|
Tensor containing source/target indices for each edge |
None
|
layout_type
|
str
|
Algorithm name for position computation |
'random'
|
weight
|
typing.Optional[torch.Tensor]
|
Optional edge weights as tensor with shape [num_edges] |
None
|
**kwargs
|
Algorithm-specific parameters |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
nodes |
Node identifier list |
|
edge_index |
Edge connectivity tensor |
|
weight |
Edge weight tensor (optional) |
|
layout_type |
Selected algorithm name |
|
kwargs |
Algorithm parameters |
Source code in src/pathpyG/visualisations/layout.py
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 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 264 265 266 267 268 269 | |
__init__
¶
Initialize layout computation with network data and parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list
|
List of unique node identifiers |
required |
edge_index
|
typing.Optional[torch.Tensor]
|
Edge connectivity tensor (creates empty if None) |
None
|
layout_type
|
str
|
Algorithm name for position computation |
'random'
|
weight
|
typing.Optional[torch.Tensor]
|
Optional edge weights tensor |
None
|
**kwargs
|
Algorithm-specific parameters |
{}
|
Source code in src/pathpyG/visualisations/layout.py
generate_layout
¶
Select and execute appropriate layout algorithm.
Routes computation to either custom grid implementation or NetworkX-based algorithms based on layout_type specification.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Node positions as {node_id: (x, y)} coordinate mapping |
Source code in src/pathpyG/visualisations/layout.py
generate_nx_layout
¶
Compute layout using NetworkX algorithms with weight support.
Converts pathpyG network to NetworkX format, applies selected algorithm with proper weight handling, and returns position dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Node positions from NetworkX layout algorithm |
Raises:
| Type | Description |
|---|---|
ValueError
|
If layout algorithm name not recognized |
Algorithm Mapping
Multiple aliases map to the same underlying NetworkX function for user convenience and compatibility with different naming conventions.
Source code in src/pathpyG/visualisations/layout.py
grid
¶
Position nodes on regular 2D grid for lattice-like structures.
Arranges nodes in a square grid pattern with uniform spacing. Useful for regular networks, lattices.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Grid positions as {node_id: (x, y)} coordinates |
Source code in src/pathpyG/visualisations/layout.py
layout
¶
Generate node positions using specified layout algorithm.
Computes 2D coordinates for all nodes in the network using various layout algorithms. Supports edge weighting for physics-based layouts and provides flexible parameter passing to underlying algorithms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
pathpyG.core.graph.Graph
|
Graph instance to generate layout for |
required |
layout
|
str
|
Algorithm name (see supported algorithms below) |
'random'
|
weight
|
None | str | typing.Iterable
|
Edge weights as attribute name, iterable, or None |
None
|
**kwargs
|
Algorithm-specific parameters passed to layout function |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Node positions as {node_id: (x, y)} coordinate mapping |
Raises:
| Type | Description |
|---|---|
ValueError
|
If weight attribute not found or weight length mismatch |
ValueError
|
If layout algorithm not recognized |
Examples:
# Basic spring layout
pos = layout(graph, 'spring')
# Weighted layout with edge attribute
pos = layout(graph, 'kamada-kawai', weight='edge_weight')
# Custom parameters
pos = layout(graph, 'spring', k=0.3, iterations=100)
Supported Algorithms
| Algorithm | Aliases | Best For |
|---|---|---|
spring |
fruchterman-reingold, fr |
General networks |
kamada-kawai |
kk, kamada |
Small/medium networks |
forceatlas2 |
fa2, force-atlas2 |
Large networks |
circular |
circle, ring |
Cycle structures |
shell |
concentric |
Hierarchical data |
grid |
lattice-2d |
Regular structures |
spectral |
eigen |
Community detection |
random |
rand |
Testing/baseline |