Onyx logo

Previous topic

Graph

Next topic

onyx.graph.dynamicgraph – Tools for working with dynamic, or online, graphs.

This Page

onyx.graph.decodergraph – A graph builder that can decode while it’s building.

class onyx.graph.decodergraph.DecoderGraph(*args)

Bases: onyx.graph.graphtools.TopologicalGraphBuilder

A TopologicalGraphBuilder suitable for doing decoding while it’s being built.

>>> builder = DecoderGraph()
>>> num_nodes = 5
>>> nodes = tuple(builder.new_node_label_is_id() for i in xrange(num_nodes))
>>> for node in nodes:
...   builder.new_arc_label_is_id(node, node) 
...   if node + 1 < num_nodes: builder.new_arc_label_is_id(node, node + 1)
...   if node + 2 < num_nodes: builder.new_arc_label_is_id(node, node + 2)
0
1
2
3
4
5
6
7
8
9
10
11
>>> tuple(builder.in_arc_iter(0))
((0, 0),)
>>> tuple(builder.in_arc_iter(num_nodes-1))
((2, 8), (3, 10), (4, 11))

A list for each end-node, a tuple of start-node-id, arc-id for each arc.

>>> builder.in_arcs
[[(0, 0)], [(0, 1), (1, 3)], [(0, 2), (1, 4), (2, 6)], [(1, 5), (2, 7), (3, 9)], [(2, 8), (3, 10), (4, 11)]]
add_graph(subgraph, node_label_callback=<function <lambda> at 0x1d06bb18>, arc_label_callback=<function <lambda> at 0x1d06bb90>)

Add an entire subgraph to this builder’s graph.

Add all the nodes and edges and their respective labels from subgraph into the builder’s graph.

Returns a triple of tuples representing the nodes of the subgraph within the builder’s graph. The items in the triple are the node indices in the builder of all the nodes from subgraph, the indices of the subgraph’s startnodes, and the indices of the subgraph’s endnodes respectively. Note that these node indices are not necessarily in the same order as they are in subgraph.

add_graph_label_is_id(subgraph)
dot_display(temp_file_prefix='graphtools_', display_tool_format='open -a /Applications/Graphviz.app %s', **kwargs)

Display a dot-generated representation of the graph. Returns the name of the temporary file that the display tool is working from. The caller is responsible for removing this file. See also dot_iter().

Optional temp_file_prefix is the prefix used for the temporary filename.

Optional display_tool_format is formatting string, with %s where the filename goes, used to generate the command that will display the file. By default it assumes you’re on a Mac and have Graphviz.app installed in the /Applications directory.

Remaining keyword arguments are handed to the dot_iter function.

dot_iter(graph_label='', graph_type='digraph', globals=(), node_label_callback=<function <lambda> at 0x1d06a5f0>, node_attributes_callback=None, arc_label_callback=<function <lambda> at 0x1d06a5f0>, arc_attributes_callback=None, arc_ports_callback=None)

Returns a generator that yields lines of text, including newlines. The text represents the graph in the DOT language for which there are many displayers. See also dot_display().

Optional argument graph_label is a string label for the graph. Optional argument graph_type defaults to ‘digraph’, can also be ‘graph’.

Optional globals is an iterable that yields strings to put in the globals section of the DOT file.

Optional node_label_callback is a function that will be called with three arguments for each node: (node_label, is_start, is_end), where node_label is the node’s label, is_start is True if the node is a start node (has no incoming arcs), and is_end is True if the node is an end node (has no outgoing arcs). The callback function should return a string which will be used as the text label for the node in the figure, or None or the empty string for no label. If this argument is not given, each node with a non-None label will be labelled with the str value of its label. If this argument is None, nodes will not be labelled.

Optional arc_label_callback is a function that will be called with the arc label for each arc. The callback function should return a string which will be used as the text label for the arc in the figure, or None or the empty string for no label. If this argument is not given, each arc with a non-None label will be labelled with the str value of its label. If this argument is None, arcs will not be labelled.

Optional node_attributes_callback is a function that returns an iterable that will yield a string for each attribute assignment for the node. It will be called with three arguments for each node: (node_label, is_start, is_end), where node_label is the node’s label, is_start is True if the node is a start node (has no incoming arcs), and is_end is True if the node is an end node (has no outgoing arcs).

Optional arc_attributes_callback is a function that returns an iterable that will yield a string for each attribute assignment for the arc. It will be called with the arc label for each arc in the graph.

Optional arc_ports_callback is a function of two arguments that is called for each arc in the graph. The arguments are the labels of the start and end nodes of the arc. The function returns a pair of strings that name the respective ports of the nodes to which the arc should attach. The strings should be empty if no port is implied.

>>> graph = FrozenGraph(GraphTables(((1, 2, 'a'), (0, 1, 2), (1, 0, 2), (3, None, 5))))
>>> print ''.join(graph.dot_iter())
digraph  { 
  n0  [label="1"];
  n1  [label="2"];
  n2  [label="a"];
  n0 -> n1  [label="3"];
  n1 -> n0;
  n2 -> n2  [label="5"];
}
<BLANKLINE>
>>> print ''.join(graph.dot_iter(graph_label='Foo',
...                              globals=['size="5,6.125";', 'ordering=out;'],
...                              node_label_callback=lambda label, is_start, is_end: ('<' if is_start else '') + str(label) + ('>' if is_end else ''),
...                              arc_label_callback=str,
...                              node_attributes_callback=lambda label, is_start, is_end: ['shape=%s' % ('octagon' if is_start or is_end else 'ellipse',), 'style=%s' % ('bold' if isinstance(label, int) else 'normal',)],
...                              arc_attributes_callback=lambda label: ['style=%s' % ('bold' if label is None else 'normal',)]))
digraph Foo { 
  label="Foo";
  size="5,6.125";
  ordering=out;
  n0  [label="1", shape=ellipse, style=bold];
  n1  [label="2", shape=ellipse, style=bold];
  n2  [label="<a>", shape=octagon, style=normal];
  n0 -> n1  [label="3", style=normal];
  n1 -> n0  [label="None", style=bold];
  n2 -> n2  [label="5", style=normal];
}
<BLANKLINE>
get_arc(arc_id)

Return a tuple of the (start_node, end_node, arc_label) for the arc associated with the arc_id.

get_arc_label(arc_id)

Return the arc label associated with the arc_id.

get_node_label(node_id)

Return the node label associated with the node_id.

in_arc_iter(end_node_id)
new_arc(start_node, end_node, arclabel=None)
new_arc_label_is_id(start_node, end_node)

Creates a new arc in the graph, with arclabel being the id of the arc. Returns the id of the arc, a non-negative integer.

new_node(nodelabel=None)

Creates a new node in the graph, with optional nodelabel. Returns the id of the node, a non-negative integer.

new_node_label_is_id()

Creates a new node in the graph, with nodelabel being the id of the node. Returns the id of the node, a non-negative integer.

num_arcs

The number of arcs in the graph.

num_nodes

The number of nodes in the graph.

text_iter()

Returns a generator that yields lines of text, including newlines. The text represents the graph in a human-readable, serializable form.

verify()