File: list_input_test.py

package info (click to toggle)
bottleneck 1.2.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 564 kB
  • sloc: ansic: 4,414; python: 1,742; makefile: 68
file content (53 lines) | stat: -rw-r--r-- 1,664 bytes parent folder | download | duplicates (3)
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
"Check that functions can handle list input"

import warnings

import numpy as np
from numpy.testing import assert_array_almost_equal
import bottleneck as bn
from .util import DTYPES


def test_list_input():
    "Check that functions can handle list input"
    for func in bn.get_functions('all'):
        if func.__name__ != 'replace':
            yield unit_maker, func


def lists(dtypes=DTYPES):
    "Iterator that yields lists to use for unit testing."
    ss = {}
    ss[1] = {'size':  4, 'shapes': [(4,)]}
    ss[2] = {'size':  6, 'shapes': [(1, 6), (2, 3)]}
    ss[3] = {'size':  6, 'shapes': [(1, 2, 3)]}
    ss[4] = {'size': 24, 'shapes': [(1, 2, 3, 4)]}
    for ndim in ss:
        size = ss[ndim]['size']
        shapes = ss[ndim]['shapes']
        a = np.arange(size)
        for shape in shapes:
            a = a.reshape(shape)
            for dtype in dtypes:
                yield a.astype(dtype).tolist()


def unit_maker(func):
    "Test that bn.xxx gives the same output as bn.slow.xxx for list input."
    msg = '\nfunc %s | input %s (%s) | shape %s\n'
    msg += '\nInput array:\n%s\n'
    name = func.__name__
    func0 = eval('bn.slow.%s' % name)
    for i, a in enumerate(lists()):
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            try:
                actual = func(a)
                desired = func0(a)
            except TypeError:
                actual = func(a, 2)
                desired = func0(a, 2)
        a = np.array(a)
        tup = (name, 'a'+str(i), str(a.dtype), str(a.shape), a)
        err_msg = msg % tup
        assert_array_almost_equal(actual, desired, err_msg=err_msg)