示例#1
0
    def graph(self, graph):
        if self._graph:
            raise Exception('Graph is already set')

        if graph is True:
            self._graph = Graph()
        elif isinstance(graph, str):
            self._graph = Graph(label=graph)
        elif isinstance(graph, dict):
            self._graph = Graph(**kwargs)
        elif graph:
            self._graph = graph
示例#2
0
def test_07():
    g = Graph()
    n1 = g.add_node('node1')
    n2 = g.add_node('node2')
    g._wrap_fcns(toucher, printer)

    out1 = n1._add_output('o1')
    out2 = n1._add_output('o2')

    _, final = n2._add_pair('i1', 'o1')
    n2._add_input('i2')

    (out1, out2) >> n2

    final.data
示例#3
0
def test_02():
    """Simple test of the graph plotter"""
    g = Graph()
    n1 = g.add_node('node1')
    n2 = g.add_node('node2')
    n3 = g.add_node('node3')
    g._wrap_fcns(toucher, printer)

    out1 = n1._add_output('o1')
    out2 = n1._add_output('o2')

    _, out3 = n2._add_pair('i1', 'o1')
    n2._add_input('i2')

    _, final = n3._add_pair('i1', 'o1')

    (out1, out2) >> n2
    out3 >> n3

    d = GraphDot(g)
    d.savegraph('output/test2_00.png')

    final.data
    d = GraphDot(g)
    d.savegraph('output/test2_01.png')
示例#4
0
def test_08():
    g = Graph()
    n1 = g.add_node('node1')
    n2 = g.add_node('node2')
    n3 = g.add_node('node3')
    g._wrap_fcns(toucher, printer)

    out1 = n1._add_output('o1')
    out2 = n1._add_output('o2')

    _, out3 = n2._add_pair('i1', 'o1')
    n2._add_input('i2')

    _, final = n3._add_pair('i1', 'o1')

    (out1, out2) >> n2
    out3 >> n3

    print()
    final.data

    print('Taint n2')
    n2.taint()
    final.data

    print('Taint n3')
    n3.taint()
    final.data
示例#5
0
def test_01():
    """Create four nodes: sum up three of them, multiply the result by the fourth
    Use graph context to create the graph.
    Use one-line code for connecting the nodes
    """
    with Graph() as graph:
        initials = [Array(name) for name in ['n1', 'n2', 'n3', 'n4']]
        s = Adder('add')
        m = Multiplier('mul')

    (initials[3], (initials[:3] >> s)) >> m

    graph._wrap_fcns(dataprinter, printer)

    result = m.outputs.result.data
    printl(result)

    savegraph(graph, 'output/decorators_graph_01.pdf')
示例#6
0
def test_00():
    """Create four nodes: sum up three of them, multiply the result by the fourth
    Use Graph methods to build the graph
    """
    graph = Graph()
    in1 = graph.add_node('n1', nodeclass=Array)
    in2 = graph.add_node('n2', nodeclass=Array)
    in3 = graph.add_node('n3', nodeclass=Array)
    in4 = graph.add_node('n4', nodeclass=Array)
    s = graph.add_node('add', nodeclass=Adder)
    m = graph.add_node('mul', nodeclass=Multiplier)

    (in1, in2, in3) >> s
    (in4, s) >> m

    graph._wrap_fcns(dataprinter, printer)

    result = m.outputs.result.data
    printl(result)

    savegraph(graph, 'output/decorators_graph_00.pdf')
示例#7
0
def test_02():
    """Create four nodes: sum up three of them, multiply the result by the fourth
    Use graph context to create the graph.
    Use one-line code for connecting the nodes.
    Use NodeInstance decorator to convert functions directly to node instances.
    """
    with Graph() as graph:
        initials = [Array(name) for name in ['n1', 'n2', 'n3', 'n4']]

        @NodeInstance(
            name='add',
            class_kwargs=dict(missing_input_handler=MissingInputAddOne(
                output_fmt='result')))
        def s(node, inputs, outputs):
            out = None
            for input in inputs:
                if out is None:
                    out = outputs[0].data = input.data
                else:
                    out += input.data

        @NodeInstance(
            name='mul',
            class_kwargs=dict(missing_input_handler=MissingInputAddOne(
                output_fmt='result')))
        def m(node, inputs, outputs):
            out = None
            for input in inputs:
                if out is None:
                    out = outputs[0].data = input.data
                else:
                    out *= input.data

    (initials[3], (initials[:3] >> s)) >> m

    graph._wrap_fcns(dataprinter, printer)

    result = m.outputs.result.data
    printl(result)

    savegraph(graph, 'output/decorators_graph_02.pdf')
