graphid.util.util_graphviz module

Helpers for graph plotting

References

http://www.graphviz.org/content/attrs http://www.graphviz.org/doc/info/attrs.html

graphid.util.util_graphviz.dump_nx_ondisk(graph, fpath)[source]
graphid.util.util_graphviz.ensure_nonhex_color(orig_color)[source]
graphid.util.util_graphviz.show_nx(graph, with_labels=True, fnum=None, pnum=None, layout='agraph', ax=None, pos=None, img_dict=None, title=None, layoutkw=None, verbose=None, **kwargs)[source]
Parameters:
  • graph (networkx.Graph)

  • with_labels (bool) – (default = True)

  • fnum (int) – figure number(default = None)

  • pnum (tuple) – plot number(default = None)

  • layout (str) – (default = ‘agraph’)

  • ax (None) – (default = None)

  • pos (None) – (default = None)

  • img_dict (dict) – (default = None)

  • title (str) – (default = None)

  • layoutkw (None) – (default = None)

  • verbose (bool) – verbosity flag(default = None)

Kwargs:

use_image, framewidth, modify_ax, as_directed, hacknoedge, hacknode, arrow_width, fontsize, fontweight, fontname, fontfamilty, fontproperties

CommandLine

python -m graphid.util.util_graphviz show_nx --show

Example

>>> # xdoctest: +REQUIRES(module:pygraphviz)
>>> from graphid.util.util_graphviz import *  # NOQA
>>> graph = nx.DiGraph()
>>> graph.add_nodes_from(['a', 'b', 'c', 'd'])
>>> graph.add_edges_from({'a': 'b', 'b': 'c', 'b': 'd', 'c': 'd'}.items())
>>> nx.set_node_attributes(graph, name='shape', values='rect')
>>> nx.set_node_attributes(graph, name='image', values={'a': util.grab_test_imgpath('carl.jpg')})
>>> nx.set_node_attributes(graph, name='image', values={'d': util.grab_test_imgpath('astro.png')})
>>> #nx.set_node_attributes(graph, name='height', values=100)
>>> with_labels = True
>>> fnum = None
>>> pnum = None
>>> e = show_nx(graph, with_labels, fnum, pnum, layout='agraph')
>>> util.show_if_requested()
graphid.util.util_graphviz.netx_draw_images_at_positions(img_list, pos_list, size_list, color_list, framewidth_list)[source]

Overlays images on a networkx graph

References

https://gist.github.com/shobhit/3236373 http://matplotlib.org/examples/pylab_examples/demo_annotation_box.html http://stackoverflow.com/questions/11487797/mpl-overlay-small-image http://matplotlib.org/api/text_api.html http://matplotlib.org/api/offsetbox_api.html

graphid.util.util_graphviz.parse_html_graphviz_attrs()[source]
class graphid.util.util_graphviz.GRAPHVIZ_KEYS[source]

Bases: object

