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.
Attributes#
Classes#
Common tests for scorer components. Many of these just test that the component |
|
Functions#
|
|
|
Hypothesis generator that produces scored lists. |
|
|
|
|
A demo set of train, test, and recommendation data. |
|
|
|
|
Fixture to load the MovieLens 100K dataset (currently as a data frame). It skips |
|
Fixture to load the MovieLens test dataset. To use this, just include it as |
Fixture to load the MovieLens dataset, without checking for modifications. |
|
Fixture to load the test MovieLens ratings as a data frame. To use this, |
|
|
A demo set of train, test, and recommendation data, from most-popular. |
|
|
|
Set an environment variable & restore it. |
Package Contents#
- lenskit.testing.coo_arrays(shape=None, dtype=nph.floating_dtypes(endianness='=', sizes=[32, 64]), elements=st.floats(-1000000.0, 1000000.0, allow_nan=False, allow_infinity=False, width=32))#
- Return type:
- lenskit.testing.scored_lists(*, n=st.integers(0, 1000), scores=None)#
Hypothesis generator that produces scored lists.
- lenskit.testing.sparse_arrays(*, layout='csr', **kwargs)#
- lenskit.testing.sparse_tensors(*, layout='csr', **kwargs)#
- class lenskit.testing.BasicComponentTests#
- component: type[lenskit.pipeline.Component]#
- configs = []#
- test_instantiate_default()#
- test_default_config_vars()#
- test_default_config_round_trip()#
- test_config_round_trip()#
- class lenskit.testing.ScorerTests#
Bases:
TrainingTestsCommon tests for scorer components. Many of these just test that the component runs, not that it produces correct output.
- component: ClassVar[type[lenskit.pipeline.Component]]#
- can_score: ClassVar[Literal['some', 'known', 'all']] = 'known'#
What can this scorer score?
- 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.
- 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.
- invoke_scorer(pipe, **kwargs)#
- Parameters:
pipe (lenskit.pipeline.Pipeline)
- Return type:
- verify_models_equivalent(orig, copy)#
Verify that two models are equivalent.
- test_score_known(rng, ml_ds, trained_pipeline)#
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_pickle_roundrip(rng, ml_ds, trained_pipeline, trained_model)#
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
trained_model (lenskit.pipeline.Component)
- test_score_unknown_user(rng, ml_ds, trained_pipeline)#
score with an unknown user ID
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_score_unknown_item(rng, ml_ds, trained_pipeline)#
score with one target item unknown
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_score_empty_query(rng, ml_ds, trained_pipeline)#
score with an empty query
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_score_query_history(rng, ml_ds, trained_pipeline)#
score when query has user ID and history
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_score_query_history_only(rng, ml_ds, trained_pipeline)#
score when query only has history
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_score_empty_items(rng, ml_ds, trained_pipeline)#
score an empty list of items
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_pipeline (lenskit.pipeline.Pipeline)
- test_train_score_items_missing_data(rng, ml_ds)#
train and score when some entities are missing data
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
- test_train_recommend(rng, ml_ds, trained_topn_pipeline)#
Test that a full train-recommend pipeline works.
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_topn_pipeline (lenskit.pipeline.Pipeline)
- test_ray_recommend(rng, ml_ds, trained_topn_pipeline)#
Ensure pipeline can be used via Ray.
- Parameters:
rng (numpy.random.Generator)
ml_ds (lenskit.data.Dataset)
trained_topn_pipeline (lenskit.pipeline.Pipeline)
- test_run_with_doubles(ml_ratings)#
- Parameters:
ml_ratings (pandas.DataFrame)
- test_batch_prediction_accuracy(rng, ml_100k)#
- Parameters:
rng (numpy.random.Generator)
ml_100k (pandas.DataFrame)
- test_batch_top_n_accuracy(rng, ml_100k)#
- Parameters:
rng (numpy.random.Generator)
ml_100k (pandas.DataFrame)
- class lenskit.testing.DemoRecs#
Bases:
NamedTuple- split: lenskit.splitting.TTSplit#
- recommendations: lenskit.data.ItemListCollection[lenskit.data.UserIDKey]#
- lenskit.testing.demo_recs()#
A demo set of train, test, and recommendation data.
- Return type:
- lenskit.testing.ml_20m()#
- Return type:
Generator[lenskit.data.Dataset, None, None]
- 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[pandas.DataFrame, None, None]
- lenskit.testing.ml_100k_zip#
- 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.pyso it is always available in LensKit tests.- Parameters:
ml_ds_unchecked (lenskit.data.Dataset)
- Return type:
Generator[lenskit.data.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[lenskit.data.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.pyso it is always available in LensKit tests.- Return type:
Generator[pandas.DataFrame, None, None]
- lenskit.testing.ml_test_dir#
- lenskit.testing.pop_recs()#
A demo set of train, test, and recommendation data, from most-popular.
- Return type:
- lenskit.testing.msweb()#
- Return type:
- lenskit.testing.set_env_var(var, val)#
Set an environment variable & restore it.