spatial_graph_algorithms.simulate
Generate synthetic spatial graphs for benchmarking and testing.
Supported connectivity modes
| Mode | Description | Key parameters |
|---|---|---|
delaunay_corrected |
Delaunay triangulation — recommended default | — |
delaunay |
Standard Delaunay (may include long edges) | — |
knn |
k nearest neighbours | k |
epsilon / epsilon-ball |
All nodes within radius ε | epsilon |
lattice |
Regular grid | — |
distance_decay |
Gaussian-decay connection probability | quantile_scale, power_law_exp |
knn_bipartite |
k-NN between two disjoint partitions | k, bipartite_ratio |
epsilon_bipartite |
Epsilon-ball between two partitions | epsilon, bipartite_ratio |
False edges
Set false_edges_fraction=0.05 to inject 5% random long-range edges,
simulating the sequencing noise present in real Slide-tags / Pixelgen data.
Each injected edge is labelled is_false=True in sg.edge_metadata.
API Reference
spatial_graph_algorithms.simulate.generate(*, n=1000, dim=2, shape='circle', mode='delaunay_corrected', seed=None, scale=1.0, image_path=None, k=8, epsilon=0.15, bipartite_ratio=2, quantile_scale=0.05, power_law_exp=4.0, false_edges_number=None, false_edges_fraction=None)
Generate a simulated spatial graph and return it as a SpatialGraph.
Points are sampled uniformly inside the chosen shape, then connected according to mode. False (shortcut) edges can be injected to simulate the sequencing noise present in real spatial omics data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of nodes to generate. Must be > 1. |
1000
|
dim
|
int
|
Spatial dimension. Must be 2 or 3. |
2
|
shape
|
str
|
Point-cloud geometry. One of: |
'circle'
|
mode
|
str
|
Edge-construction rule. Supported modes:
|
'delaunay_corrected'
|
seed
|
int
|
Random seed for reproducible results. |
None
|
scale
|
float
|
Characteristic length scale of the point cloud. Default is 1.0. |
1.0
|
image_path
|
str
|
Path to a binary image for |
None
|
k
|
int
|
Number of neighbours for |
8
|
epsilon
|
float
|
Neighbourhood radius for |
0.15
|
bipartite_ratio
|
int
|
Ratio of the two node sets for bipartite modes (e.g. 2 → equal halves). |
2
|
quantile_scale
|
float
|
Distance quantile used as the length-scale for |
0.05
|
power_law_exp
|
float
|
Exponent controlling the steepness of the distance-decay kernel. |
4.0
|
false_edges_number
|
int
|
Exact number of false (random long-range) edges to inject. Mutually exclusive with false_edges_fraction. |
None
|
false_edges_fraction
|
float
|
Fraction of existing edges to replace with random false edges. Mutually exclusive with false_edges_number. |
None
|
Returns:
| Type | Description |
|---|---|
SpatialGraph
|
A new graph with positions, adjacency_matrix, node_metadata, and
edge_metadata populated. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n ≤ 1, dim not in {2, 3}, mode is unsupported, k ≤ 0, epsilon ≤ 0, or both false_edges_number and false_edges_fraction are provided. |
Examples:
Minimal usage:
With false-edge noise:
>>> sn = generate(n=500, mode="knn", k=6, false_edges_fraction=0.05, seed=42)
>>> sn.edge_metadata["is_false"].sum()
# number of injected false edges
Source code in src/spatial_graph_algorithms/simulate/__init__.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 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 | |