N = {'URL', 'area', 'color', 'colorscheme', 'comment', 'distortion', 'fillcolor', 'fixedsize', 'fontcolor', 'fontname', 'fontsize', 'gradientangle', 'group', 'height', 'href', 'id', 'image', 'imagepos', 'imagescale', 'label', 'labelloc', 'layer', 'margin', 'nojustify', 'ordering', 'orientation', 'penwidth', 'peripheries', 'pin', 'pos', 'rects', 'regular', 'root', 'samplepoints', 'shape', 'shapefile', 'showboxes', 'sides', 'skew', 'sortv', 'style', 'target', 'tooltip', 'vertices', 'width', 'xlabel', 'xlp', 'z'}
E = {'URL', 'arrowhead', 'arrowsize', 'arrowtail', 'color', 'colorscheme', 'comment', 'constraint', 'decorate', 'dir', 'edgeURL', 'edgehref', 'edgetarget', 'edgetooltip', 'fillcolor', 'fontcolor', 'fontname', 'fontsize', 'headURL', 'head_lp', 'headclip', 'headhref', 'headlabel', 'headport', 'headtarget', 'headtooltip', 'href', 'id', 'label', 'labelURL', 'labelangle', 'labeldistance', 'labelfloat', 'labelfontcolor', 'labelfontname', 'labelfontsize', 'labelhref', 'labeltarget', 'labeltooltip', 'layer', 'len', 'lhead', 'lp', 'ltail', 'minlen', 'nojustify', 'penwidth', 'pos', 'samehead', 'sametail', 'showboxes', 'style', 'tailURL', 'tail_lp', 'tailclip', 'tailhref', 'taillabel', 'tailport', 'tailtarget', 'tailtooltip', 'target', 'tooltip', 'weight', 'xlabel', 'xlp'}
G = {'Damping', 'K', 'URL', '_background', 'bb', 'bgcolor', 'center', 'charset', 'clusterrank', 'colorscheme', 'comment', 'compound', 'concentrate', 'defaultdist', 'dim', 'dimen', 'diredgeconstraints', 'dpi', 'epsilon', 'esep', 'fontcolor', 'fontname', 'fontnames', 'fontpath', 'fontsize', 'forcelabels', 'gradientangle', 'href', 'id', 'imagepath', 'inputscale', 'label', 'label_scheme', 'labeljust', 'labelloc', 'landscape', 'layerlistsep', 'layers', 'layerselect', 'layersep', 'layout', 'levels', 'levelsgap', 'lheight', 'lp', 'lwidth', 'margin', 'maxiter', 'mclimit', 'mindist', 'mode', 'model', 'mosek', 'newrank', 'nodesep', 'nojustify', 'normalize', 'notranslate', 'nslimit\nnslimit1', 'ordering', 'orientation', 'outputorder', 'overlap', 'overlap_scaling', 'overlap_shrink', 'pack', 'packmode', 'pad', 'page', 'pagedir', 'quadtree', 'quantum', 'rankdir', 'ranksep', 'ratio', 'remincross', 'repulsiveforce', 'resolution', 'root', 'rotate', 'rotation', 'scale', 'searchsize', 'sep', 'showboxes', 'size', 'smoothing', 'sortv', 'splines', 'start', 'style', 'stylesheet', 'target', 'truecolor', 'viewport', 'voro_margin', 'xdotversion'}
graphid.util.util_graphviz.get_explicit_graph(graph)[source]
Parameters:

graph (nx.Graph)

graphid.util.util_graphviz.get_nx_layout(graph, layout, layoutkw=None, verbose=None)[source]
graphid.util.util_graphviz.apply_graph_layout_attrs(graph, layout_info)[source]
graphid.util.util_graphviz.patch_pygraphviz()[source]

Hacks around a python3 problem in 1.3.1 of pygraphviz

graphid.util.util_graphviz.make_agraph(graph_)[source]
graphid.util.util_graphviz._groupby_prelayout(graph_, layoutkw, groupby)[source]

sets pin attr of graph_ inplace in order to nodes according to specified layout.

graphid.util.util_graphviz.nx_agraph_layout(orig_graph, inplace=False, verbose=None, return_agraph=False, groupby=None, **layoutkw)[source]

Uses graphviz and custom code to determine position attributes of nodes and edges.

Parameters:

groupby (str) – if not None then nodes will be grouped by this attributes and groups will be layed out separately and then stacked together in a grid

References

http://www.graphviz.org/content/attrs http://www.graphviz.org/doc/info/attrs.html

CommandLine

python -m graphid.util.util_graphviz nx_agraph_layout --show

Doctest

