def savegraph(self, fname, verbose=True): if verbose: printl('Write output file:', fname) if fname.endswith('.dot'): self._graph.write(fname) else: self._graph.layout(prog='dot') self._graph.draw(fname)
def test_00(): array >> adder (array, adder) >> multiplier graph._wrap_fcns(dataprinter, printer) result = multiplier.outputs[0].data printl(result) savegraph(graph, 'output/decorators_static_graph_00.pdf')
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')
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')
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')
def multiplier(): global call_counter call_counter += 1 printl('Call Multiplier ({})'.format(call_counter))
def adder(): global call_counter call_counter += 1 printl('Call Adder ({})'.format(call_counter))
def array(): global call_counter call_counter += 1 printl('Call array ({})'.format(call_counter))
def printer(fcn, node, inputs, outputs): printl('Evaluate {node}'.format(node=node.name)) with next_level(): fcn(node, inputs, outputs) printl('... done with {node}'.format(node=node.name))
def toucher(fcn, node, inputs, outputs): for i, input in enumerate(inputs): printl('touch input {: 2d} {}.{}'.format(i, node.name, input.name)) with next_level(): input.touch() fcn(node, inputs, outputs)
def dataprinter(fcn, node, inputs, outputs): fcn(node, inputs, outputs) for i, output in enumerate(outputs): printl('{: 2d} {}: {!s}'.format(i, output.name, output._data))
def after_printer(fcn, node, inputs, outputs): with next_level(): fcn(node, inputs, outputs) printl('Evaluate {node}: {label}'.format(node=node.name, label=node.label()))