test_regression.py 8.3 KB

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