-
Notifications
You must be signed in to change notification settings - Fork 238
refactor: change r precision calculation and documentation #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import pytest | ||
|
|
||
| from docarray.math.evaluation import ( | ||
| average_precision, | ||
| dcg_at_k, | ||
| f1_score_at_k, | ||
| hit_at_k, | ||
| ndcg_at_k, | ||
| precision_at_k, | ||
| r_precision, | ||
| recall_at_k, | ||
| reciprocal_rank, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.25), | ||
| ([], 0), | ||
| ([1, 1, 1], 1), | ||
| ([0, 0], 0), | ||
| ], | ||
| ) | ||
| def test_r_precision(binary_relevance, score): | ||
| assert abs(r_precision(binary_relevance) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 4.0 / 7, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.5, 2), | ||
| ([], 0, None), | ||
| ([1, 1, 1], 1, None), | ||
| ([0, 0], 0, None), | ||
| ], | ||
| ) | ||
| def test_precision_at_k(binary_relevance, score, k): | ||
| assert abs(precision_at_k(binary_relevance, k=k) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 1, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 0, 1), | ||
| ([], 0, None), | ||
| ([1, 1, 1], 1, None), | ||
| ([0, 0], 0, None), | ||
| ], | ||
| ) | ||
| def test_hit_at_k(binary_relevance, score, k): | ||
| assert abs(hit_at_k(binary_relevance, k=k) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], (1.0 / 2 + 2.0 / 5 + 3.0 / 6 + 4.0 / 7) / 4), | ||
| ([], 0), | ||
| ([1, 1, 1], 1), | ||
| ([0, 0], 0), | ||
| ], | ||
| ) | ||
| def test_average_precision(binary_relevance, score): | ||
| assert abs(average_precision(binary_relevance) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.5), | ||
| ([], 0), | ||
| ([1, 1, 1], 1.0), | ||
| ([0, 0], 0), | ||
| ], | ||
| ) | ||
| def test_reciprocal_rank(binary_relevance, score): | ||
| assert abs(reciprocal_rank(binary_relevance) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, max_rel, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 4.0 / 7, 7, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 1, 4, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.25, 4, 2), | ||
| ([], 0, 4, None), | ||
| ([1, 1, 1], 0.75, 4, None), | ||
| ([0, 0], 0, 4, None), | ||
| ], | ||
| ) | ||
| def test_recall_at_k(binary_relevance, score, max_rel, k): | ||
| calculated_score = recall_at_k(binary_relevance, max_rel=max_rel, k=k) | ||
| assert abs(calculated_score - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, max_rel, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 4.0 / 7, 7, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 2 / (1 / (4 / 7) + 1), 4, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 2 / (1 / 0.5 + 1 / 0.25), 4, 2), | ||
| ([], 0, 4, None), | ||
| ([1, 1, 1], 2 / (1 / 0.75 + 1), 4, None), | ||
| ([0, 0], 0, 4, None), | ||
| ], | ||
| ) | ||
| def test_f1_score_at_k(binary_relevance, score, max_rel, k): | ||
| calculated_score = f1_score_at_k(binary_relevance, max_rel=max_rel, k=k) | ||
| assert abs(calculated_score - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, method, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 2.1737, 0, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 1.7073, 1, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 1, 0, 4), | ||
| ([], 0, 0, None), | ||
| ([1, 1, 1], 2.6309, 0, None), | ||
| ([0, 0], 0, 0, None), | ||
| ], | ||
| ) | ||
| def test_dcg_at_k(binary_relevance, score, method, k): | ||
| assert abs(dcg_at_k(binary_relevance, method=method, k=k) - score) < 0.001 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "binary_relevance, score, method, k", | ||
| [ | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.6942, 0, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.6665, 1, None), | ||
| ([0, 1, 0, 0, 1, 1, 1], 0.3194, 0, 4), | ||
| ([], 0, 0, None), | ||
| ([1, 1, 1], 1, 0, None), | ||
| ([0, 0], 0, 0, None), | ||
| ], | ||
| ) | ||
| def test_ndcg_at_k(binary_relevance, score, method, k): | ||
| assert abs(ndcg_at_k(binary_relevance, method=method, k=k) - score) < 0.001 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there needs to be some tests testing the new behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree. Currently there are no tests for the metric functions. I have created a test which tests the behavior together with the evaluate function functions in #617 (which has to be adopted -after one of those Pars is merged), but I can also create tests which directly test the functions docarray.math here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add unit tests yes