Skip to content
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
35 changes: 35 additions & 0 deletions hands_on/numerical_fuzzing/test_var.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Importing libraries required
import numpy as np
from math import isclose
from numpy.testing import assert_allclose

# deterministic test
def test_var_deterministic():
# Given
x = np.array([1,2,3,4])
expected_var = 1.25

# When
calculated_variance = x.var()

# Then
assert isclose(x.var(), expected_var)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert isclose(calculated_variance, expected_var)



# numerical fuzzing test
def test_var_fuzzing():
# Setting the seed for random values generators
rand_state = np.random.RandomState(20000)

# Defining rows and columns for the array
N, D = 100, 4

# Defining expected variances (we choose them as we want)
expected = np.linspace(0.4, 1.2, D)

# Generating random values N(0,1) biased with variance
x = rand_state.randn(N, D) * np.sqrt(expected)

variances = np.var(x, axis=0)

np.testing.assert_allclose(variances, expected, rtol=5e-1)
19 changes: 19 additions & 0 deletions hands_on/pyanno_voting/pyanno/tests/test_voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
17 changes: 17 additions & 0 deletions hands_on/pyanno_voting/pyanno/voting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)