Skip to content

Commit

Permalink
add basic edge support for dot (assumes every edge has label), move b…
Browse files Browse the repository at this point in the history
…ase_pos_ar generation to general layout calcs
  • Loading branch information
swhalemwo committed Mar 17, 2020
1 parent 807e54c commit 22d9ba2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
21 changes: 21 additions & 0 deletions layout_optim/cpp/pytest_elp.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,25 @@
# maybe have to multiply by constant?
disp_vec *= force_mult

from graphviz import Digraph
dg = Digraph()

dg.graph_attr = {'rankdir': 'BT', 'dpi': '72'}

dg.edge('a', 'b', label = 'kkk')

import json

dg_gv_piped = dg.pipe(format = 'json')
dg_gv_parsed = json.loads(dg_gv_piped)

27 86.997
27 36.026
27 47.808
27 63.439
27 76.842'

is lp?

dg_gv_parsed['edges'][0]['lp']

63 changes: 48 additions & 15 deletions obvz.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def signal_received(self, new_graph_str):
self.layout_type = new_graph_dict['layout_type']
update_me = 1



# only command is to redraw
if list(new_graph_dict.keys())[0] == 'redraw':
Expand Down Expand Up @@ -384,6 +384,7 @@ def signal_received(self, new_graph_str):
self.node_str = new_graph_dict['nodes']
logging.info(['new self.node_str: ', self.node_str])
update_me = 1
update_texts = 1

# check if node texts have been modified
if self.node_texts_raw != new_graph_dict['node_texts'] and new_graph_dict['node_texts'] != None:
Expand Down Expand Up @@ -662,6 +663,8 @@ def set_node_positions(self, nodes_to_add):

def recalculate_layout(self):
"""overall command to manage layout re-calculations"""
self.base_pos_ar = np.array([(self.g.nodes[i]['x'],self.g.nodes[i]['y']) for i in self.g.nodes])

if self.layout_type == 'force':
self.recalc_layout_force()

Expand All @@ -675,7 +678,7 @@ def recalc_layout_force(self):
logging.info('force recalculating starting')

# get node array
self.base_pos_ar = np.array([(self.g.nodes[i]['x'],self.g.nodes[i]['y']) for i in self.g.nodes])
# self.base_pos_ar = np.array([(self.g.nodes[i]['x'],self.g.nodes[i]['y']) for i in self.g.nodes])
# base_pos_ar = np.array([(g.nodes[i]['x'],g.nodes[i]['y']) for i in g.nodes])

pos_nds = np.copy(self.base_pos_ar)
Expand Down Expand Up @@ -724,14 +727,13 @@ def recalc_layout_force(self):
elbl_cnct_nds = []
c = 0
for v in self.g.nodes:
if len(v) > 4:
if v[0:4] == 'lbl_':
# g.nodes[v]['e_lbl'] = 1
logging.info(["v: ", v, ", c: ", c])
elbl_pos_list.append(c)
cnct_nodes = list(self.g.predecessors(v)) + list(self.g.successors(v))
logging.info(["connected nodes: ", cnct_nodes])
elbl_cnct_nds.append([self.vd[cnct_nodes[0]], self.vd[cnct_nodes[1]]])
if self.g.nodes[v]['nd_tp'] == 'lbl':
# g.nodes[v]['e_lbl'] = 1
logging.info(["v: ", v, ", c: ", c])
elbl_pos_list.append(c)
cnct_nodes = list(self.g.predecessors(v)) + list(self.g.successors(v))
logging.info(["connected nodes: ", cnct_nodes])
elbl_cnct_nds.append([self.vd[cnct_nodes[0]], self.vd[cnct_nodes[1]]])
c +=1

elbl_pos_list = np.array(elbl_pos_list)
Expand Down Expand Up @@ -786,23 +788,54 @@ def recalc_layout_dot(self):

dg.graph_attr = {'rankdir': 'BT', 'dpi': '72'}

dg.edges([i for i in self.g.edges])
if self.use_edge_labels != True:
dg.edges([i for i in self.g.edges])

for i in self.g.nodes:
# dg.node(i) = {'width': self.g.nodes[i]['width']/96, 'height': self.g.nodes[i]['height']/96}
dg.node(i, width = str(self.g.nodes[i]['width']/72), height = str(self.g.nodes[i]['height']/72))
for n in self.g.nodes:
# dg.node(i) = {'width': self.g.nodes[i]['width']/96, 'height': self.g.nodes[i]['height']/96}
dg.node(n, width = str(self.g.nodes[n]['width']/72), height = str(self.g.nodes[n]['height']/72))


# assumes now that every edge has a label over which the out/in-going nodes can be retrieved
if self.use_edge_labels == True:
dg_edges = []

for n in self.g.nodes:
if self.g.nodes[n]['nd_tp'] == 'lbl':
node_out = list(self.g.predecessors(n))[0]
node_in = list(self.g.successors(n))[0]
dg.edge(node_out, node_in, label = self.g.nodes[n]['title'])

logging.info(['graph:\n', str(dg)])

# logging.info(['graph:\n', dg])

dg_gv_piped = dg.pipe(format = 'json')
dg_gv_parsed = json.loads(dg_gv_piped)


# loop over edges? have to get get info somehow
if self.use_edge_labels == True:

for e in dg_gv_parsed['edges']:
lbl_text = e['label']
lbl_x, lbl_y = e['lp'].split(',')
head_id = e['tail']
tail_id = e['head']
head_name = dg_gv_parsed['objects'][head_id]['name']
tail_name = dg_gv_parsed['objects'][tail_id]['name']
elbl_nd_name = elbl_nd_name = "lbl_" + head_name + "_" + tail_name + "_" + lbl_text
self.g.nodes[elbl_nd_name]['x'] = float(lbl_x)
self.g.nodes[elbl_nd_name]['y'] = float(lbl_y)


for i in dg_gv_parsed['objects']:
posx, posy = i['pos'].split(',')
self.g.nodes[i['name']]['x'] = float(posx) + 20
self.g.nodes[i['name']]['x'] = float(posx) + 20 # tf is this +20 doing here
self.g.nodes[i['name']]['y'] = float(posy)

pos_nds = np.array([(self.g.nodes[i]['x'],self.g.nodes[i]['y']) for i in self.g.nodes])
logging.info(['pos_nds:\n', pos_nds])

self.chng_ar = (pos_nds - self.base_pos_ar)/self.step

Expand Down

0 comments on commit 22d9ba2

Please sign in to comment.