-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeshSubplot.py
111 lines (88 loc) · 3.93 KB
/
meshSubplot.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
import uuid
from ipywidgets import Output, HBox
import meshplot as mp
rendertype = 'JUPYTER'
import numpy as np
import networkx as nx
class Subplot():
def __init__(self, data, view, s):
if data == None:
self.rows = []
self.hboxes = []
else:
self.rows = data.rows
if s[0] != 1 or s[1] != 1:
if data == None: # Intialize subplot array
cnt = 0
for r in range(s[0]):
row = []
for c in range(s[1]):
row.append(Output())
cnt += 1
self.rows.append(row)
for r in self.rows:
hbox = HBox(r)
if rendertype == "JUPYTER":
display(hbox)
self.hboxes.append(hbox)
out = self.rows[int(s[2]/s[1])][s[2]%s[1]]
if rendertype == "JUPYTER":
with out:
display(view._renderer)
self.rows[int(s[2]/s[1])][s[2]%s[1]] = view
def save(self, filename=""):
if filename == "":
uid = str(uuid.uuid4()) + ".html"
else:
filename = filename.replace(".html", "")
uid = filename + '.html'
s = ""
imports = True
for r in self.rows:
for v in r:
s1 = v.to_html(imports=imports, html_frame=False)
s = s + s1
imports = False
s = "<html>\n<body>\n" + s + "\n</body>\n</html>"
with open(uid, "w") as f:
f.write(s)
print("Plot saved to file %s."%uid)
def to_html(self, imports=True, html_frame=True):
s = ""
for r in self.rows:
for v in r:
s1 = v.to_html(imports=imports, html_frame=html_frame)
s = s + s1
imports = False
return s
def subplot(f, c = 'red', uv=None, n=None, shading={}, s=[1, 1, 0], data=None, **kwargs):
shading={'point_size':0.05, "point_color": c, "line_color": c, "width":400, "height":400}
view = mp.Viewer(settings = {"width": 500, "height": 500, "antialias": True, "scale": 1.5, "background": "#ffffff",
"fov": 30})
#obj = view.add_points(np.array([ f.nodes[v]['posicion'] for v in f.nodes]), shading=shading)
obj = view.add_points( np.array([f.nodes[v]['posicion'] for v in f.nodes if f.nodes[v]['root'] == True]), shading={'point_size':.02, 'point_color':'red'})
if len(np.array([ f.nodes[v]['posicion'] for v in f.nodes if f.nodes[v]['root'] == False])) != 0:
obj = view.add_points( np.array([f.nodes[v]['posicion'] for v in f.nodes if f.nodes[v]['root'] == False]), shading={'point_size':.02, 'point_color':'black'})
for arista in f.edges:
obj = view.add_lines( f.nodes[arista[0]]['posicion'], f.nodes[arista[1]]['posicion'], shading = shading)
subplot = Subplot(data, view, s)
return subplot
def plotTree( root, dec ):
graph = nx.Graph()
root.toGraph( graph, 0, dec, 0)
edges=nx.get_edge_attributes(graph,'procesada')
p = mp.plot( np.array([ graph.nodes[v]['posicion'] for v in graph.nodes if graph.nodes[v]['root'] == True]), shading={'point_size':0.05, 'point_color':'red'}, return_plot=True)
if len(np.array([ graph.nodes[v]['posicion'] for v in graph.nodes if graph.nodes[v]['root'] == False])) != 0:
p.add_points( np.array([graph.nodes[v]['posicion'] for v in graph.nodes if graph.nodes[v]['root'] == False]), shading={'point_size':.05, 'point_color':'black'})
for arista in graph.edges:
p.add_lines( graph.nodes[arista[0]]['posicion'], graph.nodes[arista[1]]['posicion'])
return
def sTree( root, dec, s, c, d=None):
"plot trees next to each other"
graph = nx.Graph()
root.toGraph( graph, 0, dec, 0)
if d:
subplot(graph, c=c, s=s, data = d)
else:
d = subplot(graph, c=c, s=s)
return d