From 18d9c5e9221426abc43b4d685b61f12e384a0e47 Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Thu, 10 Mar 2022 16:19:53 -0700 Subject: [PATCH 1/5] Fix nDCG truncation bug (#309) --- lenskit/metrics/topn.py | 4 ++-- tests/test_topn_ndcg.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lenskit/metrics/topn.py b/lenskit/metrics/topn.py index ae1b70f3c..7f616c53d 100644 --- a/lenskit/metrics/topn.py +++ b/lenskit/metrics/topn.py @@ -300,11 +300,11 @@ def ndcg(recs, truth, discount=np.log2, k=None): The maximum list length. """ - tpos = truth.index.get_indexer(recs['item']) - if k is not None: recs = recs.iloc[:k] + tpos = truth.index.get_indexer(recs['item']) + if 'rating' in truth.columns: i_rates = np.sort(truth.rating.values)[::-1] if k is not None: diff --git a/tests/test_topn_ndcg.py b/tests/test_topn_ndcg.py index 5d1601d9a..2fdf9e573 100644 --- a/tests/test_topn_ndcg.py +++ b/tests/test_topn_ndcg.py @@ -87,6 +87,14 @@ def test_ndcg_perfect(): assert ndcg(recs, truth) == approx(1.0) +def test_ndcg_perfect_k_short(): + recs = pd.DataFrame({'item': [2, 3, 1]}) + truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]}) + truth = truth.set_index('item') + assert ndcg(recs, truth, k=2) == approx(1.0) + assert ndcg(recs[:2], truth, k=2) == approx(1.0) + + def test_ndcg_wrong(): recs = pd.DataFrame({'item': [1, 2]}) truth = pd.DataFrame({'item': [1, 2, 3], 'rating': [3.0, 5.0, 4.0]}) From bbbcec3458dfcc7cec1ffdb6898c11266500afdd Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Thu, 10 Mar 2022 16:44:12 -0700 Subject: [PATCH 2/5] Freeze conda-lock version --- lkbuild/boot-env.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lkbuild/boot-env.yml b/lkbuild/boot-env.yml index 892591fbf..e8d9d7fae 100644 --- a/lkbuild/boot-env.yml +++ b/lkbuild/boot-env.yml @@ -7,5 +7,5 @@ dependencies: - invoke=1 - requests=2 - pip -- conda-lock +- conda-lock=0.13 - mamba From f2e8aba1274ee7299547a94b547e1b91bb1269ab Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Thu, 10 Mar 2022 17:00:24 -0700 Subject: [PATCH 3/5] lock ipython version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 68b0f0bfc..6c1dd1e2d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,7 @@ dev = [ "flake8 >= 3", "coverage >= 5", "pytest-cov >= 2.12", - "ipython >= 7", + "ipython == 7.*", "docopt >= 0.6", "tqdm >= 4", "sphinx-autobuild >= 2021", From b4e2be92d741f5539b0241fc5ebce6a87e12a35b Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Thu, 10 Mar 2022 17:05:33 -0700 Subject: [PATCH 4/5] fix ipython version --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 6c1dd1e2d..b0844d4c2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,6 +63,7 @@ doc = [ "sphinxcontrib-bibtex >= 2.0", "sphinx_rtd_theme >= 0.5", "nbsphinx >= 0.8", + "ipython == 7.*", ] demo = [ "notebook >= 6", From 690818dd5193378b3d89a80c14e728c6c5c998cb Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Thu, 10 Mar 2022 17:22:56 -0700 Subject: [PATCH 5/5] weird missing things? --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index b0844d4c2..8c625d2ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,6 +64,7 @@ doc = [ "sphinx_rtd_theme >= 0.5", "nbsphinx >= 0.8", "ipython == 7.*", + "notebook >=6", ] demo = [ "notebook >= 6",