# baseline()

Use a value from earlier runs of the validation as the expected value of a step.

Usage

[Source](https://github.com/posit-dev/pointblank/blob/main/pointblank/_baseline.py#L169-L307)

``` python
baseline(
    stat="last",
    window=None,
    min_runs=1,
)
```

A step that measures something about the table (its row count, its column count, the fraction of Null values in a column, or the sum, average, or standard deviation of a column) usually compares the measure with a fixed value. With [baseline()](../reference/baseline.md#pointblank.baseline) in place of that value, the step compares the measure with what it was in earlier runs instead: "the row count is within 10% of the last run's", or "the sum of `amount` is within 20% of the median of the last 7 runs".

The earlier runs are given to the validation with `Validate(history=)`, as 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 as a [`MultiValidate`](../reference/MultiValidate.md#pointblank.MultiValidate) object. At interrogation, the baseline is resolved to a number from the runs of the same table (the same `tbl_name=`) that ended before this run started, and the step then runs as if that number had been given: `tol=` and the comparison apply as they always do. So `col_sum_ge(columns="amount", value=pb.baseline(), tol=0.05)` checks that the sum didn't fall more than 5% below that of the last run.

[baseline()](../reference/baseline.md#pointblank.baseline) can be used in these places:

- `count=` of [`row_count_match()`](../reference/Validate.row_count_match.md#pointblank.Validate.row_count_match) and [`col_count_match()`](../reference/Validate.col_count_match.md#pointblank.Validate.col_count_match)
- `p=` of [`col_pct_null()`](../reference/Validate.col_pct_null.md#pointblank.Validate.col_pct_null)
- `value=` of the aggregate methods, like [`col_sum_eq()`](../reference/Validate.col_sum_eq.md#pointblank.Validate.col_sum_eq), [`col_avg_gt()`](../reference/Validate.col_avg_gt.md#pointblank.Validate.col_avg_gt), and [`col_sd_le()`](../reference/Validate.col_sd_le.md#pointblank.Validate.col_sd_le)

## Parameters

`stat: Literal["last", "mean", "median", "min", "max"] = "last"`  
How the earlier values of the measure are summarized: `"last"` (the default) takes the value of the latest run, and `"mean"`, `"median"`, `"min"`, and `"max"` take the mean, median, lowest, or highest value of the runs in the window.

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

`min_runs: int = 1`  
The number of earlier runs needed for a baseline. With fewer runs than this (as on the first run of a new validation), there's no baseline to compare with, so the step passes and the report has a note saying so. The default is `1`.

## Returns

`Baseline`  
A marker for the expected value, which is resolved when the validation is interrogated.

## Finding The Earlier Values

The values of a measure are found in the earlier runs by a key made from what is measured: the measure (e.g., the row count, or the sum), the column, and the step's `pre=` and segment. The value being compared with, the tolerance, and the comparison aren't part of the key, so the history of a measure isn't lost when the check around it changes. A `row_count_match()` step with `count=1000` in earlier runs and `count=pb.baseline()` now shares the history of the row count, and [col_sum_eq()](../reference/Validate.col_sum_eq.md#pointblank.Validate.col_sum_eq) and [col_sum_ge()](../reference/Validate.col_sum_ge.md#pointblank.Validate.col_sum_ge) steps on the same column share the history of the sum.

Runs that don't have the measure aren't counted: those without a step for it, those whose step couldn't be evaluated, and those written by versions of Pointblank before measures were kept in results files. A step that uses a baseline raises an error at interrogation if the validation has no `tbl_name=` or no `history=`, since the earlier runs couldn't be found without them.

## Examples

Let's say that an `orders` table is validated every day. Here are three days of runs, each checking the table's row count against a fixed value:

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


def orders_on(n_rows):
    return pl.DataFrame({"amount": [20.0] * n_rows})


runs = [
    pb.Validate(data=orders_on(n), tbl_name="orders").row_count_match(count=100).interrogate()
    for n in [98, 104, 101]
]

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

Today's table has 82 rows. Rather than a fixed count, the step can compare the row count with that of the last run, allowing 10% either way:

``` python
validation = (
    pb.Validate(data=orders_on(82), tbl_name="orders", history=history)
    .row_count_match(count=pb.baseline(), tol=0.1)
    .interrogate()
)

validation
```

[TABLE]

The last run had 101 rows, which is the expected value shown in the report, and 82 rows is more than 10% fewer, so the step fails. The note below the table says where the baseline came from.

A baseline can also summarize several runs. Here, the sum of `amount` is compared with the median of the last three runs, which is steadier than the value of any one of them:

``` python
validation = (
    pb.Validate(data=orders_on(82), tbl_name="orders", history=history)
    .col_sum_eq(columns="amount", value=pb.baseline(stat="median", window=3), tol=0.2)
    .interrogate()
)

validation
```

[TABLE]

None of the earlier runs had a step for the sum of `amount`, so there's no baseline yet. The step passes, with a note saying so, and the sum it measured becomes part of the history once this run's results are kept (with [write_results()](../reference/write_results.md#pointblank.write_results), in a directory that's given to `history=` next time).

## See Also

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

[write_results()](../reference/write_results.md#pointblank.write_results)  
Keep the results of a run for later runs to compare with.

[ref()](../reference/ref.md#pointblank.ref)  
Compare with a column of reference data instead.
