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

Unit Tests for Viterbi and Entropy #1163

Open
wants to merge 2 commits into
base: master
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
63 changes: 63 additions & 0 deletions test/src/unittests/standard/test_viterbi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python

# Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Essentia
#
# Essentia is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the Affero GNU General Public License
# version 3 along with this program. If not, see http://www.gnu.org/licenses/



from essentia_test import *
from viterbi_trellis import ViterbiTrellis

class TestViterbi(TestCase):


def testEmpty(self):
p1 = [[],[]]
p2 = []
p3 = []
p4 = []
p5 = []
emptyOutput = Viterbi()(p1,p2,p3,p4,p5)
print(emptyOutput)


# Inpute parameters are
"""
observationProbabilities (vector_vector_real) - the observation probabilities
initialization (vector_real) - the initialization
fromIndex (undefined) - the transition matrix from index
toIndex (undefined) - the transition matrix to index
transitionProbabilities (vector_real) - the transition probabilities matrix
"""

def testRegression(self):
# Example from https://pypi.org/project/viterbi-trellis/
p1 = [[2, 6, 4], [4, 6], [0, 2, 6]]
v = ViterbiTrellis(p1, lambda x: x / 2.0, lambda x, y: abs(y - x))
best_path = v.viterbi_best_path() # result is [2, 0, 1]

p4 = []
p5 = []
output = Viterbi()(p1,lambda x: x / 2.0, lambda x, y: abs(y - x),p4,p5)
print(output)



suite = allTests(TestViterbi)

if __name__ == '__main__':
TextTestRunner(verbosity=2).run(suite)
60 changes: 60 additions & 0 deletions test/src/unittests/stats/test_entropy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
#
# This file is part of Essentia
#
# Essentia is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation (FSF), either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the Affero GNU General Public License
# version 3 along with this program. If not, see http://www.gnu.org/licenses/



from essentia_test import *


class TestEntropy(TestCase):

def testZero(self):
self.assertEqual(Entropy()(zeros(256)), 0)

def testOnes(self):
# Test for different powers of 2
self.assertEqual(Entropy()(ones(16)), 4)
self.assertEqual(Entropy()(ones(32)), 5)
self.assertEqual(Entropy()(ones(64)), 6)

def testEmpty(self):
self.assertRaises(EssentiaException, lambda: Entropy()([]))

def testInvalidInput(self):
input = [10, 10 , 10, 10, -10]
self.assertRaises(RuntimeError, lambda: Entropy()(input))

def testRegression(self):
# Check value with online calculator
output = Entropy()([0.9, 0.8, 0.7, 0.6])
#https://planetcalc.com/2476/
expected_output = 1.98387111 # from above calculator, 8 decimal places
self.assertAlmostEqual(expected_output, output, 8)

output = Entropy()([0.9, 0.8, 0.7, 0.6, 0.9, 0.8, 0.7, 0.6])
#https://planetcalc.com/2476/
expected_output = 1.0+1.98387111 # from above calculator, 8 decimal places
self.assertAlmostEqual(expected_output, output, 8)



suite = allTests(TestEntropy)

if __name__ == '__main__':
TextTestRunner(verbosity=2).run(suite)