API_CHANGES.txt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. .. -*- rest -*-
  2. ==================================================
  3. API changes in the new masked array implementation
  4. ==================================================
  5. Masked arrays are subclasses of ndarray
  6. ---------------------------------------
  7. Contrary to the original implementation, masked arrays are now regular
  8. ndarrays::
  9. >>> x = masked_array([1,2,3],mask=[0,0,1])
  10. >>> print isinstance(x, numpy.ndarray)
  11. True
  12. ``_data`` returns a view of the masked array
  13. --------------------------------------------
  14. Masked arrays are composed of a ``_data`` part and a ``_mask``. Accessing the
  15. ``_data`` part will return a regular ndarray or any of its subclass, depending
  16. on the initial data::
  17. >>> x = masked_array(numpy.matrix([[1,2],[3,4]]),mask=[[0,0],[0,1]])
  18. >>> print x._data
  19. [[1 2]
  20. [3 4]]
  21. >>> print type(x._data)
  22. <class 'numpy.matrixlib.defmatrix.matrix'>
  23. In practice, ``_data`` is implemented as a property, not as an attribute.
  24. Therefore, you cannot access it directly, and some simple tests such as the
  25. following one will fail::
  26. >>>x._data is x._data
  27. False
  28. ``filled(x)`` can return a subclass of ndarray
  29. ----------------------------------------------
  30. The function ``filled(a)`` returns an array of the same type as ``a._data``::
  31. >>> x = masked_array(numpy.matrix([[1,2],[3,4]]),mask=[[0,0],[0,1]])
  32. >>> y = filled(x)
  33. >>> print type(y)
  34. <class 'numpy.matrixlib.defmatrix.matrix'>
  35. >>> print y
  36. matrix([[ 1, 2],
  37. [ 3, 999999]])
  38. ``put``, ``putmask`` behave like their ndarray counterparts
  39. -----------------------------------------------------------
  40. Previously, ``putmask`` was used like this::
  41. mask = [False,True,True]
  42. x = array([1,4,7],mask=mask)
  43. putmask(x,mask,[3])
  44. which translated to::
  45. x[~mask] = [3]
  46. (Note that a ``True``-value in a mask suppresses a value.)
  47. In other words, the mask had the same length as ``x``, whereas
  48. ``values`` had ``sum(~mask)`` elements.
  49. Now, the behaviour is similar to that of ``ndarray.putmask``, where
  50. the mask and the values are both the same length as ``x``, i.e.
  51. ::
  52. putmask(x,mask,[3,0,0])
  53. ``fill_value`` is a property
  54. ----------------------------
  55. ``fill_value`` is no longer a method, but a property::
  56. >>> print x.fill_value
  57. 999999
  58. ``cumsum`` and ``cumprod`` ignore missing values
  59. ------------------------------------------------
  60. Missing values are assumed to be the identity element, i.e. 0 for
  61. ``cumsum`` and 1 for ``cumprod``::
  62. >>> x = N.ma.array([1,2,3,4],mask=[False,True,False,False])
  63. >>> print x
  64. [1 -- 3 4]
  65. >>> print x.cumsum()
  66. [1 -- 4 8]
  67. >> print x.cumprod()
  68. [1 -- 3 12]
  69. ``bool(x)`` raises a ValueError
  70. -------------------------------
  71. Masked arrays now behave like regular ``ndarrays``, in that they cannot be
  72. converted to booleans:
  73. ::
  74. >>> x = N.ma.array([1,2,3])
  75. >>> bool(x)
  76. Traceback (most recent call last):
  77. File "<stdin>", line 1, in <module>
  78. ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
  79. ==================================
  80. New features (non exhaustive list)
  81. ==================================
  82. ``mr_``
  83. -------
  84. ``mr_`` mimics the behavior of ``r_`` for masked arrays::
  85. >>> np.ma.mr_[3,4,5]
  86. masked_array(data = [3 4 5],
  87. mask = False,
  88. fill_value=999999)
  89. ``anom``
  90. --------
  91. The ``anom`` method returns the deviations from the average (anomalies).