-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathexample_twitter_multiplex_visualization.py
More file actions
63 lines (48 loc) · 1.78 KB
/
Copy pathexample_twitter_multiplex_visualization.py
File metadata and controls
63 lines (48 loc) · 1.78 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Simple visualization of a multiplex Twitter network.
Loads example multiplex edges, maps layer ids to names, and renders a
basic circular layout. Uses network_type="multiplex" so same users are
connected across layers automatically.
SKIP_CI: slow - Takes more than 10 seconds to complete
"""
from __future__ import annotations
import os
from typing import Dict, List
from py3plex.core import multinet
from py3plex.utils import get_dataset_path
from py3plex.visualization.multilayer import draw_multilayer_default, plt
def load_layer_map(path: str) -> Dict[str, str]:
"""Load the mapping between numeric layer ids and readable names."""
layer_map: Dict[str, str] = {}
with open(path, encoding="utf-8") as twl:
for line in twl:
idx, lname = line.strip().split()
layer_map[idx] = lname
return layer_map
def main() -> int:
layers_file = get_dataset_path("twitterlayers.txt")
edges_file = get_dataset_path("test13.edges")
if not (os.path.exists(layers_file) and os.path.exists(edges_file)):
print("Twitter multiplex datasets not found; skipping example.")
print(f"Expected: {layers_file}")
print(f"Expected: {edges_file}")
return 0
layer_map = load_layer_map(layers_file)
network = multinet.multi_layer_network(network_type="multiplex").load_network(
edges_file,
directed=False,
input_type="multiplex_edges",
)
labels, graphs, _ = network.get_layers()
readable_labels: List[str] = [layer_map.get(layer, layer) for layer in labels]
draw_multilayer_default(
graphs,
display=False,
background_shape="circle",
labels=readable_labels,
node_size=1,
)
plt.show()
return 0
if __name__ == "__main__":
raise SystemExit(main())