NetworkX Adapter
NetworkX conversion helpers from annnet.adapters.networkx_adapter.
annnet.adapters.networkx_adapter
Classes
Functions
to_nx(graph, directed=True, hyperedge_mode='skip', slice=None, slices=None, public_only=False, reify_prefix='he::')
Export AnnNet → (networkx.AnnNet, manifest). Manifest preserves hyperedges with per-endpoint coefficients, slices, vertex/edge attrs, and stable edge IDs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
AnnNet
|
|
required |
directed
|
bool
|
|
True
|
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) |
save_manifest(manifest, path)
Write manifest to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest
|
dict
|
Manifest dictionary from to_nx(). |
required |
path
|
str
|
Output file path (typically .json extension). |
required |
Returns:
| Type | Description |
|---|---|
None
|
|
Raises:
| Type | Description |
|---|---|
OSError
|
If file cannot be written. |
load_manifest(path)
Load manifest from JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to manifest JSON file created by save_manifest(). |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Manifest dictionary suitable for from_nx(). |
Raises:
| Type | Description |
|---|---|
OSError
|
If file cannot be read. |
JSONDecodeError
|
If file contains invalid JSON. |
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).
to_backend(graph, **kwargs)
Export AnnNet to NetworkX without manifest (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
AnnNet
|
Source AnnNet instance to export. |
required |
**kwargs
|
Forwarded to _export_legacy(). Supported: - directed : bool, default True Export as MultiDiGraph (True) or MultiGraph (False). - skip_hyperedges : bool, default True If True, drop hyperedges. If False, expand them (cartesian product for directed, clique for undirected). - public_only : bool, default False Strip attributes starting with "__" if True. |
{}
|
Returns:
| Type | Description |
|---|---|
MultiGraph | MultiDiGraph
|
NetworkX graph containing binary edges only. Hyperedges are either dropped or expanded. No manifest is returned, so round-tripping will lose hyperedge structure, slices, and precise edge IDs. |
Notes
This is a lossy export. Use to_nx() with manifest for full fidelity.
from_nx_only(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::')
Best-effort import from a bare NetworkX graph (no manifest). hyperedge: "none" (default) | "reified" When "reified", detect hyperedge nodes + membership edges and rebuild true hyperedges.