Trait Metric

Source
pub trait Metric<Input> {
    type Output;

    // Required method
    fn observe_opt(&mut self, x: Input) -> Option<Self::Output>;

    // Provided method
    fn observe(&mut self, x: Input) -> f64
       where Self::Output: Into<f64> { ... }
}
Expand description

A common interface accross metrics, with an invaluable Metric::observe method.

§Examples and Usage:

See crate::metrics for examples.

Required Associated Types§

Source

type Output

The type of elements recorded by the metric. For a delta of last vs current, this might be an n-dimentional vector. It typically needs to be an Owned type, rather than a reference.

For a moving average of gradient magnitudes, T would be f64, as only the norm value is being collected.

Required Methods§

Source

fn observe_opt(&mut self, x: Input) -> Option<Self::Output>

Similar to observe except None is returned instead of f64::NAN to indicate not enough data collected. Useful if you like

observe_opt(x).is_some_and( |m| m < 0.00001 )

style coding.

Provided Methods§

Source

fn observe(&mut self, x: Input) -> f64
where Self::Output: Into<f64>,

Records the value, and return the calculated metric. If there are not enough samples to calculate the metric, f64::NAN is returned, which will always compare false. So a tolerance check observe(x) < 0.0001 will fail until enough samples have been collected. If you don’t like NANs, then Metric::observe_opt is your friend.

Implementors§