---
url: /stable/api/sklink.utils.estimator_checks.md
description: >-
  API reference of sklink.utils.estimator_checks. Checks that every relational
  estimator must pass.
---

::: v-pre

# `sklink.utils.estimator_checks` {#sklink.utils.estimator_checks}

Checks that every relational estimator must pass.

These checks complement scikit-learn's
[`check_estimator`](https://scikit-learn.org/stable/modules/generated/sklearn.utils.estimator_checks.check_estimator.html) (which covers flat mode,
`database=None`) with the contracts that only exist
when an estimator reads a [`Database`](./sklink#sklink.Database): one output row per input
row, no dependence on the batch or on the physical order of the tables, no
leakage from unreachable or future rows, persistence with
[`detach`](./sklink#sklink.detach) / [`rebind`](./sklink#sklink.rebind), thread safety and error
messages without data values.

The estimator passed in must be configured for relational mode: `database`
is a [`Database`](./sklink#sklink.Database) and `entity` names one of its tables. Every
check works on a clone and builds its own inputs from that configuration:

* `X` holds only the key columns (and the cutoff column when `time` is
  set), one row per row of the entity table, in key order. With column names
  (`key=None` or names) `X` is a pandas DataFrame, or a Polars one if
  pandas is missing; with integer positions it is a NumPy array, and the
  positions of `key` and `time` must cover `0 .. n_columns - 1`.
* Cutoffs, in temporal mode, alternate between the 50th and 80th percentiles
  of all the values of the declared time columns.
* `y` is a deterministic function of the entity row (its position modulo 2
  for classifiers, modulo 3 for regressors); transformers are fitted without
  `y`.
* Modified databases (shuffled rows, extra unreachable entities, rows after
  every cutoff, no time columns) are new [`Database`](./sklink#sklink.Database) objects with
  the same declarations as `estimator.database`.

The outputs compared are those of `transform`, `predict`,
`predict_proba` and `decision_function`, whichever the estimator has.

Each check is a function of the estimator with a stable name, so that
`expected_failed_checks` can refer to it.

**Functions:**

* [**check_batch_independence**](#sklink.utils.estimator_checks.check_batch_independence): Check that an output is the same alone, in a subset or reordered (LEAK-03).
* [**check_clone_shares_database**](#sklink.utils.estimator_checks.check_clone_shares_database): `clone`, `copy` and `deepcopy` keep the same `Database` (DB-14).
* [**check_flat_mode**](#sklink.utils.estimator_checks.check_flat_mode): With `database=None` the estimator is a plain tabular one (DATA-07).
* [**check_future_rows_ignored**](#sklink.utils.estimator_checks.check_future_rows_ignored): Rows after every cutoff change nothing in temporal mode (VIS-03).
* [**check_input_equivalence**](#sklink.utils.estimator_checks.check_input_equivalence): pandas, Polars and positional `ndarray` inputs give the same (DATA-08).
* [**check_no_data_in_errors**](#sklink.utils.estimator_checks.check_no_data_in_errors): Error messages name tables and columns, never data values (API-07).
* [**check_one_prediction_per_row**](#sklink.utils.estimator_checks.check_one_prediction_per_row): One output row per row of `X`, in order, duplicates kept (DATA-04).
* [**check_pickle_detach_rebind**](#sklink.utils.estimator_checks.check_pickle_detach_rebind): Pickle, `detach` and `rebind` keep outputs identical (DB-18 to DB-20).
* [**check_refit_resets_state**](#sklink.utils.estimator_checks.check_refit_resets_state): Check that a second `fit` discards what the first one learned (SK-03).
* [**check_relational_estimator**](#sklink.utils.estimator_checks.check_relational_estimator): Run every relational check on a configured estimator.
* [**check_row_order_invariance**](#sklink.utils.estimator_checks.check_row_order_invariance): Shuffling the rows of every table changes nothing (DET-02).
* [**check_static_database**](#sklink.utils.estimator_checks.check_static_database): Without any date column the estimator works and warns nothing (TIME-02).
* [**check_threadsafe_predict**](#sklink.utils.estimator_checks.check_threadsafe_predict): Check that a fitted estimator evaluates concurrently from 8 threads (PAR-02).
* [**check_unknown_entity**](#sklink.utils.estimator_checks.check_unknown_entity): Keys missing from the entity table follow `handle_unknown_entity` (ENG-03).
* [**check_unreachable_rows_ignored**](#sklink.utils.estimator_checks.check_unreachable_rows_ignored): Rows unreachable from the training samples change nothing (LEAK-01).
* [**parametrize_with_relational_checks**](#sklink.utils.estimator_checks.parametrize_with_relational_checks): Pytest decorator that parametrizes a test over estimators and checks.
* [**relational_checks**](#sklink.utils.estimator_checks.relational_checks): List the relational checks, in a stable order.

## `sklink.utils.estimator_checks.check_batch_independence` {#sklink.utils.estimator_checks.check_batch_independence}

```python
check_batch_independence(estimator: Any) -> None
```

Check that an output is the same alone, in a subset or reordered (LEAK-03).

## `sklink.utils.estimator_checks.check_clone_shares_database` {#sklink.utils.estimator_checks.check_clone_shares_database}

```python
check_clone_shares_database(estimator: Any) -> None
```

`clone`, `copy` and `deepcopy` keep the same `Database` (DB-14).

Also checks that no table is copied while cloning, fitted or not.

## `sklink.utils.estimator_checks.check_flat_mode` {#sklink.utils.estimator_checks.check_flat_mode}

```python
check_flat_mode(estimator: Any) -> None
```

With `database=None` the estimator is a plain tabular one (DATA-07).

The full scikit-learn suite runs in the contract tests; this check makes
sure the relational configuration can be switched off.

## `sklink.utils.estimator_checks.check_future_rows_ignored` {#sklink.utils.estimator_checks.check_future_rows_ignored}

```python
check_future_rows_ignored(estimator: Any) -> None
```

Rows after every cutoff change nothing in temporal mode (VIS-03).

Only for estimators with `time` set; otherwise `unittest.SkipTest`.

## `sklink.utils.estimator_checks.check_input_equivalence` {#sklink.utils.estimator_checks.check_input_equivalence}

```python
check_input_equivalence(estimator: Any) -> None
```

pandas, Polars and positional `ndarray` inputs give the same (DATA-08).

Every available input kind is fitted and evaluated on its own; then an
estimator fitted on a DataFrame evaluates a NumPy array with the same
columns, resolved by position.

## `sklink.utils.estimator_checks.check_no_data_in_errors` {#sklink.utils.estimator_checks.check_no_data_in_errors}

```python
check_no_data_in_errors(estimator: Any) -> None
```

Error messages name tables and columns, never data values (API-07).

Errors are provoked on a database whose category labels are recognizable
secrets: an unknown key, a missing key, a detached model and a rebind to a
database that lacks the tables the model reads.

## `sklink.utils.estimator_checks.check_one_prediction_per_row` {#sklink.utils.estimator_checks.check_one_prediction_per_row}

```python
check_one_prediction_per_row(estimator: Any) -> None
```

One output row per row of `X`, in order, duplicates kept (DATA-04).

## `sklink.utils.estimator_checks.check_pickle_detach_rebind` {#sklink.utils.estimator_checks.check_pickle_detach_rebind}

```python
check_pickle_detach_rebind(estimator: Any) -> None
```

Pickle, `detach` and `rebind` keep outputs identical (DB-18 to DB-20).

## `sklink.utils.estimator_checks.check_refit_resets_state` {#sklink.utils.estimator_checks.check_refit_resets_state}

```python
check_refit_resets_state(estimator: Any) -> None
```

Check that a second `fit` discards what the first one learned (SK-03).

The first `fit` uses a larger database with more entities and unseen
categories; after the second `fit` the estimator must equal a fresh one.

## `sklink.utils.estimator_checks.check_relational_estimator` {#sklink.utils.estimator_checks.check_relational_estimator}

```python
check_relational_estimator(estimator: Any, *, expected_failed_checks: Mapping[str, str] | None = None) -> None
```

Run every relational check on a configured estimator.

**Parameters:**

* **estimator** (estimator): Configured for relational mode: `database` is a
  [`Database`](./sklink#sklink.Database) and `entity` one of its tables. See the
  module documentation for how `X` and `y` are built.
* **expected_failed_checks** (dict of str to str): Checks that are known not to apply, by name, with the reason. They are
  not run.

**Raises:**

* [AssertionError](https://docs.python.org/3/library/exceptions.html#AssertionError): From the first check that fails; the note names the check.

**Examples:**

```pycon
>>> from sklink.datasets import load_shop
>>> from sklink.feature_extraction import RelationalFeaturizer
>>> from sklink.utils.estimator_checks import check_relational_estimator
>>> database = load_shop().database
>>> check_relational_estimator(
...     RelationalFeaturizer(database, entity="customers", key=0)
... )
```

## `sklink.utils.estimator_checks.check_row_order_invariance` {#sklink.utils.estimator_checks.check_row_order_invariance}

```python
check_row_order_invariance(estimator: Any) -> None
```

Shuffling the rows of every table changes nothing (DET-02).

## `sklink.utils.estimator_checks.check_static_database` {#sklink.utils.estimator_checks.check_static_database}

```python
check_static_database(estimator: Any) -> None
```

Without any date column the estimator works and warns nothing (TIME-02).

The database is rebuilt without time and datetime columns and the
estimator is set to static mode (`time=None`); fitting, evaluating and
batch independence are checked there with every warning turned into an
error. (Duplicated samples have their own check.)

## `sklink.utils.estimator_checks.check_threadsafe_predict` {#sklink.utils.estimator_checks.check_threadsafe_predict}

```python
check_threadsafe_predict(estimator: Any) -> None
```

Check that a fitted estimator evaluates concurrently from 8 threads (PAR-02).

## `sklink.utils.estimator_checks.check_unknown_entity` {#sklink.utils.estimator_checks.check_unknown_entity}

```python
check_unknown_entity(estimator: Any) -> None
```

Keys missing from the entity table follow `handle_unknown_entity` (ENG-03).

`"error"` raises [`UnknownEntityError`](./sklink.exceptions#sklink.exceptions.UnknownEntityError) in
`fit` and when evaluating, without the key value in the message;
`"empty"` returns one row per sample and leaves the other samples
unchanged.

## `sklink.utils.estimator_checks.check_unreachable_rows_ignored` {#sklink.utils.estimator_checks.check_unreachable_rows_ignored}

```python
check_unreachable_rows_ignored(estimator: Any) -> None
```

Rows unreachable from the training samples change nothing (LEAK-01).

New entities, with child rows that carry unseen categories and numbers,
are added to the database. They are not in `X`, so the fitted model
(catalog, vocabularies, thresholds) and its outputs must not change.

## `sklink.utils.estimator_checks.parametrize_with_relational_checks` {#sklink.utils.estimator_checks.parametrize_with_relational_checks}

```python
parametrize_with_relational_checks(estimators: Iterable[Any], *, expected_failed_checks: Callable[[Any], Mapping[str, str]] | None = None) -> Any
```

Pytest decorator that parametrizes a test over estimators and checks.

The decorated test receives `estimator` and `check` and calls
`check(estimator)`. Ids are deterministic (class, non-default parameters
and check name), so the suite can run with pytest-xdist.

**Parameters:**

* **estimators** (iterable of estimators): Configured relational estimators.
* **expected_failed_checks** ([callable](https://docs.python.org/3/library/functions.html#callable)): Called with each estimator, returns `{check_name: reason}`; those
  checks are marked `xfail(strict=True)`.

**Returns:**

* **decorator** (parametrize):

**Examples:**

```pycon
>>> from sklink.datasets import load_shop
>>> from sklink.feature_extraction import RelationalFeaturizer
>>> from sklink.utils.estimator_checks import parametrize_with_relational_checks
>>> database = load_shop().database
>>> @parametrize_with_relational_checks(
...     [RelationalFeaturizer(database, entity="customers", key=0)]
... )
... def test_relational(estimator, check):
...     check(estimator)
```

## `sklink.utils.estimator_checks.relational_checks` {#sklink.utils.estimator_checks.relational_checks}

```python
relational_checks(estimator: Any = None) -> list[Callable[[Any], None]]
```

List the relational checks, in a stable order.

**Parameters:**

* **estimator** (estimator): If given, only the checks that apply to it: `check_future_rows_ignored`
  is left out when `estimator.time` is `None`.

**Returns:**

* **checks** (list of callable): Each takes the configured estimator and raises `AssertionError` when
  the contract is broken. Its `__name__` is stable.

:::
