-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path58_export_networkx.py
More file actions
38 lines (33 loc) · 1.09 KB
/
Copy path58_export_networkx.py
File metadata and controls
38 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
58. Export to NetworkX
FAST: <1s runtime
Dependencies: py3plex (core), networkx
Demonstrates .to_networkx() for exporting results to NetworkX graph.
"""
from py3plex.core import multinet
from py3plex.dsl import Q, L
# Create small multilayer network
net = multinet.multi_layer_network(directed=False)
net.add_nodes([
{'source': 'Alice', 'type': 'social'},
{'source': 'Bob', 'type': 'social'},
{'source': 'Carol', 'type': 'social'},
])
net.add_edges([
{'source': 'Alice', 'target': 'Bob', 'source_type': 'social', 'target_type': 'social'},
{'source': 'Alice', 'target': 'Carol', 'source_type': 'social', 'target_type': 'social'},
])
# DSL: Query and export to NetworkX
result = (
Q.nodes()
.compute("degree", "betweenness_centrality")
.execute(net)
)
# Export to NetworkX graph
nx_graph = result.to_networkx()
print(f"NetworkX graph type: {type(nx_graph)}")
print(f"Nodes: {nx_graph.number_of_nodes()}")
print(f"Edges: {nx_graph.number_of_edges()}")
print(f"\nNode attributes (sample):")
for node, attrs in list(nx_graph.nodes(data=True))[:2]:
print(f" {node}: {attrs}")