8.1.1.6. add_function

Dispatcher.add_function(function_id=None, function=None, inputs=None, outputs=None, input_domain=None, weight=None, inp_weight=None, out_weight=None, description=None, filters=None, await_domain=None, await_result=None, **kwargs)[source]

Add a single function node to dispatcher.

Parameters:
  • function_id (str, optional) – Function node id. If None will be assigned as <fun.__name__>.
  • function (callable, optional) – Data node estimation function.
  • inputs (list, optional) – Ordered arguments (i.e., data node ids) needed by the function.
  • outputs (list, optional) – Ordered results (i.e., data node ids) returned by the function.
  • input_domain (callable, optional) – A function that checks if input values satisfy the function domain. This can be any function that takes the same inputs of the function and returns True if input values satisfy the domain, otherwise False. In this case the dispatch algorithm doesn’t pass on the node.
  • weight (float, int, optional) – Node weight. It is a weight coefficient that is used by the dispatch algorithm to estimate the minimum workflow.
  • inp_weight (dict[str, float | int], optional) – Edge weights from data nodes to the function node. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
  • out_weight (dict[str, float | int], optional) – Edge weights from the function node to data nodes. It is a dictionary (key=data node id) with the weight coefficients used by the dispatch algorithm to estimate the minimum workflow.
  • description (str, optional) – Function node’s description.
  • filters (list[function], optional) – A list of functions that are invoked after the invocation of the main function.
  • await_domain (bool|int|float, optional) – If True the Dispatcher waits all input results before executing the input_domain function. If a number is defined this is used as timeout for Future.result method [default: True]. Note this is used when asynchronous or parallel execution is enable.
  • await_result (bool|int|float, optional) – If True the Dispatcher waits output results before assigning them to the workflow. If a number is defined this is used as timeout for Future.result method [default: False]. Note this is used when asynchronous or parallel execution is enable.
  • kwargs (keyword arguments, optional) – Set additional node attributes using key=value.
Returns:

Function node id.

Return type:

str

——————————————————————–

Example:

Add a function node:

>>> def my_function(a, b):
...     c = a + b
...     d = a - b
...     return c, d
...
>>> dsp.add_function(function=my_function, inputs=['a', 'b'],
...                  outputs=['c', 'd'])
'my_function'

Add a function node with domain:

>>> from math import log
>>> def my_log(a, b):
...     return log(b - a)
...
>>> def my_domain(a, b):
...     return a < b
...
>>> dsp.add_function(function=my_log, inputs=['a', 'b'],
...                  outputs=['e'], input_domain=my_domain)
'my_log'