Skip to content
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

WIP: Add support for NaN and mixed types in infer_labels() #13

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion audmetric/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def infer_labels(
labels in sorted order

"""
return sorted(list(set(truth) | set(prediction)))
truth = _remove_nan(truth)
prediction = _remove_nan(prediction)
return sorted(list(set(truth) | set(prediction)), key=str)


def scores_per_subgroup_and_class(
Expand Down Expand Up @@ -104,3 +106,10 @@ def scores_per_subgroup_and_class(
zero_division=zero_division,
)
return score


def _remove_nan(sequence):
return [
s for s in sequence
if not isinstance(s, float) or not np.isnan(s)
]
45 changes: 45 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,51 @@
import audmetric


@pytest.mark.parametrize(
'truth, prediction, expected_labels',
[
(
['a', 'b'],
['c', 'd'],
['a', 'b', 'c', 'd'],
),
(
['d', 'c'],
['b', 'a'],
['a', 'b', 'c', 'd'],
),
(
['a', 'a'],
['a', 'a'],
['a'],
),
(
[0, 1, 2],
[2, 2, 2],
[0, 1, 2],
),
(
['a', 1],
['a', 0],
[0, 1, 'a'],
),
(
[0, 1],
[np.NaN, 1],
[0, 1],
),
(
['a', 'b'],
[np.NaN, 'c'],
['a', 'b', 'c'],
),
]
)
def test_infer_labels(truth, prediction, expected_labels):
labels = audmetric.utils.infer_labels(truth, prediction)
assert expected_labels == labels


@pytest.mark.parametrize(
'truth,prediction,protected_variable,metric,labels,subgroups,'
'zero_division,expected',
Expand Down