API Reference
context
This module contains functions that manipulate the context of the graph, i.e., the values assigned to its nodes.
clear_values(graph, *args)
Clear (i.e. erase) values in the graph nodes.
Parameters
graph : grapes Graph The graph whose values are to be cleared. *args : hashables (typically strings) Names of nodes whose values are to be cleared. If no names are given, all values are cleared.
Notes
Frozen nodes are not cleared.
Source code in grapes/context.py
get_dict_of_values(graph, list_of_keys)
Get dictionary of key:value pairs corresponding to list of keys.
Parameters
graph : grapes Graph The graph from which to get the values. list_of_keys : list of hashables (typically strings) List of names of nodes whose values are required.
Returns
dict Dictionary whose keys are the elements of list_of_keys and whose values are the corresponding node values.
Source code in grapes/context.py
get_internal_context(graph, exclude_recipes=False)
Get the internal context.
Parameters
graph : grapes Graph The graph whose context is to be retrieved. exclude_recipes : bool Whether to exclude recipes from the returned dictionary or keep them, default: False.
Returns
dict Dictionary with the current values of the graph nodes.
Source code in grapes/context.py
get_kwargs_values(graph, dictionary)
Get values from the graph, using a dictionary that works like function kwargs.
Parameters
graph : grapes Graph The graph from which to get the values. dictionary : dict Dictionary whose keys are to be interpreted as keys for function kwargs, while values in dictionary are node names.
Returns
dict A dict with the same keys of the input dictionary, but with values replaced by the values of the nodes.
Source code in grapes/context.py
get_list_of_values(graph, list_of_keys)
Get list of values corresponding to list of keys.
Parameters
graph : grapes Graph The graph from which to get the values. list_of_keys : list of hashables (typically strings) List of names of nodes whose values are required.
Returns
list List like list_of_keys which contains values of nodes.
Source code in grapes/context.py
set_internal_context(graph, dictionary)
Clear all values and then set a new internal context with a dictionary.
Parameters
graph : grapes Graph The graph to update. dictionary : dict Dictionary with the new values.
Notes
Only keys that are already nodes in the graph are considered. Frozen nodes are not cleared.
Source code in grapes/context.py
update_internal_context(graph, dictionary)
Update the internal context with a dictionary.
Parameters
graph : grapes Graph The graph to update. dictionary : dict Dictionary with the new values. Keys are node names, values are the values to assign to those nodes.
Notes
Only keys that are already nodes in the graph are considered.
Source code in grapes/context.py
update_recipes_from_module(graph, module)
Update the internal context to assign to recipes the functions with the same name taken from a module.
Parameters
graph : grapes Graph The graph to update. module : module Module from which to load the functions.
Notes
Only functions whose name matches a recipe in the graph are loaded.
Source code in grapes/context.py
core
This module is the core of the grapes package. It contains the Graph class.
Graph
Class that represents a graph of nodes.
Source code in grapes/core.py
__eq__(other)
Equality check between graphs based on all members.
Parameters
other : Graph Graph to compare with.
Returns
bool True if the graphs are equal, False otherwise.
Notes
Two graphs are equal if they are isomorphic and all their node and edge attributes are equal (including values).
Source code in grapes/core.py
__getitem__(node)
Get the value of a node with [].
Parameters
node : hashable (typically string) Name of the node whose value is to be retrieved.
Returns
value : any Value of the node.
Raises
ValueError If the node has no value. KeyError If the node does not exist.
Source code in grapes/core.py
__init__(nx_digraph=None)
Initialize a Graph object.
Parameters
nx_digraph : networkx.DiGraph, optional A pre-existing directed graph to initialize from. If None, an empty graph is created. Default is None.
Source code in grapes/core.py
__setitem__(node, value)
Set the value of a node with [].
Parameters
node : hashable (typically string) Name of the node whose value is to be set. value : any Value to set.
Raises
KeyError If the node does not exist.
Source code in grapes/core.py
design
This module contains functions to design the graph.
add_multiple_conditional(graph, name, conditions, possibilities)
Interface to add a multiple conditional to the graph. A multiple conditional is a node that takes the value of one of its possibilities depending on which of its condition nodes evaluates to True.
Parameters
graph : grapes Graph The graph to which to add the conditional. name : hashable (typically string) Name of the conditional node to add. conditions : list of hashables (typically strings) Names of the condition nodes to add. possibilities : list of hashables (typically strings) Names of the nodes to add as possibilities.
Raises
ValueError If the number of possibilities is not equal to the number of conditions or to the number of conditions plus one (to allow for a default possibility).
Source code in grapes/design.py
add_simple_conditional(graph, name, condition, value_true, value_false)
Interface to add a conditional to the graph. A conditional is a node that takes the value of another node (one of the possibilities) depending on the boolean value of a condition node.
Parameters
graph : grapes Graph The graph to which to add the conditional. name : hashable (typically string) Name of the conditional node to add. condition : hashable (typically string) Name of the condition node to add. The condition node controls which of the possibility nodes passes its value to the conditional. value_true : hashable (typically string) Name of the node to add as possibility if the condition is true. value_false : hashable (typically string) Name of the node to add as possibility if the condition is false.
Source code in grapes/design.py
add_step(graph, name, recipe=None, *args, **kwargs)
Interface to add a node to the graph, with all its dependencies.
Parameters
graph : grapes Graph The graph to which the step is to be added. name : hashable (typically string) Name of the node to add. recipe : hashable (typically string), optional Name of the recipe node to add, if any. Default is None. args : hashables (typically strings) Names of nodes to add as positional dependencies. *kwargs : hashables (typically strings) Names of nodes to add as keyword dependencies. Keys in the dicts are the keywords to be used when calling the recipe.
Raises
ValueError If a node with dependencies is added without a recipe.
Source code in grapes/design.py
add_step_quick(graph, name, recipe)
Interface to quickly add a step by passing a name and a function.
The recipe node takes the name of the passed function. Dependency nodes are built from the args and kwonlyargs of the passed function.
Parameters
graph : grapes Graph The graph to which to add the step. name : hashable (typically string) Name of the node to add. recipe : function Function to be used as recipe.
Raises
TypeError If the passed recipe is not a function. ValueError If the passed function has varargs or varkwargs, which are not supported.
Notes
If the function is unnamed (i.e. a lambda), it is automatically renamed to "recipe_for_" + name.
Source code in grapes/design.py
edit_step(graph, name, recipe=None, *args, **kwargs)
Interface to edit an existing node, changing its predecessors.
Parameters
graph : grapes Graph The graph to which to add the step. name : hashable (typically string) Name of the node to edit. recipe : hashable (typically string), optional Name of the recipe node to add, if any. Default is None. args : hashables (typically strings) Names of nodes to add as positional dependencies. *kwargs : hashables (typically strings) Names of nodes to add as keyword dependencies. Keys in the dicts are the keywords to be used when calling the recipe.
Raises
KeyError If the node does not exist.
Source code in grapes/design.py
finalize_definition(graph)
Perform operations that should typically be done after the definition of a graph is completed.
Currently, this freezes all values, because it is assumed that values given during definition are to be frozen. It also marks dependencies of recipes as recipes themselves.
Parameters
graph : grapes Graph The graph to finalize.
Source code in grapes/design.py
remove_step(graph, name)
Interface to remove an existing node, without changing anything else.
Parameters
graph : grapes Graph The graph from which to remove the step. name : hashable (typically string) Name of the node to remove.
Raises
KeyError If the node does not exist.
Source code in grapes/design.py
evaluate
This module contains functions to evaluate the graph, i.e. call the recipes or assign conditionals to compute values of nodes.
Usually, it is unnecessary to call these functions directly, as the more convenient interface is in the util module.
evaluate_conditional(graph, conditional, continue_on_fail=False)
Evaluate a conditional node in the graph.
Parameters
graph : grapes Graph The graph containing the conditional node. conditional : hashable (typically string) The name of the conditional node to evaluate. continue_on_fail : bool, optional If True, continue evaluation even if an error occurs. Default is False.
Raises
ValueError If no condition is true or evaluation fails and continue_on_fail is False.
Source code in grapes/evaluate.py
evaluate_standard(graph, node, continue_on_fail=False)
Evaluate a standard node in the graph.
Parameters
graph : grapes Graph The graph containing the node to evaluate. node : hashable (typically string) The name of the standard node to evaluate. continue_on_fail : bool, optional If True, continue evaluation even if an error occurs. Default is False.
Raises
Exception If evaluation fails and continue_on_fail is False.
Source code in grapes/evaluate.py
evaluate_target(graph, target, continue_on_fail=False)
Evaluate a target node in the graph (any type of node).
Parameters
graph : grapes Graph The graph containing the node to evaluate. target : hashable (typically string) The name of the node to evaluate. continue_on_fail : bool, optional If True, continue evaluation even if an error occurs. Default is False.
Raises
ValueError If the node type is not supported.
Notes
If continue_on_fail is False, any error during evaluation will raise an exception.
Source code in grapes/evaluate.py
execute_to_targets(graph, *targets)
Evaluate all nodes in the graph required to reach the specified targets.
Parameters
graph : grapes Graph The graph containing the nodes to evaluate. *targets : hashables (typically strings) Names of one or more target nodes to evaluate.
Source code in grapes/evaluate.py
execute_towards_all_conditions_of_conditional(graph, conditional)
Progress towards all conditions of a specific conditional node, stopping if one is found true.
Parameters
graph : grapes Graph The graph containing the conditional node. conditional : hashable (typically string) The name of the conditional node whose conditions are to be evaluated.
Source code in grapes/evaluate.py
execute_towards_conditions(graph, *conditions)
Progress towards the specified conditions, stopping if one is found true.
Parameters
graph : grapes Graph The graph containing the conditions. *conditions : hashables (typically strings) Names of one or more (condition) nodes to evaluate.
Source code in grapes/evaluate.py
progress_towards_targets(graph, *targets)
Progress towards the specified targets by evaluating nodes, continuing on failure.
Parameters
graph : grapes Graph The graph containing the nodes to evaluate. *targets : hashables (typically strings) Names of one or more target nodes to evaluate.
Source code in grapes/evaluate.py
features
This module contains functions that manipulate the features of the nodes.
compute_topological_generation_indexes(graph)
Compute and set the topological generation indexes for all nodes in the graph.
Parameters
graph : grapes Graph The graph containing the nodes.
Source code in grapes/features.py
freeze(graph, *args)
Freeze nodes in the graph, preventing their values from being changed.
Parameters
graph : grapes Graph The graph containing the nodes. *args : hashables (typically strings) Node names to freeze. If empty, all nodes are frozen.
Source code in grapes/features.py
get_all_ancestors_target(graph, target)
Get all the ancestors of a node.
Parameters
graph : grapes Graph The graph containing the nodes. target : str The name of the target node.
Returns
set Set of ancestor node names.
Source code in grapes/features.py
get_all_conditionals(graph)
Get set of all conditional nodes in the graph.
Parameters
graph : grapes Graph The graph containing the nodes.
Returns
set Set of conditional node names.
Source code in grapes/features.py
get_all_nodes(graph, exclude_recipes=False)
Get all nodes in the graph.
Parameters
graph : grapes Graph The graph containing the nodes. exclude_recipes : bool, optional If True, exclude recipe nodes. Default is False.
Returns
set Set of node names.
Source code in grapes/features.py
get_all_recipes(graph)
Get all the recipe nodes in the graph.
Parameters
graph : grapes Graph The graph containing the nodes.
Returns
set Set of recipe node names.
Source code in grapes/features.py
get_all_sinks(graph, exclude_recipes=False)
Get all sink nodes in the graph (nodes with no outgoing edges).
Parameters
graph : grapes Graph The graph containing the nodes. exclude_recipes : bool, optional If True, exclude recipe nodes. Default is False.
Returns
set Set of sink node names.
Source code in grapes/features.py
get_all_sources(graph, exclude_recipes=False)
Get all source nodes in the graph (nodes with no incoming edges).
Parameters
graph : grapes Graph The graph containing the nodes. exclude_recipes : bool, optional If True, exclude recipe nodes. Default is False.
Returns
set Set of source node names.
Source code in grapes/features.py
get_args(graph, node)
Get the positional arguments for a node's recipe.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
list List of argument names.
Source code in grapes/features.py
get_conditions(graph, node)
Get the conditions associated with a conditional node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
list List of condition node names.
Source code in grapes/features.py
get_has_value(graph, node)
Check if a node has a value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
bool True if the node has a value, False otherwise.
Source code in grapes/features.py
get_is_frozen(graph, node)
Check if a node is frozen.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
bool True if the node is frozen, False otherwise.
Source code in grapes/features.py
get_is_recipe(graph, node)
Check if a node is a recipe node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
bool True if the node is a recipe, False otherwise.
Source code in grapes/features.py
get_kwargs(graph, node)
Get the keyword arguments for a node's recipe.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
dict Dictionary of keyword argument names and node names.
Source code in grapes/features.py
get_node_attribute(graph, node, attribute)
Get the value of a specific attribute for a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. attribute : str The attribute to retrieve.
Returns
Any The value of the attribute.
Raises
ValueError If the attribute is not present or is None.
Source code in grapes/features.py
get_possibilities(graph, node)
Get the possible outcomes associated with a conditional node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
list List of possible outcome node names.
Source code in grapes/features.py
get_recipe(graph, node)
Get the recipe associated with a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
hashable (typically string) The name of the node that acts as recipe for the passed node.
Source code in grapes/features.py
get_topological_generation_index(graph, node)
Get the topological generation index of a node. It does not compute it, just retrieves the stored value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
int The topological generation index.
Source code in grapes/features.py
get_topological_generations(graph)
Return list of topological generations of the graph.
Parameters
graph : grapes Graph The graph containing the nodes.
Returns
list List of generations, each generation is a list of node names.
Source code in grapes/features.py
get_topological_order(graph)
Return list of nodes in topological order, i.e., from dependencies to targets.
Parameters
graph : grapes Graph The graph containing the nodes.
Returns
list List of node names in topological order.
Source code in grapes/features.py
get_type(graph, node)
Get the type of a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
str The type of the node.
Source code in grapes/features.py
get_value(graph, node)
Get the value of a node if it has a value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
Any The value of the node.
Raises
ValueError If the node does not have a value.
Source code in grapes/features.py
make_recipe_dependencies_also_recipes(graph)
Make dependencies (predecessors) of recipes also recipes, if they have only recipe successors.
Parameters
graph : grapes Graph The graph containing the nodes.
Source code in grapes/features.py
set_args(graph, node, args)
Set the positional arguments for a node's recipe.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. args : list List of argument names.
Source code in grapes/features.py
set_conditions(graph, node, conditions)
Set the conditions for a conditional node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. conditions : list List of condition node names.
Source code in grapes/features.py
set_has_value(graph, node, has_value)
Set whether a node has a value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. has_value : bool Whether the node has a value.
Source code in grapes/features.py
set_is_frozen(graph, node, is_frozen)
Set whether a node is frozen.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. is_frozen : bool Whether the node is frozen.
Source code in grapes/features.py
set_is_recipe(graph, node, is_recipe)
Set whether a node is a recipe node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. is_recipe : bool Whether the node is a recipe.
Source code in grapes/features.py
set_kwargs(graph, node, kwargs)
Set the keyword arguments for a node's recipe.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. kwargs : dict Dictionary of keyword argument names and node names.
Source code in grapes/features.py
set_node_attribute(graph, node, attribute, value)
Set the value of a specific attribute for a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. attribute : str The attribute to set. value : Any The value to assign.
Source code in grapes/features.py
set_possibilities(graph, node, possibilities)
Set the possible outcomes for a conditional node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. possibilities : list List of possible outcome node names.
Source code in grapes/features.py
set_recipe(graph, node, recipe)
Set the recipe for a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. recipe : hashable (typically string) The name of the node that will act as recipe.
Source code in grapes/features.py
set_topological_generation_index(graph, node, index)
Set the topological generation index of a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. index : int The generation index to assign.
Source code in grapes/features.py
set_type(graph, node, type)
Set the type of a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. type : str The type to assign.
Source code in grapes/features.py
set_value(graph, node, value)
Set the value of a node and mark it as having a value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. value : Any The value to assign.
Source code in grapes/features.py
unfreeze(graph, *args)
Unfreeze nodes in the graph, allowing their values to be changed.
Parameters
graph : grapes Graph The graph containing the nodes. *args : hashables (typically strings) Node names to unfreeze. If empty, all nodes are unfrozen.
Source code in grapes/features.py
unset_value(graph, node)
Unset the value of a node and mark it as not having a value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Source code in grapes/features.py
function_composer
This module contains some tools to compose functions into one.
function_compose(func, subfuncs, func_dependencies, subfuncs_dependencies, func_signature, subfuncs_signatures)
Compose functions.
Parameters
func : callable External function subfuncs : list of callables List of internal functions to be composed with func. If identity_token is passed here, it is replaced by the value of the argument. func_dependencies : list of hashables Names of the arguments of func (usually they should correspond to node names in a graph). subfuncs_dependencies : list of lists of hashables Names of the arguments that are passed to the subfuncs when calling the new function (usually they should correspond to node names in a graph). func_signature : list of hashables Names of the arguments of the old func. subfuncs_signatures : list of lists of hashables Names of the arguments of the old subfuncs.
Returns
callable A new function that represents the composition of func and subfuncs.
Source code in grapes/function_composer.py
function_compose_simple(func, subfuncs, func_dependencies, subfuncs_dependencies, func_signature=None, subfuncs_signatures=None)
Compose functions, without the need to pass signatures, as they are found automatically.
Parameters
func : callable External function. subfuncs : list of callables List of internal functions to be composed with func. func_dependencies : list of hashables Names of the arguments of the new function (usually they should correspond to node names in a graph). subfuncs_dependencies : list of lists of hashables Names of the arguments that are passed to the subfuncs when calling the new function (usually they should correspond to node names in a graph). func_signature : list of hashables Names of the arguments of the old func. subfuncs_signatures : list of lists of hashables Names of the arguments of the old subfuncs.
Returns
callable A new function that represents the composition of func and subfuncs.
Raises
ValueError If func or any of the subfuncs have varargs, as they cannot be handled automatically.
Source code in grapes/function_composer.py
merge
This module contains functions to merge, split and verify the compatibility of graphs.
check_compatibility(first, second)
Check if two graphs can be composed (merged).
Parameters
first : grapes Graph The first graph. second : grapes Graph The second graph.
Returns
bool True if the graphs are compatible, False otherwise.
Source code in grapes/merge.py
check_compatibility_nodes(first_graph, first_node, second_graph, second_node)
Check if two nodes from different graphs are compatible for merging. Nodes are compatible if they have the same type, and if they have values or dependencies, those values or dependencies must be the same. However, if only one of the nodes has dependencies, they are still considered compatible.
Parameters
first_graph : grapes Graph The first graph containing the node. first_node : hashable (typically string) The name of the node in the first graph. second_graph : grapes Graph The second graph containing the node. second_node : hashable (typically string) The name of the node in the second graph.
Returns
bool True if the nodes are compatible, False otherwise.
Source code in grapes/merge.py
get_subgraph(graph, nodes)
Get a subgraph containing only the specified nodes.
Parameters
graph : grapes Graph The original graph. nodes : list or set of hashables (typically strings) The node names to include in the subgraph.
Returns
grapes Graph A new graph containing only the specified nodes.
Source code in grapes/merge.py
merge(first, second, *others)
Merge any number (>=2) of graphs into a single graph.
Parameters
first : grapes Graph The first graph. second : grapes Graph The second graph. *others : grapes Graph Any other graphs to merge.
Returns
grapes Graph The merged graph.
Raises
ValueError If any pair of graphs are not compatible.
Source code in grapes/merge.py
merge_two(first, second)
Merge two compatible graphs into a new graph.
Parameters
first : grapes Graph The first graph. second : grapes Graph The second graph.
Returns
grapes Graph The merged graph.
Raises
ValueError If the graphs are not compatible.
Source code in grapes/merge.py
path
This module contains functions to get the path needed to reach a target from valued nodes.
get_path_to_conditional(graph, conditional)
Get the path from the last valued nodes to a conditional node.
Parameters
graph : grapes Graph The graph containing the nodes. conditional : hashable (typically string) The name of the conditional node.
Returns
set Set of node names representing the path from valued nodes to the conditional node.
Source code in grapes/path.py
get_path_to_standard(graph, node)
Get the path from the last valued nodes to a standard node.
Parameters
graph : grapes Graph The graph containing the nodes. node : hashable (typically string) The name of the standard node.
Returns
set Set of node names representing the path from valued nodes to the standard node.
Source code in grapes/path.py
get_path_to_target(graph, target)
Get the path from the last valued nodes to a target node.
Parameters
graph : grapes Graph The graph containing the nodes. target : hashable (typically string) The name of the target node.
Returns
set Set of node names representing the path from valued nodes to the target.
Raises
ValueError If the node type is not supported.
Source code in grapes/path.py
reachability
This module contains functions to manipulate the reachability status of targets from valued nodes.
clear_reachabilities(graph, *args)
Clear reachabilities in the graph nodes.
Parameters
graph : grapes Graph The graph containing the nodes. *args : hashable (typically string) Node names to clear. If empty, all nodes are cleared.
Source code in grapes/reachability.py
compute_reachability_conditional(graph, conditional)
Compute the reachability of a conditional node.
Parameters
graph : grapes Graph The graph containing the node. conditional : hashable (typically string) The name of the conditional node.
Source code in grapes/reachability.py
compute_reachability_standard(graph, node)
Compute the reachability of a standard node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the standard node.
Source code in grapes/reachability.py
compute_reachability_target(graph, target)
Compute the reachability of a target node.
Parameters
graph : grapes Graph The graph containing the node. target : hashable (typically string) The name of the target node.
Raises
ValueError If the node type is not supported.
Source code in grapes/reachability.py
compute_reachability_targets(graph, *targets)
Compute the reachability of multiple target nodes.
Parameters
graph : grapes Graph The graph containing the nodes. *targets : hashables (typically strings) Node names to check reachability for.
Source code in grapes/reachability.py
get_best_reachability(graph, *nodes)
Get the best (most reachable) reachability value among a set of nodes.
Parameters
graph : grapes Graph The graph containing the nodes. *nodes : hashables (typically strings) Node names to check.
Returns
str The best reachability value ("reachable", "uncertain", or "unreachable").
Source code in grapes/reachability.py
get_has_reachability(graph, node)
Check if a node has a reachability value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
bool True if the node has a reachability value, False otherwise.
Source code in grapes/reachability.py
get_reachability(graph, node)
Get the reachability value of a node. It does not compute it, just retrieves it.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Returns
str The reachability value ("unreachable", "uncertain", or "reachable").
Raises
KeyError If the node does not have a reachability value.
Source code in grapes/reachability.py
get_worst_reachability(graph, *nodes)
Get the worst (least reachable) reachability value among a set of nodes.
Parameters
graph : grapes Graph The graph containing the nodes. *nodes : hashables (typically strings) Node names to check.
Returns
str The worst reachability value ("unreachable", "uncertain", or "reachable").
Source code in grapes/reachability.py
set_has_reachability(graph, node, has_reachability)
Set whether a node has a reachability value.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. has_reachability : bool Whether the node has a reachability value.
Source code in grapes/reachability.py
set_reachability(graph, node, reachability)
Set the reachability value of a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node. reachability : str The reachability value ("unreachable", "uncertain", or "reachable").
Raises
ValueError If the reachability value is not valid.
Source code in grapes/reachability.py
unset_reachability(graph, node)
Unset the reachability value of a node.
Parameters
graph : grapes Graph The graph containing the node. node : hashable (typically string) The name of the node.
Source code in grapes/reachability.py
simplify
This module contains functions to simplify the graph, reducing the number of nodes or converting conditional to standard nodes.
convert_all_conditionals_to_trivial_steps(graph, execute_towards_conditions=False)
Convert all conditional nodes in the graph to trivial steps.
Parameters
graph : grapes Graph The graph containing the nodes. execute_towards_conditions : bool, optional Whether to execute the graph towards the conditions until one is found true. Default is False.
Source code in grapes/simplify.py
convert_conditional_to_trivial_step(graph, conditional, execute_towards_conditions=False)
Convert a conditional node to a trivial step that returns the dependency corresponding to the true condition.
Parameters
graph : grapes Graph The graph containing the nodes. conditional : hashable (typically string) The name of the conditional node to be converted. execute_towards_conditions : bool, optional Whether to execute the graph towards the conditions until one is found true. Default is False.
Raises
ValueError If no condition is true and there is no default possibility.
Source code in grapes/simplify.py
simplify_all_dependencies(graph, node, exclude=set())
Simplify all dependencies of a node except those in the exclude set.
Parameters
graph : grapes Graph The graph containing the nodes. node : hashable (typically string) The name of the node to simplify. exclude : set or iterable, optional Dependencies to exclude from simplification. Default is empty set.
Source code in grapes/simplify.py
simplify_dependency(graph, node, dependency)
Simplify a dependency of a node by merging its computation into that of the node. For example if the graph g is a->b->c and we simplify b as a dependency of c (calling simplify_dependency(g, c, b)), the graph becomes a->c, with c now computing what b used to compute as well.
Parameters
graph : grapes Graph The graph containing the nodes. node : hashable (typically string) The name of the node that will include in its computation that of the dependency. dependency : hashable (typically string) The name of the dependency to eliminate and merge into the node.
Raises
TypeError If the dependency or its recipe is not a standard node.
Source code in grapes/simplify.py
util
This module contains utility functions for grapes. These are the main functions that an user (not designer) should use to interact with a grapes graph.
check_feasibility_of_execution(graph, context, *targets, inplace=False)
Check the feasibility of executing a graph up to the desired targets given a context.
Parameters
graph : grapes Graph Graph of the computation. context : dict Dictionary of the initial context of the computation (input). targets : hashables (typically strings) Indicator of what to compute (desired output). inplace : bool Whether to modify graph and context inplace (default: False).
Returns
feasibility : str One of "reachable", "unreachable", "uncertain". missing_dependencies : set Set of nodes that are missing in the context and prevent the computation from being feasible.
Source code in grapes/util.py
context_from_file(file_name)
Load a file (any of the supported formats) into a dictionary.
Parameters
file_name : str Path to the file.
Returns
dict Content of the file as dictionary.
Source code in grapes/util.py
context_from_json_file(file_name)
Load a json file into a dictionary.
Parameters
file_name : str Path to the json file.
Returns
dict Content of the file as dictionary.
Source code in grapes/util.py
context_from_toml_file(file_name)
Load a toml file into a dictionary.
Parameters
file_name : str Path to the toml file.
Returns
dict Content of the file as dictionary.
Source code in grapes/util.py
execute_graph_from_context(graph, context, *targets, inplace=False, check_feasibility=True)
Execute a graph up to the desired targets given a context.
Parameters
graph : grapes Graph Graph of the computation. context : dict Dictionary of the initial context of the computation (input). targets : hashables (typically strings) Indicator of what to compute (desired output). inplace : bool Whether to modify graph and context inplace (default: False). check_feasibility : bool Whether to check the feasibility of the computation, which slows performance (default: True).
Returns
grapes Graph Graph with context updated after computation.
Source code in grapes/util.py
get_execution_subgraph(graph, context, *targets)
Get the subgraph that would be executed to compute the desired targets given a context.
Parameters
graph : grapes Graph Graph of the computation. context : dict Dictionary of the initial context of the computation (input). targets : hashables (typically strings) Indicator of what to compute (desired output).
Returns
grapes Graph Subgraph that would be executed to compute the desired targets given the context.
Source code in grapes/util.py
json_from_graph(graph)
Get a JSON string representing the context of a graph.
Parameters
graph : grapes Graph Graph containing the context to convert to JSON.
Returns
str JSON string that prettily represents the context of the graph.
Source code in grapes/util.py
lambdify_graph(graph, input_keys, target, constants={})
Convert a graph into a function that can be called with the desired inputs and returns the desired output. This function is independent from the rest of grapes, and does not require any other grapes function to work.
Parameters
graph : grapes Graph Graph of the computation. input_keys : list or set of strings Keys in the graph that will be treated as inputs of the function. target : string (or name of a node in the graph) Key in the graph that will be treated as output of the function. constants : dict Keys and values that are assigned to the graph before the function creation and are present when the function is called, default: {}. If a key is both in constants and input_keys, it is treated as input (i.e., the value in constants is ignored).
Returns
function A function that can be called with the desired inputs and returns the desired output.
Source code in grapes/util.py
wrap_graph_with_function(graph, input_keys, *targets, constants={}, input_as_kwargs=True, output_as_dict=True)
Wrap a the execution of a graph into a function that can be called with the desired inputs and returns the desired outputs.
Parameters
graph : grapes Graph Graph of the computation. input_keys : hashables (typically strings) Keys in the graph that will be treated as inputs of the function. targets : hashables (typically strings) Keys in the graph that will be treated as outputs of the function. constants : dict Keys and values that are assigned to the graph before the function creation and are present when the function is called, default is empty set. If a key is both in constants and input_keys, it is treated as input (i.e., the value in constants is ignored). input_as_kwargs : bool Whether the input of the function is a set of keyword arguments (True) or a list (False), default: True. output_as_dict : bool Whether the output of the function is a dictionary (True) or a list (False), default: True.
Returns
function A function that can be called with the desired inputs and returns the desired outputs.
Source code in grapes/util.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
visualize
This module contains tools that allow the visualization of a graph using graphviz and pygraphviz.
best_text_from_background_color(r, g, b, a=1.0)
Get the best text color (white or black) for a given background color.
Parameters
r : float Red color channel in [0, 1]. g : float Green color channel in [0, 1]. b : float Blue color channel in [0, 1]. a : float, optional Alpha color channel in [0, 1]. Default is 1.0. This value is ignored.
Returns
tuple Tuple of RGBA values representing pure white (1, 1, 1, 1) or pure black (0, 0, 0, 1) depending on luminance of input color.
Source code in grapes/visualize.py
draw_to_file(graphviz_graph, filename, format='pdf', prog='dot')
Draw a pygraphviz AGraph to a file.
Parameters
graphviz_graph : pygraphviz.AGraph The graph to be drawn. filename : str The name of the file where to save the drawing. format : str, optional The format of the output file (default: "pdf"). See graphviz documentation for supported formats. prog : str, optional The layout program to use (default: "dot"). See graphviz documentation for supported programs.
Source code in grapes/visualize.py
draw_to_screen(graphviz_graph, format='png', prog='dot')
Draw a pygraphviz AGraph to an image and display it using matplotlib.
Parameters
graphviz_graph : pygraphviz.AGraph The graph to be drawn. format : str, optional The format of the output image (default: "png"). See graphviz documentation for supported formats. prog : str, optional The layout program to use (default: "dot"). See graphviz documentation for supported programs.
Source code in grapes/visualize.py
get_graphviz_digraph(graph, hide_recipes=False, pretty_names=False, include_values=False, color_mode='none', colormap='viridis', **attrs)
Create a pygraphviz AGraph from a grapes graph.
Parameters
graph : grapes Graph The graph to visualize. hide_recipes : bool, optional If True, recipe nodes are hidden. Default is False. pretty_names : bool, optional If True, node labels are prettified. Default is False. include_values : bool, optional If True, node values are included in labels. Default is False. color_mode : str, optional Coloring mode for nodes ("none", "by_generation", "sources_and_sinks", default is "none"). colormap : str, optional Name of matplotlib colormap to use. Default is "viridis". **attrs Additional attributes for the AGraph.
Returns
pygraphviz.AGraph The constructed pygraphviz AGraph.
Source code in grapes/visualize.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
hex_string_from_rgba(r, g, b, a)
Get a formatted hex string from RGBA values.
Parameters
r : float Red color channel in [0, 1]. g : float Green color channel in [0, 1]. b : float Blue color channel in [0, 1]. a : float Alpha color channel in [0, 1].
Returns
str Formatted hex string starting with # and then 8 hex characters (4 bytes).
Source code in grapes/visualize.py
prettify_label(name)
Prettify a node label by capitalizing and replacing underscores with spaces.
Parameters
name : str The original node name.
Returns
str The prettified label.
Source code in grapes/visualize.py
write_dotfile(graphviz_graph, filename)
Write a pygraphviz AGraph to a dot file.
Parameters
graphviz_graph : pygraphviz.AGraph The graph to be written. filename : str The name of the file where to save the dot file.
Source code in grapes/visualize.py
write_string(graphviz_graph)
Get the string representation of a pygraphviz AGraph.
Parameters
graphviz_graph : pygraphviz.AGraph The graph to be converted to string.
Returns
str The string representation of the graph in dot format.
Source code in grapes/visualize.py
options: show_submodules: true