From 49606793b94606be3ea6d94f7e7a8be941e09f21 Mon Sep 17 00:00:00 2001 From: cvf-bcn-gituser Date: Thu, 27 May 2021 18:29:35 +0200 Subject: [PATCH] Startign new tests viterbi, entropy --- test/src/unittests/standard/test_viterbi.py | 63 +++++++++++++++++++++ test/src/unittests/stats/test_entropy.py | 60 ++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 test/src/unittests/standard/test_viterbi.py create mode 100644 test/src/unittests/stats/test_entropy.py diff --git a/test/src/unittests/standard/test_viterbi.py b/test/src/unittests/standard/test_viterbi.py new file mode 100644 index 000000000..4835bce8c --- /dev/null +++ b/test/src/unittests/standard/test_viterbi.py @@ -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) diff --git a/test/src/unittests/stats/test_entropy.py b/test/src/unittests/stats/test_entropy.py new file mode 100644 index 000000000..1e693be9b --- /dev/null +++ b/test/src/unittests/stats/test_entropy.py @@ -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)