CustomCallable#
- class CustomCallable(function, vocabulary, role: str, owner: str)#
Bases:
objectA user-supplied function bound to a fixed keyword vocabulary.
Both
CustomDynamicModelandCustomMeasurementlet a script hand Scarabaeus a plain Python function. To keep those functions readable, they are invoked with keyword arguments and declare only the context entries they actually need. This class performs the binding once, at construction, so a misspelt argument is reported immediately rather than on the first evaluation deep inside an integrator or a partials loop.It also carries the central finite-difference helpers used to fill in any Jacobian the user did not supply analytically, so the two model families cannot drift apart in how they differentiate.
- Parameters:
- functioncallable
User-supplied function.
- vocabularytuple of str
Context names the consumer is able to provide.
- rolestr
Human-readable description of what the function is for, used in error messages (e.g.
"acceleration").- ownerstr
Name of the model that owns the function, used in error messages.
- Attributes:
Methods
__call__(context)Invoke the user function with the subset of context it declared.
default_step(values, relative)Return a per-component central-difference step.
finite_difference(evaluate, values, steps, ...)Central-difference Jacobian of evaluate with respect to values.
reads(name)Report whether this callable can see name.
- Raises:
- CustomCallable.SignatureError
If function is not callable, uses
*args, or declares an argument outside vocabulary.
See also
scarabaeus.CustomDynamicModelUser-defined force model.
scarabaeus.CustomMeasurementUser-defined measurement model.
- static default_step(values: ndarray, relative: float) ndarray#
Return a per-component central-difference step.
- Parameters:
- valuesnumpy.ndarray
Point the derivative is taken at.
- relativefloat
Relative step size.
- Returns:
- numpy.ndarray
relative * max(|values|, 1), elementwise.
- static finite_difference(evaluate, values: ndarray, steps, n_rows: int)#
Central-difference Jacobian of evaluate with respect to values.
- Parameters:
- evaluatecallable
Takes a perturbed copy of values and returns an
(n_rows,)array.- valuesnumpy.ndarray
Point the Jacobian is evaluated at, shape
(n_cols,).- stepsnumpy.ndarray
Per-component absolute step sizes, shape
(n_cols,).- n_rowsint
Number of rows of the resulting Jacobian.
- Returns:
- numpy.ndarray
Jacobian of shape
(n_rows, n_cols).
- reads(name: str) bool#
Report whether this callable can see name.
A callable that takes the whole state (or
**kwargs) could read the quantity from there, so it counts. Used to decide, without evaluating anything, whether a Jacobian block is structurally present.- Parameters:
- namestr
Context name, e.g.
"velocity".
- Returns:
- bool
Truewhen the callable declares name or a container holding it.
- property function#
The wrapped user function.
- exception SignatureError#
Bases:
ValueErrorRaised when a user callable declares an argument that is not supplied.