sklink.utils.estimator_checks
Checks that every relational estimator must pass.
These checks complement scikit-learn's check_estimator (which covers flat mode, database=None) with the contracts that only exist when an estimator reads a 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 / rebind, thread safety and error messages without data values.
How the checks build their data
The estimator passed in must be configured for relational mode: database is a Database and entity names one of its tables. Every check works on a clone and builds its own inputs from that configuration:
Xholds only the key columns (and the cutoff column whentimeis set), one row per row of the entity table, in key order. With column names (key=Noneor names)Xis a pandas DataFrame, or a Polars one if pandas is missing; with integer positions it is a NumPy array, and the positions ofkeyandtimemust cover0 .. n_columns - 1.- Cutoffs, in temporal mode, alternate between the 50th and 80th percentiles of all the values of the declared time columns.
yis a deterministic function of the entity row (its position modulo 2 for classifiers, modulo 3 for regressors); transformers are fitted withouty.- Modified databases (shuffled rows, extra unreachable entities, rows after every cutoff, no time columns) are new
Databaseobjects with the same declarations asestimator.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: Check that an output is the same alone, in a subset or reordered (LEAK-03).
- check_clone_shares_database:
clone,copyanddeepcopykeep the sameDatabase(DB-14). - check_flat_mode: With
database=Nonethe estimator is a plain tabular one (DATA-07). - check_future_rows_ignored: Rows after every cutoff change nothing in temporal mode (VIS-03).
- check_input_equivalence: pandas, Polars and positional
ndarrayinputs give the same (DATA-08). - check_no_data_in_errors: Error messages name tables and columns, never data values (API-07).
- check_one_prediction_per_row: One output row per row of
X, in order, duplicates kept (DATA-04). - check_pickle_detach_rebind: Pickle,
detachandrebindkeep outputs identical (DB-18 to DB-20). - check_refit_resets_state: Check that a second
fitdiscards what the first one learned (SK-03). - check_relational_estimator: Run every relational check on a configured estimator.
- check_row_order_invariance: Shuffling the rows of every table changes nothing (DET-02).
- check_static_database: Without any date column the estimator works and warns nothing (TIME-02).
- check_threadsafe_predict: Check that a fitted estimator evaluates concurrently from 8 threads (PAR-02).
- check_unknown_entity: Keys missing from the entity table follow
handle_unknown_entity(ENG-03). - check_unreachable_rows_ignored: Rows unreachable from the training samples change nothing (LEAK-01).
- parametrize_with_relational_checks: Pytest decorator that parametrizes a test over estimators and checks.
- relational_checks: List the relational checks, in a stable order.
sklink.utils.estimator_checks.check_batch_independence
check_batch_independence(estimator: Any) -> NoneCheck that an output is the same alone, in a subset or reordered (LEAK-03).
sklink.utils.estimator_checks.check_clone_shares_database
check_clone_shares_database(estimator: Any) -> Noneclone, 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
check_flat_mode(estimator: Any) -> NoneWith 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
check_future_rows_ignored(estimator: Any) -> NoneRows 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
check_input_equivalence(estimator: Any) -> Nonepandas, 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
check_no_data_in_errors(estimator: Any) -> NoneError 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
check_one_prediction_per_row(estimator: Any) -> NoneOne output row per row of X, in order, duplicates kept (DATA-04).
sklink.utils.estimator_checks.check_pickle_detach_rebind
check_pickle_detach_rebind(estimator: Any) -> NonePickle, detach and rebind keep outputs identical (DB-18 to DB-20).
sklink.utils.estimator_checks.check_refit_resets_state
check_refit_resets_state(estimator: Any) -> NoneCheck 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
check_relational_estimator(estimator: Any, *, expected_failed_checks: Mapping[str, str] | None = None) -> NoneRun every relational check on a configured estimator.
Parameters:
- estimator (
estimator): Configured for relational mode:databaseis aDatabaseandentityone of its tables. See the module documentation for howXandyare 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: From the first check that fails; the note names the check.
Examples:
>>> 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
check_row_order_invariance(estimator: Any) -> NoneShuffling the rows of every table changes nothing (DET-02).
sklink.utils.estimator_checks.check_static_database
check_static_database(estimator: Any) -> NoneWithout 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
check_threadsafe_predict(estimator: Any) -> NoneCheck that a fitted estimator evaluates concurrently from 8 threads (PAR-02).
sklink.utils.estimator_checks.check_unknown_entity
check_unknown_entity(estimator: Any) -> NoneKeys missing from the entity table follow handle_unknown_entity (ENG-03).
"error" raises 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
check_unreachable_rows_ignored(estimator: Any) -> NoneRows 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
parametrize_with_relational_checks(estimators: Iterable[Any], *, expected_failed_checks: Callable[[Any], Mapping[str, str]] | None = None) -> AnyPytest 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): Called with each estimator, returns{check_name: reason}; those checks are markedxfail(strict=True).
Returns:
- decorator (
parametrize):
Examples:
>>> 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
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_ignoredis left out whenestimator.timeisNone.
Returns:
- checks (
list of callable): Each takes the configured estimator and raisesAssertionErrorwhen the contract is broken. Its__name__is stable.