-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualization.py
92 lines (72 loc) · 3.2 KB
/
visualization.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import networkx as nx
import json
import matplotlib.pyplot as plt
from pygraphviz import *
from networkx.algorithms.community import greedy_modularity_communities
from algo_improved import *
import math
from dotenv import load_dotenv
import os
load_dotenv()
WITH_NODE_LABELS = False
WITH_EDGE_LABELS = False
WITH_NODE_NUM_DISPLAY = True
NODE_NUM_SCALAR = 5
print(os.getenv("ANNEAL_MINR"), type(os.getenv("ANNEAL_MINR")))
print(os.getenv("ANNEAL_MAXR"), type(os.getenv("ANNEAL_MAXR")))
print(os.getenv("ANNEAL_GAMMA"), type(os.getenv("ANNEAL_GAMMA")))
"""
gamma determines the speed of declining
"""
PARAMS = {
"minR": float(os.getenv("ANNEAL_MINR")),
"maxR": float(os.getenv("ANNEAL_MAXR")),
"gamma": float(os.getenv("ANNEAL_GAMMA")),
}
def radisuCalc(minR, maxR, gamma, value):
if value is not None:
if value >=1:
return maxR - (maxR- minR)*math.exp(-gamma*(value - 1))
return 0
return 0
def draw_graph_edgeLabel(G, nodes_position, edge_labels=None, intersections=None, c=None , node_labels=None, node_size=None):
D = {}
for clustering, items in enumerate(c):
for item in items:
D[item] = clustering
colorMap =[D[node] for node in G.nodes]
plt.figure(figsize=(60, 60))
if not WITH_NODE_NUM_DISPLAY:
nx.draw(G, nodes_position, node_size=300,
edge_color='gray', with_labels=WITH_NODE_LABELS, node_color=colorMap, cmap=plt.cm.turbo, labels=node_labels, font_size=14)
else:
nx.draw(G, nodes_position, node_size=node_size,
edge_color='gray', with_labels=WITH_NODE_LABELS, node_color=colorMap, cmap=plt.cm.turbo, labels=node_labels, font_size=14)
if WITH_EDGE_LABELS:
nx.draw_networkx_edge_labels(
G, nodes_position, edge_labels=edge_labels, font_color='red', font_size=8)
plt.scatter([ind['x'] for ind in intersections], [ind['y']
for ind in intersections], color='purple', alpha=0.8, s=8)
plt.savefig('resultEdge.png')
plt.show()
with open("./data/annealed/nodes_links_222.json", "r") as f:
# with open("./results/step2Finished_trial.json", "r") as f:
rawData = f.read()
parsedData = json.loads(rawData)
parsedNodeData = parsedData["nodes"]
parsedLinksData = parsedData["links"]
processedNodes = [(str(node["id"]), {"x": round(node["x"], 2), "y": round(node["y"], 2)}) for node in parsedNodeData]
nodesPosition = {str(node[0]): [node[1]["x"], node[1]["y"]] for node in processedNodes}
A = AGraph()
A.add_nodes_from([node["id"] for node in parsedNodeData])
for link in parsedLinksData:
A.add_edge(str(link["source"]["id"]), str(link["target"]["id"]), len=link["len"])
G = nx.nx_agraph.from_agraph(A)
edge_labels = {(str(link['source']['id']), str(link['target']['id'])): str(link['id']) for link in parsedLinksData}
node_labels = {str(key) : str(key) for key in nodesPosition.keys()}
node_size = [NODE_NUM_SCALAR*radisuCalc(PARAMS["minR"], PARAMS["maxR"], PARAMS["gamma"], node["num"]) for node in parsedNodeData]
## c only for individual cluster
c = list(greedy_modularity_communities(G))
crossings = findIntersection(parsedLinksData)
print(len(crossings))
draw_graph_edgeLabel(G, nodesPosition, edge_labels, crossings, c, node_labels, node_size)