SpatialGraph
The central data container for the entire pipeline. Every function accepts
and returns SpatialGraph objects.
Constructor patterns
| Method | Use when |
|---|---|
SpatialGraph(adjacency_matrix, ...) |
You already have a scipy sparse matrix |
SpatialGraph.from_edge_list(path_or_df) |
Loading from a CSV edge list |
SpatialGraph.from_positions(array) |
Starting from a point cloud with no edges |
Key attributes
| Attribute | Type | Description |
|---|---|---|
adjacency_matrix |
scipy.sparse.csr_matrix |
Symmetric adjacency, zero diagonal |
positions |
np.ndarray or None |
Ground-truth coordinates (n, dim) |
reconstructed_positions |
np.ndarray or None |
Set by reconstruct() |
edge_metadata |
pd.DataFrame or None |
Includes is_false column when simulated |
node_metadata |
pd.DataFrame or None |
Per-node attributes |
graph |
nx.Graph |
Lazy NetworkX view (cached) |
API Reference
spatial_graph_algorithms.network.SpatialGraph
dataclass
Core graph and coordinate container for spatial network analysis.
All pipeline functions accept and return SpatialGraph objects. The
adjacency matrix is stored internally as a CSR sparse matrix with the
diagonal forced to zero. When keep_lcc is True (the default), only
the largest connected component is retained on construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
positions
|
ndarray
|
Ground-truth node coordinates, shape |
None
|
reconstructed_positions
|
ndarray
|
Coordinates recovered by a reconstruction algorithm, same shape as
positions. Set by :func: |
None
|
node_metadata
|
DataFrame
|
Per-node attributes. Row i corresponds to node i. |
None
|
edge_metadata
|
DataFrame
|
Per-edge attributes with columns |
None
|
node_id_map
|
dict
|
Mapping from original node identifiers to integer indices |
None
|
keep_lcc
|
bool
|
If |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
adjacency_matrix |
csr_matrix
|
Symmetric, unweighted adjacency matrix. Any format accepted by
|
n_nodes |
int
|
Number of nodes (always cheap — direct from adjacency shape). |
n_edges |
int
|
Number of undirected edges (always cheap — |
edge_density |
float
|
Fraction of all possible edges present. |
degree_sequence |
ndarray
|
Per-node degree array; computed once on first access and cached. Cache is cleared automatically when the adjacency matrix changes. |
mean_degree |
float
|
Mean of :attr: |
degree_distribution |
ndarray
|
Counts per degree value; derived from :attr: |
mean_shortest_path |
float or None
|
Mean pairwise shortest-path length. |
shortest_path_distribution |
ndarray or None
|
Histogram of pairwise shortest-path lengths. |
Examples:
Build from a scipy sparse matrix:
>>> import scipy.sparse
>>> adj = scipy.sparse.eye(3, format="csr") * 0 # 3 isolated nodes
>>> sn = SpatialGraph(adjacency_matrix=adj, keep_lcc=False)
>>> sn.adjacency_matrix.shape
(3, 3)
Source code in src/spatial_graph_algorithms/network.py
38 39 40 41 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 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 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 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 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 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 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 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
Attributes
adjacency_matrix
property
writable
CSR adjacency matrix with zero diagonal and no duplicate entries.
graph
property
NetworkX undirected graph built lazily from the adjacency matrix.
The result is cached; assigning a new adjacency_matrix invalidates the cache automatically.
Returns:
| Type | Description |
|---|---|
Graph
|
Undirected graph with integer node labels |
n_nodes
property
Number of nodes.
n_edges
property
Number of undirected edges (no self-loops).
edge_density
property
Fraction of all possible edges that are present.
Returns 0.0 for graphs with fewer than 2 nodes.
degree_sequence
property
Degree of every node as a 1-D integer array, computed once and cached.
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
mean_degree
property
Mean node degree.
degree_distribution
property
Counts per degree value: index k gives the number of degree-k nodes.
Returns:
| Type | Description |
|---|---|
ndarray
|
Shape |
mean_shortest_path
property
Mean pairwise shortest-path length, or None if not yet computed.
Populated automatically when a graph-structure computation (e.g. in
:mod:spatial_graph_algorithms.metrics) runs and caches the result, or
by direct assignment to sn._mean_shortest_path_cache.
TODO: implement lazy auto-computation via
scipy.sparse.csgraph.shortest_path (O(n²)–O(n³); too expensive to
trigger silently on large graphs).
shortest_path_distribution
property
Histogram of pairwise shortest-path lengths, or None if not yet computed.
Index k gives the count of node pairs at shortest-path distance k.
Populated alongside :attr:mean_shortest_path.
TODO: implement together with mean_shortest_path.
n_connected_components
property
Number of connected components in the graph.
Computed lazily on first access and cached. For graphs constructed
with keep_lcc=True (the default) this is always 1 after
construction; graphs loaded with keep_lcc=False may have more.
largest_component_fraction
property
Fraction of nodes in the largest connected component.
1.0 when the graph is fully connected. Computed together with
:attr:n_connected_components and cached on first access.
false_edge_fraction
property
Fraction of edges marked as false (injected noise), or None.
Returns None when :attr:edge_metadata is absent or has no
is_false column (e.g. real-data graphs loaded via
:meth:from_edge_list).
Examples:
has_ground_truth
property
True when positions were set at construction (simulated or labelled data).
Returns False for graphs loaded from a raw edge list where no
ground-truth coordinates or false-edge labels are available. Pipeline
functions that require ground truth (e.g. :func:metrics.evaluate with
positions comparison) can check this flag before running.
Examples:
Functions
from_edge_list(source, source_col='source', target_col='target', **kwargs)
classmethod
Construct a SpatialGraph from a CSV edge list or a DataFrame.
This is the entry point for observational (real) data. Only the
topology is known, so :attr:positions, :attr:edge_metadata, and
:attr:node_metadata are all None after construction. Use
:attr:has_ground_truth to check this programmatically.
Node identifiers in the edge list can be arbitrary (strings, integers,
etc.). They are remapped to a contiguous integer range 0..n-1 and
the mapping is stored in :attr:node_id_map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str or DataFrame
|
Path to a CSV file or a DataFrame with at least two columns containing source and target node identifiers. |
required |
source_col
|
str
|
Column name for the source node. Default is |
'source'
|
target_col
|
str
|
Column name for the target node. Default is |
'target'
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to the constructor (e.g.
|
{}
|
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
A new instance with adjacency_matrix and node_id_map populated.
positions, edge_metadata, and node_metadata are all |
Raises:
| Type | Description |
|---|---|
KeyError
|
If source_col or target_col are not present in the data. |
ValueError
|
If any node identifier is not an integer. Convert non-integer IDs
before calling this method, e.g.
|
Examples:
>>> import pandas as pd
>>> df = pd.DataFrame({"source": [0, 1, 2], "target": [1, 2, 0]})
>>> sn = SpatialGraph.from_edge_list(df)
>>> sn.has_ground_truth
False
>>> sn = SpatialGraph.from_edge_list("edges.csv", source_col="u", target_col="v")
Source code in src/spatial_graph_algorithms/network.py
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 | |
from_positions(source, **kwargs)
classmethod
Construct a SpatialGraph from a point-cloud (no edges).
The adjacency matrix is initialised as an empty n × n matrix. Use
:func:spatial_graph_algorithms.simulate.generate if you want edges built
automatically from the positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str, pd.DataFrame, or np.ndarray
|
Node coordinates. A CSV path or DataFrame is converted to a float array; numeric columns are used as spatial dimensions. |
required |
**kwargs
|
Any
|
Additional keyword arguments forwarded to the constructor. Note
that keep_lcc defaults to |
{}
|
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
A new instance with positions set and an empty adjacency matrix. |
Examples:
>>> import numpy as np
>>> pts = np.random.default_rng(0).random((50, 2))
>>> sn = SpatialGraph.from_positions(pts)
>>> sn.positions.shape
(50, 2)
Source code in src/spatial_graph_algorithms/network.py
to_edge_dataframe()
Export unique edges as a DataFrame.
Returns:
| Type | Description |
|---|---|
DataFrame
|
Two-column DataFrame with integer columns |
Source code in src/spatial_graph_algorithms/network.py
to_positions_dataframe()
Export node positions as a DataFrame.
Column names are x, y for 2-D and x, y, z for 3-D.
Higher-dimensional arrays use x0, x1, ... notation.
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per node, one column per spatial dimension. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If :attr: |
Source code in src/spatial_graph_algorithms/network.py
graph_report(*, plot=True, figsize=(4.5, 3.0), with_all_pairs=False, bins=30, max_edge_sample=50000, max_pair_sample=200000, seed=None)
Return a human-readable summary of the graph and optionally show plots.
When plot is True, two figures are displayed:
- Degree distribution — histogram of node degrees with mean and median marked.
- Edge length histogram — requires positions. With with_all_pairs enabled, overlays the all-pairs distance distribution as a reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plot
|
bool
|
If |
True
|
figsize
|
tuple of float
|
Figure size |
(4.5, 3.0)
|
with_all_pairs
|
bool
|
When |
False
|
bins
|
int
|
Number of bins for the edge-length histogram. |
30
|
max_edge_sample
|
int or None
|
Maximum edges used for the edge-length histogram; larger graphs
are sampled. |
50000
|
max_pair_sample
|
int
|
Maximum all-pair distances sampled for the reference line. |
200000
|
seed
|
int or None
|
Random seed for reproducible sampling. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Multi-line summary of basic graph statistics. |
Source code in src/spatial_graph_algorithms/network.py
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 | |
copy()
Return a deep copy of this SpatialGraph.
The copy is constructed with keep_lcc=False because LCC extraction
has already been applied to the source object.
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
Independent copy with all arrays and DataFrames deep-copied. |
Source code in src/spatial_graph_algorithms/network.py
subgraph(node_indices)
Return a new SpatialGraph restricted to node_indices.
All metadata fields are sliced consistently and edge indices are
remapped to the new contiguous range 0..len(node_indices)-1.
This covers both one-off node selection and repeated LCC re-extraction
after pipeline steps that change graph topology.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_indices
|
array-like of int
|
Indices of nodes to keep, in any order. Duplicates are silently deduplicated; order is preserved after deduplication. |
required |
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
New instance with |
Examples:
>>> from spatial_graph_algorithms.simulate import generate
>>> sg = generate(n=100, seed=0)
>>> sg_sub = sg.subgraph(range(20))
>>> sg_sub.n_nodes
20
Source code in src/spatial_graph_algorithms/network.py
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 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 | |
lcc_subgraph()
Return a new SpatialGraph containing only the largest connected component.
Unlike the keep_lcc=True constructor option, this method can be
called at any point in the pipeline — after denoising, BFS sampling,
or manual edge filtering — without discarding metadata.
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
New instance restricted to the LCC, with all metadata fields filtered and remapped consistently. |
Examples:
>>> from spatial_graph_algorithms.simulate import generate
>>> sg = generate(n=100, seed=0, keep_lcc=False)
>>> sg_lcc = sg.lcc_subgraph()
>>> sg_lcc.n_connected_components
1
Source code in src/spatial_graph_algorithms/network.py
replace(**changes)
Return a new SpatialGraph with selected fields replaced.
Unlike :meth:copy, the adjacency matrix is shared by reference when
not in changes, making pipeline steps that only attach positions or
metadata essentially free in terms of adjacency allocation. The
NetworkX cache is also shared when the adjacency matrix is unchanged.
Passing a adjacency_matrix that is a different object from the
current one emits :class:AdjacencyReplaceWarning to surface the
allocation cost on large graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**changes
|
Any
|
Fields to override. Valid keys: |
{}
|
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
New instance. Shares |
Warns:
| Type | Description |
|---|---|
AdjacencyReplaceWarning
|
When adjacency_matrix is replaced with a different object. |
Examples:
>>> sn_rec = sn.replace(reconstructed_positions=coords)
>>> sn_rec.adjacency_matrix is sn.adjacency_matrix
True
Source code in src/spatial_graph_algorithms/network.py
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
spatial_graph_algorithms.network.to_igraph(sn)
Convert a SpatialGraph to an undirected igraph.Graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sn
|
SpatialGraph
|
Source graph. Only the adjacency matrix is used; metadata is not transferred. |
required |
Returns:
| Type | Description |
|---|---|
Graph
|
Undirected unweighted graph with |
Examples:
>>> from spatial_graph_algorithms.simulate import generate
>>> from spatial_graph_algorithms.network import to_igraph
>>> sn = generate(n=100, seed=0)
>>> g = to_igraph(sn)
>>> g.vcount()
100