Coverage for /usr/lib/python3/dist-packages/scipy/__init__.py: 78%

76 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-06-14 15:25 +0200

1""" 

2SciPy: A scientific computing package for Python 

3================================================ 

4 

5Documentation is available in the docstrings and 

6online at https://docs.scipy.org. 

7 

8Contents 

9-------- 

10SciPy imports all the functions from the NumPy namespace, and in 

11addition provides: 

12 

13Subpackages 

14----------- 

15Using any of these subpackages requires an explicit import. For example, 

16``import scipy.cluster``. 

17 

18:: 

19 

20 cluster --- Vector Quantization / Kmeans 

21 constants --- Physical and mathematical constants and units 

22 datasets --- Dataset methods 

23 fft --- Discrete Fourier transforms 

24 fftpack --- Legacy discrete Fourier transforms 

25 integrate --- Integration routines 

26 interpolate --- Interpolation Tools 

27 io --- Data input and output 

28 linalg --- Linear algebra routines 

29 misc --- Utilities that don't have another home. 

30 ndimage --- N-D image package 

31 odr --- Orthogonal Distance Regression 

32 optimize --- Optimization Tools 

33 signal --- Signal Processing Tools 

34 sparse --- Sparse Matrices 

35 spatial --- Spatial data structures and algorithms 

36 special --- Special functions 

37 stats --- Statistical Functions 

38 

39Public API in the main SciPy namespace 

40-------------------------------------- 

41:: 

42 

43 __version__ --- SciPy version string 

44 LowLevelCallable --- Low-level callback function 

45 show_config --- Show scipy build configuration 

46 test --- Run scipy unittests 

47 

48""" 

49 

50from numpy import show_config as show_numpy_config 

51if show_numpy_config is None: 

52 raise ImportError( 

53 "Cannot import SciPy when running from NumPy source directory.") 

54from numpy import __version__ as __numpy_version__ 

55 

56# Import numpy symbols to scipy name space (DEPRECATED) 

57from ._lib.deprecation import _deprecated 

58import numpy as np 

59_msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, ' 

60 'use numpy.{0} instead') 

61 

62# deprecate callable objects from numpy, skipping classes and modules 

63import types as _types # noqa: E402 

64for _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 

73del np, _types 

74 

75from numpy.random import rand, randn 

76_msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, ' 

77 'use numpy.random.{0} instead') 

78rand = _deprecated(_msg.format('rand'))(rand) 

79randn = _deprecated(_msg.format('randn'))(randn) 

80 

81# fft is especially problematic, so was removed in SciPy 1.6.0 

82from numpy.fft import ifft 

83ifft = _deprecated('scipy.ifft is deprecated and will be removed in SciPy ' 

84 '2.0.0, use scipy.fft.ifft instead')(ifft) 

85 

86from numpy.lib import scimath # noqa: E402 

87_msg = ('scipy.{0} is deprecated and will be removed in SciPy 2.0.0, ' 

88 'use numpy.lib.scimath.{0} instead') 

89for _key in scimath.__all__: 

90 _fun = getattr(scimath, _key) 

91 if callable(_fun): 

92 _fun = _deprecated(_msg.format(_key))(_fun) 

93 globals()[_key] = _fun 

94del scimath 

95del _msg, _fun, _key, _deprecated 

96 

97# We first need to detect if we're being called as part of the SciPy 

98# setup procedure itself in a reliable manner. 

99try: 

100 __SCIPY_SETUP__ 

101except NameError: 

102 __SCIPY_SETUP__ = False 

103 

104 

105if __SCIPY_SETUP__: 

106 import sys 

107 sys.stderr.write('Running from SciPy source directory.\n') 

108 del sys 

109else: 

110 try: 

111 from scipy.__config__ import show as show_config 

112 except ImportError as e: 

113 msg = """Error importing SciPy: you cannot import SciPy while 

114 being in scipy source directory; please exit the SciPy source 

115 tree first and relaunch your Python interpreter.""" 

116 raise ImportError(msg) from e 

117 

118 from scipy.version import version as __version__ 

119 

120 # Allow distributors to run custom init code 

121 from . import _distributor_init 

122 del _distributor_init 

123 

124 from scipy._lib import _pep440 

125 # In maintenance branch, change to np_maxversion N+3 if numpy is at N 

126 # See setup.py for more details 

127 np_minversion = '1.21.6' 

128 np_maxversion = '1.28.0' 

129 if (_pep440.parse(__numpy_version__) < _pep440.Version(np_minversion) or 

130 _pep440.parse(__numpy_version__) >= _pep440.Version(np_maxversion)): 

131 import warnings 

132 warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}" 

133 f" is required for this version of SciPy (detected " 

134 f"version {__numpy_version__})", 

135 UserWarning) 

136 del _pep440 

137 

138 # This is the first import of an extension module within SciPy. If there's 

139 # a general issue with the install, such that extension modules are missing 

140 # or cannot be imported, this is where we'll get a failure - so give an 

141 # informative error message. 

142 try: 

143 from scipy._lib._ccallback import LowLevelCallable 

144 except ImportError as e: 

145 msg = "The `scipy` install you are using seems to be broken, " + \ 

146 "(extension modules cannot be imported), " + \ 

147 "please try reinstalling." 

148 raise ImportError(msg) from e 

149 

150 from scipy._lib._testutils import PytestTester 

151 test = PytestTester(__name__) 

152 del PytestTester 

153 

154 submodules = [ 

155 'cluster', 

156 'constants', 

157 'datasets', 

158 'fft', 

159 'fftpack', 

160 'integrate', 

161 'interpolate', 

162 'io', 

163 'linalg', 

164 'misc', 

165 'ndimage', 

166 'odr', 

167 'optimize', 

168 'signal', 

169 'sparse', 

170 'spatial', 

171 'special', 

172 'stats' 

173 ] 

174 

175 __all__ = submodules + [ 

176 'LowLevelCallable', 

177 'test', 

178 'show_config', 

179 '__version__', 

180 ] 

181 

182 def __dir__(): 

183 return __all__ 

184 

185 import importlib as _importlib 

186 

187 def __getattr__(name): 

188 if name in submodules: 

189 return _importlib.import_module(f'scipy.{name}') 

190 else: 

191 try: 

192 return globals()[name] 

193 except KeyError: 

194 raise AttributeError( 

195 f"Module 'scipy' has no attribute '{name}'" 

196 )