Пример #1
0
def visualise_graph(Gx, filename):

    try:
        import pygraphviz as pgv
    except ImportError:
        raise RuntimeError("Install pygraphviz")

    if Gx.number_of_nodes() > 400:
        print("Skipping visualisation")
        return

    G = pgv.AGraph(strict=False, directed=True)
    for nd, v in Gx.nodes.items():
        ex = v['expression']
        label = ex.__class__.__name__
        if isinstance(ex, Sum):
            label = '+'
        elif isinstance(ex, Product):
            label = '*'
        elif isinstance(ex, Division):
            label = '/'
        elif isinstance(ex, (IntValue, FloatValue)):
            label = ex.value()
        elif isinstance(ex, (Indexed, ReferenceValue)):
            label = str(ex)
        G.add_node(nd, label='[%d] %s' % (nd, label))

        arg = strip_modified_terminal(ex)
        if isinstance(arg, Argument):
            G.get_node(nd).attr['shape'] = 'box'

        t = v.get('target')
        if t:
            G.get_node(nd).attr['label'] += ':' + str(t)
            G.get_node(nd).attr['shape'] = 'hexagon'

        c = v.get('component')
        if c:
            G.get_node(nd).attr['label'] += ', comp={}'.format(c)

    for nd, eds in Gx.out_edges.items():
        for ed in eds:
            G.add_edge(nd, ed)

    G.layout(prog='dot')
    G.draw(filename)
Пример #2
0
def build_argument_indices(S):
    """Build ordered list of indices to modified arguments."""

    arg_indices = []
    for i, v in S.nodes.items():
        arg = strip_modified_terminal(v['expression'])
        if isinstance(arg, Argument):
            arg_indices.append(i)

    # Make a canonical ordering of vertex indices for modified arguments
    def arg_ordering_key(i):
        """Return a key for sorting argument vertex indices.
        Key is based on the properties of the modified terminal."""
        mt = analyse_modified_terminal(S.nodes[i]['expression'])
        return mt.argument_ordering_key()

    ordered_arg_indices = sorted(arg_indices, key=arg_ordering_key)
    return ordered_arg_indices