8.1.1.48. __doc__¶
- Dispatcher.__doc__ = "\n It provides a data structure to process a complex system of functions.\n\n The scope of this data structure is to compute the shortest workflow between\n input and output data nodes.\n\n A workflow is a sequence of function calls.\n\n **------------------------------------------------------------------------**\n\n **Example**:\n\n As an example, here is a system of equations:\n\n :math:`b - a = c`\n\n :math:`log(c) = d_{from-log}`\n\n :math:`d = (d_{from-log} + d_{initial-guess}) / 2`\n\n that will be solved assuming that :math:`a = 0`, :math:`b = 1`, and\n :math:`d_{initial-guess} = 4`.\n\n **Steps**\n\n Create an empty dispatcher::\n\n >>> dsp = Dispatcher(name='Dispatcher')\n\n Add data nodes to the dispatcher map::\n\n >>> dsp.add_data(data_id='a')\n 'a'\n >>> dsp.add_data(data_id='c')\n 'c'\n\n Add a data node with a default value to the dispatcher map::\n\n >>> dsp.add_data(data_id='b', default_value=1)\n 'b'\n\n Add a function node::\n\n >>> def diff_function(a, b):\n ... return b - a\n ...\n >>> dsp.add_function('diff_function', function=diff_function,\n ... inputs=['a', 'b'], outputs=['c'])\n 'diff_function'\n\n Add a function node with domain::\n\n >>> from math import log\n ...\n >>> def log_domain(x):\n ... return x > 0\n ...\n >>> dsp.add_function('log', function=log, inputs=['c'], outputs=['d'],\n ... input_domain=log_domain)\n 'log'\n\n Add a data node with function estimation and callback function.\n\n - function estimation: estimate one unique output from multiple\n estimations.\n - callback function: is invoked after computing the output.\n\n >>> def average_fun(kwargs):\n ... '''\n ... Returns the average of node estimations.\n ...\n ... :param kwargs:\n ... Node estimations.\n ... :type kwargs: dict\n ...\n ... :return:\n ... The average of node estimations.\n ... :rtype: float\n ... '''\n ...\n ... x = kwargs.values()\n ... return sum(x) / len(x)\n ...\n >>> def callback_fun(x):\n ... print('(log(1) + 4) / 2 = %.1f' % x)\n ...\n >>> dsp.add_data(data_id='d', default_value=4, wait_inputs=True,\n ... function=average_fun, callback=callback_fun)\n 'd'\n\n .. dispatcher:: dsp\n :opt: graph_attr={'ratio': '1'}\n\n >>> dsp\n <...>\n\n Dispatch the function calls to achieve the desired output data node `d`:\n\n .. dispatcher:: outputs\n :opt: graph_attr={'ratio': '1'}\n :code:\n\n >>> outputs = dsp.dispatch(inputs={'a': 0}, outputs=['d'])\n (log(1) + 4) / 2 = 2.0\n >>> outputs\n Solution({'a': 0, 'b': 1, 'c': 1, 'd': 2.0})\n "
The dispatcher’s description.