Skip to content

NetworkX adapter

Convert between an AnnNet object and a NetworkX graph. NetworkX holds nodes, binary edges and their attributes. to_nx also gives back a manifest with what NetworkX cannot hold, such as hyperedges, slices and layers. Give the manifest to from_nx to rebuild the full graph. To run a NetworkX algorithm without converting yourself, use anet.nx.

annnet.adapters.networkx_adapter.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).

annnet.adapters.networkx_adapter.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(anet)) 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)

annnet.adapters.networkx_adapter.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.