test_regression.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import numpy as np
  2. from numpy.testing import (
  3. assert_, assert_array_equal, assert_allclose, suppress_warnings
  4. )
  5. class TestRegression:
  6. def test_masked_array_create(self):
  7. # Ticket #17
  8. x = np.ma.masked_array([0, 1, 2, 3, 0, 4, 5, 6],
  9. mask=[0, 0, 0, 1, 1, 1, 0, 0])
  10. assert_array_equal(np.ma.nonzero(x), [[1, 2, 6, 7]])
  11. def test_masked_array(self):
  12. # Ticket #61
  13. np.ma.array(1, mask=[1])
  14. def test_mem_masked_where(self):
  15. # Ticket #62
  16. from numpy.ma import masked_where, MaskType
  17. a = np.zeros((1, 1))
  18. b = np.zeros(a.shape, MaskType)
  19. c = masked_where(b, a)
  20. a-c
  21. def test_masked_array_multiply(self):
  22. # Ticket #254
  23. a = np.ma.zeros((4, 1))
  24. a[2, 0] = np.ma.masked
  25. b = np.zeros((4, 2))
  26. a*b
  27. b*a
  28. def test_masked_array_repeat(self):
  29. # Ticket #271
  30. np.ma.array([1], mask=False).repeat(10)
  31. def test_masked_array_repr_unicode(self):
  32. # Ticket #1256
  33. repr(np.ma.array("Unicode"))
  34. def test_atleast_2d(self):
  35. # Ticket #1559
  36. a = np.ma.masked_array([0.0, 1.2, 3.5], mask=[False, True, False])
  37. b = np.atleast_2d(a)
  38. assert_(a.mask.ndim == 1)
  39. assert_(b.mask.ndim == 2)
  40. def test_set_fill_value_unicode_py3(self):
  41. # Ticket #2733
  42. a = np.ma.masked_array(['a', 'b', 'c'], mask=[1, 0, 0])
  43. a.fill_value = 'X'
  44. assert_(a.fill_value == 'X')
  45. def test_var_sets_maskedarray_scalar(self):
  46. # Issue gh-2757
  47. a = np.ma.array(np.arange(5), mask=True)
  48. mout = np.ma.array(-1, dtype=float)
  49. a.var(out=mout)
  50. assert_(mout._data == 0)
  51. def test_ddof_corrcoef(self):
  52. # See gh-3336
  53. x = np.ma.masked_equal([1, 2, 3, 4, 5], 4)
  54. y = np.array([2, 2.5, 3.1, 3, 5])
  55. # this test can be removed after deprecation.
  56. with suppress_warnings() as sup:
  57. sup.filter(DeprecationWarning, "bias and ddof have no effect")
  58. r0 = np.ma.corrcoef(x, y, ddof=0)
  59. r1 = np.ma.corrcoef(x, y, ddof=1)
  60. # ddof should not have an effect (it gets cancelled out)
  61. assert_allclose(r0.data, r1.data)
  62. def test_mask_not_backmangled(self):
  63. # See gh-10314. Test case taken from gh-3140.
  64. a = np.ma.MaskedArray([1., 2.], mask=[False, False])
  65. assert_(a.mask.shape == (2,))
  66. b = np.tile(a, (2, 1))
  67. # Check that the above no longer changes a.shape to (1, 2)
  68. assert_(a.mask.shape == (2,))
  69. assert_(b.shape == (2, 2))
  70. assert_(b.mask.shape == (2, 2))
  71. def test_empty_list_on_structured(self):
  72. # See gh-12464. Indexing with empty list should give empty result.
  73. ma = np.ma.MaskedArray([(1, 1.), (2, 2.), (3, 3.)], dtype='i4,f4')
  74. assert_array_equal(ma[[]], ma[:0])
  75. def test_masked_array_tobytes_fortran(self):
  76. ma = np.ma.arange(4).reshape((2,2))
  77. assert_array_equal(ma.tobytes(order='F'), ma.T.tobytes())