# trend()

Reach a threshold level when a step's failure rate rises above its usual rate.

Usage

[Source](https://github.com/posit-dev/pointblank/blob/main/pointblank/thresholds.py#L111-L256)

``` python
trend(
    warning=None,
    error=None,
    critical=None,
    stat="median",
    window=7,
    min_runs=3,
)
```

The levels of [`Thresholds`](../reference/Thresholds.md#pointblank.Thresholds) are fixed: a step reaches the warning level when, say, 5% of its test units fail. A step that usually has 1% failing and suddenly has 4% stays below that, although something has clearly changed. With [trend()](../reference/trend.md#pointblank.trend) as the `trend=` of the thresholds, a step also reaches a level when its fraction of failing test units rose by a set amount from what it was in earlier runs of the validation. So `pb.Thresholds(warning=0.05, trend=pb.trend(warning=0.02))` gives a warning when 5% or more of the test units fail, or when the fraction failing is 2 percentage points (or more) above its median in the last 7 runs.

The earlier runs are given to the validation with `Validate(history=)`, as for [`baseline()`](../reference/baseline.md#pointblank.baseline): a directory (or a glob pattern, or a list of paths) of results files written by [`write_results()`](../reference/write_results.md#pointblank.write_results), or a [`MultiValidate`](../reference/MultiValidate.md#pointblank.MultiValidate) object. The runs used are those of the same table (the same `tbl_name=`) that ended before this run started.

## Parameters

`warning: float | None = None`  
The rise in the fraction of failing test units that reaches the 'warning' level, as a number greater than 0 and at most 1 (`0.02` is a rise of 2 percentage points). The default, `None`, is no rise for the level (its fixed threshold still applies).

`error: float | None = None`  
The rise that reaches the 'error' level.

`critical: float | None = None`  
The rise that reaches the 'critical' level.

`stat: Literal["last", "mean", "median", "min", "max"] = "median"`  
How the failing fractions of the step in the earlier runs are summarized into the usual rate: `"median"` (the default), `"mean"`, `"min"`, `"max"`, or `"last"` (the fraction of the latest run).

`window: int | None = 7`  
The number of earlier runs to use: the last `window=` runs that have the step. The default is `7`, and `None` uses all of them. This has no effect with `stat="last"`.

`min_runs: int = 3`  
The number of earlier runs needed for a usual rate. With fewer runs than this (as in the first days of a new validation), only the fixed thresholds apply. The default is `3`.

## Returns

`Trend`  
The rises, for the `trend=` argument of [`Thresholds`](../reference/Thresholds.md#pointblank.Thresholds).

## Finding The Earlier Failure Rates

A step is found in the earlier runs by its step key, which is made from what the step checks (its method, columns, values, `pre=`, and segment) and not from its thresholds, actions, or brief. Adding a trend to the thresholds of a step, or changing it, keeps the step's history. Runs in which the step wasn't evaluated, was inactive, or had no test units aren't counted.

A trend can be part of the global thresholds of `Validate(thresholds=)`, so that every step reaches a level when its failure rate rises, or of the `thresholds=` of a single step. The validation needs a `tbl_name=` and a `history=` for the earlier runs to be found, and interrogating one without them raises an error.

When a rise reaches a level, the report has a note on the step with the fraction failing and the usual rate it was compared with. The level is reached as with a fixed threshold: actions for the level are run, and the level counts in `assert_below_threshold()` and in the reports of [`MultiValidate`](../reference/MultiValidate.md#pointblank.MultiValidate) and `pb report`.

## Examples

Let's say that an `orders` table is validated every day, and that about 1% of its rows have no email address. Here are a week of runs with the check of the `email` column:

``` python
import pointblank as pb
import polars as pl


def orders_with(n_missing):
    return pl.DataFrame({"email": [None] * n_missing + ["a@example.com"] * (500 - n_missing)})


runs = [
    pb.Validate(data=orders_with(n), tbl_name="orders")
    .col_vals_not_null(columns="email")
    .interrogate()
    for n in [5, 4, 6, 5, 5, 7, 4]
]

history = pb.MultiValidate(*runs)
```

Today, 20 of the 500 rows (4%) have no email address. That's below a fixed warning threshold of 5%, but it's a rise of 3 percentage points from the usual rate:

``` python
validation = (
    pb.Validate(
        data=orders_with(20),
        tbl_name="orders",
        history=history,
        thresholds=pb.Thresholds(warning=0.05, trend=pb.trend(warning=0.02)),
    )
    .col_vals_not_null(columns="email")
    .interrogate()
)

validation
```

[TABLE]

The step reached the warning level, and the note below the table says that the fraction failing rose from 1% (the median of the last 7 runs) to 4%. Without the trend, the step would have passed below every threshold.

## See Also

[Thresholds](../reference/Thresholds.md#pointblank.Thresholds)  
The fixed thresholds, which take a trend as `trend=`.

[baseline()](../reference/baseline.md#pointblank.baseline)  
Compare what a step measures with earlier runs instead.

[Validate](../reference/Validate.md#pointblank.Validate)  
The `history=` argument, which gives the earlier runs.
