test_ufunclike.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from __future__ import division, absolute_import, print_function
  2. import numpy as np
  3. import numpy.core as nx
  4. import numpy.lib.ufunclike as ufl
  5. from numpy.testing import (
  6. assert_, assert_equal, assert_array_equal, assert_warns, assert_raises
  7. )
  8. class TestUfunclike(object):
  9. def test_isposinf(self):
  10. a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
  11. out = nx.zeros(a.shape, bool)
  12. tgt = nx.array([True, False, False, False, False, False])
  13. res = ufl.isposinf(a)
  14. assert_equal(res, tgt)
  15. res = ufl.isposinf(a, out)
  16. assert_equal(res, tgt)
  17. assert_equal(out, tgt)
  18. a = a.astype(np.complex)
  19. with assert_raises(TypeError):
  20. ufl.isposinf(a)
  21. def test_isneginf(self):
  22. a = nx.array([nx.inf, -nx.inf, nx.nan, 0.0, 3.0, -3.0])
  23. out = nx.zeros(a.shape, bool)
  24. tgt = nx.array([False, True, False, False, False, False])
  25. res = ufl.isneginf(a)
  26. assert_equal(res, tgt)
  27. res = ufl.isneginf(a, out)
  28. assert_equal(res, tgt)
  29. assert_equal(out, tgt)
  30. a = a.astype(np.complex)
  31. with assert_raises(TypeError):
  32. ufl.isneginf(a)
  33. def test_fix(self):
  34. a = nx.array([[1.0, 1.1, 1.5, 1.8], [-1.0, -1.1, -1.5, -1.8]])
  35. out = nx.zeros(a.shape, float)
  36. tgt = nx.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]])
  37. res = ufl.fix(a)
  38. assert_equal(res, tgt)
  39. res = ufl.fix(a, out)
  40. assert_equal(res, tgt)
  41. assert_equal(out, tgt)
  42. assert_equal(ufl.fix(3.14), 3)
  43. def test_fix_with_subclass(self):
  44. class MyArray(nx.ndarray):
  45. def __new__(cls, data, metadata=None):
  46. res = nx.array(data, copy=True).view(cls)
  47. res.metadata = metadata
  48. return res
  49. def __array_wrap__(self, obj, context=None):
  50. if isinstance(obj, MyArray):
  51. obj.metadata = self.metadata
  52. return obj
  53. def __array_finalize__(self, obj):
  54. self.metadata = getattr(obj, 'metadata', None)
  55. return self
  56. a = nx.array([1.1, -1.1])
  57. m = MyArray(a, metadata='foo')
  58. f = ufl.fix(m)
  59. assert_array_equal(f, nx.array([1, -1]))
  60. assert_(isinstance(f, MyArray))
  61. assert_equal(f.metadata, 'foo')
  62. # check 0d arrays don't decay to scalars
  63. m0d = m[0,...]
  64. m0d.metadata = 'bar'
  65. f0d = ufl.fix(m0d)
  66. assert_(isinstance(f0d, MyArray))
  67. assert_equal(f0d.metadata, 'bar')
  68. def test_deprecated(self):
  69. # NumPy 1.13.0, 2017-04-26
  70. assert_warns(DeprecationWarning, ufl.fix, [1, 2], y=nx.empty(2))
  71. assert_warns(DeprecationWarning, ufl.isposinf, [1, 2], y=nx.empty(2))
  72. assert_warns(DeprecationWarning, ufl.isneginf, [1, 2], y=nx.empty(2))
  73. def test_scalar(self):
  74. x = np.inf
  75. actual = np.isposinf(x)
  76. expected = np.True_
  77. assert_equal(actual, expected)
  78. assert_equal(type(actual), type(expected))
  79. x = -3.4
  80. actual = np.fix(x)
  81. expected = np.float64(-3.0)
  82. assert_equal(actual, expected)
  83. assert_equal(type(actual), type(expected))
  84. out = np.array(0.0)
  85. actual = np.fix(x, out=out)
  86. assert_(actual is out)