From daeaefacb89dba23412ae6738e212ef3e6048016 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 11:57:34 +0200 Subject: [PATCH 1/2] test for voting.py Fixes: #2 --- .../pyanno_voting/pyanno/tests/test_voting.py | 19 +++++++++++++++++++ hands_on/pyanno_voting/pyanno/voting.py | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/hands_on/pyanno_voting/pyanno/tests/test_voting.py b/hands_on/pyanno_voting/pyanno/tests/test_voting.py index f21a10c..a9fde4d 100644 --- a/hands_on/pyanno_voting/pyanno/tests/test_voting.py +++ b/hands_on/pyanno_voting/pyanno/tests/test_voting.py @@ -3,6 +3,8 @@ from pyanno import voting from pyanno.voting import MISSING_VALUE as MV +from math import isclose + def test_labels_count(): annotations = [ @@ -41,3 +43,20 @@ def test_majority_vote_empty_item(): expected = [1, MV, 2] result = voting.majority_vote(annotations) assert result == expected + +def test_labels_frequency(): + matrix = [ + [1, 2, 2, -1], + [2, 2, 2, 2], + [1, 1, 3, 3], + [1, 3, 3, 2], + [-1, 2, 3, 1], + [-1, -1, -1, 3], + ] + result = voting.labels_frequency(matrix, 4) + + assert np.all([res != None for res in result]) + assert len(result) == 4 + assert np.all(voting.labels_frequency([[-1, -1, -1, -1],[-1, -1, -1, -1]], 4) == np.zeros(4)) + assert np.all([i >= 0 and i <= 1 for i in result]) + assert isclose(np.sum(result),1) or isclose(np.sum(result), 0,abs_tol=1e-12) diff --git a/hands_on/pyanno_voting/pyanno/voting.py b/hands_on/pyanno_voting/pyanno/voting.py index d5b5747..3694cc7 100644 --- a/hands_on/pyanno_voting/pyanno/voting.py +++ b/hands_on/pyanno_voting/pyanno/voting.py @@ -100,3 +100,20 @@ 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_array = np.ravel(annotations) + result = np.zeros(nclasses) + + dim = 0 + for number in annotations_array: + if number != -1: + dim = dim + 1 + if dim !=0: + for cl in np.arange(nclasses): + aux = 0 + for anot in annotations_array: + if cl == anot: + aux = aux + 1 + result[cl] = aux / dim + return(result) + else: + return(0) From 30522c5b9f6b5b491cebf40016d93ace744da519 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 3 Sep 2019 12:29:08 +0200 Subject: [PATCH 2/2] refactored test_labels_frequency and test_labels_count --- .../pyanno_voting/pyanno/tests/test_voting.py | 57 +++++++++++++------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/hands_on/pyanno_voting/pyanno/tests/test_voting.py b/hands_on/pyanno_voting/pyanno/tests/test_voting.py index a9fde4d..7e89da1 100644 --- a/hands_on/pyanno_voting/pyanno/tests/test_voting.py +++ b/hands_on/pyanno_voting/pyanno/tests/test_voting.py @@ -7,6 +7,7 @@ def test_labels_count(): + #given annotations = [ [1, 2, MV, MV], [MV, MV, 3, 3], @@ -15,10 +16,49 @@ def test_labels_count(): ] nclasses = 5 expected = [0, 3, 1, 3, 0] + + #when result = voting.labels_count(annotations, nclasses) + + #then assert result == expected +def test_labels_frequency(): + #given + matrix = [ + [1, 2, 2, -1], + [2, 2, 2, 2], + [1, 1, 3, 3], + [1, 3, 3, 2], + [-1, 2, 3, 1], + [-1, -1, -1, 3], + ] + + matrix2 = [ + [-1, -1, -1, -1], + [-1, -1, -1, -1] + ] + + lowerlimit = 0 + upperlimit = 1 + nclasses = 4 + + expected2 = np.zeros(nclasses) + + + #when + result = voting.labels_frequency(matrix, nclasses) + result2 = voting.labels_frequency(matrix2, nclasses) + + #then + assert np.all([res != None for res in result]) + assert len(result) == nclasses + assert np.all(result2 == expected2) + assert np.all([i >= lowerlimit and i <= upperlimit for i in result]) + assert isclose(np.sum(result),upperlimit) or isclose(np.sum(result), lowerlimit,abs_tol=1e-12) + + def test_majority_vote(): annotations = [ [1, 2, 2, MV], @@ -43,20 +83,3 @@ def test_majority_vote_empty_item(): expected = [1, MV, 2] result = voting.majority_vote(annotations) assert result == expected - -def test_labels_frequency(): - matrix = [ - [1, 2, 2, -1], - [2, 2, 2, 2], - [1, 1, 3, 3], - [1, 3, 3, 2], - [-1, 2, 3, 1], - [-1, -1, -1, 3], - ] - result = voting.labels_frequency(matrix, 4) - - assert np.all([res != None for res in result]) - assert len(result) == 4 - assert np.all(voting.labels_frequency([[-1, -1, -1, -1],[-1, -1, -1, -1]], 4) == np.zeros(4)) - assert np.all([i >= 0 and i <= 1 for i in result]) - assert isclose(np.sum(result),1) or isclose(np.sum(result), 0,abs_tol=1e-12)