---
title: metric_policy
---

<div class="python-reference">
<a id="fastvideo.performance.metric_policy-functions"></a>
<a id="fastvideo.performance.metric_policy"></a>
<p>Metric policy for rolling performance baseline comparisons.</p>
<h2 id="fastvideo.performance.metric_policy.MetricPolicy"><code>fastvideo.performance.metric_policy.MetricPolicy</code></h2>
<pre class="doc-signature"><code>MetricPolicy(key: str, label: str, precision: int, lower_is_better: bool, threshold_percent: float, threshold_absolute: float, gated: bool = True)</code></pre>


<details><summary>Source</summary><pre><code>@dataclass(frozen=True)
class MetricPolicy:
    key: str
    label: str
    precision: int
    lower_is_better: bool
    threshold_percent: float
    threshold_absolute: float
    gated: bool = True</code></pre></details>
<h2 id="fastvideo.performance.metric_policy.MetricDelta"><code>fastvideo.performance.metric_policy.MetricDelta</code></h2>
<pre class="doc-signature"><code>MetricDelta(absolute: float, percent: float, threshold_exceeded: bool, regressed: bool)</code></pre>


<details><summary>Source</summary><pre><code>@dataclass(frozen=True)
class MetricDelta:
    absolute: float
    percent: float
    threshold_exceeded: bool
    regressed: bool</code></pre></details>
<h2 id="fastvideo.performance.metric_policy.resolve_metric_policies"><code>fastvideo.performance.metric_policy.resolve_metric_policies</code></h2>
<pre class="doc-signature"><code>resolve_metric_policies(threshold_overrides: Mapping&#91;str, Any&#93; | None) -&gt; tuple&#91;MetricPolicy, ...&#93;</code></pre>
<p>Return default metric policies with optional per-metric overrides.</p>
<details><summary>Source</summary><pre><code>def resolve_metric_policies(threshold_overrides: Mapping&#91;str, Any&#93; | None, ) -&gt; tuple&#91;MetricPolicy, ...&#93;:
    &quot;&quot;&quot;Return default metric policies with optional per-metric overrides.&quot;&quot;&quot;

    if not isinstance(threshold_overrides, Mapping):
        threshold_overrides = &#123;&#125;
    policies: list&#91;MetricPolicy&#93; = &#91;&#93;
    for base_policy in DEFAULT_METRIC_POLICIES:
        raw_override = threshold_overrides.get(base_policy.key, &#123;&#125;)
        if not isinstance(raw_override, Mapping):
            raw_override = &#123;&#125;

        threshold_percent = _optional_float(raw_override.get(&quot;threshold_percent&quot;))
        threshold_absolute = _optional_float(raw_override.get(&quot;threshold_absolute&quot;))
        gated = _optional_bool(raw_override.get(&quot;gated&quot;))

        policies.append(
            MetricPolicy(
                key=base_policy.key,
                label=base_policy.label,
                precision=base_policy.precision,
                lower_is_better=base_policy.lower_is_better,
                threshold_percent=(base_policy.threshold_percent if threshold_percent is None else threshold_percent),
                threshold_absolute=(base_policy.threshold_absolute
                                    if threshold_absolute is None else threshold_absolute),
                gated=base_policy.gated if gated is None else gated,
            ))
    return tuple(policies)</code></pre></details>
<h2 id="fastvideo.performance.metric_policy.serialize_metric_thresholds"><code>fastvideo.performance.metric_policy.serialize_metric_thresholds</code></h2>
<pre class="doc-signature"><code>serialize_metric_thresholds(policies: tuple&#91;MetricPolicy, ...&#93;) -&gt; dict&#91;str, dict&#91;str, float | bool&#93;&#93;</code></pre>

<details><summary>Source</summary><pre><code>def serialize_metric_thresholds(policies: tuple&#91;MetricPolicy, ...&#93;, ) -&gt; dict&#91;str, dict&#91;str, float | bool&#93;&#93;:
    return &#123;
        policy.key: &#123;
            &quot;threshold_percent&quot;: policy.threshold_percent,
            &quot;threshold_absolute&quot;: policy.threshold_absolute,
            &quot;gated&quot;: policy.gated,
        &#125;
        for policy in policies
    &#125;</code></pre></details>
<h2 id="fastvideo.performance.metric_policy.regression_delta"><code>fastvideo.performance.metric_policy.regression_delta</code></h2>
<pre class="doc-signature"><code>regression_delta(policy: MetricPolicy, current: float, baseline: float) -&gt; MetricDelta | None</code></pre>

<details><summary>Source</summary><pre><code>def regression_delta(
    policy: MetricPolicy,
    current: float,
    baseline: float,
) -&gt; MetricDelta | None:
    if baseline &lt;= 0:
        return None
    absolute_delta = current - baseline if policy.lower_is_better else baseline - current
    percent_delta = absolute_delta / baseline
    threshold_exceeded = (percent_delta &gt; policy.threshold_percent and absolute_delta &gt; policy.threshold_absolute)
    return MetricDelta(
        absolute=absolute_delta,
        percent=percent_delta,
        threshold_exceeded=threshold_exceeded,
        regressed=policy.gated and threshold_exceeded,
    )</code></pre></details>
</div>
