forked from uli22/nx-mayavi
/
mayavi_schmidt.py
159 lines (130 loc) · 6.82 KB
/
mayavi_schmidt.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import networkx as nx
import numpy as np
from mayavi import mlab
import random
def draw_graph3d(graph, graph_colormap='winter', bgcolor = (1, 1, 1),
node_size=0.03,
edge_color=(0.8, 0.8, 0.8), edge_size=0.002,
text_size=0.008, text_color=(0, 0, 0)):
H = nx.Graph()
## add edges
#for node, edges in graph.items():
# for edge, val in edges.items():
# if val == 1:
# H.add_edge(node, edge)
# add edges
for edge in graph:
H.add_edge(edge[0], edge[1])
G = nx.convert_node_labels_to_integers(H)
graph_pos = nx.spring_layout(G, dim=3)
# numpy array of x, y, z positions in sorted node order
xyz = np.array([graph_pos[v] for v in sorted(G)])
# scalar colors
scalars = np.array(G.nodes())+5
mlab.figure(1, bgcolor=bgcolor)
mlab.clf()
pts = mlab.points3d(xyz[:, 0], xyz[:, 1], xyz[:, 2],
scalars,
scale_factor=node_size,
scale_mode='none',
colormap=graph_colormap,
resolution=20)
for i, (x, y, z) in enumerate(xyz):
label = mlab.text(x, y, str(i), z=z,
width=text_size, name=str(i), color=text_color)
label.property.shadow = True
print(np.array(G.edges))
print(np.array(list(G.edges())))
print(type(G.edges()))
print(type(list(G.edges())))
print(len(list(G.edges())))
print(len(list(G.edges())))
print(len(G.edges()))
pts.mlab_source.dataset.lines = np.array(G.edges())
tube = mlab.pipeline.tube(pts, tube_radius=edge_size)
mlab.pipeline.surface(tube, color=edge_color)
mlab.show() # interactive window
# create tangled hypercube
def make_graph(nodes):
def make_link(graph, i1, i2):
graph[i1][i2] = 1
graph[i2][i1] = 1
n = len(nodes)
if n == 1 : return {nodes[0] : {}}
nodes1 = nodes[0:int(n / 2)]
nodes2 = nodes[int(n / 2):]
G1 = make_graph(nodes1)
G2 = make_graph(nodes2)
# merge G1 and G2 into a single graph
G = {**G1, **G2}
# link G1 and G2
random.shuffle([nodes1])
random.shuffle([nodes2])
for i in range(len(nodes1)):
make_link(G, nodes1[i], nodes2[i])
return G
# graph example
# nodes = range(16)
# graph = make_graph(nodes)
graph = [(1, 143), (2, 141), (3, 68), (3, 76), (4, 34), (5, 206), (7, 181),
(8, 188), (9, 10), (10, 15), (10, 29), (10, 36), (10, 81), (10, 85),
(10, 99), (10, 175), (10, 190), (10, 195), (11, 12), (11, 13), (11, 14),
(11, 127), (11, 211), (15, 68), (16, 130), (17, 19), (17, 66), (17, 130),
(19, 16), (19, 18), (19, 18), (19, 37), (19, 39), (19, 66), (19, 66),
(19, 74), (19, 144), (19, 158), (21, 66), (22, 23), (24, 25), (26, 141),
(28, 19), (28, 141), (28, 141), (28, 141), (28, 171), (32, 181), (36, 19),
(36, 141), (38, 161), (38, 161), (42, 171), (43, 18), (43, 18), (43, 130),
(43, 130), (44, 68), (44, 89), (46, 33), (47, 19), (47, 141), (50, 75),
(50, 76), (51, 130), (54, 34), (54, 34), (54, 105), (55, 169), (56, 66),
(56, 141), (56, 161), (58, 19), (59, 66), (59, 66), (60, 66), (60, 150),
(60, 160), (62, 66), (63, 134), (65, 66), (65, 161), (66, 35), (66, 132),
(67, 66), (67, 160), (67, 169), (68, 18), (68, 18), (68, 19), (68, 19),
(68, 20), (68, 34), (68, 35), (68, 60), (68, 66), (68, 90), (68, 145),
(68, 145), (68, 169), (69, 163), (70, 34), (71, 141), (72, 160), (72, 160),
(72, 161), (73, 66), (73, 129), (76, 66), (76, 72), (76, 75), (76, 75),
(76, 133), (76, 139), (76, 141), (76, 150), (76, 154), (76, 159), (77, 134),
(79, 141), (80, 171), (81, 66), (81, 68), (81, 89), (82, 171), (84, 108),
(84, 163), (86, 108), (87, 19), (87, 43), (87, 43), (87, 50), (87, 54),
(87, 68), (87, 163), (87, 170), (87, 213), (88, 134), (89, 1), (89, 34),
(89, 35), (89, 40), (89, 40), (89, 55), (89, 66), (89, 66), (89, 74),
(89, 141), (89, 141), (89, 149), (89, 152), (89, 152), (89, 152),
(89, 152), (90, 66), (91, 19), (91, 33), (91, 33), (91, 76), (91, 114),
(91, 171), (92, 76), (92, 108), (95, 48), (97, 68), (97, 73), (97, 213),
(98, 48), (99, 89), (100, 76), (100, 128), (101, 34), (102, 66),
(104, 171), (108, 16), (108, 19), (108, 40), (108, 53), (108, 57),
(108, 145), (108, 152), (108, 213), (108, 213), (109, 163), (110, 89),
(111, 186), (112, 68), (113, 66), (115, 33), (116, 6), (117, 25),
(119, 120), (123, 188), (124, 23), (125, 15), (125, 66), (125, 118),
(125, 189), (126, 207), (131, 134), (136, 16), (136, 35, (136, 41),
(136, 164), (137, 141), (140, 186), (141, 16), (141, 16), (141, 35),
(141, 39), (141, 49), (141, 52), (141, 67), (141, 70), (141, 74),
(141, 103), (141, 135), (141, 137), (141, 138), (141, 142), (141, 169),
(142, 75), (146, 128), (146, 168), (147, 66), (148, 141), (149, 41),
(149, 60), (149, 61), (149, 66), (149, 66), (149, 66), (149, 66),
(149, 67), (149, 107), (149, 132), (149, 141), (149, 150), (149, 153),
(151, 150), (151, 160), (155, 18), (155, 66), (155, 66), (155, 66),
(155, 230), (156, 66), (156, 150), (156, 169), (156, 169), (156, 230),
(157, 33), (157, 141), (162, 181), (163, 161), (163, 165), (163, 165),
(166, 141), (167, 128), (170, 141), (172, 48), (173, 43), (173, 50),
(173, 50), (173, 56), (173, 96), (173, 100), (173, 106), (173, 128),
(173, 136), (173, 151), (173, 163), (173, 216), (174, 28), (174, 28),
(174, 33), (174, 35), (174, 48), (174, 48), (174, 49), (174, 67),
(174, 102), (174, 136), (174, 145), (174, 149), (174, 170), (175, 128),
(175, 163), (175, 180), (177, 105), (178, 150), (180, 136), (180, 163),
(181, 15), (181, 28), (181, 28), (181, 45), (181, 47), (181, 64),
(181, 67), (181, 71), (181, 136), (181, 137), (181, 137), (181, 171),
(181, 171), (181, 171), (182, 48), (182, 68), (182, 149), (183, 76),
(185, 141), (187, 141), (188, 17), (188, 19), (188, 27), (188, 28),
(188, 29), (188, 30), (188, 33), (188, 68), (188, 81), (188, 107),
(188, 137), (188, 155), (188, 170), (188, 179), (188, 186), (188, 215),
(189, 43), (189, 66), (189, 114), (189, 114), (189, 156), (189, 182),
(189, 213), (192, 227), (193, 173), (194, 188), (196, 188),
(196, 226), (197, 94), (198, 171), (199, 210), (200, 201), (200, 202),
(200, 203), (200, 204), (200, 205), (208, 206), (209, 45), (209, 78),
(209, 81), (209, 112), (209, 121), (209, 181), (209, 181), (209, 184),
(212, 206), (214, 84), (217, 141), (218, 106), (219, 122), (219, 173),
(220, 3), (220, 87), (220, 94), (220, 151), (220, 170), (220, 181),
(220, 187), (220, 191), (220, 191), (221, 188), (222, 176), (223, 87),
(223, 181), (223, 187), (224, 42), (224, 83), (225, 31), (225, 93),
(225, 168), (228, 122), (229, 66), (231, 232), (233, 181)]
draw_graph3d(graph)