Sample subgraphs for training
Choose a topology sampling method by its selection rule, not just its output shape. An induced subgraph contains every eligible edge between selected nodes. A neighbor expansion instead follows chosen edges and need not contain every edge between the reached nodes. See Sampling for graph learning for ID domains and the distinction between seed filters and eligible-graph filters.
Method |
Selection |
|---|---|
|
Seed edges, sampled neighborhoods, optional training negatives |
|
One independent edge-seeded expansion per group, with component offsets |
|
Uniform node draw and every eligible induced edge under a required cap |
|
One uniform random balanced partition block, not a cut-optimized partition |
Recover graph edges from local rows
This standalone example needs no backend. It draws three of four nodes, retains all eligible induced edges, then maps their local endpoints back to application IDs. It also packs two seed-edge groups without merging their node frames.
Standalone example. Install with python -m pip install "gestaltdb".
1from tempfile import TemporaryDirectory
2import numpy as np
3from gestaltdb.sampling import SamplerEngine, SamplerSnapshot
4
5with TemporaryDirectory() as root:
6 snapshot = SamplerSnapshot.from_edge_arrays(
7 f"{root}/snapshot", external_node_ids=["a", "b", "c", "d"],
8 external_edge_ids=["ab", "ac", "bd", "cd"],
9 external_relation_ids=["R"],
10 src_int=[0, 0, 1, 2], dst_int=[1, 2, 3, 3], rel_int=[0, 0, 0, 0],
11 )
12 engine = SamplerEngine(snapshot, seed=7)
13 batch = engine.sample_induced_subgraph(3, max_edges=4, normalize=True)
14 names = [snapshot.external_node_id(node) for node in batch.node_ids_global]
15 pairs = {(names[source], names[target])
16 for source, target in zip(batch.senders, batch.receivers)}
17 expected = {(a, b) for a, b in [("a", "b"), ("a", "c"), ("b", "d"), ("c", "d")]
18 if a in names and b in names}
19 assert pairs == expected
20 assert len(names) == 3
21 assert np.allclose(batch.node_weights, 4 / 3)
22 assert np.allclose(batch.edge_weights, 2.0)
23 packed = engine.sample_packed_subgraphs(
24 [[snapshot.edge_int("ab")], [snapshot.edge_int("cd")]], fanouts=[1],
25 )
26 assert len(packed.graph_node_offsets) == len(packed.graph_edge_offsets) == 3
27 assert packed.positives.shape == (2, 3)
senders and receivers are local rows, not snapshot IDs. Packed results
use concatenated local frames delimited by graph_node_offsets and
graph_edge_offsets; the same global node can appear in several components.
Positive rows follow seed-group order. See Build negative training examples before
attaching training negatives.
Caps and normalization
max_edges raises if the induced edge set exceeds the cap. The method does
not truncate the edge set or silently redraw nodes. Relation/time filters restrict
eligible edges before this check; they do not change uniform node selection.
normalize=True attaches reciprocal inclusion-probability weights, not model
confidence or logical probabilities. For an induced draw of k out of N
nodes, node weights are N/k and non-loop edge weights are
N*(N-1)/(k*(k-1)); self-loops carry the node weight. Training must explicitly
use these weights. They are absent by default. Partition-block normalization uses
its own selection probability; see Bounded Induced Subgraphs for exact
contracts and Performance and Benchmarks for memory qualifications.