Skip to content

NetworkX Adapter

NetworkX conversion helpers from annnet.adapters.networkx_adapter.

annnet.adapters.networkx_adapter

AnnNet-NetworkX adapter for AnnNet.

Provides: to_nx(G) -> networkx.Graph, networkx.DiGraph, networkx.MultiGraph, or networkx.MultiDiGraph from_nx(nxG) -> AnnNet

NetworkX natively represents: - nodes/nodes - binary edges - graph, node, and edge attributes - directed, undirected, simple, and multigraph structures

AnnNet-specific structures such as hyperedges, slices, multilayer metadata, per-edge directedness, and richer attribute tables are preserved through manifest-style graph attributes where possible.

Classes

Functions

to_nx
to_nx(
    graph,
    directed=True,
    hyperedge_mode="skip",
    hyperedges=None,
    coefficients="error",
    slice=None,
    slices=None,
    public_only=False,
    reify_prefix="he::",
)

Export AnnNet → (networkx graph, manifest).

Both halves are returned because the manifest carries the state a NetworkX graph cannot hold, and :func:from_nx takes it as its second argument: from_nx(*to_nx(G)) is the round trip.

Manifest preserves hyperedges with per-endpoint coefficients, slices, node/edge attrs, and stable edge IDs.

Parameters:

Name Type Description Default
graph AnnNet
required
directed bool
True
hyperedges str

The same choice as hyperedge_mode, under the name every exporter shares, and accepting "star" and "clique" as aliases for "reify" and "expand". Takes precedence when both are given.

None
coefficients ('error', 'drop')

What to do when the projection would drop per-member coefficients. Only "expand" loses them, and losing them silently is what this refuses.

"error"
hyperedge_mode ('skip', 'expand', 'reify')
"skip"
slice str

Export single slice only (affects which hyperedges are reified).

None
slices list[str]

Export union of specified slices (affects which hyperedges are reified).

None
public_only bool
False

Returns:

Type Description
tuple[AnnNet, dict]

(nxG, manifest)

from_nx
from_nx(
    nxG,
    manifest,
    *,
    hyperedge="none",
    he_node_flag="is_hyperedge",
    he_id_attr="eid",
    role_attr="role",
    coeff_attr="coeff",
    membership_attr="membership_of",
    reify_prefix="he::"
)

Reconstruct a AnnNet from NetworkX graph + manifest.

hyperedge: "none" (default) | "reified" When "reified", also detect hyperedge nodes in nxG and rebuild true hyperedges (in addition to those specified in the manifest).

from_nx_without_manifest
from_nx_without_manifest(
    nxG,
    *,
    hyperedge="none",
    he_node_flag="is_hyperedge",
    he_id_attr="eid",
    role_attr="role",
    coeff_attr="coeff",
    membership_attr="membership_of",
    reify_prefix="he::"
)

Public façade for best-effort import from bare NetworkX graphs.