示例#8
0
def test_02a():
    """Simple test of the graph plotter"""
    g = Graph()
    n1 = g.add_node('node1')
    n2 = g.add_node('node2')
    n3 = g.add_node('node3')
    n4 = g.add_node('node4')
    g._wrap_fcns(toucher, printer)

    out1 = n1._add_output('o1')

    in2, out2 = n2._add_pair('i1', 'o1')
    in3, out3 = n3._add_pair('i1', 'o1')
    in4, out4 = n4._add_pair('i1', 'o1')

    out1.repeat() >> (in2, in3, in4)

    d = GraphDot(g)
    d.savegraph('output/test2a_00.png')

    print(out4.data)
    d = GraphDot(g)
    d.savegraph('output/test2a_01.png')
#!/usr/bin/env python

from __future__ import print_function
from dagflow.node_deco import NodeInstanceStatic
from dagflow.graph import Graph
from dagflow.graphviz import savegraph
from dagflow.input_extra import MissingInputAddOne
import numpy as N
from dagflow.wrappers import *

from dagflow.printl import printl, set_prefix_function, current_level
set_prefix_function(lambda: '{:<2d} '.format(current_level()), )

call_counter = 0

with Graph() as graph:

    @NodeInstanceStatic()
    def array():
        global call_counter
        call_counter += 1
        printl('Call array ({})'.format(call_counter))

    @NodeInstanceStatic()
    def adder():
        global call_counter
        call_counter += 1
        printl('Call Adder ({})'.format(call_counter))

    @NodeInstanceStatic()
    def multiplier():
示例#10
0
def test_graph_big_01():
    """Create a graph of nodes and test evaluation features"""
    g = Graph()
    label = None

    def plot(suffix=''):
        global counter
        d = GraphDot(g)
        newlabel = label and label + suffix or suffix
        if newlabel is not None:
            d.set_label(newlabel)
        d.savegraph('output/test_graph_big_{:03d}.png'.format(counter))
        counter += 1

    def plotter(fcn, node, inputs, outputs):
        plot('[start evaluating {}]'.format(node.name))
        fcn(node, inputs, outputs)
        plot('[done evaluating {}]'.format(node.name))

    A1 = g.add_node('A1')
    A2 = g.add_node('A2', auto_freeze=True, label='{name}|frozen')
    A3 = g.add_node('A3', immediate=True, label='{name}|immediate')
    B = g.add_node('B')
    C1 = g.add_node('C1')
    C2 = g.add_node('C2')
    D = g.add_node('D')
    E = g.add_node('E')
    F = g.add_node('F')
    H = g.add_node('H')
    P = g.add_node('P', immediate=True, label='{name}|immediate')

    g._wrap_fcns(toucher, printer, plotter)

    A1._add_output('o1')
    A2._add_output('o1')
    P._add_output('o1')
    A3._add_pair('i1', 'o1')
    B._add_pair(('i1', 'i2', 'i3', 'i4'), ('o1', 'o2'))
    C1._add_output('o1')
    C2._add_output('o1')
    D._add_pair('i1', 'o1')
    D._add_pair('i2', 'o2')
    H._add_pair('i1', 'o1')
    _, other = F._add_pair('i1', 'o1')
    _, final = E._add_pair('i1', 'o1')

    (A1, A2, (P >> A3), D[:1]) >> B >> (E, H)
    ((C1, C2) >> D[:, 1]) >> F

    g.print()

    label = 'Initial graph state.'
    plot()

    label = 'Read E...'
    plot()
    plot()
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Taint D.'
    plot()
    plot()
    plot()
    D.taint()
    plot()
    label = 'Read F...'
    other.data
    label = 'Done reading F.'
    plot()

    label = 'Read E...'
    plot()
    plot()
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Taint A2.'
    plot()
    plot()
    plot()
    A2.taint()
    plot()
    label = 'Read E...'
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Unfreeze A2 (tainted).'
    plot()
    plot()
    plot()
    A2.unfreeze()
    plot()
    label = 'Read E...'
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Unfreeze A2 (not tainted).'
    plot()
    plot()
    plot()
    A2.unfreeze()
    plot()
    label = 'Read E...'
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Taint P'
    plot()
    plot()
    plot()
    P.taint()
    plot()
    label = 'Read E...'
    plot()
    final.data
    label = 'Done reading E.'
    plot()

    label = 'Invalidate P'
    plot()
    plot()
    plot()
    P.invalid = True
    plot()

    label = 'Validate P'
    plot()
    plot()
    plot()
    P.invalid = False
    plot()
    label = 'Read E...'
    plot()
    final.data
    label = 'Done reading E.'
    plot()