API#
- class progeval.ProgEval(track_dependence=None, transformer=None, **initial_values)#
- __init__(track_dependence=None, transformer=None, **initial_values)#
This class implements a cached computational graph.
Nodes of the computational graph are only evaluated if they are requested and values are cached to avoid re-computation. If the graph is changed (by re-assigning nodes), only parts that depend on the change are re-evaluated.
Computational graphs can be created in two ways:
Dynamically, by assigning nodes to a
ProgEval()object. Assignments can be made either as e.g.graph.x = lambda a: 2*a, or by invokinggraph.register(name, function).Statically, by subclassing ProgEval. All class methods that do not start with an underscore are interpreted as computational nodes.
The two methods can also be mixed.
- Parameters
track_dependence (
Optional[bool]) – Whether to track dependence in the graph. The default is true. If set to false, changing the computational graph will not force dependent values to be re-computed.transformer (
Optional[Callable[[Callable,bool,str],Union[Callable,tuple[Callable,Signature]]]]) – Can be used to transform every computational node in the graph. The function must take the function, whether it is static, and the name as inputs. Static indicates whether the first argument of the function is self, i.e. an instance of the computational graph. The output must be a function with the same call signature, or a tuple of the modified function and the new call signature.**initial_values – Input values can be set in a convenient way by passing them as arguments here. This is equivalent to setting them afterwards via e.g.
self.x = x.
- clear_cache()#
Remove all cached values of the computational graph.
- compute_all_quantities()#
Evaluate all quantities in the computational graph.
- Return type
dict[str,Any]- Returns
A dictionary mapping the name of the quantity to its value.
- register(name, function, args=None)#
Add a new node to the computational graph.
The given function specifies how to compute the quantity with the given name. If the second argument is not callable, it is instead interpreted as the value of the quantity.
If the third argument is given, it specifies the quantities that must be computed first and passed to the function. Otherwise, this is automatically derived from the argument names of the function.
- Parameters
name – Name of node/quantity in computational graph.
function (
Union[Callable,Any]) – Specifies how to compute the quantity or is its value.args (
Optional[Sequence[str]]) – Overrides the argument names of the function.
- Return type
None