-
Notifications
You must be signed in to change notification settings - Fork 5
/
ifcgraph.py
92 lines (62 loc) · 2.17 KB
/
ifcgraph.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
90
91
92
import matplotlib.pyplot as plt
import networkx as nx
from itertools import count
import matplotlib.patches as mpatches
from matplotlib import pylab
class IfcEntity:
def __init__(self, data):
self.ifctype = data["ifc_type"]
self.id = data["id"]
def __str__(self):
return "#" + str(self.id) + "_" + self.ifctype
__repr__ = __str__
def get_cmap(n, name="hsv"):
"""Returns a function that maps each index in 0, 1, ..., n-1 to a distinct
RGB color; the keyword argument name must be a standard mpl colormap name."""
return plt.cm.get_cmap(name, n)
G = nx.Graph()
colors = []
color_map = []
ifctypes = set()
types_color = {}
for e in ents.values():
for r in e["attributes"][1]:
G.add_edge(e["id"], r)
color_map.append("red")
ifctypes.add(e["ifc_type"])
ifctypes.add(ents[r]["ifc_type"])
cmap = get_cmap(len(ifctypes), "rainbow")
colors = [cmap(i) for i in range(len(ifctypes))]
type_color_mapping = {}
cols = []
i = 0
for eid in G.nodes():
t = ents[eid]["ifc_type"]
# print(t)
if t in type_color_mapping.keys():
cols.append(type_color_mapping[t])
else:
type_color_mapping[t] = colors[i]
i = i + 1
cols.append(type_color_mapping[t])
# k controls the distance between the nodes and varies between 0 and 1
# iterations is the number of times simulated annealing is run
# default k=0.1 and iterations=50
pos = nx.spring_layout(G, k=0.8, iterations=60) # positions for all nodes
nodes = G.nodes()
print(get_cmap(len(nodes)))
cmap = get_cmap(len(nodes))
c_map = [cmap(i) for i in range(len(nodes))]
nc = nx.draw_networkx_nodes(G, pos, nodelist=nodes, node_color=cols, node_size=200)
# edges
elarge = [(u, v) for (u, v, d) in G.edges(data=True)]
nx.draw_networkx_edges(G, pos, edgelist=elarge, width=1)
nx.draw_networkx_labels(G, pos, font_size=8, font_family="sans-serif")
red_patch = mpatches.Patch(color="red", label="The red data")
blue_patch = mpatches.Patch(color="blue", label="The blue data")
patches = []
for k, v in type_color_mapping.items():
patches.append(mpatches.Patch(color=v, label=k))
plt.legend(handles=patches, fontsize=8)
plt.axis("off")
plt.show()