test_regression.py 3.1 KB

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