scenic.core.lazy_eval
Support for lazy evaluation of expressions and specifiers.
Lazy evaluation is necessary for expressions like 30 deg relative to roadDirection
where roadDirection
is a vector field and so defines a different heading at
different positions. Scenic defers evaluation of such expressions until they are used in
the definition of an object, when the required context (here, a position) is available.
This is implemented by representing lazy values as special objects which capture all
operations applied to them (in a similar way to Distribution
objects). The main class
of such objects is DelayedArgument
: in the above example, the relative to
operator returns such an object. However, since lazy values can appear as arguments to
distributions, Distribution
objects can also require lazy evaluation (prior to
sampling); therefore both of these classes derive from a common abstract class
LazilyEvaluable
.
Summary of Module Members
Functions
Dependencies which must be sampled before this value. |
|
Whether this value requires either sampling or lazy evaluation. |
|
Utility function for creating a lazily-evaluated function call. |
|
|
|
Whether the given value requires lazy evaluation. |
|
Whether this value requires sampling. |
|
Set of properties needed to evaluate the given value, if any. |
|
Wrap a Python object in a |
|
Evaluate something in the context of an object being constructed. |
Classes
Specifier arguments requiring other properties to be evaluated first. |
|
Values which may require evaluation in the context of an object being constructed. |
Member Details
- class LazilyEvaluable(requiredProps, dependencies=())[source]
Values which may require evaluation in the context of an object being constructed.
If a LazilyEvaluable specifies any properties it depends on, then it cannot be evaluated to a normal value except during the construction of an object which already has values for those properties.
- Parameters:
requiredProps – sequence of strings naming all properties which this value can depend on (formally, which must exist in the object passed as the context to
evaluateIn
).dependencies – for internal use only (see
Samplable
).
- Attributes:
_requiredProperties – set of strings as above.
- evaluateIn(context)[source]
Evaluate this value in the context of an object being constructed.
The object must define all of the properties on which this value depends.
- class DelayedArgument(requiredProps, value, _internal=False)[source]
Bases:
LazilyEvaluable
Specifier arguments requiring other properties to be evaluated first.
The value of a DelayedArgument is given by a function mapping the context (object under construction) to a value.
Note
When called from a dynamic behavior, constructors for delayed arguments return actual evaluations, not
DelayedArgument
objects. The agent running the behavior is used as the evaluation context.- Parameters:
requiredProps – see
LazilyEvaluable
.value – function taking a single argument (the context) and returning the corresponding evaluation of this object.
_internal (bool) – set to
True
for internal uses that need to suppress the exceptional handling of calls from dynamic behaviors above.
- makeDelayedFunctionCall(func, args, kwargs={})[source]
Utility function for creating a lazily-evaluated function call.
- valueInContext(value, context)[source]
Evaluate something in the context of an object being constructed.
- toLazyValue(thing)[source]
Wrap a Python object in a
DelayedArgument
if it needs lazy evaluation.