From: Paul Brossier Date: Wed, 31 Oct 2018 18:36:31 +0000 (+0100) Subject: Merge branch 'fix/pyfvec' X-Git-Tag: 0.4.8~89 X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=7a54b37160fa859f8decb918aebbea14dec4c31b;p=aubio.git Merge branch 'fix/pyfvec' --- diff --git a/python/lib/aubio/__init__.py b/python/lib/aubio/__init__.py index 87a357da..98c913cf 100644 --- a/python/lib/aubio/__init__.py +++ b/python/lib/aubio/__init__.py @@ -30,7 +30,7 @@ from .midiconv import * from .slicing import * class fvec(numpy.ndarray): - """fvec(input_arg=1024, **kwargs) + """fvec(input_arg=1024) A vector holding float samples. If `input_arg` is an `int`, a 1-dimensional vector of length `input_arg` @@ -43,16 +43,6 @@ class fvec(numpy.ndarray): input_arg : `int` or `array_like` Can be a positive integer, or any object that can be converted to a numpy array with :func:`numpy.array`. - **kwargs - Additional keyword arguments passed to :func:`numpy.zeros`, if - `input_arg` is an integer, or to :func:`numpy.array`. Should not - include `dtype`, which is already specified as - :data:`aubio.float_type`. - - Returns - ------- - numpy.ndarray - Array of shape `(length,)`. Examples -------- @@ -80,10 +70,15 @@ class fvec(numpy.ndarray): numpy.zeros : create a numpy array filled with zeros numpy.array : create a numpy array from an existing object """ - def __new__(cls, input_arg=1024, **kwargs): + def __new__(cls, input_arg=1024): if isinstance(input_arg, int): if input_arg == 0: raise ValueError("vector length of 1 or more expected") - return numpy.zeros(input_arg, dtype=float_type, **kwargs) + return numpy.zeros(input_arg, dtype=float_type, order='C') else: - return numpy.array(input_arg, dtype=float_type, **kwargs) + np_input = numpy.array(input_arg, dtype=float_type, order='C') + if len(np_input.shape) != 1: + raise ValueError("input_arg should have shape (n,)") + if np_input.shape[0] == 0: + raise ValueError("vector length of 1 or more expected") + return np_input diff --git a/python/tests/test_fvec.py b/python/tests/test_fvec.py index 4e50f0f0..60623ac9 100755 --- a/python/tests/test_fvec.py +++ b/python/tests/test_fvec.py @@ -60,6 +60,14 @@ class aubio_fvec_wrong_values(TestCase): self.assertRaises(IndexError, a.__getitem__, 3) self.assertRaises(IndexError, a.__getitem__, 2) + def test_wrong_dimensions(self): + a = np.array([[[1, 2], [3, 4]]], dtype=float_type) + self.assertRaises(ValueError, fvec, a) + + def test_wrong_size(self): + a = np.ndarray([0,], dtype=float_type) + self.assertRaises(ValueError, fvec, a) + class aubio_wrong_fvec_input(TestCase): """ uses min_removal to test PyAubio_IsValidVector """