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 msg = """
26
27IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
28
29Importing the numpy C-extensions failed. This error can happen for
30many reasons, often due to issues with your setup or how NumPy was
31installed.
32
33We have compiled some common reasons and troubleshooting tips at:
34
35 https://numpy.org/devdocs/user/troubleshooting-importerror.html
36
37Please note and check the following:
38
39 * The Python version is: Python%d.%d from "%s"
40 * The NumPy version is: "%s"
41
42and make sure that they are the versions you expect.
43Please carefully study the documentation linked above for further help.
44
45Original error was: %s
46""" % (sys.version_info[0], sys.version_info[1], sys.executable,
47 __version__, exc)
48 raise ImportError(msg) from exc
49finally:
50 for envkey in env_added:
51 del os.environ[envkey]
52del envkey
53del env_added
54del os
55
56from . import umath
57
58# Check that multiarray,umath are pure python modules wrapping
59# _multiarray_umath and not either of the old c-extension modules
60if not (hasattr(multiarray, '_multiarray_umath') and
61 hasattr(umath, '_multiarray_umath')):
62 import sys
63 path = sys.modules['numpy'].__path__
64 msg = ("Something is wrong with the numpy installation. "
65 "While importing we detected an older version of "
66 "numpy in {}. One method of fixing this is to repeatedly uninstall "
67 "numpy until none is found, then reinstall this version.")
68 raise ImportError(msg.format(path))
69
70from . import numerictypes as nt
71from .numerictypes import sctypeDict, sctypes
72
73multiarray.set_typeDict(nt.sctypeDict)
74from . import (
75 _machar,
76 einsumfunc,
77 fromnumeric,
78 function_base,
79 getlimits,
80 numeric,
81 shape_base,
82)
83from .einsumfunc import *
84from .fromnumeric import *
85from .function_base import *
86from .getlimits import *
87
88# Note: module name memmap is overwritten by a class with same name
89from .memmap import *
90from .numeric import *
91from .records import recarray, record
92from .shape_base import *
93
94del nt
95
96# do this after everything else, to minimize the chance of this misleadingly
97# appearing in an import-time traceback
98# add these for module-freeze analysis (like PyInstaller)
99from . import (
100 _add_newdocs,
101 _add_newdocs_scalars,
102 _dtype,
103 _dtype_ctypes,
104 _internal,
105 _methods,
106)
107from .numeric import absolute as abs
108
109acos = numeric.arccos
110acosh = numeric.arccosh
111asin = numeric.arcsin
112asinh = numeric.arcsinh
113atan = numeric.arctan
114atanh = numeric.arctanh
115atan2 = numeric.arctan2
116concat = numeric.concatenate
117bitwise_left_shift = numeric.left_shift
118bitwise_invert = numeric.invert
119bitwise_right_shift = numeric.right_shift
120permute_dims = numeric.transpose
121pow = numeric.power
122
123__all__ = [
124 "abs", "acos", "acosh", "asin", "asinh", "atan", "atanh", "atan2",
125 "bitwise_invert", "bitwise_left_shift", "bitwise_right_shift", "concat",
126 "pow", "permute_dims", "memmap", "sctypeDict", "record", "recarray"
127]
128__all__ += numeric.__all__
129__all__ += function_base.__all__
130__all__ += getlimits.__all__
131__all__ += shape_base.__all__
132__all__ += einsumfunc.__all__
133
134
135def _ufunc_reduce(func):
136 # Report the `__name__`. pickle will try to find the module. Note that
137 # pickle supports for this `__name__` to be a `__qualname__`. It may
138 # make sense to add a `__qualname__` to ufuncs, to allow this more
139 # explicitly (Numba has ufuncs as attributes).
140 # See also: https://github.com/dask/distributed/issues/3450
141 return func.__name__
142
143
144def _DType_reconstruct(scalar_type):
145 # This is a work-around to pickle type(np.dtype(np.float64)), etc.
146 # and it should eventually be replaced with a better solution, e.g. when
147 # DTypes become HeapTypes.
148 return type(dtype(scalar_type))
149
150
151def _DType_reduce(DType):
152 # As types/classes, most DTypes can simply be pickled by their name:
153 if not DType._legacy or DType.__module__ == "numpy.dtypes":
154 return DType.__name__
155
156 # However, user defined legacy dtypes (like rational) do not end up in
157 # `numpy.dtypes` as module and do not have a public class at all.
158 # For these, we pickle them by reconstructing them from the scalar type:
159 scalar_type = DType.type
160 return _DType_reconstruct, (scalar_type,)
161
162
163def __getattr__(name):
164 # Deprecated 2022-11-22, NumPy 1.25.
165 if name == "MachAr":
166 import warnings
167 warnings.warn(
168 "The `np._core.MachAr` is considered private API (NumPy 1.24)",
169 DeprecationWarning, stacklevel=2,
170 )
171 return _machar.MachAr
172 raise AttributeError(f"Module {__name__!r} has no attribute {name!r}")
173
174
175import copyreg
176
177copyreg.pickle(ufunc, _ufunc_reduce)
178copyreg.pickle(type(dtype), _DType_reduce, _DType_reconstruct)
179
180# Unclutter namespace (must keep _*_reconstruct for unpickling)
181del copyreg, _ufunc_reduce, _DType_reduce
182
183from numpy._pytesttester import PytestTester
184
185test = PytestTester(__name__)
186del PytestTester