From 2da1ff2b764990f28b4b0e543b08a74e867de3f5 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 11:34:32 +0200 Subject: [PATCH 1/2] added test for label frequency --- hands_on/pyanno_voting/pyanno/tests/test_voting.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hands_on/pyanno_voting/pyanno/tests/test_voting.py b/hands_on/pyanno_voting/pyanno/tests/test_voting.py index f21a10c..e962d40 100644 --- a/hands_on/pyanno_voting/pyanno/tests/test_voting.py +++ b/hands_on/pyanno_voting/pyanno/tests/test_voting.py @@ -1,5 +1,5 @@ import numpy as np - +from numpy.testing import assert_array_equal from pyanno import voting from pyanno.voting import MISSING_VALUE as MV @@ -16,6 +16,11 @@ def test_labels_count(): result = voting.labels_count(annotations, nclasses) assert result == expected +def test_labels_frequency(): + annotations = [[1, 1, 2], [-1, 1, 2]] + classes = 4 + correctout = np.array([ 0. , 0.6, 0.4, 0. ]) + assert_array_equal(voting.labels_frequency(annotations, classes), correctout) def test_majority_vote(): annotations = [ From b2d23e5a726b7d8835f5b1c49ae978a42c8402fb Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 11:35:05 +0200 Subject: [PATCH 2/2] add function body of frequency labels --- hands_on/pyanno_voting/pyanno/voting.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/hands_on/pyanno_voting/pyanno/voting.py b/hands_on/pyanno_voting/pyanno/voting.py index d5b5747..7efc577 100644 --- a/hands_on/pyanno_voting/pyanno/voting.py +++ b/hands_on/pyanno_voting/pyanno/voting.py @@ -100,3 +100,15 @@ def labels_frequency(annotations, nclasses): freq[k] is the frequency of elements of class k in `annotations`, i.e. their count over the number of total of observed (non-missing) elements """ + annotations = np.array(annotations) + annotations_flat = annotations.flatten() + + result_list = [] + total_valid = sum(annotations_flat != -1) + for c in range(nclasses): + cnt = 0 + for a in annotations_flat: + if a == c: + cnt += 1 + result_list.append(cnt / total_valid) + return np.array(result_list)