test_regression.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import numpy as np
  2. from numpy.testing import assert_, assert_array_equal
  3. class TestRegression:
  4. def test_masked_array_create(self):
  5. # Ticket #17
  6. x = np.ma.masked_array([0, 1, 2, 3, 0, 4, 5, 6],
  7. mask=[0, 0, 0, 1, 1, 1, 0, 0])
  8. assert_array_equal(np.ma.nonzero(x), [[1, 2, 6, 7]])
  9. def test_masked_array(self):
  10. # Ticket #61
  11. np.ma.array(1, mask=[1])
  12. def test_mem_masked_where(self):
  13. # Ticket #62
  14. from numpy.ma import MaskType, masked_where
  15. a = np.zeros((1, 1))
  16. b = np.zeros(a.shape, MaskType)
  17. c = masked_where(b, a)
  18. a - c
  19. def test_masked_array_multiply(self):
  20. # Ticket #254
  21. a = np.ma.zeros((4, 1))
  22. a[2, 0] = np.ma.masked
  23. b = np.zeros((4, 2))
  24. a * b
  25. b * a
  26. def test_masked_array_repeat(self):
  27. # Ticket #271
  28. np.ma.array([1], mask=False).repeat(10)
  29. def test_masked_array_repr_unicode(self):
  30. # Ticket #1256
  31. repr(np.ma.array("Unicode"))
  32. def test_atleast_2d(self):
  33. # Ticket #1559
  34. a = np.ma.masked_array([0.0, 1.2, 3.5], mask=[False, True, False])
  35. b = np.atleast_2d(a)
  36. assert_(a.mask.ndim == 1)
  37. assert_(b.mask.ndim == 2)
  38. def test_set_fill_value_unicode_py3(self):
  39. # Ticket #2733
  40. a = np.ma.masked_array(['a', 'b', 'c'], mask=[1, 0, 0])
  41. a.fill_value = 'X'
  42. assert_(a.fill_value == 'X')
  43. def test_var_sets_maskedarray_scalar(self):
  44. # Issue gh-2757
  45. a = np.ma.array(np.arange(5), mask=True)
  46. mout = np.ma.array(-1, dtype=float)
  47. a.var(out=mout)
  48. assert_(mout._data == 0)
  49. def test_mask_not_backmangled(self):
  50. # See gh-10314. Test case taken from gh-3140.
  51. a = np.ma.MaskedArray([1., 2.], mask=[False, False])
  52. assert_(a.mask.shape == (2,))
  53. b = np.tile(a, (2, 1))
  54. # Check that the above no longer changes a.shape to (1, 2)
  55. assert_(a.mask.shape == (2,))
  56. assert_(b.shape == (2, 2))
  57. assert_(b.mask.shape == (2, 2))
  58. def test_empty_list_on_structured(self):
  59. # See gh-12464. Indexing with empty list should give empty result.
  60. ma = np.ma.MaskedArray([(1, 1.), (2, 2.), (3, 3.)], dtype='i4,f4')
  61. assert_array_equal(ma[[]], ma[:0])
  62. def test_masked_array_tobytes_fortran(self):
  63. ma = np.ma.arange(4).reshape((2, 2))
  64. assert_array_equal(ma.tobytes(order='F'), ma.T.tobytes())
  65. def test_structured_array(self):
  66. # see gh-22041
  67. np.ma.array((1, (b"", b"")),
  68. dtype=[("x", np.int_),
  69. ("y", [("i", np.void), ("j", np.void)])])