Graph
Primary graph objects from annnet.core.graph.
The main graph API centers on AnnNet/Graph, bulk vertex and edge
construction with add_vertices and add_edges, graph-owned accessors
(slices, layers, attrs, views, ops, idx, cache), annotation
tables (obs, var, uns), and backend accessors (nx, ig, gt).
AnnNet
annnet.core.graph.AnnNet
Incidence-based graph with slices, multilayer coordinates, and rich edge types.
AnnNet stores topology in a sparse incidence matrix backed by canonical entity and edge registries. A row represents an entity, typically a vertex or an edge-entity, and a column represents an edge. The class supports:
- binary directed and undirected edges
- hyperedges, including directed head/tail hyperedges
- edge-entities that can themselves participate as endpoints
- slice membership and per-slice edge weights
- optional multilayer coordinates on vertices and edges
- dataframe-backed attribute storage
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directed
|
bool | None
|
Default directedness for newly created binary edges. If |
None
|
v
|
int
|
Initial row capacity for the incidence matrix. |
0
|
e
|
int
|
Initial column capacity for the incidence matrix. |
0
|
annotations
|
dict | None
|
Pre-built annotation tables to use instead of creating empty tables. |
None
|
annotations_backend
|
('auto', 'polars', 'pandas', 'pyarrow')
|
Preferred backend for newly initialized annotation tables. |
"auto"
|
aspects
|
dict[str, list[str]] | None
|
Initial multilayer aspect declaration. If omitted, the graph starts
flat with a single placeholder aspect |
None
|
**kwargs
|
Any
|
Initial graph-level attributes stored in :attr: |
{}
|
Notes
Directed incidence columns use positive values for sources or heads and negative values for targets or tails. Undirected binary edges and undirected hyperedges use positive values for all incident entities.
See Also
add_vertex add_edge add_vertices add_edges view
Attributes
num_vertices
property
Number of stored vertices.
Returns:
| Type | Description |
|---|---|
int
|
Same value as :attr: |
num_edges
property
Number of structural edges.
Returns:
| Type | Description |
|---|---|
int
|
Same value as :attr: |
nv
property
Number of stored vertices.
Returns:
| Type | Description |
|---|---|
int
|
Count of entities whose internal kind is |
ne
property
Number of structural edges.
Returns:
| Type | Description |
|---|---|
int
|
Count of incidence-matrix edge columns. |
shape
property
Graph shape as (num_vertices, num_edges).
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Vertex and edge counts. |
V
property
All vertices as an immutable tuple.
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Vertex IDs in graph iteration order. |
E
property
All edges as an immutable tuple.
Returns:
| Type | Description |
|---|---|
tuple[str, ...]
|
Edge identifiers in graph iteration order. |
obs
property
Notebook-friendly vertex attribute table.
Returns:
| Type | Description |
|---|---|
DataFrame - like
|
|
Notes
This is the quickest way to inspect vertex annotations in a notebook. It returns the underlying vertex-attribute table unchanged.
Examples:
>>> G = AnnNet()
>>> G.add_vertices([{'vertex_id': 'A', 'kind': 'gene'}])
>>> G.obs
>>> G.views.vertices()
Use :attr:obs when you want the raw vertex table directly. Use
:attr:views when you want an explicit namespace for materialized
tables.
var
property
Notebook-friendly edge attribute table.
Returns:
| Type | Description |
|---|---|
DataFrame - like
|
|
Notes
This is the direct edge-annotation table. It is often the most useful object to display in a notebook after edge insertion or IO.
Examples:
uns
property
Graph-level unstructured metadata.
Returns:
| Type | Description |
|---|---|
dict
|
Mutable dictionary of graph-level attributes. |
attrs
property
Attribute operations namespace.
Returns:
| Type | Description |
|---|---|
AttributesAccessor
|
Manager for graph-, vertex-, edge-, slice-, and edge-slice annotations. |
Notes
Use this namespace for graph-, vertex-, edge-, and slice-level annotations.
Examples:
views
property
Materialized table namespace.
Returns:
| Type | Description |
|---|---|
ViewsAccessor
|
Manager for dataframe-style materialized views. |
Notes
This is the preferred namespace for notebook inspection and export of graph tables.
Examples:
ops
property
Structural operations namespace.
Returns:
| Type | Description |
|---|---|
OperationsAccessor
|
Manager for subgraphs, copies, reversals, incidence extraction, and memory inspection. |
Examples:
layers
property
Layer operations namespace.
Returns:
| Type | Description |
|---|---|
LayerAccessor
|
Manager for multilayer aspects, layer coordinates, supra matrices, and layer set operations. |
Notes
All multilayer configuration and layer-aware analysis lives here.
Examples:
slices
property
Slice operations namespace.
Returns:
| Type | Description |
|---|---|
SliceManager
|
Manager exposing slice creation, membership, set operations, and slice-level analysis. |
Examples:
idx
property
Index lookup namespace.
Returns:
| Type | Description |
|---|---|
IndexManager
|
Manager for entity-to-row and edge-to-column index lookups. |
cache
property
Sparse matrix cache namespace.
Returns:
| Type | Description |
|---|---|
CacheManager
|
Manager for derived sparse matrix formats such as CSR and CSC. |
nx
property
ig
property
Igraph interoperability namespace.
Returns:
| Type | Description |
|---|---|
_IGBackendAccessor
|
Lazy proxy that converts to igraph only when requested. |
gt
property
graph-tool interoperability namespace.
Returns:
| Type | Description |
|---|---|
_GTBackendAccessor
|
Lazy proxy that converts to graph-tool only when requested. |
is_multilayer
property
Whether the graph has declared multilayer aspects.
Returns:
| Type | Description |
|---|---|
bool
|
|
Functions
add_vertices
Add one vertex or many vertices.
This is the canonical public entry point for vertex creation. Use it for both single-vertex and batch insertion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
str | dict | tuple | Iterable
|
Vertex specification or iterable of specifications. Accepted single-vertex forms are:
Accepted batch forms are iterables of the same specifications. |
required |
slice
|
str
|
Slice receiving the inserted vertices. If omitted, the active slice is used. |
None
|
layer
|
str | tuple | dict
|
Layer coordinate for inserted vertices in multilayer graphs. A string is valid only for single-aspect graphs; a tuple must already be in aspect order; a dict maps aspect name to layer value. |
None
|
**attributes
|
Any
|
Attributes applied to a single vertex. These are merged with
attributes in |
{}
|
Returns:
| Type | Description |
|---|---|
str | list[str]
|
The inserted vertex ID for a single vertex, or a list of vertex IDs for batch insertion. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a dictionary vertex specification does not contain
|
Notes
Vertex attributes are stored in :attr:obs and can be edited through
:attr:attrs. In multilayer graphs, omitting layer places the
vertex on the placeholder layer coordinate.
Examples:
add_edges
Add one edge, many edges, or hyperedges.
This is the canonical public entry point for all edge creation. It handles binary edges, directed and undirected edges, edge-entities, and hyperedges through the shape of the input specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Edge specification. Common forms are:
|
()
|
**kwargs
|
Any
|
Options for single-edge or batch insertion. |
{}
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
source |
str
|
Source endpoint for a binary edge. |
src |
str
|
Source endpoint for a binary edge. |
target |
str
|
Target endpoint for a binary edge. |
tgt |
str
|
Target endpoint for a binary edge. |
weight |
float
|
Incidence weight for the edge. |
edge_id |
str
|
Explicit edge identifier. If omitted, an |
directed |
bool
|
Directedness for a single edge. |
edge_directed |
bool
|
Directedness for edge specs in batch input. |
slice |
str
|
Slice receiving the inserted edges. If omitted, the active slice is used. |
as_entity |
bool
|
If |
parallel |
('update', 'error', 'parallel')
|
Policy for single-edge insertion when |
propagate |
('none', 'shared', 'all')
|
Slice propagation policy. |
flexible |
dict
|
Data-driven direction policy. Requires keys |
default_weight |
float
|
Batch default for edge specs without an explicit weight. |
default_edge_directed |
bool
|
Batch default directedness. |
default_propagate |
('none', 'shared', 'all')
|
Batch default propagation policy. |
default_edge_type |
str
|
Batch default edge type stored in the edge record. |
default_slice_weight |
float
|
Batch per-slice weight override. |
Returns:
| Type | Description |
|---|---|
str | list[str]
|
Edge ID for single-edge insertion, or a list of edge IDs for batch insertion. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If unsupported keyword arguments are supplied for batch insertion. |
ValueError
|
If the edge specification is structurally invalid. |
Notes
Hyperedges are detected from dictionaries containing "members" for
undirected hyperedges or "head"/"tail" for directed hyperedges.
Binary edges use "source"/"target" or "src"/"tgt".
For a full guide covering all input forms, dispatch logic, parallel policy, propagation, flexible direction, and batch formats, see the Adding edges explanation page.
Examples:
remove_vertices
Remove one vertex or many vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_ids
|
str | tuple | Iterable[str | tuple]
|
Vertex ID, explicit multilayer vertex key, or iterable of IDs/keys. |
required |
errors
|
('raise', 'ignore')
|
|
"raise"
|
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Incident edges are removed with each vertex.
Examples:
remove_edges
Remove one edge or many edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_ids
|
str | Iterable[str]
|
Edge ID or iterable of edge IDs to remove. |
required |
errors
|
('raise', 'ignore')
|
|
"raise"
|
Returns:
| Type | Description |
|---|---|
None
|
|
Examples:
has_vertex
Check whether a vertex exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_id
|
str | tuple
|
Bare vertex ID, or explicit |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Notes
In multilayer graphs, a bare vertex ID returns True if that vertex
is present on at least one layer coordinate.
has_edge
Check whether an edge exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source endpoint. |
None
|
target
|
str
|
Target endpoint. |
None
|
edge_id
|
str
|
Edge identifier. |
None
|
Returns:
| Type | Description |
|---|---|
bool | tuple[bool, list[str]]
|
If only |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the argument combination is invalid. |
Examples:
vertices
Return all vertex IDs.
Returns:
| Type | Description |
|---|---|
list[str]
|
Vertex identifiers, excluding edge-entities. |
Notes
In multilayer graphs, the returned IDs are bare vertex IDs. Use
G.idx or layer-specific methods when layer coordinates are needed.
edges
Return all structural edge IDs.
Returns:
| Type | Description |
|---|---|
list[str]
|
Edge identifiers for edges with an incidence-matrix column. |
degree
Return the incidence degree of a vertex or edge-entity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str | tuple
|
Vertex ID, edge-entity ID, or explicit multilayer entity key. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of non-zero incidence entries in the entity row. Missing
entities have degree |
incident_edges
Return edges incident to one or more vertices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
str | Iterable[str]
|
One vertex identifier or an iterable of identifiers. |
required |
direction
|
('in', 'out', 'both')
|
Directional filter applied to binary edges. Undirected edges are
included for both |
"in"
|
Returns:
| Type | Description |
|---|---|
list[tuple[int, EdgeView]]
|
Pairs of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
number_of_vertices
Return the number of vertices.
Returns:
| Type | Description |
|---|---|
int
|
Number of stored vertex entities. |
See Also
nv : Property alias for the same count. num_vertices : Descriptive property alias for the same count.
number_of_edges
Return the number of edges.
Returns:
| Type | Description |
|---|---|
int
|
Number of structural edges with incidence columns. |
See Also
ne : Property alias for the same count. num_edges : Descriptive property alias for the same count.
read
classmethod
Read a graph from the native .annnet format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Input file path. |
required |
**kwargs
|
Passed to |
{}
|
Returns:
| Type | Description |
|---|---|
AnnNet
|
Deserialized graph. |
Examples:
write
view
Create a lazy graph view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertices
|
Iterable[str]
|
Vertex IDs to include. |
None
|
edges
|
Iterable[str]
|
Edge IDs to include. |
None
|
slices
|
Iterable[str]
|
Slice IDs to include. |
None
|
predicate
|
callable
|
Predicate used for additional filtering. |
None
|
Returns:
| Type | Description |
|---|---|
GraphView
|
View object backed by this graph. |
Notes
Views are lightweight filters over an existing graph. Use
:attr:views for materialized dataframe views.
global_count
Count unique members present across slices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kind
|
('vertices', 'edges', 'entities')
|
Membership domain. |
"vertices"
|
Returns:
| Type | Description |
|---|---|
int
|
Number of unique members observed in slice membership. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Notes
This is a slice-membership count, not a storage count. For graph
storage counts, use :attr:num_vertices and :attr:num_edges.
get_vertex
Return the vertex identifier stored at an internal row index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Incidence-matrix row index. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Vertex ID at |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Notes
This is an index-level lookup. Most user code should use
:meth:vertices unless it specifically needs row-index mapping.
get_edge
Return an :class:EdgeView for the requested edge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | str
|
Incidence-matrix column index or edge ID. |
required |
Returns:
| Type | Description |
|---|---|
EdgeView
|
A tuple-shaped record. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If an edge ID is unknown. |
edge_list
Materialize binary edges as endpoint tuples.
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, float]]
|
Tuples of |
make_undirected
Convert all existing edges to undirected form in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drop_flexible
|
bool
|
If |
True
|
update_default
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
AnnNet
|
The modified graph, returned for chaining. |
Notes
Directed binary edges are rewritten from signed incidence
(+w, -w) to unsigned incidence (+w, +w). Directed hyperedges are
converted to undirected hyperedges over the union of their head and
tail members.
Examples: