__init__.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. """
  2. NumPy
  3. =====
  4. Provides
  5. 1. An array object of arbitrary homogeneous items
  6. 2. Fast mathematical operations over arrays
  7. 3. Linear Algebra, Fourier Transforms, Random Number Generation
  8. How to use the documentation
  9. ----------------------------
  10. Documentation is available in two forms: docstrings provided
  11. with the code, and a loose standing reference guide, available from
  12. `the NumPy homepage <https://numpy.org>`_.
  13. We recommend exploring the docstrings using
  14. `IPython <https://ipython.org>`_, an advanced Python shell with
  15. TAB-completion and introspection capabilities. See below for further
  16. instructions.
  17. The docstring examples assume that `numpy` has been imported as ``np``::
  18. >>> import numpy as np
  19. Code snippets are indicated by three greater-than signs::
  20. >>> x = 42
  21. >>> x = x + 1
  22. Use the built-in ``help`` function to view a function's docstring::
  23. >>> help(np.sort)
  24. ... # doctest: +SKIP
  25. For some objects, ``np.info(obj)`` may provide additional help. This is
  26. particularly true if you see the line "Help on ufunc object:" at the top
  27. of the help() page. Ufuncs are implemented in C, not Python, for speed.
  28. The native Python help() does not know how to view their help, but our
  29. np.info() function does.
  30. To search for documents containing a keyword, do::
  31. >>> np.lookfor('keyword')
  32. ... # doctest: +SKIP
  33. General-purpose documents like a glossary and help on the basic concepts
  34. of numpy are available under the ``doc`` sub-module::
  35. >>> from numpy import doc
  36. >>> help(doc)
  37. ... # doctest: +SKIP
  38. Available subpackages
  39. ---------------------
  40. lib
  41. Basic functions used by several sub-packages.
  42. random
  43. Core Random Tools
  44. linalg
  45. Core Linear Algebra Tools
  46. fft
  47. Core FFT routines
  48. polynomial
  49. Polynomial tools
  50. testing
  51. NumPy testing tools
  52. distutils
  53. Enhancements to distutils with support for
  54. Fortran compilers support and more (for Python <= 3.11).
  55. Utilities
  56. ---------
  57. test
  58. Run numpy unittests
  59. show_config
  60. Show numpy build configuration
  61. matlib
  62. Make everything matrices.
  63. __version__
  64. NumPy version string
  65. Viewing documentation using IPython
  66. -----------------------------------
  67. Start IPython and import `numpy` usually under the alias ``np``: `import
  68. numpy as np`. Then, directly past or use the ``%cpaste`` magic to paste
  69. examples into the shell. To see which functions are available in `numpy`,
  70. type ``np.<TAB>`` (where ``<TAB>`` refers to the TAB key), or use
  71. ``np.*cos*?<ENTER>`` (where ``<ENTER>`` refers to the ENTER key) to narrow
  72. down the list. To view the docstring for a function, use
  73. ``np.cos?<ENTER>`` (to view the docstring) and ``np.cos??<ENTER>`` (to view
  74. the source code).
  75. Copies vs. in-place operation
  76. -----------------------------
  77. Most of the functions in `numpy` return a copy of the array argument
  78. (e.g., `np.sort`). In-place versions of these functions are often
  79. available as array methods, i.e. ``x = np.array([1,2,3]); x.sort()``.
  80. Exceptions to this rule are documented.
  81. """
  82. # start delvewheel patch
  83. def _delvewheel_patch_1_5_1():
  84. import os
  85. libs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'numpy.libs'))
  86. if os.path.isdir(libs_dir):
  87. os.add_dll_directory(libs_dir)
  88. _delvewheel_patch_1_5_1()
  89. del _delvewheel_patch_1_5_1
  90. # end delvewheel patch
  91. import sys
  92. import warnings
  93. from ._globals import _NoValue, _CopyMode
  94. # These exceptions were moved in 1.25 and are hidden from __dir__()
  95. from .exceptions import (
  96. ComplexWarning, ModuleDeprecationWarning, VisibleDeprecationWarning,
  97. TooHardError, AxisError)
  98. # If a version with git hash was stored, use that instead
  99. from . import version
  100. from .version import __version__
  101. # We first need to detect if we're being called as part of the numpy setup
  102. # procedure itself in a reliable manner.
  103. try:
  104. __NUMPY_SETUP__
  105. except NameError:
  106. __NUMPY_SETUP__ = False
  107. if __NUMPY_SETUP__:
  108. sys.stderr.write('Running from numpy source directory.\n')
  109. else:
  110. # Allow distributors to run custom init code before importing numpy.core
  111. from . import _distributor_init
  112. try:
  113. from numpy.__config__ import show as show_config
  114. except ImportError as e:
  115. msg = """Error importing numpy: you should not try to import numpy from
  116. its source directory; please exit the numpy source tree, and relaunch
  117. your python interpreter from there."""
  118. raise ImportError(msg) from e
  119. __all__ = [
  120. 'exceptions', 'ModuleDeprecationWarning', 'VisibleDeprecationWarning',
  121. 'ComplexWarning', 'TooHardError', 'AxisError']
  122. # mapping of {name: (value, deprecation_msg)}
  123. __deprecated_attrs__ = {}
  124. from . import core
  125. from .core import *
  126. from . import compat
  127. from . import exceptions
  128. from . import dtypes
  129. from . import lib
  130. # NOTE: to be revisited following future namespace cleanup.
  131. # See gh-14454 and gh-15672 for discussion.
  132. from .lib import *
  133. from . import linalg
  134. from . import fft
  135. from . import polynomial
  136. from . import random
  137. from . import ctypeslib
  138. from . import ma
  139. from . import matrixlib as _mat
  140. from .matrixlib import *
  141. # Deprecations introduced in NumPy 1.20.0, 2020-06-06
  142. import builtins as _builtins
  143. _msg = (
  144. "module 'numpy' has no attribute '{n}'.\n"
  145. "`np.{n}` was a deprecated alias for the builtin `{n}`. "
  146. "To avoid this error in existing code, use `{n}` by itself. "
  147. "Doing this will not modify any behavior and is safe. {extended_msg}\n"
  148. "The aliases was originally deprecated in NumPy 1.20; for more "
  149. "details and guidance see the original release note at:\n"
  150. " https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations")
  151. _specific_msg = (
  152. "If you specifically wanted the numpy scalar type, use `np.{}` here.")
  153. _int_extended_msg = (
  154. "When replacing `np.{}`, you may wish to use e.g. `np.int64` "
  155. "or `np.int32` to specify the precision. If you wish to review "
  156. "your current use, check the release note link for "
  157. "additional information.")
  158. _type_info = [
  159. ("object", ""), # The NumPy scalar only exists by name.
  160. ("bool", _specific_msg.format("bool_")),
  161. ("float", _specific_msg.format("float64")),
  162. ("complex", _specific_msg.format("complex128")),
  163. ("str", _specific_msg.format("str_")),
  164. ("int", _int_extended_msg.format("int"))]
  165. __former_attrs__ = {
  166. n: _msg.format(n=n, extended_msg=extended_msg)
  167. for n, extended_msg in _type_info
  168. }
  169. # Future warning introduced in NumPy 1.24.0, 2022-11-17
  170. _msg = (
  171. "`np.{n}` is a deprecated alias for `{an}`. (Deprecated NumPy 1.24)")
  172. # Some of these are awkward (since `np.str` may be preferable in the long
  173. # term), but overall the names ending in 0 seem undesirable
  174. _type_info = [
  175. ("bool8", bool_, "np.bool_"),
  176. ("int0", intp, "np.intp"),
  177. ("uint0", uintp, "np.uintp"),
  178. ("str0", str_, "np.str_"),
  179. ("bytes0", bytes_, "np.bytes_"),
  180. ("void0", void, "np.void"),
  181. ("object0", object_,
  182. "`np.object0` is a deprecated alias for `np.object_`. "
  183. "`object` can be used instead. (Deprecated NumPy 1.24)")]
  184. # Some of these could be defined right away, but most were aliases to
  185. # the Python objects and only removed in NumPy 1.24. Defining them should
  186. # probably wait for NumPy 1.26 or 2.0.
  187. # When defined, these should possibly not be added to `__all__` to avoid
  188. # import with `from numpy import *`.
  189. __future_scalars__ = {"bool", "long", "ulong", "str", "bytes", "object"}
  190. __deprecated_attrs__.update({
  191. n: (alias, _msg.format(n=n, an=an)) for n, alias, an in _type_info})
  192. import math
  193. __deprecated_attrs__['math'] = (math,
  194. "`np.math` is a deprecated alias for the standard library `math` "
  195. "module (Deprecated Numpy 1.25). Replace usages of `np.math` with "
  196. "`math`")
  197. del math, _msg, _type_info
  198. from .core import abs
  199. # now that numpy modules are imported, can initialize limits
  200. core.getlimits._register_known_types()
  201. __all__.extend(['__version__', 'show_config'])
  202. __all__.extend(core.__all__)
  203. __all__.extend(_mat.__all__)
  204. __all__.extend(lib.__all__)
  205. __all__.extend(['linalg', 'fft', 'random', 'ctypeslib', 'ma'])
  206. # Remove min and max from __all__ to avoid `from numpy import *` override
  207. # the builtins min/max. Temporary fix for 1.25.x/1.26.x, see gh-24229.
  208. __all__.remove('min')
  209. __all__.remove('max')
  210. __all__.remove('round')
  211. # Remove one of the two occurrences of `issubdtype`, which is exposed as
  212. # both `numpy.core.issubdtype` and `numpy.lib.issubdtype`.
  213. __all__.remove('issubdtype')
  214. # These are exported by np.core, but are replaced by the builtins below
  215. # remove them to ensure that we don't end up with `np.long == np.int_`,
  216. # which would be a breaking change.
  217. del long, unicode
  218. __all__.remove('long')
  219. __all__.remove('unicode')
  220. # Remove things that are in the numpy.lib but not in the numpy namespace
  221. # Note that there is a test (numpy/tests/test_public_api.py:test_numpy_namespace)
  222. # that prevents adding more things to the main namespace by accident.
  223. # The list below will grow until the `from .lib import *` fixme above is
  224. # taken care of
  225. __all__.remove('Arrayterator')
  226. del Arrayterator
  227. # These names were removed in NumPy 1.20. For at least one release,
  228. # attempts to access these names in the numpy namespace will trigger
  229. # a warning, and calling the function will raise an exception.
  230. _financial_names = ['fv', 'ipmt', 'irr', 'mirr', 'nper', 'npv', 'pmt',
  231. 'ppmt', 'pv', 'rate']
  232. __expired_functions__ = {
  233. name: (f'In accordance with NEP 32, the function {name} was removed '
  234. 'from NumPy version 1.20. A replacement for this function '
  235. 'is available in the numpy_financial library: '
  236. 'https://pypi.org/project/numpy-financial')
  237. for name in _financial_names}
  238. # Filter out Cython harmless warnings
  239. warnings.filterwarnings("ignore", message="numpy.dtype size changed")
  240. warnings.filterwarnings("ignore", message="numpy.ufunc size changed")
  241. warnings.filterwarnings("ignore", message="numpy.ndarray size changed")
  242. # oldnumeric and numarray were removed in 1.9. In case some packages import
  243. # but do not use them, we define them here for backward compatibility.
  244. oldnumeric = 'removed'
  245. numarray = 'removed'
  246. def __getattr__(attr):
  247. # Warn for expired attributes, and return a dummy function
  248. # that always raises an exception.
  249. import warnings
  250. import math
  251. try:
  252. msg = __expired_functions__[attr]
  253. except KeyError:
  254. pass
  255. else:
  256. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  257. def _expired(*args, **kwds):
  258. raise RuntimeError(msg)
  259. return _expired
  260. # Emit warnings for deprecated attributes
  261. try:
  262. val, msg = __deprecated_attrs__[attr]
  263. except KeyError:
  264. pass
  265. else:
  266. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  267. return val
  268. if attr in __future_scalars__:
  269. # And future warnings for those that will change, but also give
  270. # the AttributeError
  271. warnings.warn(
  272. f"In the future `np.{attr}` will be defined as the "
  273. "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
  274. if attr in __former_attrs__:
  275. raise AttributeError(__former_attrs__[attr])
  276. if attr == 'testing':
  277. import numpy.testing as testing
  278. return testing
  279. elif attr == 'Tester':
  280. "Removed in NumPy 1.25.0"
  281. raise RuntimeError("Tester was removed in NumPy 1.25.")
  282. raise AttributeError("module {!r} has no attribute "
  283. "{!r}".format(__name__, attr))
  284. def __dir__():
  285. public_symbols = globals().keys() | {'testing'}
  286. public_symbols -= {
  287. "core", "matrixlib",
  288. # These were moved in 1.25 and may be deprecated eventually:
  289. "ModuleDeprecationWarning", "VisibleDeprecationWarning",
  290. "ComplexWarning", "TooHardError", "AxisError"
  291. }
  292. return list(public_symbols)
  293. # Pytest testing
  294. from numpy._pytesttester import PytestTester
  295. test = PytestTester(__name__)
  296. del PytestTester
  297. def _sanity_check():
  298. """
  299. Quick sanity checks for common bugs caused by environment.
  300. There are some cases e.g. with wrong BLAS ABI that cause wrong
  301. results under specific runtime conditions that are not necessarily
  302. achieved during test suite runs, and it is useful to catch those early.
  303. See https://github.com/numpy/numpy/issues/8577 and other
  304. similar bug reports.
  305. """
  306. try:
  307. x = ones(2, dtype=float32)
  308. if not abs(x.dot(x) - float32(2.0)) < 1e-5:
  309. raise AssertionError()
  310. except AssertionError:
  311. msg = ("The current Numpy installation ({!r}) fails to "
  312. "pass simple sanity checks. This can be caused for example "
  313. "by incorrect BLAS library being linked in, or by mixing "
  314. "package managers (pip, conda, apt, ...). Search closed "
  315. "numpy issues for similar problems.")
  316. raise RuntimeError(msg.format(__file__)) from None
  317. _sanity_check()
  318. del _sanity_check
  319. def _mac_os_check():
  320. """
  321. Quick Sanity check for Mac OS look for accelerate build bugs.
  322. Testing numpy polyfit calls init_dgelsd(LAPACK)
  323. """
  324. try:
  325. c = array([3., 2., 1.])
  326. x = linspace(0, 2, 5)
  327. y = polyval(c, x)
  328. _ = polyfit(x, y, 2, cov=True)
  329. except ValueError:
  330. pass
  331. if sys.platform == "darwin":
  332. with warnings.catch_warnings(record=True) as w:
  333. _mac_os_check()
  334. # Throw runtime error, if the test failed Check for warning and error_message
  335. error_message = ""
  336. if len(w) > 0:
  337. error_message = "{}: {}".format(w[-1].category.__name__, str(w[-1].message))
  338. msg = (
  339. "Polyfit sanity test emitted a warning, most likely due "
  340. "to using a buggy Accelerate backend."
  341. "\nIf you compiled yourself, more information is available at:"
  342. "\nhttps://numpy.org/doc/stable/user/building.html#accelerated-blas-lapack-libraries"
  343. "\nOtherwise report this to the vendor "
  344. "that provided NumPy.\n{}\n".format(error_message))
  345. raise RuntimeError(msg)
  346. del _mac_os_check
  347. # We usually use madvise hugepages support, but on some old kernels it
  348. # is slow and thus better avoided.
  349. # Specifically kernel version 4.6 had a bug fix which probably fixed this:
  350. # https://github.com/torvalds/linux/commit/7cf91a98e607c2f935dbcc177d70011e95b8faff
  351. import os
  352. use_hugepage = os.environ.get("NUMPY_MADVISE_HUGEPAGE", None)
  353. if sys.platform == "linux" and use_hugepage is None:
  354. # If there is an issue with parsing the kernel version,
  355. # set use_hugepages to 0. Usage of LooseVersion will handle
  356. # the kernel version parsing better, but avoided since it
  357. # will increase the import time. See: #16679 for related discussion.
  358. try:
  359. use_hugepage = 1
  360. kernel_version = os.uname().release.split(".")[:2]
  361. kernel_version = tuple(int(v) for v in kernel_version)
  362. if kernel_version < (4, 6):
  363. use_hugepage = 0
  364. except ValueError:
  365. use_hugepages = 0
  366. elif use_hugepage is None:
  367. # This is not Linux, so it should not matter, just enable anyway
  368. use_hugepage = 1
  369. else:
  370. use_hugepage = int(use_hugepage)
  371. # Note that this will currently only make a difference on Linux
  372. core.multiarray._set_madvise_hugepage(use_hugepage)
  373. del use_hugepage
  374. # Give a warning if NumPy is reloaded or imported on a sub-interpreter
  375. # We do this from python, since the C-module may not be reloaded and
  376. # it is tidier organized.
  377. core.multiarray._multiarray_umath._reload_guard()
  378. # default to "weak" promotion for "NumPy 2".
  379. core._set_promotion_state(
  380. os.environ.get("NPY_PROMOTION_STATE",
  381. "weak" if _using_numpy2_behavior() else "legacy"))
  382. # Tell PyInstaller where to find hook-numpy.py
  383. def _pyinstaller_hooks_dir():
  384. from pathlib import Path
  385. return [str(Path(__file__).with_name("_pyinstaller").resolve())]
  386. # Remove symbols imported for internal use
  387. del os
  388. # Remove symbols imported for internal use
  389. del sys, warnings