Graph (graph)¶
Terms¶
- Graph
A set of vertices (nodes) and edges joining pairs of them. It may be disconnected and may have cycles. The type constant is
GEOM_GRAPH,"graph".kindThe
write_graphargument choosing the geometry type:"graph"(the default) or"skeleton". It is not stored.
Introduction¶
The graph type stores any vertex–edge graph, such as a vascular network
with loops. For trees, skeleton stores fewer records.
Every edge is a link record (links_convention: explicit). An edge whose
ends share a chunk goes in links/0/0.0.0/; any other edge goes in the
array named by the offset between its chunks. A stored permutation lets a
reader return each edge’s endpoints in written order.
Objects come from object_ids=, one ID per node; without it, every node
is in object 0. Each chunk has one fragment per object. What every type
shares is on Geometry types.
Technical reference¶
Arrays present¶
At level 0 of a store written by write_graph(kind="graph"):
Path |
Present |
Description |
|---|---|---|
|
Always |
One fragment per object per chunk |
|
Always, possibly empty |
Intra-chunk edges, chunk-local indices |
|
When an edge crosses chunks |
Cross-chunk edges |
|
Always |
Fragment index over the intra-chunk link rows |
|
Always |
One manifest per object |
|
With |
|
|
With |
One row per edge, mirroring |
|
With the matching argument |
The root attributes:
{"geometry_types": ["graph"], "links_convention": "explicit", ...}
Write and read API¶
import numpy as np
from zarr_vectors.building import write_graph
from zarr_vectors.types.graphs import read_graph
rng = np.random.default_rng(0)
n_nodes = 500
positions = rng.uniform(0, 1000, (n_nodes, 3)).astype(np.float32)
edges = rng.integers(0, n_nodes, (1200, 2)).astype(np.int64)
edges = edges[edges[:, 0] != edges[:, 1]]
cell = (np.arange(n_nodes) % 4).astype(np.int64) # owning object of each node
summary = write_graph(
"network.zarrvectors",
positions,
edges,
chunk_shape=(500.0, 500.0, 500.0),
object_ids=cell,
link_attributes={"weight": rng.uniform(0, 1, len(edges)).astype(np.float32)},
)
print(summary)
# {'node_count': 500, 'edge_count': 1198, 'chunk_count': 8, 'intra_edge_count': 152,
# 'cross_edge_count': 1046, 'object_count': 4, 'kind': 'graph'}
result = read_graph("network.zarrvectors", object_ids=[2])
print(result["node_count"], result["edge_count"]) # 125 71
edges is (M, 2) of node indices; any other shape raises ArrayError.
A kind other than "graph" or "skeleton" raises ValueError, and
out_of_bounds="ignore" raises ArrayError, since it would leave edges
dangling. write_graph has no directed argument: it always writes an
undirected family. The deprecated is_tree, node_attributes and
edge_attributes arguments still work but warn. Through the dataset API,
use ds.add_graph(positions, edges, tree=False, ...).
read_graph returns no vertex, link or object attributes. It returns:
Key |
Type |
Description |
|---|---|---|
|
|
Node positions |
|
|
Edges as indices into |
|
|
Sizes of the result |
|
|
Owning object of each node; only when |
A scoped read returns the induced subgraph: the nodes the scope selects and the edges with both ends among them. So an edge between two objects comes back only when both are asked for.
Validation¶
Only the common checks apply. No level checks self-loops, duplicate edges or connectivity.