test_regression.py 8.1 KB

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