File size: 1,956 Bytes
c7e47b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from __future__ import print_function

import unittest
import numpy as np
import os
from medleydb import MultiTrack
from medleydb.annotate import activation_conf as A


def array_almost_equal(array1, array2, tolerance=1e-7):
    diff = np.abs(array1 - array2)
    num_not_equal = diff > tolerance
    print("number of unequal elements: %s" % np.sum(num_not_equal))
    return np.sum(num_not_equal) == 0


class TestComputeActivationConfidence(unittest.TestCase):

    def test_defaults(self):
        mtrack = MultiTrack('LizNelson_Rainfall')
        C, actual_index = A.compute_activation_confidence(mtrack)
        actual_shape = C.shape
        expected_shape = (6135, 6)
        self.assertEqual(expected_shape, actual_shape)
        
        expected_index = [1, 2, 3, 4, 5]
        self.assertEqual(expected_index, actual_index)


class TestTrackEnergy(unittest.TestCase):

    def test_compute_energy(self):
        wave = np.ones((20, ))
        win_len = 10
        win = np.ones((10, ))
        actual = A.track_energy(wave, win_len, win)
        expected = np.array([0.5, 1., 1., 1., 0.5])
        self.assertTrue(np.allclose(expected, actual))

class TestHwr(unittest.TestCase):

    def test_neg_pos(self):
        x = np.array([-0.5, 0, 0.5])
        expected = np.array([0, 0, 0.5])
        actual = A.hwr(x)

    def test_pos(self):
        x = np.array([7, 0, 0.5])
        expected = np.array([7, 0, 0.5])
        actual = A.hwr(x)


class TestWriteActivationsToCsv(unittest.TestCase):

    def test_default(self):
        activations = np.array([
            [0.0, 1.0, 1.0, 0.4],
            [0.5, 0.9, 0.9, 0.7],
            [1.0, 0.8, 0.8, 0.8]
        ])
        mtrack = MultiTrack('Phoenix_ScotchMorris')
        stem_idx_list = [1, 2, 3]
        A.write_activations_to_csv(mtrack, activations, stem_idx_list)
        self.assertTrue(os.path.exists(mtrack.activation_conf_fpath))

        os.remove(mtrack.activation_conf_fpath)