def NodeInstanceStatic(fcn=None, **kwargsinstance): """ Function signature _fcn() """ kwargsinstance.setdefault('output', 'result') kwargsinstance['class_kwargs']=dict(parent=StaticNode, missing_input_handler=MissingInputAddOne()) return _NodeInstance(fcn, **kwargsinstance)
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')
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()), ) @NodeClass(output='array') def Array(node, inputs, outputs): """Creates a note with single data output with predefined array""" outputs[0].data = N.arange(5, dtype='d') @NodeClass(missing_input_handler=MissingInputAddOne(output_fmt='result')) def Adder(node, inputs, outputs): """Adds all the inputs together""" out = None for input in inputs: if out is None: out = outputs[0].data = input.data.copy() else: out += input.data @NodeClass(missing_input_handler=MissingInputAddOne(output_fmt='result')) def Multiplier(node, inputs, outputs): """Multiplies all the inputs together""" out = None for input in inputs:
def __init__(self, name): FunctionNode.__init__(self, name) self._missing_input_handler = MissingInputAddOne(output_fmt='result')