Ignore:
Timestamp:
Jun 22, 2016, 1:00:10 PM (8 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
4b9443c4
Parents:
60fc05b (diff), 6769586 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into notes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/tests/test_cvec.py

    r60fc05b rf264b17  
    11#! /usr/bin/env python
    22
    3 from numpy.testing import TestCase, run_module_suite
    4 from numpy.testing import assert_equal, assert_almost_equal
    5 from aubio import cvec
    6 from numpy import array, shape, pi
     3from unittest import main
     4import numpy as np
     5from numpy.testing import TestCase, assert_equal
     6from aubio import cvec, fvec, float_type
     7
     8wrong_type = 'float32' if float_type == 'float64' else 'float64'
    79
    810class aubio_cvec_test_case(TestCase):
     
    1012    def test_vector_created_with_zeroes(self):
    1113        a = cvec(10)
    12         shape(a.norm)
    13         shape(a.phas)
    14         a.norm[0]
     14        assert_equal(a.norm.shape[0], 10 / 2 + 1)
     15        assert_equal(a.phas.shape[0], 10 / 2 + 1)
     16        _ = a.norm[0]
    1517        assert_equal(a.norm, 0.)
    1618        assert_equal(a.phas, 0.)
     
    4244    def test_assign_cvec_phas_slice(self):
    4345        spec = cvec(1024)
    44         spec.phas[39:-1] = -pi
     46        spec.phas[39:-1] = -np.pi
    4547        assert_equal(spec.phas[0:39], 0)
    46         assert_equal(spec.phas[39:-1], -pi)
     48        assert_equal(spec.phas[39:-1], -np.pi)
    4749        assert_equal(spec.norm, 0)
    4850
     51    def test_assign_cvec_with_other_cvec(self):
     52        """ check dest cvec is still reachable after source was deleted """
     53        spec = cvec(1024)
     54        a = np.random.rand(1024//2+1).astype(float_type)
     55        b = np.random.rand(1024//2+1).astype(float_type)
     56        spec.norm = a
     57        spec.phas = b
     58        new_spec = spec
     59        del spec
     60        assert_equal(a, new_spec.norm)
     61        assert_equal(b, new_spec.phas)
     62        assert_equal(id(a), id(new_spec.norm))
     63        assert_equal(id(b), id(new_spec.phas))
     64
     65    def test_pass_to_numpy(self):
     66        spec = cvec(1024)
     67        norm = spec.norm
     68        phas = spec.phas
     69        del spec
     70        new_spec = cvec(1024)
     71        new_spec.norm = norm
     72        new_spec.phas = phas
     73        assert_equal(norm, new_spec.norm)
     74        assert_equal(phas, new_spec.phas)
     75        assert_equal(id(norm), id(new_spec.norm))
     76        assert_equal(id(phas), id(new_spec.phas))
     77        del norm
     78        del phas
     79        assert_equal(new_spec.norm, 0.)
     80        assert_equal(new_spec.phas, 0.)
     81        del new_spec
     82
     83    def test_assign_norm_too_large(self):
     84        a = cvec(512)
     85        b = fvec(512//2+1 + 4)
     86        with self.assertRaises(ValueError):
     87            a.norm = b
     88
     89    def test_assign_norm_too_small(self):
     90        a = cvec(512)
     91        b = fvec(512//2+1 - 4)
     92        with self.assertRaises(ValueError):
     93            a.norm = b
     94
     95    def test_assign_phas_too_large(self):
     96        a = cvec(512)
     97        b = fvec(512//2+1 + 4)
     98        with self.assertRaises(ValueError):
     99            a.phas = b
     100
     101    def test_assign_phas_too_small(self):
     102        a = cvec(512)
     103        b = fvec(512//2+1 - 4)
     104        with self.assertRaises(ValueError):
     105            a.phas = b
     106
     107    def test_cvec_repr(self):
     108        win_s = 512
     109        c = cvec(win_s)
     110        expected_repr = "aubio cvec of {:d} elements".format(win_s//2+1)
     111        self.assertEqual(repr(c), expected_repr)
     112
     113class aubio_cvec_wrong_norm_input(TestCase):
     114
     115    def test_wrong_length(self):
     116        with self.assertRaises(ValueError):
     117            cvec(-1)
     118
     119    def test_set_norm_with_scalar(self):
     120        a = cvec(512)
     121        with self.assertRaises(ValueError):
     122            a.norm = 1
     123
     124    def test_set_norm_with_scalar_array(self):
     125        a = cvec(512)
     126        with self.assertRaises(ValueError):
     127            a.norm = np.ndarray(1, dtype = 'int')
     128
     129    def test_set_norm_with_int_array(self):
     130        a = cvec(512)
     131        with self.assertRaises(ValueError):
     132            a.norm = np.zeros(512//2+1, dtype = 'int')
     133
     134    def test_set_norm_with_wrong_float_array(self):
     135        a = cvec(512)
     136        with self.assertRaises(ValueError):
     137            a.norm = np.zeros(512//2+1, dtype = wrong_type)
     138
     139    def test_set_norm_with_wrong_2d_array(self):
     140        a = cvec(512)
     141        with self.assertRaises(ValueError):
     142            a.norm = np.zeros((512//2+1, 2), dtype = float_type)
     143
    49144if __name__ == '__main__':
    50     from unittest import main
    51145    main()
Note: See TracChangeset for help on using the changeset viewer.