-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add ignore_nan argument to concordance_cc() #43
Merged
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e260b6c
Add ignore_nan argument to concordance_cc()
hagenw e6319d6
Update implementation
hagenw 294abb5
Add tests
hagenw f157132
Replace NaN values by 0
hagenw 380377f
Make implementation faster
hagenw 6ade721
Fix error
hagenw bbf53f8
Consider also truth
hagenw 7e07356
Update audmetric/core/api.py
hagenw 86de2d4
Add extra test function
hagenw 5e170d8
Update handling of NaN
hagenw 8136e95
Add more tests
hagenw 81b7cb5
Use one expected
hagenw f21d879
Remove extra NaN test
hagenw 1f57ef0
Simplify expected function
hagenw bf55c58
Adjust comment
hagenw 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 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 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 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,179 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
import audmetric | ||
|
||
|
||
def expected_ccc(truth, prediction): | ||
r"""Expecte Concordance Correlation Coefficient. | ||
|
||
This is a direct implementation of its math equation. | ||
|
||
If only a single sample is given, | ||
it should return NaN. | ||
|
||
""" | ||
prediction = np.array(list(prediction)) | ||
truth = np.array(list(truth)) | ||
|
||
if len(prediction) < 2: | ||
ccc = np.NaN | ||
else: | ||
denominator = ( | ||
prediction.std() ** 2 | ||
+ truth.std() ** 2 | ||
+ (prediction.mean() - truth.mean()) ** 2 | ||
) | ||
if denominator == 0: | ||
ccc = np.NaN | ||
else: | ||
r = np.corrcoef(list(prediction), list(truth))[0][1] | ||
numerator = 2 * r * prediction.std() * truth.std() | ||
ccc = numerator / denominator | ||
return ccc | ||
|
||
|
||
@pytest.mark.parametrize('ignore_nan', [True, False]) | ||
@pytest.mark.parametrize( | ||
'truth, prediction', | ||
[ | ||
# NOTE: this test assumes | ||
# that all truth and prediction values | ||
# do not contain NaN | ||
( | ||
np.random.randint(0, 10, size=5), | ||
np.random.randint(0, 10, size=5), | ||
), | ||
( | ||
pd.Series(np.random.randint(0, 10, size=5)).astype('Int64'), | ||
pd.Series(np.random.randint(0, 10, size=5)).astype('Int64'), | ||
), | ||
( | ||
np.random.randint(0, 10, size=1), | ||
np.random.randint(0, 10, size=1), | ||
), | ||
( | ||
np.random.randint(0, 10, size=10), | ||
np.random.randint(0, 10, size=10), | ||
), | ||
( | ||
np.random.randint(0, 2, size=100), | ||
np.random.randint(0, 2, size=100), | ||
), | ||
( | ||
np.array([]), | ||
np.array([]), | ||
), | ||
( | ||
np.zeros(10), | ||
np.zeros(10), | ||
), | ||
] | ||
) | ||
def test_concordance_cc(truth, prediction, ignore_nan): | ||
|
||
ccc = audmetric.concordance_cc(truth, prediction, ignore_nan=ignore_nan) | ||
|
||
np.testing.assert_almost_equal( | ||
ccc, | ||
expected_ccc(truth, prediction), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'truth, prediction, ignore_nan, expected', | ||
[ | ||
( | ||
[], | ||
[], | ||
True, | ||
np.NaN, | ||
), | ||
( | ||
[], | ||
[], | ||
False, | ||
np.NaN, | ||
), | ||
( | ||
[0], | ||
[0], | ||
True, | ||
np.NaN, | ||
), | ||
( | ||
[0], | ||
[0], | ||
False, | ||
np.NaN, | ||
), | ||
( | ||
[0, np.NaN], | ||
[0, np.NaN], | ||
True, | ||
np.NaN, | ||
), | ||
( | ||
[0, np.NaN], | ||
[0, np.NaN], | ||
False, | ||
np.NaN, | ||
), | ||
( | ||
[0, 1, 2, 3], | ||
[1, 2, 3, 4], | ||
True, | ||
expected_ccc([0, 1, 2, 3], [1, 2, 3, 4]), | ||
), | ||
( | ||
[np.NaN, 1, 2, 3], | ||
[np.NaN, 2, 3, 4], | ||
True, | ||
expected_ccc([1, 2, 3], [2, 3, 4]), | ||
), | ||
( | ||
[np.NaN, 1, 2, 3], | ||
[1, 2, 3, np.NaN], | ||
True, | ||
expected_ccc([1, 2], [2, 3]), | ||
), | ||
( | ||
[0, np.NaN, 2, 3], | ||
[1, 2, 3, 4], | ||
True, | ||
expected_ccc([0, 2, 3], [1, 3, 4]), | ||
), | ||
( | ||
[0, 1, 2, 3], | ||
[1, 2, np.NaN, 4], | ||
True, | ||
expected_ccc([0, 1, 3], [1, 2, 4]), | ||
), | ||
( | ||
[np.NaN, np.NaN, 2, 3], | ||
[1, 2, 3, np.NaN], | ||
True, | ||
expected_ccc([2], [3]), | ||
), | ||
( | ||
[np.NaN, np.NaN, 2, 3], | ||
[1, 2, 3, np.NaN], | ||
False, | ||
np.NaN, | ||
), | ||
] | ||
) | ||
def test_concordance_cc_ignore_nan( | ||
truth, | ||
prediction, | ||
ignore_nan, | ||
expected, | ||
): | ||
|
||
ccc = audmetric.concordance_cc(truth, prediction, ignore_nan=ignore_nan) | ||
|
||
np.testing.assert_almost_equal( | ||
ccc, | ||
expected, | ||
) |
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.
Do we actually need those special cases where we return
np.NaN
or can we simplify the function now?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.
Sorry, forgot to remove this. We don't need this and it is now removed.