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
|
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_bulk add_edges_bulk view
Attributes
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:
attrs
property
views
property
ops
property
layers
property
slices
property
idx
property
cache
property
nx
property
Lazy NetworkX proxy.
Returns:
| Type | Description |
|---|---|
_NXBackendAccessor
|
Proxy exposing NetworkX algorithms. |
ig
property
Lazy igraph proxy.
Returns:
| Type | Description |
|---|---|
_IGBackendAccessor
|
Proxy exposing igraph algorithms. |
gt
property
Lazy graph-tool proxy.
Returns:
| Type | Description |
|---|---|
_GTBackendAccessor
|
Proxy exposing graph-tool algorithms. |
Functions
add_vertices
Canonical public entry point for vertex insertion.
Accepts either a single vertex spec or an iterable of vertex specs.
add_edges
Canonical public entry point for edge insertion.
Accepts either a single edge spec or an iterable of edge specs.
remove_vertices
Remove many vertices (and their incident edges) in one pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_ids
|
Iterable[str]
|
Vertex identifiers to remove. |
required |
remove_edges
Remove many edges in one pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_ids
|
Iterable[str]
|
Edge identifiers to remove. |
required |
has_vertex
Test for the existence of a vertex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_id
|
str
|
Vertex identifier. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the vertex exists. |
has_edge
Check edge existence using one of three modes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str
|
Source entity ID. |
None
|
target
|
str
|
Target entity ID. |
None
|
edge_id
|
str
|
Edge identifier. |
None
|
Returns:
| Type | Description |
|---|---|
bool | tuple[bool, list[str]]
|
One of:
- bool, if only |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the argument combination is invalid. |
vertices
Get all vertex IDs (excluding edge-entities).
Returns:
| Type | Description |
|---|---|
list[str]
|
Vertex IDs. |
degree
Degree of a vertex or edge-entity (number of incident non-zero entries).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
entity_id
|
str
|
Entity identifier. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Degree of the entity. |
incident_edges
Iterate over 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 yielded for both directions. |
"in"
|
Yields:
| Type | Description |
|---|---|
tuple[int, tuple]
|
Pairs of |
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
|
|
write
Write the graph to the native .annnet format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Output file path. |
required |
**kwargs
|
Passed to |
{}
|
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
|
|
global_count
get_vertex
Return the vertex ID corresponding to a given internal index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Internal vertex index. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The vertex ID. |
get_edge
Return edge endpoints in a canonical form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | str
|
Internal edge index or edge ID. |
required |
Returns:
| Type | Description |
|---|---|
tuple[frozenset, frozenset]
|
|
edge_list
Materialize (source, target, edge_id, weight) for binary/vertex-edge edges.
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str, float]]
|
Tuples of |
make_undirected
Force the whole graph to be undirected in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
drop_flexible
|
bool
|
If True, clear flexible-direction policies. |
True
|
update_default
|
bool
|
If True, set the graph default to undirected for future edges. |
True
|
Notes
Binary edges are rewritten to (+w, +w) at (source, target).
Hyperedges are rewritten to +w on all members.