Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_ufunclike.py98 linesDownload Raw Back to tests
1import numpy as np
2from numpy import fix, isneginf, isposinf
3from numpy.testing import assert_, assert_array_equal, assert_equal, assert_raises
4
5
6class TestUfunclike:
7
8    def test_isposinf(self):
9        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
10        out = np.zeros(a.shape, bool)
11        tgt = np.array([True, False, False, False, False, False])
12
13        res = isposinf(a)
14        assert_equal(res, tgt)
15        res = isposinf(a, out)
16        assert_equal(res, tgt)
17        assert_equal(out, tgt)
18
19        a = a.astype(np.complex128)
20        with assert_raises(TypeError):
21            isposinf(a)
22
23    def test_isneginf(self):
24        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
25        out = np.zeros(a.shape, bool)
26        tgt = np.array([False, True, False, False, False, False])
27
28        res = isneginf(a)
29        assert_equal(res, tgt)
30        res = isneginf(a, out)
31        assert_equal(res, tgt)
32        assert_equal(out, tgt)
33
34        a = a.astype(np.complex128)
35        with assert_raises(TypeError):
36            isneginf(a)
37
38    def test_fix(self):
39        a = np.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
40        out = np.zeros(a.shape, float)
41        tgt = np.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]])
42
43        res = fix(a)
44        assert_equal(res, tgt)
45        res = fix(a, out)
46        assert_equal(res, tgt)
47        assert_equal(out, tgt)
48        assert_equal(fix(3.14), 3)
49
50    def test_fix_with_subclass(self):
51        class MyArray(np.ndarray):
52            def __new__(cls, data, metadata=None):
53                res = np.array(data, copy=True).view(cls)
54                res.metadata = metadata
55                return res
56
57            def __array_wrap__(self, obj, context=None, return_scalar=False):
58                if not isinstance(obj, MyArray):
59                    obj = obj.view(MyArray)
60                if obj.metadata is None:
61                    obj.metadata = self.metadata
62                return obj
63
64            def __array_finalize__(self, obj):
65                self.metadata = getattr(obj, 'metadata', None)
66                return self
67
68        a = np.array([1.1, -1.1])
69        m = MyArray(a, metadata='foo')
70        f = fix(m)
71        assert_array_equal(f, np.array([1, -1]))
72        assert_(isinstance(f, MyArray))
73        assert_equal(f.metadata, 'foo')
74
75        # check 0d arrays don't decay to scalars
76        m0d = m[0, ...]
77        m0d.metadata = 'bar'
78        f0d = fix(m0d)
79        assert_(isinstance(f0d, MyArray))
80        assert_equal(f0d.metadata, 'bar')
81
82    def test_scalar(self):
83        x = np.inf
84        actual = np.isposinf(x)
85        expected = np.True_
86        assert_equal(actual, expected)
87        assert_equal(type(actual), type(expected))
88
89        x = -3.4
90        actual = np.fix(x)
91        expected = np.float64(-3.0)
92        assert_equal(actual, expected)
93        assert_equal(type(actual), type(expected))
94
95        out = np.array(0.0)
96        actual = np.fix(x, out=out)
97        assert_(actual is out)
98 
codekingpro/portable-devtools · Team Ai