function
behaviour.function
What a Function record does: resolve itself to a callable, render itself as code, and answer for its own symbols.
One declared function had two runtime classes — one in classes/function that generated code, one in classes/observation that lambdified an expression — and which one a caller got depended on which module it had imported. They shared three method names and agreed on two of them; the third, render_code, meant a whole def on one and a bare expression on the other. Both are here, under names that say which is which: render_code emits the function, render_expression emits its right-hand side.
Classes
| Name | Description |
|---|---|
| FunctionBehaviour | Everything a declared function does, on both generated forms. |
| LossFunctionBehaviour | A loss function is a function with an aggregation: everything FunctionBehaviour does, plus how a per-element loss is reduced to a scalar. |
FunctionBehaviour
behaviour.function.FunctionBehaviour()Everything a declared function does, on both generated forms.
Attributes
| Name | Description |
|---|---|
| argument_names | The names of this function’s formal arguments in declared order — empty on either generated form when it declares none, which is {} on one and None on the other. |
| latex | Return LaTeX representation of the function equation. |
| metadata | Backward compatibility: return self (which is now the datamodel). |
| ontology | Access to the ontology instance if available. |
| resolved_callable | The Python callable this function records, or None when it records only an equation. |
| sympy_expression | Return the parsed SymPy expression for this function’s equation. |
Methods
| Name | Description |
|---|---|
| apply | Execute the function and call it with the given arguments. |
| execute | Compile the function into an executable callable. |
| from_datamodel | Create a Function from a generated record of one. |
| from_ontology | Create Function from an ontology instance. |
| from_python | Create Function from a Python callable. |
| get_equation | Build the function as a SymPy equation. |
| get_parameters | Return the equation’s parameters as a name-to-value mapping. |
| get_symbolic_function | Return the function as a callable SymPy Lambda. |
| plot | Plot the function’s output against its input. |
| plot_metadata_graph | Draw a graph of the function’s metadata. |
| render_code | Generate Python code for this function. |
| render_expression | Render just the function’s right-hand side as backend source code. |
| symbol_scope | The namespace this function’s equation is parsed against. |
| to_callable | Generate and execute function code, returning the callable. |
| to_jax | Generate JAX code for this function. |
| to_numpy | Generate NumPy code for this function. |
| to_python | Generate pure Python code for this function. |
apply
behaviour.function.FunctionBehaviour.apply(**kwargs)Execute the function and call it with the given arguments.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| **kwargs | Argument values passed to the compiled callable. | {} |
Returns
| Name | Type | Description |
|---|---|---|
| The result of evaluating the function. |
execute
behaviour.function.FunctionBehaviour.execute(
format='python',
fill_in_parameters=True,
parameters=None,
**kwargs,
)Compile the function into an executable callable.
Returns the recorded Python callable when one is available; otherwise lambdifies the symbolic equation for the requested backend. Supplied parameters that do not appear in the equation are discarded, and the function’s stored parameter values can optionally be substituted in before compilation.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| format | Target backend for lambdify (e.g. "python"/"numpy", "jax"); also selects the module used for numeric evaluation. |
'python' |
|
| fill_in_parameters | When True, substitute the function’s stored parameter values into the expression before compiling. |
True |
|
| parameters | Extra parameter values to substitute; entries whose symbol is absent from the equation are ignored. | None |
|
| **kwargs | Backend options; for format="jax", jit=True wraps the result in jax.jit with stepsize treated as static. |
{} |
Returns
| Name | Type | Description |
|---|---|---|
| A callable evaluating the function over its arguments. |
from_datamodel
behaviour.function.FunctionBehaviour.from_datamodel(func)Create a Function from a generated record of one.
from_ontology
behaviour.function.FunctionBehaviour.from_ontology(ontology_instance, **kwargs)Create Function from an ontology instance.
from_python
behaviour.function.FunctionBehaviour.from_python(function_instance, **kwargs)Create Function from a Python callable.
get_equation
behaviour.function.FunctionBehaviour.get_equation()Build the function as a SymPy equation.
Parses the stored right-hand-side string into an expression, treats the function’s arguments as IndexedBase symbols, and returns an equality whose left-hand side is the named function applied to its arguments.
Returns
| Name | Type | Description |
|---|---|---|
A SymPy Eq relating the function call to its parsed expression. |
get_parameters
behaviour.function.FunctionBehaviour.get_parameters(key_as_symbol=False)Return the equation’s parameters as a name-to-value mapping.
A function that declares none has none: an empty collection reads as {} on one generated form and None on the other, so both are read through here.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| key_as_symbol | When True, use SymPy Symbol objects as keys instead of plain parameter-name strings. |
False |
Returns
| Name | Type | Description |
|---|---|---|
Mapping from each parameter name (or Symbol) to its value. |
get_symbolic_function
behaviour.function.FunctionBehaviour.get_symbolic_function()Return the function as a callable SymPy Lambda.
Returns
| Name | Type | Description |
|---|---|---|
A SymPy Lambda mapping the function’s arguments to its equation. |
plot
behaviour.function.FunctionBehaviour.plot(
format='python',
plotting_kwargs=None,
**kwargs,
)Plot the function’s output against its input.
For a single-argument function, the input array (supplied via kwargs under the argument name) is plotted against the evaluated output; for multi-argument functions the output is plotted directly using the stored parameter values.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| format | Backend used to compile the function for evaluation. | 'python' |
|
| plotting_kwargs | Keyword arguments forwarded to matplotlib. |
None |
|
| **kwargs | Input values keyed by argument name. | {} |
plot_metadata_graph
behaviour.function.FunctionBehaviour.plot_metadata_graph(
ax=None,
node_kwargs=None,
edge_kwargs=None,
edge_labels=True,
)Draw a graph of the function’s metadata.
Builds a directed graph linking the function node to its equation, software requirements and arguments, then renders it with a radial layout.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| ax | Matplotlib axes to draw into; a new figure is created and returned when omitted. | None |
|
| node_kwargs | Keyword arguments forwarded to the node renderer. | None |
|
| edge_kwargs | Keyword arguments reserved for edge styling. | None |
|
| edge_labels | When True, annotate edges with their relation labels; otherwise fold the relation into the node labels. |
True |
Returns
| Name | Type | Description |
|---|---|---|
The created figure when ax is not provided, otherwise None. |
render_code
behaviour.function.FunctionBehaviour.render_code(
format='jax',
user_functions=None,
render_func=None,
)Generate Python code for this function.
Parameters
format : str Output format: ‘jax’, ‘numpy’, ‘python’ user_functions : dict, optional Custom function name mappings for the printer. Example: {‘sigmoid’: ‘sigmoid’} to preserve function name render_func : callable, optional Custom render function for model context.
Returns:
str Python code string defining the function
Examples:
func = Function.from_string(yaml_str) print(func.render_code()) def sigmoid(x): return 1/(1 + jnp.exp(-x))
render_expression
behaviour.function.FunctionBehaviour.render_expression(
format='python',
**kwargs,
)Render just the function’s right-hand side as backend source code.
render_code emits the whole def; this emits the expression alone, for a caller splicing it into code of its own.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| format | Target backend passed to the expression renderer. | 'python' |
|
| **kwargs | Additional options forwarded to the renderer. | {} |
Returns
| Name | Type | Description |
|---|---|---|
| The rendered code for the equation’s right-hand side. |
symbol_scope
behaviour.function.FunctionBehaviour.symbol_scope()The namespace this function’s equation is parsed against.
Its own parameters, plus its arguments as IndexedBase so an argument can be indexed in the body. Shared by every caller that parses this equation, so a function’s rendered graph cannot resolve a name differently from its equation.
to_callable
behaviour.function.FunctionBehaviour.to_callable(
format='jax',
user_functions=None,
namespace=None,
)Generate and execute function code, returning the callable.
Parameters
format : str Output format: ‘jax’, ‘numpy’ user_functions : dict, optional Custom function name mappings namespace : dict, optional Namespace for exec(). If None, creates one with jnp/np imports.
Returns:
callable The generated function as a callable
Examples:
func = Function.from_string(sigmoid_yaml) sigmoid = func.to_callable() sigmoid(0.0) 0.5
to_jax
behaviour.function.FunctionBehaviour.to_jax(**kwargs)Generate JAX code for this function.
to_numpy
behaviour.function.FunctionBehaviour.to_numpy(**kwargs)Generate NumPy code for this function.
to_python
behaviour.function.FunctionBehaviour.to_python(**kwargs)Generate pure Python code for this function.
LossFunctionBehaviour
behaviour.function.LossFunctionBehaviour()A loss function is a function with an aggregation: everything FunctionBehaviour does, plus how a per-element loss is reduced to a scalar.
Attributes
| Name | Description |
|---|---|
| CATEGORY | Loss functions are filed with the functions they are; the database has no category of its own for them. |
| aggregation_dimension | Return the aggregation dimension as a string (e.g., ‘node’). |
| aggregation_type | Return the aggregation type as a string (e.g., ‘mean’, ‘sum’). |
| sympy_expression | Return the parsed SymPy expression for this function’s equation. |
Methods
| Name | Description |
|---|---|
| render_code | Generate Python code for this loss function with aggregation. |
| to_callable | Generate and execute loss function code, returning the callable. |
render_code
behaviour.function.LossFunctionBehaviour.render_code(
format='jax',
user_functions=None,
inner_func_names=None,
)Generate Python code for this loss function with aggregation.
Parameters
format : str Output format: ‘jax’, ‘numpy’ user_functions : dict, optional Custom function name mappings inner_func_names : list, optional Names of inner functions that should be recognized. Example: [‘correlation’] for “1 - correlation(x, y)”
Returns:
str Python code string defining the loss function
Examples:
loss = LossFunction.from_string(loss_yaml) print(loss.render_code(inner_func_names=[‘correlation’])) def spectral_loss(sim, target): def _per_element_loss(sim, target): return 1 - correlation(sim, target) per_element_losses = jax.vmap(_per_element_loss)(sim, target) return jnp.mean(per_element_losses)
to_callable
behaviour.function.LossFunctionBehaviour.to_callable(
format='jax',
user_functions=None,
inner_func_names=None,
namespace=None,
)Generate and execute loss function code, returning the callable.
Parameters
format : str Output format: ‘jax’, ‘numpy’ user_functions : dict, optional Custom function name mappings inner_func_names : list, optional Names of inner functions to recognize namespace : dict, optional Namespace for exec(). If None, creates one with jnp/np/jax imports.
Returns:
callable The generated loss function as a callable