1"""
2Contains the core of NumPy: ndarray, ufuncs, dtypes, etc.
3
4Please note that this module is private. All functions and objects
5are available in the main ``numpy`` namespace - use that instead.
6
7"""
8
9import os
10
11from numpy.version import version as __version__
12
13# disables OpenBLAS affinity setting of the main thread that limits
14# python threads or processes to one core
15env_added = []
16for envkey in ['OPENBLAS_MAIN_FREE', 'GOTOBLAS_MAIN_FREE']:
17 if envkey not in os.environ:
18 os.environ[envkey] = '1'
19 env_added.append(envkey)
20
21try:
22 from . import multiarray
23except ImportError as exc:
24 import sys
25
26 # Bypass for the module re-initialization opt-out
27 if exc.msg == "cannot load module more than once per process":
28 raise
29
30 # Basically always, the problem should be that the C module is wrong/missing...
31 if (
32 isinstance(exc, ModuleNotFoundError)
33 and exc.name == "numpy._core._multiarray_umath"
34 ):
35 import sys
36 candidates = []
37 for path in __path__:
38 candidates.extend(
39 f for f in os.listdir(path) if f.startswith("_multiarray_umath"))
40 if len(candidates) == 0:
41 bad_c_module_info = (
42 "We found no compiled module, did NumPy build successfully?\n")
43 else:
44 candidate_str = '\n * '.join(candidates)
45 # cache_tag is documented to be possibly None, so just use name if it is
46 # this guesses at cache_tag being the same as the extension module scheme
47 tag = sys.implementation.cache_tag or sys.implementation.name
48 bad_c_module_info = (
49 f"The following compiled module files exist, but seem incompatible\n"
50 f"with with either python '{tag}' or the "
51 f"platform '{sys.platform}':\n\n * {candidate_str}\n"
52 )
53 else:
54 bad_c_module_info = ""
55
56 major, minor, *_ = sys.version_info
57 msg = f"""
58
59IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
60
61Importing the numpy C-extensions failed. This error can happen for
62many reasons, often due to issues with your setup or how NumPy was
63installed.
64{bad_c_module_info}
65We have compiled some common reasons and troubleshooting tips at:
66
67 https://numpy.org/devdocs/user/troubleshooting-importerror.html
68
69Please note and check the following:
70
71 * The Python version is: Python {major}.{minor} from "{sys.executable}"
72 * The NumPy version is: "{__version__}"
73
74and make sure that they are the versions you expect.
75
76Please carefully study the information and documentation linked above.
77This is unlikely to be a NumPy issue but will be caused by a bad install
78or environment on your machine.
79
80Original error was: {exc}
81"""
82
83 raise ImportError(msg) from exc
84finally:
85 for envkey in env_added:
86 del os.environ[envkey]
87del envkey
88del env_added
89del os
90
91from . import umath
92
93# Check that multiarray,umath are pure python modules wrapping
94# _multiarray_umath and not either of the old c-extension modules
95if not (hasattr(multiarray, '_multiarray_umath') and
96 hasattr(umath, '_multiarray_umath')):
97 import sys
98 path = sys.modules['numpy'].__path__
99 msg = ("Something is wrong with the numpy installation. "
100 "While importing we detected an older version of "
101 "numpy in {}. One method of fixing this is to repeatedly uninstall "
102 "numpy until none is found, then reinstall this version.")
103 raise ImportError(msg.format(path))
104
105from . import numerictypes as nt
106from .numerictypes import sctypeDict, sctypes
107
108multiarray.set_typeDict(nt.sctypeDict)
109from . import (
110 _machar,
111 einsumfunc,
112 fromnumeric,
113 function_base,
114 getlimits,
115 numeric,
116 shape_base,
117)
118from .einsumfunc import *
119from .fromnumeric import *
120from .function_base import *
121from .getlimits import *
122
123# Note: module name memmap is overwritten by a class with same name
124from .memmap import *
125from .numeric import *
126from .records import recarray, record
127from .shape_base import *
128
129del nt
130
131# do this after everything else, to minimize the chance of this misleadingly
132# appearing in an import-time traceback
133# add these for module-freeze analysis (like PyInstaller)
134from . import (
135 _add_newdocs,
136 _add_newdocs_scalars,
137 _dtype,
138 _dtype_ctypes,
139 _internal,
140 _methods,
141)
142from .numeric import absolute as abs
143
144acos = numeric.arccos
145acosh = numeric.arccosh
146asin = numeric.arcsin
147asinh = numeric.arcsinh
148atan = numeric.arctan
149atanh = numeric.arctanh
150atan2 = numeric.arctan2
151concat = numeric.concatenate
152bitwise_left_shift = numeric.left_shift
153bitwise_invert = numeric.invert
154bitwise_right_shift = numeric.right_shift
155permute_dims = numeric.transpose
156pow = numeric.power
157
158__all__ = [
159 "abs", "acos", "acosh", "asin", "asinh", "atan", "atanh", "atan2",
160 "bitwise_invert", "bitwise_left_shift", "bitwise_right_shift", "concat",
161 "pow", "permute_dims", "memmap", "sctypeDict", "record", "recarray"
162]
163__all__ += numeric.__all__
164__all__ += function_base.__all__
165__all__ += getlimits.__all__
166__all__ += shape_base.__all__
167__all__ += einsumfunc.__all__
168
169
170def _ufunc_reduce(func):
171 # Report the `__name__`. pickle will try to find the module. Note that
172 # pickle supports for this `__name__` to be a `__qualname__`. It may
173 # make sense to add a `__qualname__` to ufuncs, to allow this more
174 # explicitly (Numba has ufuncs as attributes).
175 # See also: https://github.com/dask/distributed/issues/3450
176 return func.__name__
177
178
179def _DType_reconstruct(scalar_type):
180 # This is a work-around to pickle type(np.dtype(np.float64)), etc.
181 # and it should eventually be replaced with a better solution, e.g. when
182 # DTypes become HeapTypes.
183 return type(dtype(scalar_type))
184
185
186def _DType_reduce(DType):
187 # As types/classes, most DTypes can simply be pickled by their name:
188 if not DType._legacy or DType.__module__ == "numpy.dtypes":
189 return DType.__name__
190
191 # However, user defined legacy dtypes (like rational) do not end up in
192 # `numpy.dtypes` as module and do not have a public class at all.
193 # For these, we pickle them by reconstructing them from the scalar type:
194 scalar_type = DType.type
195 return _DType_reconstruct, (scalar_type,)
196
197
198def __getattr__(name):
199 # Deprecated 2022-11-22, NumPy 1.25.
200 if name == "MachAr":
201 import warnings
202 warnings.warn(
203 "The `np._core.MachAr` is considered private API (NumPy 1.24)",
204 DeprecationWarning, stacklevel=2,
205 )
206 return _machar.MachAr
207 raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
208
209
210import copyreg
211
212copyreg.pickle(ufunc, _ufunc_reduce)
213copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct)
214
215# Unclutter namespace (must keep _*_reconstruct for unpickling)
216del copyreg, _ufunc_reduce, _DType_reduce
217
218from numpy._pytesttester import PytestTester
219
220test = PytestTester(__name__)
221del PytestTester