__init__.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. """
  2. SciPy: A scientific computing package for Python
  3. ================================================
  4. Documentation is available in the docstrings and
  5. online at https://docs.scipy.org.
  6. Contents
  7. --------
  8. SciPy imports all the functions from the NumPy namespace, and in
  9. addition provides:
  10. Subpackages
  11. -----------
  12. Using any of these subpackages requires an explicit import. For example,
  13. ``import scipy.cluster``.
  14. ::
  15. cluster --- Vector Quantization / Kmeans
  16. datasets --- Dataset methods
  17. fft --- Discrete Fourier transforms
  18. fftpack --- Legacy discrete Fourier transforms
  19. integrate --- Integration routines
  20. interpolate --- Interpolation Tools
  21. io --- Data input and output
  22. linalg --- Linear algebra routines
  23. linalg.blas --- Wrappers to BLAS library
  24. linalg.lapack --- Wrappers to LAPACK library
  25. misc --- Various utilities that don't have
  26. another home.
  27. ndimage --- N-D image package
  28. odr --- Orthogonal Distance Regression
  29. optimize --- Optimization Tools
  30. signal --- Signal Processing Tools
  31. signal.windows --- Window functions
  32. sparse --- Sparse Matrices
  33. sparse.linalg --- Sparse Linear Algebra
  34. sparse.linalg.dsolve --- Linear Solvers
  35. sparse.linalg.dsolve.umfpack --- :Interface to the UMFPACK library:
  36. Conjugate Gradient Method (LOBPCG)
  37. sparse.linalg.eigen --- Sparse Eigenvalue Solvers
  38. sparse.linalg.eigen.lobpcg --- Locally Optimal Block Preconditioned
  39. Conjugate Gradient Method (LOBPCG)
  40. spatial --- Spatial data structures and algorithms
  41. special --- Special functions
  42. stats --- Statistical Functions
  43. Utility tools
  44. -------------
  45. ::
  46. test --- Run scipy unittests
  47. show_config --- Show scipy build configuration
  48. show_numpy_config --- Show numpy build configuration
  49. __version__ --- SciPy version string
  50. __numpy_version__ --- Numpy version string
  51. """
  52. # start delvewheel patch
  53. def _delvewheel_init_patch_1_3_1():
  54. import ctypes
  55. import os
  56. import platform
  57. import sys
  58. libs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'scipy.libs'))
  59. is_pyinstaller = getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS')
  60. is_conda_cpython = platform.python_implementation() == 'CPython' and (hasattr(ctypes.pythonapi, 'Anaconda_GetVersion') or 'packaged by conda-forge' in sys.version)
  61. if sys.version_info[:2] >= (3, 8) and not is_conda_cpython or sys.version_info[:2] >= (3, 10):
  62. if not is_pyinstaller or os.path.isdir(libs_dir):
  63. os.add_dll_directory(libs_dir)
  64. else:
  65. load_order_filepath = os.path.join(libs_dir, '.load-order-scipy-1.10.1')
  66. if not is_pyinstaller or os.path.isfile(load_order_filepath):
  67. with open(os.path.join(libs_dir, '.load-order-scipy-1.10.1')) as file:
  68. load_order = file.read().split()
  69. for lib in load_order:
  70. lib_path = os.path.join(os.path.join(libs_dir, lib))
  71. if not is_pyinstaller or os.path.isfile(lib_path):
  72. ctypes.WinDLL(lib_path)
  73. _delvewheel_init_patch_1_3_1()
  74. del _delvewheel_init_patch_1_3_1
  75. # end delvewheel patch
  76. from numpy import show_config as show_numpy_config
  77. if show_numpy_config is None:
  78. raise ImportError(
  79. "Cannot import SciPy when running from NumPy source directory.")
  80. from numpy import __version__ as __numpy_version__
  81. # Import numpy symbols to scipy name space (DEPRECATED)
  82. from ._lib.deprecation import _deprecated
  83. import numpy as np
  84. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  85. 'use numpy.{0} instead')
  86. # deprecate callable objects from numpy, skipping classes and modules
  87. import types as _types # noqa: E402
  88. for _key in np.__all__:
  89. if _key.startswith('_'):
  90. continue
  91. _fun = getattr(np, _key)
  92. if isinstance(_fun, _types.ModuleType):
  93. continue
  94. if callable(_fun) and not isinstance(_fun, type):
  95. _fun = _deprecated(_msg.format(_key))(_fun)
  96. globals()[_key] = _fun
  97. del np, _types
  98. from numpy.random import rand, randn
  99. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  100. 'use numpy.random.{0} instead')
  101. rand = _deprecated(_msg.format('rand'))(rand)
  102. randn = _deprecated(_msg.format('randn'))(randn)
  103. # fft is especially problematic, so was removed in SciPy 1.6.0
  104. from numpy.fft import ifft
  105. ifft = _deprecated('scipy.ifft is deprecated and will be removed in SciPy '
  106. '2.0.0, use scipy.fft.ifft instead')(ifft)
  107. from numpy.lib import scimath # noqa: E402
  108. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  109. 'use numpy.lib.scimath.{0} instead')
  110. for _key in scimath.__all__:
  111. _fun = getattr(scimath, _key)
  112. if callable(_fun):
  113. _fun = _deprecated(_msg.format(_key))(_fun)
  114. globals()[_key] = _fun
  115. del scimath
  116. del _msg, _fun, _key, _deprecated
  117. # We first need to detect if we're being called as part of the SciPy
  118. # setup procedure itself in a reliable manner.
  119. try:
  120. __SCIPY_SETUP__
  121. except NameError:
  122. __SCIPY_SETUP__ = False
  123. if __SCIPY_SETUP__:
  124. import sys
  125. sys.stderr.write('Running from SciPy source directory.\n')
  126. del sys
  127. else:
  128. try:
  129. from scipy.__config__ import show as show_config
  130. except ImportError as e:
  131. msg = """Error importing SciPy: you cannot import SciPy while
  132. being in scipy source directory; please exit the SciPy source
  133. tree first and relaunch your Python interpreter."""
  134. raise ImportError(msg) from e
  135. from scipy.version import version as __version__
  136. # Allow distributors to run custom init code
  137. from . import _distributor_init
  138. del _distributor_init
  139. from scipy._lib import _pep440
  140. # In maintenance branch, change to np_maxversion N+3 if numpy is at N
  141. # See setup.py for more details
  142. np_minversion = '1.19.5'
  143. np_maxversion = '1.27.0'
  144. if (_pep440.parse(__numpy_version__) < _pep440.Version(np_minversion) or
  145. _pep440.parse(__numpy_version__) >= _pep440.Version(np_maxversion)):
  146. import warnings
  147. warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
  148. f" is required for this version of SciPy (detected "
  149. f"version {__numpy_version__})",
  150. UserWarning)
  151. del _pep440
  152. # This is the first import of an extension module within SciPy. If there's
  153. # a general issue with the install, such that extension modules are missing
  154. # or cannot be imported, this is where we'll get a failure - so give an
  155. # informative error message.
  156. try:
  157. from scipy._lib._ccallback import LowLevelCallable
  158. except ImportError as e:
  159. msg = "The `scipy` install you are using seems to be broken, " + \
  160. "(extension modules cannot be imported), " + \
  161. "please try reinstalling."
  162. raise ImportError(msg) from e
  163. from scipy._lib._testutils import PytestTester
  164. test = PytestTester(__name__)
  165. del PytestTester
  166. submodules = [
  167. 'cluster',
  168. 'datasets',
  169. 'fft',
  170. 'fftpack',
  171. 'integrate',
  172. 'interpolate',
  173. 'io',
  174. 'linalg',
  175. 'misc',
  176. 'ndimage',
  177. 'odr',
  178. 'optimize',
  179. 'signal',
  180. 'sparse',
  181. 'spatial',
  182. 'special',
  183. 'stats'
  184. ]
  185. __all__ = submodules + [
  186. 'LowLevelCallable',
  187. 'test',
  188. 'show_config',
  189. '__version__',
  190. '__numpy_version__'
  191. ]
  192. def __dir__():
  193. return __all__
  194. import importlib as _importlib
  195. def __getattr__(name):
  196. if name in submodules:
  197. return _importlib.import_module(f'scipy.{name}')
  198. else:
  199. try:
  200. return globals()[name]
  201. except KeyError:
  202. raise AttributeError(
  203. f"Module 'scipy' has no attribute '{name}'"
  204. )