robometric_frame.safety.base

Base class for robotics policy safety metrics.

Provides common functionality for trajectory validation and distance function calling patterns shared across all safety metrics.

Classes

BaseSafetyMetric(distance_fn, **kwargs)

Base class for safety metrics with trajectory validation and distance function calling.

class robometric_frame.safety.base.BaseSafetyMetric(distance_fn, **kwargs)[source]

Base class for safety metrics with trajectory validation and distance function calling.

This base class provides common functionality for safety metrics that use user-defined distance functions to evaluate trajectories against environments:

  • Trajectory validation (shape and dimensionality checks)

  • Safe calling of user-provided distance functions with error handling

  • Output validation (type, shape, and non-negativity constraints)

  • Collision detection based on distance thresholds

  • Standardized signatures for distance functions

All safety metrics use distance functions as the fundamental building block. Collision information can be derived from distances using threshold-based detection via _detect_collisions().

Subclasses should: 1. Implement metric-specific logic in update() and compute() 2. Call _compute_distances() to get validated distance values 3. Use _detect_collisions() for threshold-based collision detection

Parameters:
  • distance_fn (Callable[[Tensor, Any], Tensor]) –

    User-defined function that computes distances to obstacles. Signature: distance_fn(trajectory: Tensor, environment: Any) -> Tensor - trajectory: Shape (…, L, D) where L is trajectory length, D is spatial dims - environment: User-defined environment representation (optional) - Returns: Tensor of shape (…, L) with distances to nearest obstacle

    at each trajectory point (positive values)

  • **kwargs (Any) – Additional keyword arguments passed to the base Metric class.

Raises:

TypeError – If distance_fn is not callable.

__init__(distance_fn, **kwargs)[source]

Initialize the base safety metric.

abstract update(trajectory, environment=None)[source]

Update metric state with new trajectory data.

Subclasses must implement this method to define how to update their specific metric states.

Parameters:
  • trajectory (Tensor) – Trajectory tensor of shape (…, L, D).

  • environment (Optional[Any]) – Optional environment representation.

Return type:

None

abstract compute()[source]

Compute final metric value(s).

Subclasses must implement this method to define how to compute their final metric values from accumulated states.

Return type:

dict[str, Tensor]

Returns:

Dictionary mapping metric names to their computed values.