m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.1 KiB

6 months ago
  1. """
  2. Functions in the ``as*array`` family that promote array-likes into arrays.
  3. `require` fits this category despite its name not matching this pattern.
  4. """
  5. from .overrides import (
  6. array_function_dispatch,
  7. set_array_function_like_doc,
  8. set_module,
  9. )
  10. from .multiarray import array, asanyarray
  11. __all__ = ["require"]
  12. def _require_dispatcher(a, dtype=None, requirements=None, *, like=None):
  13. return (like,)
  14. @set_array_function_like_doc
  15. @set_module('numpy')
  16. def require(a, dtype=None, requirements=None, *, like=None):
  17. """
  18. Return an ndarray of the provided type that satisfies requirements.
  19. This function is useful to be sure that an array with the correct flags
  20. is returned for passing to compiled code (perhaps through ctypes).
  21. Parameters
  22. ----------
  23. a : array_like
  24. The object to be converted to a type-and-requirement-satisfying array.
  25. dtype : data-type
  26. The required data-type. If None preserve the current dtype. If your
  27. application requires the data to be in native byteorder, include
  28. a byteorder specification as a part of the dtype specification.
  29. requirements : str or list of str
  30. The requirements list can be any of the following
  31. * 'F_CONTIGUOUS' ('F') - ensure a Fortran-contiguous array
  32. * 'C_CONTIGUOUS' ('C') - ensure a C-contiguous array
  33. * 'ALIGNED' ('A') - ensure a data-type aligned array
  34. * 'WRITEABLE' ('W') - ensure a writable array
  35. * 'OWNDATA' ('O') - ensure an array that owns its own data
  36. * 'ENSUREARRAY', ('E') - ensure a base array, instead of a subclass
  37. ${ARRAY_FUNCTION_LIKE}
  38. .. versionadded:: 1.20.0
  39. Returns
  40. -------
  41. out : ndarray
  42. Array with specified requirements and type if given.
  43. See Also
  44. --------
  45. asarray : Convert input to an ndarray.
  46. asanyarray : Convert to an ndarray, but pass through ndarray subclasses.
  47. ascontiguousarray : Convert input to a contiguous array.
  48. asfortranarray : Convert input to an ndarray with column-major
  49. memory order.
  50. ndarray.flags : Information about the memory layout of the array.
  51. Notes
  52. -----
  53. The returned array will be guaranteed to have the listed requirements
  54. by making a copy if needed.
  55. Examples
  56. --------
  57. >>> x = np.arange(6).reshape(2,3)
  58. >>> x.flags
  59. C_CONTIGUOUS : True
  60. F_CONTIGUOUS : False
  61. OWNDATA : False
  62. WRITEABLE : True
  63. ALIGNED : True
  64. WRITEBACKIFCOPY : False
  65. UPDATEIFCOPY : False
  66. >>> y = np.require(x, dtype=np.float32, requirements=['A', 'O', 'W', 'F'])
  67. >>> y.flags
  68. C_CONTIGUOUS : False
  69. F_CONTIGUOUS : True
  70. OWNDATA : True
  71. WRITEABLE : True
  72. ALIGNED : True
  73. WRITEBACKIFCOPY : False
  74. UPDATEIFCOPY : False
  75. """
  76. if like is not None:
  77. return _require_with_like(
  78. a,
  79. dtype=dtype,
  80. requirements=requirements,
  81. like=like,
  82. )
  83. possible_flags = {'C': 'C', 'C_CONTIGUOUS': 'C', 'CONTIGUOUS': 'C',
  84. 'F': 'F', 'F_CONTIGUOUS': 'F', 'FORTRAN': 'F',
  85. 'A': 'A', 'ALIGNED': 'A',
  86. 'W': 'W', 'WRITEABLE': 'W',
  87. 'O': 'O', 'OWNDATA': 'O',
  88. 'E': 'E', 'ENSUREARRAY': 'E'}
  89. if not requirements:
  90. return asanyarray(a, dtype=dtype)
  91. else:
  92. requirements = {possible_flags[x.upper()] for x in requirements}
  93. if 'E' in requirements:
  94. requirements.remove('E')
  95. subok = False
  96. else:
  97. subok = True
  98. order = 'A'
  99. if requirements >= {'C', 'F'}:
  100. raise ValueError('Cannot specify both "C" and "F" order')
  101. elif 'F' in requirements:
  102. order = 'F'
  103. requirements.remove('F')
  104. elif 'C' in requirements:
  105. order = 'C'
  106. requirements.remove('C')
  107. arr = array(a, dtype=dtype, order=order, copy=False, subok=subok)
  108. for prop in requirements:
  109. if not arr.flags[prop]:
  110. arr = arr.copy(order)
  111. break
  112. return arr
  113. _require_with_like = array_function_dispatch(
  114. _require_dispatcher
  115. )(require)