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