>>> # xdoctest: +REQUIRES(module:pygraphviz)
>>> from graphid.util.util_graphviz import *  # NOQA
>>> import networkx as nx
>>> import itertools as it
>>> from graphid import util
>>> n, s = 9, 4
>>> offsets = list(range(0, (1 + n) * s, s))
>>> node_groups = [list(map(str, range(*o))) for o in ub.iter_window(offsets, 2)]
>>> edge_groups = [it.combinations(nodes, 2) for nodes in node_groups]
>>> graph = nx.Graph()
>>> [graph.add_nodes_from(nodes) for nodes in node_groups]
>>> [graph.add_edges_from(edges) for edges in edge_groups]
>>> for count, nodes in enumerate(node_groups):
...     nx.set_node_attributes(graph, name='id', values=ub.dzip(nodes, [count]))
>>> layoutkw = dict(prog='neato')
>>> graph1, info1 = nx_agraph_layout(graph.copy(), inplace=True, groupby='id', **layoutkw)
>>> graph2, info2 = nx_agraph_layout(graph.copy(), inplace=True, **layoutkw)
>>> graph3, _ = nx_agraph_layout(graph1.copy(), inplace=True, **layoutkw)
>>> nx.set_node_attributes(graph1, name='pin', values='true')
>>> graph4, _ = nx_agraph_layout(graph1.copy(), inplace=True, **layoutkw)
>>> # xdoc: +REQUIRES(--show)
>>> util.show_nx(graph1, layout='custom', pnum=(2, 2, 1), fnum=1)
>>> util.show_nx(graph2, layout='custom', pnum=(2, 2, 2), fnum=1)
>>> util.show_nx(graph3, layout='custom', pnum=(2, 2, 3), fnum=1)
>>> util.show_nx(graph4, layout='custom', pnum=(2, 2, 4), fnum=1)
>>> util.show_if_requested()
>>> g1pos = nx.get_node_attributes(graph1, 'pos')['1']
>>> g4pos = nx.get_node_attributes(graph4, 'pos')['1']
>>> g2pos = nx.get_node_attributes(graph2, 'pos')['1']
>>> g3pos = nx.get_node_attributes(graph3, 'pos')['1']
>>> print('g1pos = {!r}'.format(g1pos))
>>> print('g4pos = {!r}'.format(g4pos))
>>> print('g2pos = {!r}'.format(g2pos))
>>> print('g3pos = {!r}'.format(g3pos))
>>> assert np.all(g1pos == g4pos), 'points between 1 and 4 were pinned so they should be equal'
>>> #assert np.all(g2pos != g3pos), 'points between 2 and 3 were not pinned, so they should be different'

assert np.all(nx.get_node_attributes(graph1, ‘pos’)[‘1’] == nx.get_node_attributes(graph4, ‘pos’)[‘1’]) assert np.all(nx.get_node_attributes(graph2, ‘pos’)[‘1’] == nx.get_node_attributes(graph3, ‘pos’)[‘1’])

graphid.util.util_graphviz.parse_point(ptstr)[source]
graphid.util.util_graphviz.parse_anode_layout_attrs(anode)[source]
graphid.util.util_graphviz.parse_aedge_layout_attrs(aedge, translation=None)[source]

parse grpahviz splineType

graphid.util.util_graphviz._get_node_size(graph, node, node_size)[source]
graphid.util.util_graphviz.draw_network2(graph, layout_info, ax, as_directed=None, hacknoedge=False, hacknode=False, verbose=None, **kwargs)[source]
Kwargs:

use_image, arrow_width, fontsize, fontweight, fontname, fontfamilty, fontproperties

fancy way to draw networkx graphs without directly using networkx

graphid.util.util_graphviz.stack_graphs(graph_list, vert=False, pad=None)[source]
graphid.util.util_graphviz.translate_graph(graph, t_xy)[source]
graphid.util.util_graphviz.translate_graph_to_origin(graph)[source]
graphid.util.util_graphviz.get_graph_bounding_box(graph)[source]

Example

>>> # xdoctest: +REQUIRES(module:pygraphviz)
>>> graph = nx.path_graph([1, 2, 3, 4])
>>> nx_agraph_layout(graph, inplace=True)
>>> bbox = get_graph_bounding_box(graph)
>>> print(ub.urepr(bbox, nl=0))
[0.0, 0.0, 54.0, 252.0]
graphid.util.util_graphviz.nx_ensure_agraph_color(graph)[source]

changes colors to hex strings on graph attrs

graphid.util.util_graphviz.bbox_from_extent(extent)[source]
Parameters:

extent (ndarray) – tl_x, br_x, tl_y, br_y

Returns:

tl_x, tl_y, w, h

Return type:

bbox (ndarray)

Example

>>> extent = [0, 10, 0, 10]
>>> bbox = bbox_from_extent(extent)
>>> print('bbox = {}'.format(ub.urepr(list(bbox), nl=0)))
bbox = [0, 0, 10, 10]
graphid.util.util_graphviz.get_pointset_extents(pts)[source]