__init__.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """
  2. Contains the core of NumPy: ndarray, ufuncs, dtypes, etc.
  3. Please note that this module is private. All functions and objects
  4. are available in the main ``numpy`` namespace - use that instead.
  5. """
  6. from numpy.version import version as __version__
  7. import os
  8. # disables OpenBLAS affinity setting of the main thread that limits
  9. # python threads or processes to one core
  10. env_added = []
  11. for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']:
  12. if envkey not in os.environ:
  13. os.environ[envkey] = '1'
  14. env_added.append(envkey)
  15. try:
  16. from . import multiarray
  17. except ImportError as exc:
  18. import sys
  19. msg = """
  20. IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
  21. Importing the numpy C-extensions failed. This error can happen for
  22. many reasons, often due to issues with your setup or how NumPy was
  23. installed.
  24. We have compiled some common reasons and troubleshooting tips at:
  25. https://numpy.org/devdocs/user/troubleshooting-importerror.html
  26. Please note and check the following:
  27. * The Python version is: Python%d.%d from "%s"
  28. * The NumPy version is: "%s"
  29. and make sure that they are the versions you expect.
  30. Please carefully study the documentation linked above for further help.
  31. Original error was: %s
  32. """ % (sys.version_info[0], sys.version_info[1], sys.executable,
  33. __version__, exc)
  34. raise ImportError(msg)
  35. finally:
  36. for envkey in env_added:
  37. del os.environ[envkey]
  38. del envkey
  39. del env_added
  40. del os
  41. from . import umath
  42. # Check that multiarray,umath are pure python modules wrapping
  43. # _multiarray_umath and not either of the old c-extension modules
  44. if not (hasattr(multiarray, '_multiarray_umath') and
  45. hasattr(umath, '_multiarray_umath')):
  46. import sys
  47. path = sys.modules['numpy'].__path__
  48. msg = ("Something is wrong with the numpy installation. "
  49. "While importing we detected an older version of "
  50. "numpy in {}. One method of fixing this is to repeatedly uninstall "
  51. "numpy until none is found, then reinstall this version.")
  52. raise ImportError(msg.format(path))
  53. from . import numerictypes as nt
  54. multiarray.set_typeDict(nt.sctypeDict)
  55. from . import numeric
  56. from .numeric import *
  57. from . import fromnumeric
  58. from .fromnumeric import *
  59. from . import defchararray as char
  60. from . import records as rec
  61. from .records import *
  62. from .memmap import *
  63. from .defchararray import chararray
  64. from . import function_base
  65. from .function_base import *
  66. from . import machar
  67. from .machar import *
  68. from . import getlimits
  69. from .getlimits import *
  70. from . import shape_base
  71. from .shape_base import *
  72. from . import einsumfunc
  73. from .einsumfunc import *
  74. del nt
  75. from .fromnumeric import amax as max, amin as min, round_ as round
  76. from .numeric import absolute as abs
  77. # do this after everything else, to minimize the chance of this misleadingly
  78. # appearing in an import-time traceback
  79. from . import _add_newdocs
  80. from . import _add_newdocs_scalars
  81. # add these for module-freeze analysis (like PyInstaller)
  82. from . import _dtype_ctypes
  83. from . import _internal
  84. from . import _dtype
  85. from . import _methods
  86. __all__ = ['char', 'rec', 'memmap']
  87. __all__ += numeric.__all__
  88. __all__ += fromnumeric.__all__
  89. __all__ += rec.__all__
  90. __all__ += ['chararray']
  91. __all__ += function_base.__all__
  92. __all__ += machar.__all__
  93. __all__ += getlimits.__all__
  94. __all__ += shape_base.__all__
  95. __all__ += einsumfunc.__all__
  96. # We used to use `np.core._ufunc_reconstruct` to unpickle. This is unnecessary,
  97. # but old pickles saved before 1.20 will be using it, and there is no reason
  98. # to break loading them.
  99. def _ufunc_reconstruct(module, name):
  100. # The `fromlist` kwarg is required to ensure that `mod` points to the
  101. # inner-most module rather than the parent package when module name is
  102. # nested. This makes it possible to pickle non-toplevel ufuncs such as
  103. # scipy.special.expit for instance.
  104. mod = __import__(module, fromlist=[name])
  105. return getattr(mod, name)
  106. def _ufunc_reduce(func):
  107. # Report the `__name__`. pickle will try to find the module. Note that
  108. # pickle supports for this `__name__` to be a `__qualname__`. It may
  109. # make sense to add a `__qualname__` to ufuncs, to allow this more
  110. # explicitly (Numba has ufuncs as attributes).
  111. # See also: https://github.com/dask/distributed/issues/3450
  112. return func.__name__
  113. def _DType_reconstruct(scalar_type):
  114. # This is a work-around to pickle type(np.dtype(np.float64)), etc.
  115. # and it should eventually be replaced with a better solution, e.g. when
  116. # DTypes become HeapTypes.
  117. return type(dtype(scalar_type))
  118. def _DType_reduce(DType):
  119. # To pickle a DType without having to add top-level names, pickle the
  120. # scalar type for now (and assume that reconstruction will be possible).
  121. if DType is dtype:
  122. return "dtype" # must pickle `np.dtype` as a singleton.
  123. scalar_type = DType.type # pickle the scalar type for reconstruction
  124. return _DType_reconstruct, (scalar_type,)
  125. import copyreg
  126. copyreg.pickle(ufunc, _ufunc_reduce)
  127. copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct)
  128. # Unclutter namespace (must keep _*_reconstruct for unpickling)
  129. del copyreg
  130. del _ufunc_reduce
  131. del _DType_reduce
  132. from numpy._pytesttester import PytestTester
  133. test = PytestTester(__name__)
  134. del PytestTester