Quickstart¶
Build your first AnnNet in five minutes. By the end you'll have:
- A directed graph with four vertices and three edges
- A look at how to inspect it
- A save-and-reload round-trip
Nothing else. Subsequent lessons add metadata, views, slices, etc.
1. Create a graph¶
AnnNet is the central class. Pass directed=True/False at construction.
In [1]:
Copied!
from annnet import AnnNet
G = AnnNet(directed=True)
G
from annnet import AnnNet
G = AnnNet(directed=True)
G
Out[1]:
AnnNet object with n_vertices × n_edges = 0 × 0
directed: True
slices: ['default']
2. Add vertices¶
Several input shapes are accepted; pass a list of ids for the common case.
In [2]:
Copied!
G.add_vertices(['A', 'B', 'C', 'D'])
print('vertex count:', G.nv)
print('vertex ids: ', sorted(G.vertices()))
G.add_vertices(['A', 'B', 'C', 'D'])
print('vertex count:', G.nv)
print('vertex ids: ', sorted(G.vertices()))
vertex count: 4 vertex ids: ['A', 'B', 'C', 'D']
3. Add edges¶
Each edge gets an explicit edge_id and an optional weight. The first
two positional args are source and target.
In [3]:
Copied!
G.add_edges('A', 'B', edge_id='e1', weight=1.0)
G.add_edges('B', 'C', edge_id='e2', weight=2.0)
G.add_edges('C', 'D', edge_id='e3', weight=1.5)
print('edge count:', G.ne)
print('shape (nv, ne):', G.shape)
G.add_edges('A', 'B', edge_id='e1', weight=1.0)
G.add_edges('B', 'C', edge_id='e2', weight=2.0)
G.add_edges('C', 'D', edge_id='e3', weight=1.5)
print('edge count:', G.ne)
print('shape (nv, ne):', G.shape)
edge count: 3 shape (nv, ne): (4, 3)
4. Inspect¶
The fastest tabular view is G.views.edges(). It produces a dataframe
with source/target/weight columns plus any attributes you've attached.
In [4]:
Copied!
G.views.edges().head()
G.views.edges().head()
Out[4]:
shape: (3, 11)
| edge_id | kind | source | target | edge_type | head | tail | members | directed | global_weight | effective_weight |
|---|---|---|---|---|---|---|---|---|---|---|
| str | str | str | str | str | null | null | null | bool | f64 | f64 |
| "e1" | "binary" | "A" | "B" | "binary" | null | null | null | true | 1.0 | 1.0 |
| "e2" | "binary" | "B" | "C" | "binary" | null | null | null | true | 2.0 | 2.0 |
| "e3" | "binary" | "C" | "D" | "binary" | null | null | null | true | 1.5 | 1.5 |
5. Save and reload¶
.annnet is the native format. Lossless, manifest-aware.
In [5]:
Copied!
import tempfile
from pathlib import Path
from annnet import read, write
path = Path(tempfile.mkdtemp()) / 'demo.annnet'
write(G, path)
G2 = read(path)
print('reloaded:', G2.shape)
import tempfile
from pathlib import Path
from annnet import read, write
path = Path(tempfile.mkdtemp()) / 'demo.annnet'
write(G, path)
G2 = read(path)
print('reloaded:', G2.shape)
reloaded: (4, 3)
Next: 02 — Attributes & views.