__init__.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. from numpy import show_config as show_numpy_config
  53. if show_numpy_config is None:
  54. raise ImportError(
  55. "Cannot import SciPy when running from NumPy source directory.")
  56. from numpy import __version__ as __numpy_version__
  57. # Import numpy symbols to scipy name space (DEPRECATED)
  58. from ._lib.deprecation import _deprecated
  59. import numpy as np
  60. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  61. 'use numpy.{0} instead')
  62. # deprecate callable objects from numpy, skipping classes and modules
  63. import types as _types # noqa: E402
  64. for _key in np.__all__:
  65. if _key.startswith('_'):
  66. continue
  67. _fun = getattr(np, _key)
  68. if isinstance(_fun, _types.ModuleType):
  69. continue
  70. if callable(_fun) and not isinstance(_fun, type):
  71. _fun = _deprecated(_msg.format(_key))(_fun)
  72. globals()[_key] = _fun
  73. del np, _types
  74. from numpy.random import rand, randn
  75. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  76. 'use numpy.random.{0} instead')
  77. rand = _deprecated(_msg.format('rand'))(rand)
  78. randn = _deprecated(_msg.format('randn'))(randn)
  79. # fft is especially problematic, so was removed in SciPy 1.6.0
  80. from numpy.fft import ifft
  81. ifft = _deprecated('scipy.ifft is deprecated and will be removed in SciPy '
  82. '2.0.0, use scipy.fft.ifft instead')(ifft)
  83. from numpy.lib import scimath # noqa: E402
  84. _msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, '
  85. 'use numpy.lib.scimath.{0} instead')
  86. for _key in scimath.__all__:
  87. _fun = getattr(scimath, _key)
  88. if callable(_fun):
  89. _fun = _deprecated(_msg.format(_key))(_fun)
  90. globals()[_key] = _fun
  91. del scimath
  92. del _msg, _fun, _key, _deprecated
  93. # We first need to detect if we're being called as part of the SciPy
  94. # setup procedure itself in a reliable manner.
  95. try:
  96. __SCIPY_SETUP__
  97. except NameError:
  98. __SCIPY_SETUP__ = False
  99. if __SCIPY_SETUP__:
  100. import sys
  101. sys.stderr.write('Running from SciPy source directory.\n')
  102. del sys
  103. else:
  104. try:
  105. from scipy.__config__ import show as show_config
  106. except ImportError as e:
  107. msg = """Error importing SciPy: you cannot import SciPy while
  108. being in scipy source directory; please exit the SciPy source
  109. tree first and relaunch your Python interpreter."""
  110. raise ImportError(msg) from e
  111. from scipy.version import version as __version__
  112. # Allow distributors to run custom init code
  113. from . import _distributor_init
  114. del _distributor_init
  115. from scipy._lib import _pep440
  116. # In maintenance branch, change to np_maxversion N+3 if numpy is at N
  117. # See setup.py for more details
  118. np_minversion = '1.19.5'
  119. np_maxversion = '1.27.0'
  120. if (_pep440.parse(__numpy_version__) < _pep440.Version(np_minversion) or
  121. _pep440.parse(__numpy_version__) >= _pep440.Version(np_maxversion)):
  122. import warnings
  123. warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
  124. f" is required for this version of SciPy (detected "
  125. f"version {__numpy_version__})",
  126. UserWarning)
  127. del _pep440
  128. # This is the first import of an extension module within SciPy. If there's
  129. # a general issue with the install, such that extension modules are missing
  130. # or cannot be imported, this is where we'll get a failure - so give an
  131. # informative error message.
  132. try:
  133. from scipy._lib._ccallback import LowLevelCallable
  134. except ImportError as e:
  135. msg = "The `scipy` install you are using seems to be broken, " + \
  136. "(extension modules cannot be imported), " + \
  137. "please try reinstalling."
  138. raise ImportError(msg) from e
  139. from scipy._lib._testutils import PytestTester
  140. test = PytestTester(__name__)
  141. del PytestTester
  142. submodules = [
  143. 'cluster',
  144. 'datasets',
  145. 'fft',
  146. 'fftpack',
  147. 'integrate',
  148. 'interpolate',
  149. 'io',
  150. 'linalg',
  151. 'misc',
  152. 'ndimage',
  153. 'odr',
  154. 'optimize',
  155. 'signal',
  156. 'sparse',
  157. 'spatial',
  158. 'special',
  159. 'stats'
  160. ]
  161. __all__ = submodules + [
  162. 'LowLevelCallable',
  163. 'test',
  164. 'show_config',
  165. '__version__',
  166. '__numpy_version__'
  167. ]
  168. def __dir__():
  169. return __all__
  170. import importlib as _importlib
  171. def __getattr__(name):
  172. if name in submodules:
  173. return _importlib.import_module(f'scipy.{name}')
  174. else:
  175. try:
  176. return globals()[name]
  177. except KeyError:
  178. raise AttributeError(
  179. f"Module 'scipy' has no attribute '{name}'"
  180. )