Neighbors and message-passing layers
Use a neighbor draw to inspect a local relationship set. Use layers when each hop needs its own fanout, direction or relation policy. Begin with Sampling for graph learning if snapshots or compact IDs are unfamiliar.
A message-passing block is a pair of input/neighbor node frames plus edge positions for a model layer. It is not automatically a PyG or DGL object. Sampling follows the requested graph direction; exported blocks send messages from sampled neighbors to layer inputs, which can reverse stored edge direction.
Draw two layers and inspect their frames
This standalone array example needs no database backend or serializer. The graph
is a -R-> b,c and b,c -S-> d. Both first-hop edges are eligible, so fanout
two returns both. The second layer deduplicates the next frontier, not the edge
occurrences that delivered messages.
Standalone example. Install with python -m pip install "gestaltdb".
1from tempfile import TemporaryDirectory
2from gestaltdb.sampling import (
3 ExternalNeighborSamplingSpec, SamplerEngine, SamplerSnapshot,
4)
5
6with TemporaryDirectory() as root:
7 snapshot = SamplerSnapshot.from_edge_arrays(
8 f"{root}/snapshot", external_node_ids=["a", "b", "c", "d"],
9 external_edge_ids=["ab", "ac", "bd", "cd"],
10 external_relation_ids=["R", "S"],
11 src_int=[0, 0, 1, 2], dst_int=[1, 2, 3, 3], rel_int=[0, 0, 1, 1],
12 edge_weights=[1.0, 3.0, 1.0, 1.0],
13 )
14 engine = SamplerEngine(snapshot, seed=7)
15 neighbors = engine.sample_neighbors_external(
16 ["a"], fanout=2, relations=["R"], strategy="weighted",
17 )
18 assert set(neighbors.to_external(snapshot)["neighbor_nodes"]) == {"b", "c"}
19 layers = engine.sample_layers_external(["a"], [
20 ExternalNeighborSamplingSpec(2, direction="out", relations=["R"]),
21 ExternalNeighborSamplingSpec(2, direction="out", relations=["S"]),
22 ])
23 blocks = layers.to_blocks_external(snapshot)
24 first = blocks[0]
25 message_pairs = {
26 (first["src_nodes"][source], first["dst_nodes"][target])
27 for source, target in first["edge_index"].T
28 }
29 assert message_pairs == {("b", "a"), ("c", "a")}
30 assert set(blocks[1]["src_nodes"]) == {"d"}
31 assert blocks[1]["edge_index"].shape == (2, 2)
Weights affect selection, not eligibility alone. strategy="weighted" must be
explicit; persisted weights do not silently change uniform draws. Weights must
be finite and non-negative. Zero-weight candidates are excluded and a nonempty
all-zero candidate set fails. With replacement, repeated edge occurrences are
preserved in the exported edge_index.
Policies and framework boundaries
Use
direction="in"for incoming relationships,"out"for outgoing, or"any"when both are intended. Relation filters apply before the draw, not after it.balanced=Trueassigns deterministic equal quotas across relations (or node types for node seeds). A small group returns fewer rows without replacement; unused quota is not borrowed from another group.to_blocks(snapshot)keeps compact IDs;to_blocks_external(snapshot)maps node/edge/relation IDs without changing localedge_indexpositions. Each block has its own node frames, not a single global matrix index.to_pyg_blocks(snapshot)requires Torch and returns tensor dictionaries. Training, feature lookup and framework block construction remain external.
For live typed paths, see Live Typed Traversal. For relation/node-type hierarchy expansion, see Relation sub-property hierarchies and Node type hierarchies. Exact policies, balanced quotas and export metadata are documented in Graph sampling API and policy reference; RNG behavior is in Snapshot consistency: staleness, appends and compaction.