本文整理汇总了Python中pygraphviz.AGraph.add_edge方法的典型用法代码示例。如果您正苦于以下问题:Python AGraph.add_edge方法的具体用法?Python AGraph.add_edge怎么用?Python AGraph.add_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pygraphviz.AGraph
的用法示例。
在下文中一共展示了AGraph.add_edge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_graphviz
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def get_graphviz(self, triple_args_function=None,
ignore_properties=VIS_IGNORE_PROPERTIES):
"""
Create a pygraphviz graph from the tree
"""
def _id(node):
return node.uri.split("/")[-1]
g = AGraph(directed=True)
triples = list(self.get_triples())
nodeset = set(chain.from_iterable((t.subject, t.object)
for t in triples))
for n in nodeset:
label = "%s: %s" % (n.id, n.word)
for k, v in n.__dict__.iteritems():
if k not in ignore_properties:
label += "\\n%s: %s" % (k, v)
g.add_node(_id(n), label=label)
# create edges
for triple in triples:
kargs = (triple_args_function(triple)
if triple_args_function else {})
if 'label' not in kargs:
kargs['label'] = triple.predicate
g.add_edge(_id(triple.subject), _id(triple.object), **kargs)
# some theme options
g.graph_attr["rankdir"] = "BT"
g.node_attr["shape"] = "rect"
g.edge_attr["edgesize"] = 10
g.node_attr["fontsize"] = 10
g.edge_attr["fontsize"] = 10
return g
示例2: render
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def render(self, filename):
g = AGraph(strict=False, directed=True)
# create nodes
for frame_id, node in self.callers.items():
label = "{ %s }" % node
g.add_node(frame_id, shape='Mrecord', label=label,
fontsize=13, labelfontsize=13)
# create edges
for frame_id, node in self.callers.items():
child_nodes = []
for child_id in node.child_methods:
child_nodes.append(child_id)
g.add_edge(frame_id, child_id)
# order edges l to r
if len(child_nodes) > 1:
sg = g.add_subgraph(child_nodes, rank='same')
sg.graph_attr['rank'] = 'same'
prev_node = None
for child_node in child_nodes:
if prev_node:
sg.add_edge(prev_node, child_node, color="#ffffff")
prev_node = child_node
g.layout()
g.draw(path=filename, prog='dot')
print("callviz: rendered to %s" % filename)
self.clear()
示例3: plot
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def plot(table_names=None):
"""
Plot relationships between columns and tables using Graphviz.
Parameters
----------
table_names : iterable of str, optional
Names of UrbanSim registered tables to plot.
Defaults to all registered tables.
Returns
-------
graph : pygraphviz.AGraph
PyGraphviz graph object.
"""
if not table_names:
# Default to all registered tables.
table_names = simulation.list_tables()
graph = AGraph(directed=True)
graph.graph_attr['fontname'] = 'Sans'
graph.graph_attr['fontsize'] = 28
graph.node_attr['shape'] = 'box'
graph.node_attr['fontname'] = 'Sans'
graph.node_attr['fontcolor'] = 'blue'
graph.edge_attr['weight'] = 2
# Add each registered table as a subgraph with columns as nodes.
for table_name in table_names:
subgraph = graph.add_subgraph(name='cluster_' + table_name,
label=table_name, fontcolor='red')
table = simulation.get_table(table_name)
for column_name in table.columns:
full_name = table_name + '.' + column_name
subgraph.add_node(full_name, label=column_name)
# Iterate over computed columns to build nodes.
for key, wrapped_col in simulation._COLUMNS.items():
table_name = key[0]
column_name = key[1]
# Combine inputs from argument names and argument default values.
args = list(wrapped_col._argspec.args)
if wrapped_col._argspec.defaults:
default_args = list(wrapped_col._argspec.defaults)
else:
default_args = []
inputs = args[:len(args) - len(default_args)] + default_args
# Create edge from each input column to the computed column.
for input_name in inputs:
full_name = table_name + '.' + column_name
graph.add_edge(input_name, full_name)
graph.layout(prog='dot')
return graph
示例4: draw_workflow
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def draw_workflow(filename, workflow):
dot = AGraph(directed=True, strict=False) # (comment="Computing scheme")
for i, n in workflow.nodes.items():
dot.add_node(i, label="{0} \n {1}".format(n.foo.__name__, _format_arg_list(n.bound_args.args, None)))
for i in workflow.links:
for j in workflow.links[i]:
dot.add_edge(i, j[0], j[1].name) # , headlabel=j[1].name, labeldistance=1.8)
dot.layout(prog="dot")
dot.draw(filename)
示例5: _render
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def _render(self, costs=False, word_probs=False, highlight_best=False):
from pygraphviz import AGraph
graph = AGraph(directed=True)
for node_id, node_label in self.nodes.iteritems():
attributes = self._node_attr(node_id, costs=costs, word_probs=word_probs)
graph.add_node(node_id, **attributes)
for (parent_node_id, child_node_id) in self.edges:
graph.add_edge(parent_node_id, child_node_id)
self.graph = graph
if highlight_best:
self._highlight_best()
示例6: render_local
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def render_local(self, filename):
"""Renders the OBST image locally using pygraphviz."""
# Get the graph information
node_list, edge_list = self.__generate_image()
# Generate the graph
from pygraphviz import AGraph
G=AGraph(strict=True,directed=True) # Create a graph
for node in node_list:
G.add_node(node)
for edge in edge_list:
G.add_edge(edge[0], edge[1])
G.layout('dot') # Set hierarchical layout
G.draw(filename) # Save the image.
示例7: as_graph
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def as_graph(self, to=None):
from pygraphviz import AGraph
g = AGraph(directed=True)
for a in self.activities.values():
g.add_node(a.title, label=a.title)
for t in self.transitions.values():
g.add_edge(t.input.title, t.output.title, label=t.name)
if to:
g.write(to)
else:
return str(g)
示例8: to_dot
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def to_dot(self, filename, edges):
from pygraphviz import AGraph
dot = AGraph(directed=True)
for n in edges.keys():
dot.add_node(str(n))
if lib.qcgc_arena_get_blocktype(ffi.cast("cell_t *", n)) not in [
lib.BLOCK_BLACK, lib.BLOCK_WHITE]:
node = dot.get_node(str(n))
node.attr['color'] = 'red'
for n in edges.keys():
if edges[n] is not None:
dot.add_edge(str(n), str(edges[n]))
dot.layout(prog='dot')
dot.draw(filename)
示例9: render_image
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def render_image(self, filename):
"""Renders the graph image locally using pygraphviz."""
# Create a graph
G=AGraph(directed=False)
# Add nodes
for node in self.nodes:
G.add_node(node)
# Add edges
for edge in self.edges:
G.add_edge(edge[0], edge[1], color='blue')
# Give layout and draw.
G.layout('circo')
G.draw(filename) # Save the image.
# Display the output image.
os.system("gwenview %s&" % filename)
示例10: cm_json_to_graph
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def cm_json_to_graph(im_json):
"""Return pygraphviz Agraph from Kappy's contact map JSON.
Parameters
----------
im_json : dict
A JSON dict which contains a contact map generated by Kappy.
Returns
-------
graph : pygraphviz.Agraph
A graph representing the contact map.
"""
cmap_data = im_json['contact map']['map']
# Initialize the graph
graph = AGraph()
# In this loop we add sites as nodes and clusters around sites to the
# graph. We also collect edges to be added between sites later.
edges = []
for node_idx, node in enumerate(cmap_data):
sites_in_node = []
for site_idx, site in enumerate(node['node_sites']):
# We map the unique ID of the site to its name
site_key = (node_idx, site_idx)
sites_in_node.append(site_key)
graph.add_node(site_key, label=site['site_name'], style='filled',
shape='ellipse')
# Each port link is an edge from the current site to the
# specified site
if not site['site_type'] or not site['site_type'][0] == 'port':
continue
for port_link in site['site_type'][1]['port_links']:
edge = (site_key, tuple(port_link))
edges.append(edge)
graph.add_subgraph(sites_in_node,
name='cluster_%s' % node['node_type'],
label=node['node_type'])
# Finally we add the edges between the sites
for source, target in edges:
graph.add_edge(source, target)
return graph
示例11: build_graph
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def build_graph(score):
corpus= create_corpus(score)
calc_tf_idf(corpus, log_tf, log_idf)
#corpus= group_corpus(corpus, log_tf, log_idf)
g= AGraph(strict=False)
for i, d1 in enumerate(corpus):
d1_name= str(d1)
edges= [(d2, d2.similarity(d1)) for d2 in corpus[i+1:]]
edges.sort(key=lambda x:x[1], reverse=True)
edges= edges[:5]
for d2, weight in edges:
d2_name= str(d2)
g.add_edge(d1_name, d2_name)
e= g.get_edge(d1_name, d2_name)
e.attr['label']= str(weight)[:5]
#import ipdb;ipdb.set_trace()
return g
示例12: get_dot
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def get_dot(self, labeller=None):
self.labeller = labeller
a_graph = AGraph(directed=True)
nx_graph = self.graph.nx_graph
# TODO: Add some default stuff?
# a_graph.graph_attr.update(N.graph.get('graph',{}))
# a_graph.node_attr.update(N.graph.get('node',{}))
# a_graph.edge_attr.update(N.graph.get('edge',{}))
structural_nodes = []
output_nodes = []
input_nodes = []
# First, add nodes
for node in nx_graph.nodes():
name, attrs = self.get_node_attributes(node)
if self.graph.is_input(node):
input_nodes.append(name)
elif self.graph.is_structural(node):
structural_nodes.append(name)
elif self.graph.is_output(node):
output_nodes.append(name)
# Keep a reference to the original node
a_graph.add_node(name, **attrs)
# We need to add subgraphs to cluster stuff on rank
sub = a_graph.add_subgraph(input_nodes, name='input')
sub.graph_attr['rank'] = 'source'
sub = a_graph.add_subgraph(structural_nodes, name='structural')
sub.graph_attr['rank'] = 'same'
sub = a_graph.add_subgraph(output_nodes, name='output')
sub.graph_attr['rank'] = 'sink'
# Now add edges
for u, v, edgedata in nx_graph.edges_iter(data=True):
attrs = {}
a_graph.add_edge(self.graph.node_to_name(u),
self.graph.node_to_name(v),
**attrs)
return a_graph
示例13: render_image
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def render_image(node_list, edge_list):
# Generate the graph
from pygraphviz import AGraph
G=AGraph(strict=False,directed=True) # Create a graph
for node in node_list:
G.add_node(node)
for edge in edge_list:
G.add_edge(edge[0], edge[1])
G.layout('dot') # Set hierarchical layout
filename = str(time())
postfix = 0
while exists(filename+str(postfix)+".png"):
postfix+=1
filename += str(postfix) + ".png"
G.draw(filename) # Save the image.
with open(filename, "rb") as handle:
return xmlrpclib.Binary(handle.read())
示例14: write_graph
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def write_graph(probs, path):
graph = AGraph(directed=True)
next_label = 0
labels = {}
for from_state, to_states in probs.iteritems():
if from_state not in labels:
labels[from_state] = next_label
next_label += 1
for to_state in to_states:
if to_state not in labels:
labels[to_state] = next_label
next_label += 1
for label in xrange(next_label):
graph.add_node(label, fillcolor="blue", label="", style="filled")
for from_state, to_states in probs.iteritems():
for to_state, prob in to_states.iteritems():
graph.add_edge(labels[from_state], labels[to_state], label="%.2g" % prob)
# prog: neato (default), dot, twopi, circo, fdp or nop.
graph.layout()
graph.draw(path)
示例15: draw_graph
# 需要导入模块: from pygraphviz import AGraph [as 别名]
# 或者: from pygraphviz.AGraph import add_edge [as 别名]
def draw_graph(state_manager, filename):
graph = AGraph()
graph.node_attr["style"] = "filled"
graph.node_attr["shape"] = "circle"
graph.node_attr["fixedsize"] = "true"
graph.node_attr["width"] = 0.5
graph.node_attr["height"] = 0.5
# we add all nodes (keys = ID)
graph.add_nodes_from(state_manager.state.keys())
for var_id in state_manager.state:
# and for each of these nodes, we change color
node = graph.get_node(var_id)
node.attr["fillcolor"] = get_color(state_manager.state[var_id])
# finally, we add edges
for c in state_manager.constraints:
e = c.list_vars
graph.add_edge(e[0], e[1])
graph.write(filename)