|
| 1 | +Examples |
| 2 | +======== |
| 3 | + |
| 4 | + |
| 5 | +A simple example |
| 6 | +---------------- |
| 7 | + |
| 8 | +To understand what dags does, let's look at a few functions |
| 9 | +that do simple calculations. |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + def f(x, y): |
| 14 | + return x**2 + y**2 |
| 15 | +
|
| 16 | +
|
| 17 | + def g(y, z): |
| 18 | + return 0.5 * y * z |
| 19 | +
|
| 20 | +
|
| 21 | + def h(f, g): |
| 22 | + return g / f |
| 23 | +
|
| 24 | +
|
| 25 | +Combine with a single target |
| 26 | +---------------------------- |
| 27 | + |
| 28 | + |
| 29 | +Assume that we are interested in a function that calculates h, given x, y and z. |
| 30 | + |
| 31 | +We could hardcode this function as: |
| 32 | + |
| 33 | +.. code-block:: python |
| 34 | +
|
| 35 | + def hardcoded_combined(x, y, z): |
| 36 | + _f = f(x, y) |
| 37 | + _g = g(y, z) |
| 38 | + return h(_f, _g) |
| 39 | +
|
| 40 | +
|
| 41 | + hardcoded_combined(x=1, y=2, z=3) |
| 42 | +
|
| 43 | +.. code-block:: python |
| 44 | +
|
| 45 | + 0.6 |
| 46 | +
|
| 47 | +Instead, we can use dags to construct the same function: |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + from dags import concatenate_functions |
| 52 | +
|
| 53 | + combined = concatenate_functions([h, f, g], targets="h") |
| 54 | +
|
| 55 | + combined(x=1, y=2, z=3) |
| 56 | +
|
| 57 | +.. code-block:: python |
| 58 | +
|
| 59 | + 0.6 |
| 60 | +
|
| 61 | +Importantly, the order in which the functions are passed into ``concatenate_functions`` |
| 62 | +does not matter! |
| 63 | + |
| 64 | + |
| 65 | +Combine with multiple targets |
| 66 | +----------------------------- |
| 67 | + |
| 68 | +Assume that we want the same combined h function as before but we also need intermediate |
| 69 | +outputs. And we would like to have them as a dictionary. We can do this as follows: |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + combined = concatenate_functions( |
| 74 | + [h, f, g], |
| 75 | + targets=["h", "f", "g"], |
| 76 | + return_type="dict", |
| 77 | + ) |
| 78 | +
|
| 79 | + combined(x=1, y=2, z=3) |
| 80 | +
|
| 81 | +.. code-block:: python |
| 82 | +
|
| 83 | + {"h": 0.6, "f": 5, "g": 3.0} |
| 84 | +
|
| 85 | +
|
| 86 | +Renaming the output of a function |
| 87 | +--------------------------------- |
| 88 | + |
| 89 | +So far, the name of the output of the function was determined from the ``__name__`` |
| 90 | +attribute of each function. This is not enough if you want to use dags to create |
| 91 | +functions with exchangeable parts. Let's assume we have two implementations of f |
| 92 | +and want to create combined functions for both versions. |
| 93 | + |
| 94 | + |
| 95 | +.. code-block:: python |
| 96 | +
|
| 97 | + import numpy as np |
| 98 | +
|
| 99 | +
|
| 100 | + def f_standard(x, y): |
| 101 | + return x**2 + y**2 |
| 102 | +
|
| 103 | +
|
| 104 | + def f_numpy(x, y): |
| 105 | + return np.square(x) + np.square(y) |
| 106 | +
|
| 107 | +We can do that as follows: |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + combined_standard = concatenate_functions( |
| 112 | + {"f": f_standard, "g": g, "h": h}, |
| 113 | + targets="h", |
| 114 | + ) |
| 115 | +
|
| 116 | + combined_numpy = concatenate_functions( |
| 117 | + {"f": f_numpy, "g": g, "h": h}, |
| 118 | + targets="h", |
| 119 | + ) |
| 120 | +
|
| 121 | +In fact, this ability to switch out components was the primary reason we wrote dags. |
| 122 | +This functionality has, for example, been used in |
| 123 | +`GETTSIM <https://github.com/iza-institute-of-labor-economics/gettsim>`_, a |
| 124 | +framework to simulate reforms to the German tax and transfer system. |
| 125 | + |
| 126 | + |
| 127 | +Renaming the input of functions |
| 128 | +------------------------------- |
| 129 | + |
| 130 | +Sometimes, we want to re-use a general function inside dags, but the arguments of that |
| 131 | +function don't have the correct names. For example, we might have a general |
| 132 | +implementation that we could re-use for `f`: |
| 133 | + |
| 134 | + |
| 135 | +.. code-block:: python |
| 136 | +
|
| 137 | + def sum_of_squares(a, b): |
| 138 | + return a**2 + b**2 |
| 139 | +
|
| 140 | +Instead of writing a wrapper like: |
| 141 | + |
| 142 | + |
| 143 | +.. code-block:: python |
| 144 | +
|
| 145 | + def f(x, y): |
| 146 | + return sum_of_squares(a=x, b=y) |
| 147 | +
|
| 148 | +We can simply rename the arguments programmatically: |
| 149 | + |
| 150 | +.. code-block:: python |
| 151 | +
|
| 152 | + from dags.signature import rename_arguments |
| 153 | +
|
| 154 | + functions = { |
| 155 | + "f": rename_arguments(sum_of_squares, mapper={"a": "x", "b": "y"}), |
| 156 | + "g": g, |
| 157 | + "h": h, |
| 158 | + } |
| 159 | +
|
| 160 | + combined = concatenate_functions(functions, targets="h") |
| 161 | + combined(x=1, y=2, z=3) |
| 162 | +
|
| 163 | +.. code-block:: python |
| 164 | +
|
| 165 | + 0.6 |
0 commit comments