CX2
CX2 helpers from annnet.io.cx2_io.
annnet.io.cx2_io
CX2 Adapter for AnnNet AnnNet.
Provides: to_cx2(G) -> List[Dict[str, Any]] (CX2 JSON object) from_cx2(cx2_data) -> AnnNet
This adapter maps AnnNet Graphs to the Cytoscape Exchange (CX2) format. It creates a lossless representation by serializing complex features (hyperedges, slices, multilayer) into a 'manifest' stored within the CX2 'networkAttributes'.
Classes
Functions
to_cx2(G, *, export_name='annnet export', layer=None, include_inter=False, include_coupling=False, hyperedges='skip')
Convert an AnnNet graph to CX2 compliant JSON format.
The output is a list of aspect dictionaries (CX2 format). Complex AnnNet features (hyperedges, slices, multilayer structure) are serialized into a JSON string stored in networkAttributes under 'AnnNet_Manifest'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
AnnNet
|
The graph to export. |
required |
export_name
|
str
|
Name of the exported network (stored in networkAttributes). |
"annnet export"
|
layer
|
tuple of str
|
Elementary layer tuple specifying which layer to export. If provided, only the subgraph for that layer is exported. Useful for multilayer graphs where flattening creates unreadable visualizations in Cytoscape (e.g., coupling edges become self-loops). If None, exports the entire graph. Example: layer=("social", "2020") for a 2-aspect multilayer network. |
None
|
hyperedges
|
('skip', 'expand', 'reify')
|
How to handle hyperedges in the export: - "skip": Omit hyperedges entirely - "expand": Convert to cartesian product of pairwise edges - "reify": Create explicit hyperedge nodes with membership edges |
"skip"
|
Returns:
| Type | Description |
|---|---|
list of dict
|
CX2-compliant JSON structure (list of aspect dictionaries). |
Notes
- Cytoscape does not natively support multilayer networks. When exporting multilayer graphs without specifying a layer, coupling edges may appear as self-loops and the visualization becomes cluttered.
- Use the
layerparameter to export individual elementary layers for clean, interpretable Cytoscape visualizations. - The full multilayer structure is preserved in the manifest regardless
of the
layerparameter, enabling lossless round-trip via from_cx2().
from_cx2(cx2_data, *, hyperedges='manifest')
Fully robust CX2 - AnnNet importer. Supports: - manifest reconstruction (full fidelity) - reified hyperedges - expanded hyperedges - Cytoscape-edited files - sparse attribute tables - arbitrary attribute modifications
show(G, *, export_name='annnet export', layer=None, include_inter=False, include_coupling=False, hyperedges='skip', port=None, auto_open=True)
Visualize graph in web browser using Cytoscape.js.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
G
|
AnnNet
|
The graph to visualize. |
required |
export_name
|
str
|
Name of the network display. |
"annnet export"
|
layer
|
tuple of str
|
Elementary layer tuple specifying which layer to visualize. If None, shows the entire graph (may include coupling edges as self-loops). Example: layer=("social", "2020") for a 2-aspect multilayer network. |
None
|
include_inter
|
bool
|
When layer is specified, whether to include interlayer edges (edges between nodes in different layers but same aspect). Only relevant if layer is not None. |
False
|
include_coupling
|
bool
|
When layer is specified, whether to include coupling edges (edges connecting the same node across layers). Only relevant if layer is not None. Warning: Including coupling creates self-loops in the visualization. |
False
|
hyperedges
|
('skip', 'expand', 'reify')
|
How to handle hyperedges: - "skip": Omit hyperedges entirely - "expand": Convert to cartesian product of pairwise edges - "reify": Create explicit hyperedge nodes with membership edges |
"skip"
|
port
|
int
|
Port for local web server. If None, finds available port automatically. |
None
|
auto_open
|
bool
|
If True, automatically open browser. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Local URL to the visualization. |
Notes
For multilayer graphs: - Without layer parameter: shows entire flattened graph with coupling edges appearing as self-loops (messy but shows full structure) - With layer parameter: shows clean single-layer view without coupling edges (recommended for visualization)
Press Ctrl+C in terminal to stop the web server.
Examples: