lenskit.testing#

LensKit test harnesses and utilities.

This package contains utility code for testing LensKit and its components, including in derived packages. It relies on PyTest and Hypothesis.

Functions

have_memory(gb)

Check if we have at least gb gigs of memory.

integer_ids()

Hypothesis strategy to generate valid integer user/item IDs.

set_env_var(var, val)

Set an environment variable & restore it.

lenskit.testing.scored_lists(*, n=integers(min_value=0, max_value=1000), scores=None)#

Hypothesis generator that produces scored lists.

Parameters:
Return type:

SearchStrategy[ItemList]

lenskit.testing.ml_100k()#

Fixture to load the MovieLens 100K dataset (currently as a data frame). It skips the test if the ML100K data is not available.

Return type:

Generator[DataFrame, None, None]

lenskit.testing.ml_ds(ml_ds_unchecked)#

Fixture to load the MovieLens test dataset. To use this, just include it as a parameter in your test:

def test_thing_with_data(ml_ds: Dataset):
    ...

Note

This is imported in conftest.py so it is always available in LensKit tests.

Parameters:

ml_ds_unchecked (Dataset)

Return type:

Generator[Dataset, None, None]

lenskit.testing.ml_ds_unchecked()#

Fixture to load the MovieLens dataset, without checking for modifications.

Usually use ml_ds() instead.

Return type:

Generator[Dataset, None, None]

lenskit.testing.ml_ratings()#

Fixture to load the test MovieLens ratings as a data frame. To use this, just include it as a parameter in your test:

def test_thing_with_data(ml_ratings: pd.DataFrame):
    ...

Note

This is imported in conftest.py so it is always available in LensKit tests.

Return type:

Generator[DataFrame, None, None]

lenskit.testing.demo_recs()#

A demo set of train, test, and recommendation data.

Return type:

DemoRecs

lenskit.testing.pop_recs()#

A demo set of train, test, and recommendation data, from most-popular.

Return type:

DemoRecs

lenskit.testing.set_env_var(var, val)#

Set an environment variable & restore it.

class lenskit.testing.DemoRecs(split, recommendations)#

Bases: NamedTuple

Parameters:
recommendations: ItemListCollection[UserIDKey]#

Alias for field number 1

split: TTSplit#

Alias for field number 0

class lenskit.testing.ScorerTests#

Bases: TrainingTests

Common tests for scorer components. Many of these just test that the component runs, not that it produces correct output.

can_score: ClassVar[Literal['some', 'known', 'all']] = 'known'#

What can this scorer score?

expected_ndcg: ClassVar[float | tuple[float, float] | object | None] = None#

Asserts nDCG either greater than the provided expected value or between two values as tuple.

expected_rmse: ClassVar[float | tuple[float, float] | object | None] = None#

Asserts RMSE either less than the provided expected value or between two values as tuple.

test_score_empty_items(rng, ml_ds, trained_pipeline)#

score an empty list of items

Parameters:
test_score_empty_query(rng, ml_ds, trained_pipeline)#

score with an empty query

Parameters:
test_score_query_history(rng, ml_ds, trained_pipeline)#

score when query has user ID and history

Parameters:
test_score_query_history_only(rng, ml_ds, trained_pipeline)#

score when query only has history

Parameters:
test_score_unknown_item(rng, ml_ds, trained_pipeline)#

score with one target item unknown

Parameters:
test_score_unknown_user(rng, ml_ds, trained_pipeline)#

score with an unknown user ID

Parameters:
test_train_recommend(ml_ds)#

Test that a full train-recommend pipeline works.

Parameters:

ml_ds (Dataset)

test_train_score_items_missing_data(rng, ml_ds)#

train and score when some entities are missing data

Parameters:
verify_models_equivalent(orig, copy)#

Verify that two models are equivalent.