Sampling for graph learning
GestaltDB can draw neighbors from a live graph or build reusable arrays for training batches. It prepares graph structure and examples; model training stays in your framework. Start here, then choose a task below. The detailed policy and format reference remains in Graph sampling API and policy reference.
Choose live reads or a snapshot
Need |
Live |
|
|---|---|---|
Occasional reads of current relationships |
Typed adjacency, external IDs, open database handle |
Requires a separate build |
Repeated training batches |
Each draw reads stored adjacency |
Build once; reuse compact arrays after closing the database |
Property restrictions on every hop |
Selecting starting nodes alone is insufficient |
Build the eligible graph with node/edge filters |
Memory and cost |
Reservoir storage is bounded by fanout; eligible adjacency is still scanned |
RAM or memmap; builds, candidate/result buffers and validation still cost memory |
A seed is a starting entity; an RNG seed controls randomness. Fanout is the number of neighbors requested per input node. With no replacement, fewer eligible neighbors means fewer results, not padding to the requested count. A snapshot does not track subsequent database writes.
Know which IDs you have
External IDs are application identifiers such as
"drug-a". Live traversal records carry byte IDs;graph.key_to_stringdecodes them.Compact global IDs identify rows in one exact snapshot. Resolve an external ID with
snapshot.node_int(...); never reuse a row number in another snapshot.Batch-local IDs identify rows in a sampled subgraph. Map them through
batch.node_ids_globalbefore resolving external IDs.
Methods ending in _external accept external IDs but still return compact
batches. Neighbor/layer/walk batches expose to_external(snapshot) for readable
output. Convert at your application boundary rather than on every training step.
Build and reuse a filtered graph
This complete example creates a drug with two known targets. Only the high-score protein enters the snapshot, so every sampled edge stays in the eligible graph. Starting from the same drug in the unfiltered graph would still reach both proteins: filtering seeds is not filtering the traversed graph.
The example requires a verifiable current snapshot. It evaluates property filters inside the pinned build rather than selecting a cohort at a different horizon.
Standalone example. Choose the capability-qualified installation tab below.
This workflow requires a verifiable current snapshot so property filters run against one pinned source. LevelDB and plain PyRex are not equivalent alternatives.
1python -m pip install "gestaltdb[lmdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='lmdb', serializer='json')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[lmdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='lmdb', serializer='pickle')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[lmdb,msgpack]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='lmdb', serializer='messagepack')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[lmdb,protobuf]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='lmdb', serializer='protobuf')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[rocksdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='pyrex', backend_options={"transactional": True}, serializer='json')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[rocksdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='pyrex', backend_options={"transactional": True}, serializer='pickle')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[msgpack,rocksdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='pyrex', backend_options={"transactional": True}, serializer='messagepack')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
1python -m pip install "gestaltdb[protobuf,rocksdb]"
1from tempfile import TemporaryDirectory
2
3from gestaltdb.graphdb import Edge, GraphDB, Node
4from gestaltdb.sampling import SamplerEngine
5
6with TemporaryDirectory() as root:
7 graph = GraphDB.create(f"{root}/graph", backend='pyrex', backend_options={"transactional": True}, serializer='protobuf')
8 try:
9 graph.put_nodes([
10 Node("drug-a", labels=["Drug"], properties={"approved": True}),
11 Node("protein-good", labels=["Protein"], properties={"score": 0.9}),
12 Node("protein-low", labels=["Protein"], properties={"score": 0.2}),
13 ])
14 graph.put_edges_bulk([
15 Edge("good", "drug-a", "protein-good", properties={"type": "binds"}),
16 Edge("low", "drug-a", "protein-low", properties={"type": "binds"}),
17 ])
18 live = graph.sample_neighbors("drug-a", "binds", sample_size=10)
19 assert {graph.key_to_string(row["neighbor_id"]) for row in live} == {
20 "protein-good", "protein-low",
21 }
22 snapshot = graph.build_sampler_snapshot(
23 f"{root}/snapshot", read_snapshot=True,
24 node_filter=lambda node: (
25 "Drug" in node.labels and node.properties.get("approved") is True
26 or "Protein" in node.labels and node.properties.get("score", 0) >= 0.8
27 ),
28 edge_filter=lambda edge: edge.properties.get("type") == "binds",
29 )
30 finally:
31 graph.close()
32
33 engine = SamplerEngine.load(snapshot.path, mode="memmap", seed=7)
34 neighbors = engine.sample_neighbors_external(
35 ["drug-a"], fanout=10, relations=["binds"], direction="out",
36 )
37 names = neighbors.to_external(engine.snapshot)["neighbor_nodes"]
38 assert list(names) == ["protein-good"]
39 assert neighbors.offsets.tolist() == [0, 1]
40 print(list(names)) # ['protein-good']
offsets delimit each input node’s output rows. Here one input has one eligible
neighbor, despite requesting ten. Excluding a node excludes its incident edges;
retained nodes can still be isolated. Build callbacks scan decoded records, not
automatic property-index pushdown. For explicit indexed cohorts and concurrency
qualifications, see Property-Graph Filtering and Indexes.
Choose your next task
Goal |
Guide |
|---|---|
Neighbors and message-passing layers |
|
Node-induced or packed subgraphs |
|
Random walks and embedding inputs |
|
Historical point/window samples |
|
Positive rejection and training negatives |
|
Expand parent relations or node types |
Use Snapshot consistency: staleness, appends and compaction for source pinning, RNG streams and immutable artifact lifetime, Performance and Benchmarks for build/load/draw measurement, and Graph sampling API and policy reference for the complete API and format contracts. Snapshot construction materializes source data; memmap is not an out-of-core build or a hard memory limit. Check API Reference for exact signatures.