codekingpro/portable-devtools
114k
1import os
2
3import numpy as np
4from numpy.testing import (
5 _assert_valid_refcount,
6 assert_,
7 assert_array_almost_equal,
8 assert_array_equal,
9 assert_equal,
10 assert_raises,
11)
12
13
14class TestRegression:
15 def test_poly1d(self):
16 # Ticket #28
17 assert_equal(np.poly1d([1]) - np.poly1d([1, 0]),
18 np.poly1d([-1, 1]))
19
20 def test_cov_parameters(self):
21 # Ticket #91
22 x = np.random.random((3, 3))
23 y = x.copy()
24 np.cov(x, rowvar=True)
25 np.cov(y, rowvar=False)
26 assert_array_equal(x, y)
27
28 def test_mem_digitize(self):
29 # Ticket #95
30 for i in range(100):
31 np.digitize([1, 2, 3, 4], [1, 3])
32 np.digitize([0, 1, 2, 3, 4], [1, 3])
33
34 def test_unique_zero_sized(self):
35 # Ticket #205
36 assert_array_equal([], np.unique(np.array([])))
37
38 def test_mem_vectorise(self):
39 # Ticket #325
40 vt = np.vectorize(lambda *args: args)
41 vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1, 1, 2)))
42 vt(np.zeros((1, 2, 1)), np.zeros((2, 1, 1)), np.zeros((1,
43 1, 2)), np.zeros((2, 2)))
44
45 def test_mgrid_single_element(self):
46 # Ticket #339
47 assert_array_equal(np.mgrid[0:0:1j], [0])
48 assert_array_equal(np.mgrid[0:0], [])
49
50 def test_refcount_vectorize(self):
51 # Ticket #378
52 def p(x, y):
53 return 123
54 v = np.vectorize(p)
55 _assert_valid_refcount(v)
56
57 def test_poly1d_nan_roots(self):
58 # Ticket #396
59 p = np.poly1d([np.nan, np.nan, 1], r=False)
60 assert_raises(np.linalg.LinAlgError, getattr, p, "r")
61
62 def test_mem_polymul(self):
63 # Ticket #448
64 np.polymul([], [1.])
65
66 def test_mem_string_concat(self):
67 # Ticket #469
68 x = np.array([])
69 np.append(x, 'asdasd\tasdasd')
70
71 def test_poly_div(self):
72 # Ticket #553
73 u = np.poly1d([1, 2, 3])
74 v = np.poly1d([1, 2, 3, 4, 5])
75 q, r = np.polydiv(u, v)
76 assert_equal(q * v + r, u)
77
78 def test_poly_eq(self):
79 # Ticket #554
80 x = np.poly1d([1, 2, 3])
81 y = np.poly1d([3, 4])
82 assert_(x != y)
83 assert_(x == x)
84
85 def test_polyfit_build(self):
86 # Ticket #628
87 ref = [-1.06123820e-06, 5.70886914e-04, -1.13822012e-01,
88 9.95368241e+00, -3.14526520e+02]
89 x = [90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
90 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
91 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 129,
92 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141,
93 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
94 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
95 170, 171, 172, 173, 174, 175, 176]
96 y = [9.0, 3.0, 7.0, 4.0, 4.0, 8.0, 6.0, 11.0, 9.0, 8.0, 11.0, 5.0,
97 6.0, 5.0, 9.0, 8.0, 6.0, 10.0, 6.0, 10.0, 7.0, 6.0, 6.0, 6.0,
98 13.0, 4.0, 9.0, 11.0, 4.0, 5.0, 8.0, 5.0, 7.0, 7.0, 6.0, 12.0,
99 7.0, 7.0, 9.0, 4.0, 12.0, 6.0, 6.0, 4.0, 3.0, 9.0, 8.0, 8.0,
100 6.0, 7.0, 9.0, 10.0, 6.0, 8.0, 4.0, 7.0, 7.0, 10.0, 8.0, 8.0,
101 6.0, 3.0, 8.0, 4.0, 5.0, 7.0, 8.0, 6.0, 6.0, 4.0, 12.0, 9.0,
102 8.0, 8.0, 8.0, 6.0, 7.0, 4.0, 4.0, 5.0, 7.0]
103 tested = np.polyfit(x, y, 4)
104 assert_array_almost_equal(ref, tested)
105
106 def test_polydiv_type(self):
107 # Make polydiv work for complex types
108 msg = "Wrong type, should be complex"
109 x = np.ones(3, dtype=complex)
110 q, r = np.polydiv(x, x)
111 assert_(q.dtype == complex, msg)
112 msg = "Wrong type, should be float"
113 x = np.ones(3, dtype=int)
114 q, r = np.polydiv(x, x)
115 assert_(q.dtype == float, msg)
116
117 def test_histogramdd_too_many_bins(self):
118 # Ticket 928.
119 assert_raises(ValueError, np.histogramdd, np.ones((1, 10)), bins=2**10)
120
121 def test_polyint_type(self):
122 # Ticket #944
123 msg = "Wrong type, should be complex"
124 x = np.ones(3, dtype=complex)
125 assert_(np.polyint(x).dtype == complex, msg)
126 msg = "Wrong type, should be float"
127 x = np.ones(3, dtype=int)
128 assert_(np.polyint(x).dtype == float, msg)
129
130 def test_ndenumerate_crash(self):
131 # Ticket 1140
132 # Shouldn't crash:
133 list(np.ndenumerate(np.array([[]])))
134
135 def test_large_fancy_indexing(self):
136 # Large enough to fail on 64-bit.
137 nbits = np.dtype(np.intp).itemsize * 8
138 thesize = int((2**nbits)**(1.0 / 5.0) + 1)
139
140 def dp():
141 n = 3
142 a = np.ones((n,) * 5)
143 i = np.random.randint(0, n, size=thesize)
144 a[np.ix_(i, i, i, i, i)] = 0
145
146 def dp2():
147 n = 3
148 a = np.ones((n,) * 5)
149 i = np.random.randint(0, n, size=thesize)
150 a[np.ix_(i, i, i, i, i)]
151
152 assert_raises(ValueError, dp)
153 assert_raises(ValueError, dp2)
154
155 def test_void_coercion(self):
156 dt = np.dtype([('a', 'f4'), ('b', 'i4')])
157 x = np.zeros((1,), dt)
158 assert_(np.r_[x, x].dtype == dt)
159
160 def test_include_dirs(self):
161 # As a sanity check, just test that get_include
162 # includes something reasonable. Somewhat
163 # related to ticket #1405.
164 include_dirs = [np.get_include()]
165 for path in include_dirs:
166 assert_(isinstance(path, str))
167 assert_(path != '')
168
169 def test_polyder_return_type(self):
170 # Ticket #1249
171 assert_(isinstance(np.polyder(np.poly1d([1]), 0), np.poly1d))
172 assert_(isinstance(np.polyder([1], 0), np.ndarray))
173 assert_(isinstance(np.polyder(np.poly1d([1]), 1), np.poly1d))
174 assert_(isinstance(np.polyder([1], 1), np.ndarray))
175
176 def test_append_fields_dtype_list(self):
177 # Ticket #1676
178 from numpy.lib.recfunctions import append_fields
179
180 base = np.array([1, 2, 3], dtype=np.int32)
181 names = ['a', 'b', 'c']
182 data = np.eye(3).astype(np.int32)
183 dlist = [np.float64, np.int32, np.int32]
184 try:
185 append_fields(base, names, data, dlist)
186 except Exception:
187 raise AssertionError
188
189 def test_loadtxt_fields_subarrays(self):
190 # For ticket #1936
191 from io import StringIO
192
193 dt = [("a", 'u1', 2), ("b", 'u1', 2)]
194 x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
195 assert_equal(x, np.array([((0, 1), (2, 3))], dtype=dt))
196
197 dt = [("a", [("a", 'u1', (1, 3)), ("b", 'u1')])]
198 x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
199 assert_equal(x, np.array([(((0, 1, 2), 3),)], dtype=dt))
200
201 dt = [("a", 'u1', (2, 2))]
202 x = np.loadtxt(StringIO("0 1 2 3"), dtype=dt)
203 assert_equal(x, np.array([(((0, 1), (2, 3)),)], dtype=dt))
204
205 dt = [("a", 'u1', (2, 3, 2))]
206 x = np.loadtxt(StringIO("0 1 2 3 4 5 6 7 8 9 10 11"), dtype=dt)
207 data = [((((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 9), (10, 11))),)]
208 assert_equal(x, np.array(data, dtype=dt))
209
210 def test_nansum_with_boolean(self):
211 # gh-2978
212 a = np.zeros(2, dtype=bool)
213 try:
214 np.nansum(a)
215 except Exception:
216 raise AssertionError
217
218 def test_py3_compat(self):
219 # gh-2561
220 # Test if the oldstyle class test is bypassed in python3
221 class C:
222 """Old-style class in python2, normal class in python3"""
223 pass
224
225 out = open(os.devnull, 'w')
226 try:
227 np.info(C(), output=out)
228 except AttributeError:
229 raise AssertionError
230 finally:
231 out.close()
232 