From 1e58d31d1b1939765b8ea33944cf67e0d9bc7cf9 Mon Sep 17 00:00:00 2001 From: SkafteNicki Date: Mon, 16 Aug 2021 11:16:13 +0200 Subject: [PATCH 1/3] fix --- CHANGELOG.md | 2 ++ tests/regression/test_spearman.py | 6 ++++++ torchmetrics/functional/regression/spearman.py | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc65276574..1b809f5aad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Fixed bug in the ranking of samples in `SpearmanCorrCoef` metric ([]()) + ## [0.5.0] - 2021-08-09 diff --git a/tests/regression/test_spearman.py b/tests/regression/test_spearman.py index 890f075a56e..e919793c478 100644 --- a/tests/regression/test_spearman.py +++ b/tests/regression/test_spearman.py @@ -36,12 +36,17 @@ target=torch.randn(NUM_BATCHES, BATCH_SIZE), ) +_specific_input = Input( + preds=torch.stack([torch.tensor([1., 0., 4., 1., 0., 3. ,0.]) for _ in range(NUM_BATCHES)]), + target=torch.stack([torch.tensor([4., 0., 3., 3., 3., 1., 1.]) for _ in range(NUM_BATCHES)]) +) @pytest.mark.parametrize( "preds, target", [ (_single_target_inputs1.preds, _single_target_inputs1.target), (_single_target_inputs2.preds, _single_target_inputs2.target), + (_specific_input.preds, _specific_input.target) ], ) def test_ranking(preds, target): @@ -64,6 +69,7 @@ def _sk_metric(preds, target): [ (_single_target_inputs1.preds, _single_target_inputs1.target), (_single_target_inputs2.preds, _single_target_inputs2.target), + (_specific_input.preds, _specific_input.target) ], ) class TestSpearmanCorrcoef(MetricTester): diff --git a/torchmetrics/functional/regression/spearman.py b/torchmetrics/functional/regression/spearman.py index 108c0671310..a583ff85393 100644 --- a/torchmetrics/functional/regression/spearman.py +++ b/torchmetrics/functional/regression/spearman.py @@ -47,7 +47,7 @@ def _rank_data(data: Tensor) -> Tensor: repeats = _find_repeats(data) for r in repeats: - condition = rank == r + condition = data == r rank[condition] = rank[condition].mean() return rank From 90f5b7a9db94fe55be4ca2f0dbc0a1c5093f331d Mon Sep 17 00:00:00 2001 From: Nicki Skafte Date: Mon, 16 Aug 2021 11:17:50 +0200 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b809f5aad4..fe48f43e6cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Fixed bug in the ranking of samples in `SpearmanCorrCoef` metric ([]()) +- Fixed bug in the ranking of samples in `SpearmanCorrCoef` metric ([#448](https://github.com/PyTorchLightning/metrics/pull/448)) ## [0.5.0] - 2021-08-09 From 07e435500facbda6e505175d62564f61a3413722 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 16 Aug 2021 09:17:54 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/regression/test_spearman.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/regression/test_spearman.py b/tests/regression/test_spearman.py index e919793c478..ef59dd7a697 100644 --- a/tests/regression/test_spearman.py +++ b/tests/regression/test_spearman.py @@ -37,16 +37,17 @@ ) _specific_input = Input( - preds=torch.stack([torch.tensor([1., 0., 4., 1., 0., 3. ,0.]) for _ in range(NUM_BATCHES)]), - target=torch.stack([torch.tensor([4., 0., 3., 3., 3., 1., 1.]) for _ in range(NUM_BATCHES)]) + preds=torch.stack([torch.tensor([1.0, 0.0, 4.0, 1.0, 0.0, 3.0, 0.0]) for _ in range(NUM_BATCHES)]), + target=torch.stack([torch.tensor([4.0, 0.0, 3.0, 3.0, 3.0, 1.0, 1.0]) for _ in range(NUM_BATCHES)]), ) + @pytest.mark.parametrize( "preds, target", [ (_single_target_inputs1.preds, _single_target_inputs1.target), (_single_target_inputs2.preds, _single_target_inputs2.target), - (_specific_input.preds, _specific_input.target) + (_specific_input.preds, _specific_input.target), ], ) def test_ranking(preds, target): @@ -69,7 +70,7 @@ def _sk_metric(preds, target): [ (_single_target_inputs1.preds, _single_target_inputs1.target), (_single_target_inputs2.preds, _single_target_inputs2.target), - (_specific_input.preds, _specific_input.target) + (_specific_input.preds, _specific_input.target), ], ) class TestSpearmanCorrcoef(MetricTester):