Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- env-llmeval/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py +551 -0
- env-llmeval/lib/python3.10/site-packages/scipy/fftpack/basic.py +20 -0
- env-llmeval/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py +19 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__init__.py +131 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__odrpack.cpython-310-x86_64-linux-gnu.so +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_add_newdocs.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_models.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_odrpack.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/models.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/odrpack.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/_add_newdocs.py +34 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/_models.py +315 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/_odrpack.py +1150 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/models.py +20 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/odrpack.py +21 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__pycache__/test_odr.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/test_odr.py +565 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_basinhopping.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_bracket.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_chandrupatla.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_constraints.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_dcsrch.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_differentiable_functions.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_differentialevolution.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_direct_py.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_dual_annealing.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_isotonic.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_lbfgsb_py.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linesearch.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_doc.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_highs.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_ip.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_simplex.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_util.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_milp.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_minimize.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_minpack_py.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_nnls.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_nonlin.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_numdiff.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_optimize.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_qap.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_remove_redundancy.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_shgo.cpython-310.pyc +0 -0
.gitattributes
CHANGED
@@ -167,3 +167,4 @@ env-llmeval/lib/python3.10/site-packages/scipy/special/_ufuncs.cpython-310-x86_6
|
|
167 |
env-llmeval/lib/python3.10/site-packages/scipy/spatial/_ckdtree.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
168 |
env-llmeval/lib/python3.10/site-packages/scipy/spatial/_qhull.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
169 |
env-llmeval/lib/python3.10/site-packages/scipy/fft/_pocketfft/pypocketfft.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
|
|
|
167 |
env-llmeval/lib/python3.10/site-packages/scipy/spatial/_ckdtree.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
168 |
env-llmeval/lib/python3.10/site-packages/scipy/spatial/_qhull.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
169 |
env-llmeval/lib/python3.10/site-packages/scipy/fft/_pocketfft/pypocketfft.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
170 |
+
env-llmeval/lib/python3.10/site-packages/scipy/optimize/_highs/_highs_wrapper.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -text
|
env-llmeval/lib/python3.10/site-packages/scipy/fftpack/_pseudo_diffs.py
ADDED
@@ -0,0 +1,551 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Differential and pseudo-differential operators.
|
3 |
+
"""
|
4 |
+
# Created by Pearu Peterson, September 2002
|
5 |
+
|
6 |
+
__all__ = ['diff',
|
7 |
+
'tilbert','itilbert','hilbert','ihilbert',
|
8 |
+
'cs_diff','cc_diff','sc_diff','ss_diff',
|
9 |
+
'shift']
|
10 |
+
|
11 |
+
from numpy import pi, asarray, sin, cos, sinh, cosh, tanh, iscomplexobj
|
12 |
+
from . import convolve
|
13 |
+
|
14 |
+
from scipy.fft._pocketfft.helper import _datacopied
|
15 |
+
|
16 |
+
|
17 |
+
_cache = {}
|
18 |
+
|
19 |
+
|
20 |
+
def diff(x,order=1,period=None, _cache=_cache):
|
21 |
+
"""
|
22 |
+
Return kth derivative (or integral) of a periodic sequence x.
|
23 |
+
|
24 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
25 |
+
and y, respectively, then::
|
26 |
+
|
27 |
+
y_j = pow(sqrt(-1)*j*2*pi/period, order) * x_j
|
28 |
+
y_0 = 0 if order is not 0.
|
29 |
+
|
30 |
+
Parameters
|
31 |
+
----------
|
32 |
+
x : array_like
|
33 |
+
Input array.
|
34 |
+
order : int, optional
|
35 |
+
The order of differentiation. Default order is 1. If order is
|
36 |
+
negative, then integration is carried out under the assumption
|
37 |
+
that ``x_0 == 0``.
|
38 |
+
period : float, optional
|
39 |
+
The assumed period of the sequence. Default is ``2*pi``.
|
40 |
+
|
41 |
+
Notes
|
42 |
+
-----
|
43 |
+
If ``sum(x, axis=0) = 0`` then ``diff(diff(x, k), -k) == x`` (within
|
44 |
+
numerical accuracy).
|
45 |
+
|
46 |
+
For odd order and even ``len(x)``, the Nyquist mode is taken zero.
|
47 |
+
|
48 |
+
"""
|
49 |
+
tmp = asarray(x)
|
50 |
+
if order == 0:
|
51 |
+
return tmp
|
52 |
+
if iscomplexobj(tmp):
|
53 |
+
return diff(tmp.real,order,period)+1j*diff(tmp.imag,order,period)
|
54 |
+
if period is not None:
|
55 |
+
c = 2*pi/period
|
56 |
+
else:
|
57 |
+
c = 1.0
|
58 |
+
n = len(x)
|
59 |
+
omega = _cache.get((n,order,c))
|
60 |
+
if omega is None:
|
61 |
+
if len(_cache) > 20:
|
62 |
+
while _cache:
|
63 |
+
_cache.popitem()
|
64 |
+
|
65 |
+
def kernel(k,order=order,c=c):
|
66 |
+
if k:
|
67 |
+
return pow(c*k,order)
|
68 |
+
return 0
|
69 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=order,
|
70 |
+
zero_nyquist=1)
|
71 |
+
_cache[(n,order,c)] = omega
|
72 |
+
overwrite_x = _datacopied(tmp, x)
|
73 |
+
return convolve.convolve(tmp,omega,swap_real_imag=order % 2,
|
74 |
+
overwrite_x=overwrite_x)
|
75 |
+
|
76 |
+
|
77 |
+
del _cache
|
78 |
+
|
79 |
+
|
80 |
+
_cache = {}
|
81 |
+
|
82 |
+
|
83 |
+
def tilbert(x, h, period=None, _cache=_cache):
|
84 |
+
"""
|
85 |
+
Return h-Tilbert transform of a periodic sequence x.
|
86 |
+
|
87 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
88 |
+
and y, respectively, then::
|
89 |
+
|
90 |
+
y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j
|
91 |
+
y_0 = 0
|
92 |
+
|
93 |
+
Parameters
|
94 |
+
----------
|
95 |
+
x : array_like
|
96 |
+
The input array to transform.
|
97 |
+
h : float
|
98 |
+
Defines the parameter of the Tilbert transform.
|
99 |
+
period : float, optional
|
100 |
+
The assumed period of the sequence. Default period is ``2*pi``.
|
101 |
+
|
102 |
+
Returns
|
103 |
+
-------
|
104 |
+
tilbert : ndarray
|
105 |
+
The result of the transform.
|
106 |
+
|
107 |
+
Notes
|
108 |
+
-----
|
109 |
+
If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd, then
|
110 |
+
``tilbert(itilbert(x)) == x``.
|
111 |
+
|
112 |
+
If ``2 * pi * h / period`` is approximately 10 or larger, then
|
113 |
+
numerically ``tilbert == hilbert``
|
114 |
+
(theoretically oo-Tilbert == Hilbert).
|
115 |
+
|
116 |
+
For even ``len(x)``, the Nyquist mode of ``x`` is taken zero.
|
117 |
+
|
118 |
+
"""
|
119 |
+
tmp = asarray(x)
|
120 |
+
if iscomplexobj(tmp):
|
121 |
+
return tilbert(tmp.real, h, period) + \
|
122 |
+
1j * tilbert(tmp.imag, h, period)
|
123 |
+
|
124 |
+
if period is not None:
|
125 |
+
h = h * 2 * pi / period
|
126 |
+
|
127 |
+
n = len(x)
|
128 |
+
omega = _cache.get((n, h))
|
129 |
+
if omega is None:
|
130 |
+
if len(_cache) > 20:
|
131 |
+
while _cache:
|
132 |
+
_cache.popitem()
|
133 |
+
|
134 |
+
def kernel(k, h=h):
|
135 |
+
if k:
|
136 |
+
return 1.0/tanh(h*k)
|
137 |
+
|
138 |
+
return 0
|
139 |
+
|
140 |
+
omega = convolve.init_convolution_kernel(n, kernel, d=1)
|
141 |
+
_cache[(n,h)] = omega
|
142 |
+
|
143 |
+
overwrite_x = _datacopied(tmp, x)
|
144 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
145 |
+
|
146 |
+
|
147 |
+
del _cache
|
148 |
+
|
149 |
+
|
150 |
+
_cache = {}
|
151 |
+
|
152 |
+
|
153 |
+
def itilbert(x,h,period=None, _cache=_cache):
|
154 |
+
"""
|
155 |
+
Return inverse h-Tilbert transform of a periodic sequence x.
|
156 |
+
|
157 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
158 |
+
and y, respectively, then::
|
159 |
+
|
160 |
+
y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j
|
161 |
+
y_0 = 0
|
162 |
+
|
163 |
+
For more details, see `tilbert`.
|
164 |
+
|
165 |
+
"""
|
166 |
+
tmp = asarray(x)
|
167 |
+
if iscomplexobj(tmp):
|
168 |
+
return itilbert(tmp.real,h,period) + \
|
169 |
+
1j*itilbert(tmp.imag,h,period)
|
170 |
+
if period is not None:
|
171 |
+
h = h*2*pi/period
|
172 |
+
n = len(x)
|
173 |
+
omega = _cache.get((n,h))
|
174 |
+
if omega is None:
|
175 |
+
if len(_cache) > 20:
|
176 |
+
while _cache:
|
177 |
+
_cache.popitem()
|
178 |
+
|
179 |
+
def kernel(k,h=h):
|
180 |
+
if k:
|
181 |
+
return -tanh(h*k)
|
182 |
+
return 0
|
183 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
184 |
+
_cache[(n,h)] = omega
|
185 |
+
overwrite_x = _datacopied(tmp, x)
|
186 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
187 |
+
|
188 |
+
|
189 |
+
del _cache
|
190 |
+
|
191 |
+
|
192 |
+
_cache = {}
|
193 |
+
|
194 |
+
|
195 |
+
def hilbert(x, _cache=_cache):
|
196 |
+
"""
|
197 |
+
Return Hilbert transform of a periodic sequence x.
|
198 |
+
|
199 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
200 |
+
and y, respectively, then::
|
201 |
+
|
202 |
+
y_j = sqrt(-1)*sign(j) * x_j
|
203 |
+
y_0 = 0
|
204 |
+
|
205 |
+
Parameters
|
206 |
+
----------
|
207 |
+
x : array_like
|
208 |
+
The input array, should be periodic.
|
209 |
+
_cache : dict, optional
|
210 |
+
Dictionary that contains the kernel used to do a convolution with.
|
211 |
+
|
212 |
+
Returns
|
213 |
+
-------
|
214 |
+
y : ndarray
|
215 |
+
The transformed input.
|
216 |
+
|
217 |
+
See Also
|
218 |
+
--------
|
219 |
+
scipy.signal.hilbert : Compute the analytic signal, using the Hilbert
|
220 |
+
transform.
|
221 |
+
|
222 |
+
Notes
|
223 |
+
-----
|
224 |
+
If ``sum(x, axis=0) == 0`` then ``hilbert(ihilbert(x)) == x``.
|
225 |
+
|
226 |
+
For even len(x), the Nyquist mode of x is taken zero.
|
227 |
+
|
228 |
+
The sign of the returned transform does not have a factor -1 that is more
|
229 |
+
often than not found in the definition of the Hilbert transform. Note also
|
230 |
+
that `scipy.signal.hilbert` does have an extra -1 factor compared to this
|
231 |
+
function.
|
232 |
+
|
233 |
+
"""
|
234 |
+
tmp = asarray(x)
|
235 |
+
if iscomplexobj(tmp):
|
236 |
+
return hilbert(tmp.real)+1j*hilbert(tmp.imag)
|
237 |
+
n = len(x)
|
238 |
+
omega = _cache.get(n)
|
239 |
+
if omega is None:
|
240 |
+
if len(_cache) > 20:
|
241 |
+
while _cache:
|
242 |
+
_cache.popitem()
|
243 |
+
|
244 |
+
def kernel(k):
|
245 |
+
if k > 0:
|
246 |
+
return 1.0
|
247 |
+
elif k < 0:
|
248 |
+
return -1.0
|
249 |
+
return 0.0
|
250 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
251 |
+
_cache[n] = omega
|
252 |
+
overwrite_x = _datacopied(tmp, x)
|
253 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
254 |
+
|
255 |
+
|
256 |
+
del _cache
|
257 |
+
|
258 |
+
|
259 |
+
def ihilbert(x):
|
260 |
+
"""
|
261 |
+
Return inverse Hilbert transform of a periodic sequence x.
|
262 |
+
|
263 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
264 |
+
and y, respectively, then::
|
265 |
+
|
266 |
+
y_j = -sqrt(-1)*sign(j) * x_j
|
267 |
+
y_0 = 0
|
268 |
+
|
269 |
+
"""
|
270 |
+
return -hilbert(x)
|
271 |
+
|
272 |
+
|
273 |
+
_cache = {}
|
274 |
+
|
275 |
+
|
276 |
+
def cs_diff(x, a, b, period=None, _cache=_cache):
|
277 |
+
"""
|
278 |
+
Return (a,b)-cosh/sinh pseudo-derivative of a periodic sequence.
|
279 |
+
|
280 |
+
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
|
281 |
+
and y, respectively, then::
|
282 |
+
|
283 |
+
y_j = -sqrt(-1)*cosh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
284 |
+
y_0 = 0
|
285 |
+
|
286 |
+
Parameters
|
287 |
+
----------
|
288 |
+
x : array_like
|
289 |
+
The array to take the pseudo-derivative from.
|
290 |
+
a, b : float
|
291 |
+
Defines the parameters of the cosh/sinh pseudo-differential
|
292 |
+
operator.
|
293 |
+
period : float, optional
|
294 |
+
The period of the sequence. Default period is ``2*pi``.
|
295 |
+
|
296 |
+
Returns
|
297 |
+
-------
|
298 |
+
cs_diff : ndarray
|
299 |
+
Pseudo-derivative of periodic sequence `x`.
|
300 |
+
|
301 |
+
Notes
|
302 |
+
-----
|
303 |
+
For even len(`x`), the Nyquist mode of `x` is taken as zero.
|
304 |
+
|
305 |
+
"""
|
306 |
+
tmp = asarray(x)
|
307 |
+
if iscomplexobj(tmp):
|
308 |
+
return cs_diff(tmp.real,a,b,period) + \
|
309 |
+
1j*cs_diff(tmp.imag,a,b,period)
|
310 |
+
if period is not None:
|
311 |
+
a = a*2*pi/period
|
312 |
+
b = b*2*pi/period
|
313 |
+
n = len(x)
|
314 |
+
omega = _cache.get((n,a,b))
|
315 |
+
if omega is None:
|
316 |
+
if len(_cache) > 20:
|
317 |
+
while _cache:
|
318 |
+
_cache.popitem()
|
319 |
+
|
320 |
+
def kernel(k,a=a,b=b):
|
321 |
+
if k:
|
322 |
+
return -cosh(a*k)/sinh(b*k)
|
323 |
+
return 0
|
324 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
325 |
+
_cache[(n,a,b)] = omega
|
326 |
+
overwrite_x = _datacopied(tmp, x)
|
327 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
328 |
+
|
329 |
+
|
330 |
+
del _cache
|
331 |
+
|
332 |
+
|
333 |
+
_cache = {}
|
334 |
+
|
335 |
+
|
336 |
+
def sc_diff(x, a, b, period=None, _cache=_cache):
|
337 |
+
"""
|
338 |
+
Return (a,b)-sinh/cosh pseudo-derivative of a periodic sequence x.
|
339 |
+
|
340 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
341 |
+
and y, respectively, then::
|
342 |
+
|
343 |
+
y_j = sqrt(-1)*sinh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
|
344 |
+
y_0 = 0
|
345 |
+
|
346 |
+
Parameters
|
347 |
+
----------
|
348 |
+
x : array_like
|
349 |
+
Input array.
|
350 |
+
a,b : float
|
351 |
+
Defines the parameters of the sinh/cosh pseudo-differential
|
352 |
+
operator.
|
353 |
+
period : float, optional
|
354 |
+
The period of the sequence x. Default is 2*pi.
|
355 |
+
|
356 |
+
Notes
|
357 |
+
-----
|
358 |
+
``sc_diff(cs_diff(x,a,b),b,a) == x``
|
359 |
+
For even ``len(x)``, the Nyquist mode of x is taken as zero.
|
360 |
+
|
361 |
+
"""
|
362 |
+
tmp = asarray(x)
|
363 |
+
if iscomplexobj(tmp):
|
364 |
+
return sc_diff(tmp.real,a,b,period) + \
|
365 |
+
1j*sc_diff(tmp.imag,a,b,period)
|
366 |
+
if period is not None:
|
367 |
+
a = a*2*pi/period
|
368 |
+
b = b*2*pi/period
|
369 |
+
n = len(x)
|
370 |
+
omega = _cache.get((n,a,b))
|
371 |
+
if omega is None:
|
372 |
+
if len(_cache) > 20:
|
373 |
+
while _cache:
|
374 |
+
_cache.popitem()
|
375 |
+
|
376 |
+
def kernel(k,a=a,b=b):
|
377 |
+
if k:
|
378 |
+
return sinh(a*k)/cosh(b*k)
|
379 |
+
return 0
|
380 |
+
omega = convolve.init_convolution_kernel(n,kernel,d=1)
|
381 |
+
_cache[(n,a,b)] = omega
|
382 |
+
overwrite_x = _datacopied(tmp, x)
|
383 |
+
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
|
384 |
+
|
385 |
+
|
386 |
+
del _cache
|
387 |
+
|
388 |
+
|
389 |
+
_cache = {}
|
390 |
+
|
391 |
+
|
392 |
+
def ss_diff(x, a, b, period=None, _cache=_cache):
|
393 |
+
"""
|
394 |
+
Return (a,b)-sinh/sinh pseudo-derivative of a periodic sequence x.
|
395 |
+
|
396 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
397 |
+
and y, respectively, then::
|
398 |
+
|
399 |
+
y_j = sinh(j*a*2*pi/period)/sinh(j*b*2*pi/period) * x_j
|
400 |
+
y_0 = a/b * x_0
|
401 |
+
|
402 |
+
Parameters
|
403 |
+
----------
|
404 |
+
x : array_like
|
405 |
+
The array to take the pseudo-derivative from.
|
406 |
+
a,b
|
407 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
408 |
+
operator.
|
409 |
+
period : float, optional
|
410 |
+
The period of the sequence x. Default is ``2*pi``.
|
411 |
+
|
412 |
+
Notes
|
413 |
+
-----
|
414 |
+
``ss_diff(ss_diff(x,a,b),b,a) == x``
|
415 |
+
|
416 |
+
"""
|
417 |
+
tmp = asarray(x)
|
418 |
+
if iscomplexobj(tmp):
|
419 |
+
return ss_diff(tmp.real,a,b,period) + \
|
420 |
+
1j*ss_diff(tmp.imag,a,b,period)
|
421 |
+
if period is not None:
|
422 |
+
a = a*2*pi/period
|
423 |
+
b = b*2*pi/period
|
424 |
+
n = len(x)
|
425 |
+
omega = _cache.get((n,a,b))
|
426 |
+
if omega is None:
|
427 |
+
if len(_cache) > 20:
|
428 |
+
while _cache:
|
429 |
+
_cache.popitem()
|
430 |
+
|
431 |
+
def kernel(k,a=a,b=b):
|
432 |
+
if k:
|
433 |
+
return sinh(a*k)/sinh(b*k)
|
434 |
+
return float(a)/b
|
435 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
436 |
+
_cache[(n,a,b)] = omega
|
437 |
+
overwrite_x = _datacopied(tmp, x)
|
438 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
439 |
+
|
440 |
+
|
441 |
+
del _cache
|
442 |
+
|
443 |
+
|
444 |
+
_cache = {}
|
445 |
+
|
446 |
+
|
447 |
+
def cc_diff(x, a, b, period=None, _cache=_cache):
|
448 |
+
"""
|
449 |
+
Return (a,b)-cosh/cosh pseudo-derivative of a periodic sequence.
|
450 |
+
|
451 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
452 |
+
and y, respectively, then::
|
453 |
+
|
454 |
+
y_j = cosh(j*a*2*pi/period)/cosh(j*b*2*pi/period) * x_j
|
455 |
+
|
456 |
+
Parameters
|
457 |
+
----------
|
458 |
+
x : array_like
|
459 |
+
The array to take the pseudo-derivative from.
|
460 |
+
a,b : float
|
461 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
462 |
+
operator.
|
463 |
+
period : float, optional
|
464 |
+
The period of the sequence x. Default is ``2*pi``.
|
465 |
+
|
466 |
+
Returns
|
467 |
+
-------
|
468 |
+
cc_diff : ndarray
|
469 |
+
Pseudo-derivative of periodic sequence `x`.
|
470 |
+
|
471 |
+
Notes
|
472 |
+
-----
|
473 |
+
``cc_diff(cc_diff(x,a,b),b,a) == x``
|
474 |
+
|
475 |
+
"""
|
476 |
+
tmp = asarray(x)
|
477 |
+
if iscomplexobj(tmp):
|
478 |
+
return cc_diff(tmp.real,a,b,period) + \
|
479 |
+
1j*cc_diff(tmp.imag,a,b,period)
|
480 |
+
if period is not None:
|
481 |
+
a = a*2*pi/period
|
482 |
+
b = b*2*pi/period
|
483 |
+
n = len(x)
|
484 |
+
omega = _cache.get((n,a,b))
|
485 |
+
if omega is None:
|
486 |
+
if len(_cache) > 20:
|
487 |
+
while _cache:
|
488 |
+
_cache.popitem()
|
489 |
+
|
490 |
+
def kernel(k,a=a,b=b):
|
491 |
+
return cosh(a*k)/cosh(b*k)
|
492 |
+
omega = convolve.init_convolution_kernel(n,kernel)
|
493 |
+
_cache[(n,a,b)] = omega
|
494 |
+
overwrite_x = _datacopied(tmp, x)
|
495 |
+
return convolve.convolve(tmp,omega,overwrite_x=overwrite_x)
|
496 |
+
|
497 |
+
|
498 |
+
del _cache
|
499 |
+
|
500 |
+
|
501 |
+
_cache = {}
|
502 |
+
|
503 |
+
|
504 |
+
def shift(x, a, period=None, _cache=_cache):
|
505 |
+
"""
|
506 |
+
Shift periodic sequence x by a: y(u) = x(u+a).
|
507 |
+
|
508 |
+
If x_j and y_j are Fourier coefficients of periodic functions x
|
509 |
+
and y, respectively, then::
|
510 |
+
|
511 |
+
y_j = exp(j*a*2*pi/period*sqrt(-1)) * x_f
|
512 |
+
|
513 |
+
Parameters
|
514 |
+
----------
|
515 |
+
x : array_like
|
516 |
+
The array to take the pseudo-derivative from.
|
517 |
+
a : float
|
518 |
+
Defines the parameters of the sinh/sinh pseudo-differential
|
519 |
+
period : float, optional
|
520 |
+
The period of the sequences x and y. Default period is ``2*pi``.
|
521 |
+
"""
|
522 |
+
tmp = asarray(x)
|
523 |
+
if iscomplexobj(tmp):
|
524 |
+
return shift(tmp.real,a,period)+1j*shift(tmp.imag,a,period)
|
525 |
+
if period is not None:
|
526 |
+
a = a*2*pi/period
|
527 |
+
n = len(x)
|
528 |
+
omega = _cache.get((n,a))
|
529 |
+
if omega is None:
|
530 |
+
if len(_cache) > 20:
|
531 |
+
while _cache:
|
532 |
+
_cache.popitem()
|
533 |
+
|
534 |
+
def kernel_real(k,a=a):
|
535 |
+
return cos(a*k)
|
536 |
+
|
537 |
+
def kernel_imag(k,a=a):
|
538 |
+
return sin(a*k)
|
539 |
+
omega_real = convolve.init_convolution_kernel(n,kernel_real,d=0,
|
540 |
+
zero_nyquist=0)
|
541 |
+
omega_imag = convolve.init_convolution_kernel(n,kernel_imag,d=1,
|
542 |
+
zero_nyquist=0)
|
543 |
+
_cache[(n,a)] = omega_real,omega_imag
|
544 |
+
else:
|
545 |
+
omega_real,omega_imag = omega
|
546 |
+
overwrite_x = _datacopied(tmp, x)
|
547 |
+
return convolve.convolve_z(tmp,omega_real,omega_imag,
|
548 |
+
overwrite_x=overwrite_x)
|
549 |
+
|
550 |
+
|
551 |
+
del _cache
|
env-llmeval/lib/python3.10/site-packages/scipy/fftpack/basic.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
3 |
+
# included below.
|
4 |
+
|
5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
6 |
+
|
7 |
+
__all__ = [ # noqa: F822
|
8 |
+
'fft','ifft','fftn','ifftn','rfft','irfft',
|
9 |
+
'fft2','ifft2'
|
10 |
+
]
|
11 |
+
|
12 |
+
|
13 |
+
def __dir__():
|
14 |
+
return __all__
|
15 |
+
|
16 |
+
|
17 |
+
def __getattr__(name):
|
18 |
+
return _sub_module_deprecation(sub_package="fftpack", module="basic",
|
19 |
+
private_modules=["_basic"], all=__all__,
|
20 |
+
attribute=name)
|
env-llmeval/lib/python3.10/site-packages/scipy/fftpack/realtransforms.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
2 |
+
# Use the `scipy.fftpack` namespace for importing the functions
|
3 |
+
# included below.
|
4 |
+
|
5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
6 |
+
|
7 |
+
__all__ = [ # noqa: F822
|
8 |
+
'dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn'
|
9 |
+
]
|
10 |
+
|
11 |
+
|
12 |
+
def __dir__():
|
13 |
+
return __all__
|
14 |
+
|
15 |
+
|
16 |
+
def __getattr__(name):
|
17 |
+
return _sub_module_deprecation(sub_package="fftpack", module="realtransforms",
|
18 |
+
private_modules=["_realtransforms"], all=__all__,
|
19 |
+
attribute=name)
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__init__.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
=================================================
|
3 |
+
Orthogonal distance regression (:mod:`scipy.odr`)
|
4 |
+
=================================================
|
5 |
+
|
6 |
+
.. currentmodule:: scipy.odr
|
7 |
+
|
8 |
+
Package Content
|
9 |
+
===============
|
10 |
+
|
11 |
+
.. autosummary::
|
12 |
+
:toctree: generated/
|
13 |
+
|
14 |
+
Data -- The data to fit.
|
15 |
+
RealData -- Data with weights as actual std. dev.s and/or covariances.
|
16 |
+
Model -- Stores information about the function to be fit.
|
17 |
+
ODR -- Gathers all info & manages the main fitting routine.
|
18 |
+
Output -- Result from the fit.
|
19 |
+
odr -- Low-level function for ODR.
|
20 |
+
|
21 |
+
OdrWarning -- Warning about potential problems when running ODR.
|
22 |
+
OdrError -- Error exception.
|
23 |
+
OdrStop -- Stop exception.
|
24 |
+
|
25 |
+
polynomial -- Factory function for a general polynomial model.
|
26 |
+
exponential -- Exponential model
|
27 |
+
multilinear -- Arbitrary-dimensional linear model
|
28 |
+
unilinear -- Univariate linear model
|
29 |
+
quadratic -- Quadratic model
|
30 |
+
|
31 |
+
Usage information
|
32 |
+
=================
|
33 |
+
|
34 |
+
Introduction
|
35 |
+
------------
|
36 |
+
|
37 |
+
Why Orthogonal Distance Regression (ODR)? Sometimes one has
|
38 |
+
measurement errors in the explanatory (a.k.a., "independent")
|
39 |
+
variable(s), not just the response (a.k.a., "dependent") variable(s).
|
40 |
+
Ordinary Least Squares (OLS) fitting procedures treat the data for
|
41 |
+
explanatory variables as fixed, i.e., not subject to error of any kind.
|
42 |
+
Furthermore, OLS procedures require that the response variables be an
|
43 |
+
explicit function of the explanatory variables; sometimes making the
|
44 |
+
equation explicit is impractical and/or introduces errors. ODR can
|
45 |
+
handle both of these cases with ease, and can even reduce to the OLS
|
46 |
+
case if that is sufficient for the problem.
|
47 |
+
|
48 |
+
ODRPACK is a FORTRAN-77 library for performing ODR with possibly
|
49 |
+
non-linear fitting functions. It uses a modified trust-region
|
50 |
+
Levenberg-Marquardt-type algorithm [1]_ to estimate the function
|
51 |
+
parameters. The fitting functions are provided by Python functions
|
52 |
+
operating on NumPy arrays. The required derivatives may be provided
|
53 |
+
by Python functions as well, or may be estimated numerically. ODRPACK
|
54 |
+
can do explicit or implicit ODR fits, or it can do OLS. Input and
|
55 |
+
output variables may be multidimensional. Weights can be provided to
|
56 |
+
account for different variances of the observations, and even
|
57 |
+
covariances between dimensions of the variables.
|
58 |
+
|
59 |
+
The `scipy.odr` package offers an object-oriented interface to
|
60 |
+
ODRPACK, in addition to the low-level `odr` function.
|
61 |
+
|
62 |
+
Additional background information about ODRPACK can be found in the
|
63 |
+
`ODRPACK User's Guide
|
64 |
+
<https://docs.scipy.org/doc/external/odrpack_guide.pdf>`_, reading
|
65 |
+
which is recommended.
|
66 |
+
|
67 |
+
Basic usage
|
68 |
+
-----------
|
69 |
+
|
70 |
+
1. Define the function you want to fit against.::
|
71 |
+
|
72 |
+
def f(B, x):
|
73 |
+
'''Linear function y = m*x + b'''
|
74 |
+
# B is a vector of the parameters.
|
75 |
+
# x is an array of the current x values.
|
76 |
+
# x is in the same format as the x passed to Data or RealData.
|
77 |
+
#
|
78 |
+
# Return an array in the same format as y passed to Data or RealData.
|
79 |
+
return B[0]*x + B[1]
|
80 |
+
|
81 |
+
2. Create a Model.::
|
82 |
+
|
83 |
+
linear = Model(f)
|
84 |
+
|
85 |
+
3. Create a Data or RealData instance.::
|
86 |
+
|
87 |
+
mydata = Data(x, y, wd=1./power(sx,2), we=1./power(sy,2))
|
88 |
+
|
89 |
+
or, when the actual covariances are known::
|
90 |
+
|
91 |
+
mydata = RealData(x, y, sx=sx, sy=sy)
|
92 |
+
|
93 |
+
4. Instantiate ODR with your data, model and initial parameter estimate.::
|
94 |
+
|
95 |
+
myodr = ODR(mydata, linear, beta0=[1., 2.])
|
96 |
+
|
97 |
+
5. Run the fit.::
|
98 |
+
|
99 |
+
myoutput = myodr.run()
|
100 |
+
|
101 |
+
6. Examine output.::
|
102 |
+
|
103 |
+
myoutput.pprint()
|
104 |
+
|
105 |
+
|
106 |
+
References
|
107 |
+
----------
|
108 |
+
.. [1] P. T. Boggs and J. E. Rogers, "Orthogonal Distance Regression,"
|
109 |
+
in "Statistical analysis of measurement error models and
|
110 |
+
applications: proceedings of the AMS-IMS-SIAM joint summer research
|
111 |
+
conference held June 10-16, 1989," Contemporary Mathematics,
|
112 |
+
vol. 112, pg. 186, 1990.
|
113 |
+
|
114 |
+
"""
|
115 |
+
# version: 0.7
|
116 |
+
# author: Robert Kern <[email protected]>
|
117 |
+
# date: 2006-09-21
|
118 |
+
|
119 |
+
from ._odrpack import *
|
120 |
+
from ._models import *
|
121 |
+
from . import _add_newdocs
|
122 |
+
|
123 |
+
# Deprecated namespaces, to be removed in v2.0.0
|
124 |
+
from . import models, odrpack
|
125 |
+
|
126 |
+
__all__ = [s for s in dir()
|
127 |
+
if not (s.startswith('_') or s in ('odr_stop', 'odr_error'))]
|
128 |
+
|
129 |
+
from scipy._lib._testutils import PytestTester
|
130 |
+
test = PytestTester(__name__)
|
131 |
+
del PytestTester
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__odrpack.cpython-310-x86_64-linux-gnu.so
ADDED
Binary file (223 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (4.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_add_newdocs.cpython-310.pyc
ADDED
Binary file (1.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_models.cpython-310.pyc
ADDED
Binary file (9.07 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/_odrpack.cpython-310.pyc
ADDED
Binary file (37.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/models.cpython-310.pyc
ADDED
Binary file (650 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/__pycache__/odrpack.cpython-310.pyc
ADDED
Binary file (679 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/_add_newdocs.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from numpy.lib import add_newdoc
|
2 |
+
|
3 |
+
add_newdoc('scipy.odr', 'odr',
|
4 |
+
"""
|
5 |
+
odr(fcn, beta0, y, x, we=None, wd=None, fjacb=None, fjacd=None, extra_args=None,
|
6 |
+
ifixx=None, ifixb=None, job=0, iprint=0, errfile=None, rptfile=None, ndigit=0,
|
7 |
+
taufac=0.0, sstol=-1.0, partol=-1.0, maxit=-1, stpb=None, stpd=None, sclb=None,
|
8 |
+
scld=None, work=None, iwork=None, full_output=0)
|
9 |
+
|
10 |
+
Low-level function for ODR.
|
11 |
+
|
12 |
+
See Also
|
13 |
+
--------
|
14 |
+
ODR : The ODR class gathers all information and coordinates the running of the
|
15 |
+
main fitting routine.
|
16 |
+
Model : The Model class stores information about the function you wish to fit.
|
17 |
+
Data : The data to fit.
|
18 |
+
RealData : Data with weights as actual std. dev.s and/or covariances.
|
19 |
+
|
20 |
+
Notes
|
21 |
+
-----
|
22 |
+
This is a function performing the same operation as the `ODR`,
|
23 |
+
`Model`, and `Data` classes together. The parameters of this
|
24 |
+
function are explained in the class documentation.
|
25 |
+
|
26 |
+
""")
|
27 |
+
|
28 |
+
add_newdoc('scipy.odr.__odrpack', '_set_exceptions',
|
29 |
+
"""
|
30 |
+
_set_exceptions(odr_error, odr_stop)
|
31 |
+
|
32 |
+
Internal function: set exception classes.
|
33 |
+
|
34 |
+
""")
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/_models.py
ADDED
@@ -0,0 +1,315 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Collection of Model instances for use with the odrpack fitting package.
|
2 |
+
"""
|
3 |
+
import numpy as np
|
4 |
+
from scipy.odr._odrpack import Model
|
5 |
+
|
6 |
+
__all__ = ['Model', 'exponential', 'multilinear', 'unilinear', 'quadratic',
|
7 |
+
'polynomial']
|
8 |
+
|
9 |
+
|
10 |
+
def _lin_fcn(B, x):
|
11 |
+
a, b = B[0], B[1:]
|
12 |
+
b.shape = (b.shape[0], 1)
|
13 |
+
|
14 |
+
return a + (x*b).sum(axis=0)
|
15 |
+
|
16 |
+
|
17 |
+
def _lin_fjb(B, x):
|
18 |
+
a = np.ones(x.shape[-1], float)
|
19 |
+
res = np.concatenate((a, x.ravel()))
|
20 |
+
res.shape = (B.shape[-1], x.shape[-1])
|
21 |
+
return res
|
22 |
+
|
23 |
+
|
24 |
+
def _lin_fjd(B, x):
|
25 |
+
b = B[1:]
|
26 |
+
b = np.repeat(b, (x.shape[-1],)*b.shape[-1], axis=0)
|
27 |
+
b.shape = x.shape
|
28 |
+
return b
|
29 |
+
|
30 |
+
|
31 |
+
def _lin_est(data):
|
32 |
+
# Eh. The answer is analytical, so just return all ones.
|
33 |
+
# Don't return zeros since that will interfere with
|
34 |
+
# ODRPACK's auto-scaling procedures.
|
35 |
+
|
36 |
+
if len(data.x.shape) == 2:
|
37 |
+
m = data.x.shape[0]
|
38 |
+
else:
|
39 |
+
m = 1
|
40 |
+
|
41 |
+
return np.ones((m + 1,), float)
|
42 |
+
|
43 |
+
|
44 |
+
def _poly_fcn(B, x, powers):
|
45 |
+
a, b = B[0], B[1:]
|
46 |
+
b.shape = (b.shape[0], 1)
|
47 |
+
|
48 |
+
return a + np.sum(b * np.power(x, powers), axis=0)
|
49 |
+
|
50 |
+
|
51 |
+
def _poly_fjacb(B, x, powers):
|
52 |
+
res = np.concatenate((np.ones(x.shape[-1], float),
|
53 |
+
np.power(x, powers).flat))
|
54 |
+
res.shape = (B.shape[-1], x.shape[-1])
|
55 |
+
return res
|
56 |
+
|
57 |
+
|
58 |
+
def _poly_fjacd(B, x, powers):
|
59 |
+
b = B[1:]
|
60 |
+
b.shape = (b.shape[0], 1)
|
61 |
+
|
62 |
+
b = b * powers
|
63 |
+
|
64 |
+
return np.sum(b * np.power(x, powers-1), axis=0)
|
65 |
+
|
66 |
+
|
67 |
+
def _exp_fcn(B, x):
|
68 |
+
return B[0] + np.exp(B[1] * x)
|
69 |
+
|
70 |
+
|
71 |
+
def _exp_fjd(B, x):
|
72 |
+
return B[1] * np.exp(B[1] * x)
|
73 |
+
|
74 |
+
|
75 |
+
def _exp_fjb(B, x):
|
76 |
+
res = np.concatenate((np.ones(x.shape[-1], float), x * np.exp(B[1] * x)))
|
77 |
+
res.shape = (2, x.shape[-1])
|
78 |
+
return res
|
79 |
+
|
80 |
+
|
81 |
+
def _exp_est(data):
|
82 |
+
# Eh.
|
83 |
+
return np.array([1., 1.])
|
84 |
+
|
85 |
+
|
86 |
+
class _MultilinearModel(Model):
|
87 |
+
r"""
|
88 |
+
Arbitrary-dimensional linear model
|
89 |
+
|
90 |
+
This model is defined by :math:`y=\beta_0 + \sum_{i=1}^m \beta_i x_i`
|
91 |
+
|
92 |
+
Examples
|
93 |
+
--------
|
94 |
+
We can calculate orthogonal distance regression with an arbitrary
|
95 |
+
dimensional linear model:
|
96 |
+
|
97 |
+
>>> from scipy import odr
|
98 |
+
>>> import numpy as np
|
99 |
+
>>> x = np.linspace(0.0, 5.0)
|
100 |
+
>>> y = 10.0 + 5.0 * x
|
101 |
+
>>> data = odr.Data(x, y)
|
102 |
+
>>> odr_obj = odr.ODR(data, odr.multilinear)
|
103 |
+
>>> output = odr_obj.run()
|
104 |
+
>>> print(output.beta)
|
105 |
+
[10. 5.]
|
106 |
+
|
107 |
+
"""
|
108 |
+
|
109 |
+
def __init__(self):
|
110 |
+
super().__init__(
|
111 |
+
_lin_fcn, fjacb=_lin_fjb, fjacd=_lin_fjd, estimate=_lin_est,
|
112 |
+
meta={'name': 'Arbitrary-dimensional Linear',
|
113 |
+
'equ': 'y = B_0 + Sum[i=1..m, B_i * x_i]',
|
114 |
+
'TeXequ': r'$y=\beta_0 + \sum_{i=1}^m \beta_i x_i$'})
|
115 |
+
|
116 |
+
|
117 |
+
multilinear = _MultilinearModel()
|
118 |
+
|
119 |
+
|
120 |
+
def polynomial(order):
|
121 |
+
"""
|
122 |
+
Factory function for a general polynomial model.
|
123 |
+
|
124 |
+
Parameters
|
125 |
+
----------
|
126 |
+
order : int or sequence
|
127 |
+
If an integer, it becomes the order of the polynomial to fit. If
|
128 |
+
a sequence of numbers, then these are the explicit powers in the
|
129 |
+
polynomial.
|
130 |
+
A constant term (power 0) is always included, so don't include 0.
|
131 |
+
Thus, polynomial(n) is equivalent to polynomial(range(1, n+1)).
|
132 |
+
|
133 |
+
Returns
|
134 |
+
-------
|
135 |
+
polynomial : Model instance
|
136 |
+
Model instance.
|
137 |
+
|
138 |
+
Examples
|
139 |
+
--------
|
140 |
+
We can fit an input data using orthogonal distance regression (ODR) with
|
141 |
+
a polynomial model:
|
142 |
+
|
143 |
+
>>> import numpy as np
|
144 |
+
>>> import matplotlib.pyplot as plt
|
145 |
+
>>> from scipy import odr
|
146 |
+
>>> x = np.linspace(0.0, 5.0)
|
147 |
+
>>> y = np.sin(x)
|
148 |
+
>>> poly_model = odr.polynomial(3) # using third order polynomial model
|
149 |
+
>>> data = odr.Data(x, y)
|
150 |
+
>>> odr_obj = odr.ODR(data, poly_model)
|
151 |
+
>>> output = odr_obj.run() # running ODR fitting
|
152 |
+
>>> poly = np.poly1d(output.beta[::-1])
|
153 |
+
>>> poly_y = poly(x)
|
154 |
+
>>> plt.plot(x, y, label="input data")
|
155 |
+
>>> plt.plot(x, poly_y, label="polynomial ODR")
|
156 |
+
>>> plt.legend()
|
157 |
+
>>> plt.show()
|
158 |
+
|
159 |
+
"""
|
160 |
+
|
161 |
+
powers = np.asarray(order)
|
162 |
+
if powers.shape == ():
|
163 |
+
# Scalar.
|
164 |
+
powers = np.arange(1, powers + 1)
|
165 |
+
|
166 |
+
powers.shape = (len(powers), 1)
|
167 |
+
len_beta = len(powers) + 1
|
168 |
+
|
169 |
+
def _poly_est(data, len_beta=len_beta):
|
170 |
+
# Eh. Ignore data and return all ones.
|
171 |
+
return np.ones((len_beta,), float)
|
172 |
+
|
173 |
+
return Model(_poly_fcn, fjacd=_poly_fjacd, fjacb=_poly_fjacb,
|
174 |
+
estimate=_poly_est, extra_args=(powers,),
|
175 |
+
meta={'name': 'Sorta-general Polynomial',
|
176 |
+
'equ': 'y = B_0 + Sum[i=1..%s, B_i * (x**i)]' % (len_beta-1),
|
177 |
+
'TeXequ': r'$y=\beta_0 + \sum_{i=1}^{%s} \beta_i x^i$' %
|
178 |
+
(len_beta-1)})
|
179 |
+
|
180 |
+
|
181 |
+
class _ExponentialModel(Model):
|
182 |
+
r"""
|
183 |
+
Exponential model
|
184 |
+
|
185 |
+
This model is defined by :math:`y=\beta_0 + e^{\beta_1 x}`
|
186 |
+
|
187 |
+
Examples
|
188 |
+
--------
|
189 |
+
We can calculate orthogonal distance regression with an exponential model:
|
190 |
+
|
191 |
+
>>> from scipy import odr
|
192 |
+
>>> import numpy as np
|
193 |
+
>>> x = np.linspace(0.0, 5.0)
|
194 |
+
>>> y = -10.0 + np.exp(0.5*x)
|
195 |
+
>>> data = odr.Data(x, y)
|
196 |
+
>>> odr_obj = odr.ODR(data, odr.exponential)
|
197 |
+
>>> output = odr_obj.run()
|
198 |
+
>>> print(output.beta)
|
199 |
+
[-10. 0.5]
|
200 |
+
|
201 |
+
"""
|
202 |
+
|
203 |
+
def __init__(self):
|
204 |
+
super().__init__(_exp_fcn, fjacd=_exp_fjd, fjacb=_exp_fjb,
|
205 |
+
estimate=_exp_est,
|
206 |
+
meta={'name': 'Exponential',
|
207 |
+
'equ': 'y= B_0 + exp(B_1 * x)',
|
208 |
+
'TeXequ': r'$y=\beta_0 + e^{\beta_1 x}$'})
|
209 |
+
|
210 |
+
|
211 |
+
exponential = _ExponentialModel()
|
212 |
+
|
213 |
+
|
214 |
+
def _unilin(B, x):
|
215 |
+
return x*B[0] + B[1]
|
216 |
+
|
217 |
+
|
218 |
+
def _unilin_fjd(B, x):
|
219 |
+
return np.ones(x.shape, float) * B[0]
|
220 |
+
|
221 |
+
|
222 |
+
def _unilin_fjb(B, x):
|
223 |
+
_ret = np.concatenate((x, np.ones(x.shape, float)))
|
224 |
+
_ret.shape = (2,) + x.shape
|
225 |
+
|
226 |
+
return _ret
|
227 |
+
|
228 |
+
|
229 |
+
def _unilin_est(data):
|
230 |
+
return (1., 1.)
|
231 |
+
|
232 |
+
|
233 |
+
def _quadratic(B, x):
|
234 |
+
return x*(x*B[0] + B[1]) + B[2]
|
235 |
+
|
236 |
+
|
237 |
+
def _quad_fjd(B, x):
|
238 |
+
return 2*x*B[0] + B[1]
|
239 |
+
|
240 |
+
|
241 |
+
def _quad_fjb(B, x):
|
242 |
+
_ret = np.concatenate((x*x, x, np.ones(x.shape, float)))
|
243 |
+
_ret.shape = (3,) + x.shape
|
244 |
+
|
245 |
+
return _ret
|
246 |
+
|
247 |
+
|
248 |
+
def _quad_est(data):
|
249 |
+
return (1.,1.,1.)
|
250 |
+
|
251 |
+
|
252 |
+
class _UnilinearModel(Model):
|
253 |
+
r"""
|
254 |
+
Univariate linear model
|
255 |
+
|
256 |
+
This model is defined by :math:`y = \beta_0 x + \beta_1`
|
257 |
+
|
258 |
+
Examples
|
259 |
+
--------
|
260 |
+
We can calculate orthogonal distance regression with an unilinear model:
|
261 |
+
|
262 |
+
>>> from scipy import odr
|
263 |
+
>>> import numpy as np
|
264 |
+
>>> x = np.linspace(0.0, 5.0)
|
265 |
+
>>> y = 1.0 * x + 2.0
|
266 |
+
>>> data = odr.Data(x, y)
|
267 |
+
>>> odr_obj = odr.ODR(data, odr.unilinear)
|
268 |
+
>>> output = odr_obj.run()
|
269 |
+
>>> print(output.beta)
|
270 |
+
[1. 2.]
|
271 |
+
|
272 |
+
"""
|
273 |
+
|
274 |
+
def __init__(self):
|
275 |
+
super().__init__(_unilin, fjacd=_unilin_fjd, fjacb=_unilin_fjb,
|
276 |
+
estimate=_unilin_est,
|
277 |
+
meta={'name': 'Univariate Linear',
|
278 |
+
'equ': 'y = B_0 * x + B_1',
|
279 |
+
'TeXequ': '$y = \\beta_0 x + \\beta_1$'})
|
280 |
+
|
281 |
+
|
282 |
+
unilinear = _UnilinearModel()
|
283 |
+
|
284 |
+
|
285 |
+
class _QuadraticModel(Model):
|
286 |
+
r"""
|
287 |
+
Quadratic model
|
288 |
+
|
289 |
+
This model is defined by :math:`y = \beta_0 x^2 + \beta_1 x + \beta_2`
|
290 |
+
|
291 |
+
Examples
|
292 |
+
--------
|
293 |
+
We can calculate orthogonal distance regression with a quadratic model:
|
294 |
+
|
295 |
+
>>> from scipy import odr
|
296 |
+
>>> import numpy as np
|
297 |
+
>>> x = np.linspace(0.0, 5.0)
|
298 |
+
>>> y = 1.0 * x ** 2 + 2.0 * x + 3.0
|
299 |
+
>>> data = odr.Data(x, y)
|
300 |
+
>>> odr_obj = odr.ODR(data, odr.quadratic)
|
301 |
+
>>> output = odr_obj.run()
|
302 |
+
>>> print(output.beta)
|
303 |
+
[1. 2. 3.]
|
304 |
+
|
305 |
+
"""
|
306 |
+
|
307 |
+
def __init__(self):
|
308 |
+
super().__init__(
|
309 |
+
_quadratic, fjacd=_quad_fjd, fjacb=_quad_fjb, estimate=_quad_est,
|
310 |
+
meta={'name': 'Quadratic',
|
311 |
+
'equ': 'y = B_0*x**2 + B_1*x + B_2',
|
312 |
+
'TeXequ': '$y = \\beta_0 x^2 + \\beta_1 x + \\beta_2'})
|
313 |
+
|
314 |
+
|
315 |
+
quadratic = _QuadraticModel()
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/_odrpack.py
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Python wrappers for Orthogonal Distance Regression (ODRPACK).
|
3 |
+
|
4 |
+
Notes
|
5 |
+
=====
|
6 |
+
|
7 |
+
* Array formats -- FORTRAN stores its arrays in memory column first, i.e., an
|
8 |
+
array element A(i, j, k) will be next to A(i+1, j, k). In C and, consequently,
|
9 |
+
NumPy, arrays are stored row first: A[i, j, k] is next to A[i, j, k+1]. For
|
10 |
+
efficiency and convenience, the input and output arrays of the fitting
|
11 |
+
function (and its Jacobians) are passed to FORTRAN without transposition.
|
12 |
+
Therefore, where the ODRPACK documentation says that the X array is of shape
|
13 |
+
(N, M), it will be passed to the Python function as an array of shape (M, N).
|
14 |
+
If M==1, the 1-D case, then nothing matters; if M>1, then your
|
15 |
+
Python functions will be dealing with arrays that are indexed in reverse of
|
16 |
+
the ODRPACK documentation. No real issue, but watch out for your indexing of
|
17 |
+
the Jacobians: the i,jth elements (@f_i/@x_j) evaluated at the nth
|
18 |
+
observation will be returned as jacd[j, i, n]. Except for the Jacobians, it
|
19 |
+
really is easier to deal with x[0] and x[1] than x[:,0] and x[:,1]. Of course,
|
20 |
+
you can always use the transpose() function from SciPy explicitly.
|
21 |
+
|
22 |
+
* Examples -- See the accompanying file test/test.py for examples of how to set
|
23 |
+
up fits of your own. Some are taken from the User's Guide; some are from
|
24 |
+
other sources.
|
25 |
+
|
26 |
+
* Models -- Some common models are instantiated in the accompanying module
|
27 |
+
models.py . Contributions are welcome.
|
28 |
+
|
29 |
+
Credits
|
30 |
+
=======
|
31 |
+
|
32 |
+
* Thanks to Arnold Moene and Gerard Vermeulen for fixing some killer bugs.
|
33 |
+
|
34 |
+
Robert Kern
|
35 | |
36 |
+
|
37 |
+
"""
|
38 |
+
import os
|
39 |
+
|
40 |
+
import numpy
|
41 |
+
from warnings import warn
|
42 |
+
from scipy.odr import __odrpack
|
43 |
+
|
44 |
+
__all__ = ['odr', 'OdrWarning', 'OdrError', 'OdrStop',
|
45 |
+
'Data', 'RealData', 'Model', 'Output', 'ODR',
|
46 |
+
'odr_error', 'odr_stop']
|
47 |
+
|
48 |
+
odr = __odrpack.odr
|
49 |
+
|
50 |
+
|
51 |
+
class OdrWarning(UserWarning):
|
52 |
+
"""
|
53 |
+
Warning indicating that the data passed into
|
54 |
+
ODR will cause problems when passed into 'odr'
|
55 |
+
that the user should be aware of.
|
56 |
+
"""
|
57 |
+
pass
|
58 |
+
|
59 |
+
|
60 |
+
class OdrError(Exception):
|
61 |
+
"""
|
62 |
+
Exception indicating an error in fitting.
|
63 |
+
|
64 |
+
This is raised by `~scipy.odr.odr` if an error occurs during fitting.
|
65 |
+
"""
|
66 |
+
pass
|
67 |
+
|
68 |
+
|
69 |
+
class OdrStop(Exception):
|
70 |
+
"""
|
71 |
+
Exception stopping fitting.
|
72 |
+
|
73 |
+
You can raise this exception in your objective function to tell
|
74 |
+
`~scipy.odr.odr` to stop fitting.
|
75 |
+
"""
|
76 |
+
pass
|
77 |
+
|
78 |
+
|
79 |
+
# Backwards compatibility
|
80 |
+
odr_error = OdrError
|
81 |
+
odr_stop = OdrStop
|
82 |
+
|
83 |
+
__odrpack._set_exceptions(OdrError, OdrStop)
|
84 |
+
|
85 |
+
|
86 |
+
def _conv(obj, dtype=None):
|
87 |
+
""" Convert an object to the preferred form for input to the odr routine.
|
88 |
+
"""
|
89 |
+
|
90 |
+
if obj is None:
|
91 |
+
return obj
|
92 |
+
else:
|
93 |
+
if dtype is None:
|
94 |
+
obj = numpy.asarray(obj)
|
95 |
+
else:
|
96 |
+
obj = numpy.asarray(obj, dtype)
|
97 |
+
if obj.shape == ():
|
98 |
+
# Scalar.
|
99 |
+
return obj.dtype.type(obj)
|
100 |
+
else:
|
101 |
+
return obj
|
102 |
+
|
103 |
+
|
104 |
+
def _report_error(info):
|
105 |
+
""" Interprets the return code of the odr routine.
|
106 |
+
|
107 |
+
Parameters
|
108 |
+
----------
|
109 |
+
info : int
|
110 |
+
The return code of the odr routine.
|
111 |
+
|
112 |
+
Returns
|
113 |
+
-------
|
114 |
+
problems : list(str)
|
115 |
+
A list of messages about why the odr() routine stopped.
|
116 |
+
"""
|
117 |
+
|
118 |
+
stopreason = ('Blank',
|
119 |
+
'Sum of squares convergence',
|
120 |
+
'Parameter convergence',
|
121 |
+
'Both sum of squares and parameter convergence',
|
122 |
+
'Iteration limit reached')[info % 5]
|
123 |
+
|
124 |
+
if info >= 5:
|
125 |
+
# questionable results or fatal error
|
126 |
+
|
127 |
+
I = (info//10000 % 10,
|
128 |
+
info//1000 % 10,
|
129 |
+
info//100 % 10,
|
130 |
+
info//10 % 10,
|
131 |
+
info % 10)
|
132 |
+
problems = []
|
133 |
+
|
134 |
+
if I[0] == 0:
|
135 |
+
if I[1] != 0:
|
136 |
+
problems.append('Derivatives possibly not correct')
|
137 |
+
if I[2] != 0:
|
138 |
+
problems.append('Error occurred in callback')
|
139 |
+
if I[3] != 0:
|
140 |
+
problems.append('Problem is not full rank at solution')
|
141 |
+
problems.append(stopreason)
|
142 |
+
elif I[0] == 1:
|
143 |
+
if I[1] != 0:
|
144 |
+
problems.append('N < 1')
|
145 |
+
if I[2] != 0:
|
146 |
+
problems.append('M < 1')
|
147 |
+
if I[3] != 0:
|
148 |
+
problems.append('NP < 1 or NP > N')
|
149 |
+
if I[4] != 0:
|
150 |
+
problems.append('NQ < 1')
|
151 |
+
elif I[0] == 2:
|
152 |
+
if I[1] != 0:
|
153 |
+
problems.append('LDY and/or LDX incorrect')
|
154 |
+
if I[2] != 0:
|
155 |
+
problems.append('LDWE, LD2WE, LDWD, and/or LD2WD incorrect')
|
156 |
+
if I[3] != 0:
|
157 |
+
problems.append('LDIFX, LDSTPD, and/or LDSCLD incorrect')
|
158 |
+
if I[4] != 0:
|
159 |
+
problems.append('LWORK and/or LIWORK too small')
|
160 |
+
elif I[0] == 3:
|
161 |
+
if I[1] != 0:
|
162 |
+
problems.append('STPB and/or STPD incorrect')
|
163 |
+
if I[2] != 0:
|
164 |
+
problems.append('SCLB and/or SCLD incorrect')
|
165 |
+
if I[3] != 0:
|
166 |
+
problems.append('WE incorrect')
|
167 |
+
if I[4] != 0:
|
168 |
+
problems.append('WD incorrect')
|
169 |
+
elif I[0] == 4:
|
170 |
+
problems.append('Error in derivatives')
|
171 |
+
elif I[0] == 5:
|
172 |
+
problems.append('Error occurred in callback')
|
173 |
+
elif I[0] == 6:
|
174 |
+
problems.append('Numerical error detected')
|
175 |
+
|
176 |
+
return problems
|
177 |
+
|
178 |
+
else:
|
179 |
+
return [stopreason]
|
180 |
+
|
181 |
+
|
182 |
+
class Data:
|
183 |
+
"""
|
184 |
+
The data to fit.
|
185 |
+
|
186 |
+
Parameters
|
187 |
+
----------
|
188 |
+
x : array_like
|
189 |
+
Observed data for the independent variable of the regression
|
190 |
+
y : array_like, optional
|
191 |
+
If array-like, observed data for the dependent variable of the
|
192 |
+
regression. A scalar input implies that the model to be used on
|
193 |
+
the data is implicit.
|
194 |
+
we : array_like, optional
|
195 |
+
If `we` is a scalar, then that value is used for all data points (and
|
196 |
+
all dimensions of the response variable).
|
197 |
+
If `we` is a rank-1 array of length q (the dimensionality of the
|
198 |
+
response variable), then this vector is the diagonal of the covariant
|
199 |
+
weighting matrix for all data points.
|
200 |
+
If `we` is a rank-1 array of length n (the number of data points), then
|
201 |
+
the i'th element is the weight for the i'th response variable
|
202 |
+
observation (single-dimensional only).
|
203 |
+
If `we` is a rank-2 array of shape (q, q), then this is the full
|
204 |
+
covariant weighting matrix broadcast to each observation.
|
205 |
+
If `we` is a rank-2 array of shape (q, n), then `we[:,i]` is the
|
206 |
+
diagonal of the covariant weighting matrix for the i'th observation.
|
207 |
+
If `we` is a rank-3 array of shape (q, q, n), then `we[:,:,i]` is the
|
208 |
+
full specification of the covariant weighting matrix for each
|
209 |
+
observation.
|
210 |
+
If the fit is implicit, then only a positive scalar value is used.
|
211 |
+
wd : array_like, optional
|
212 |
+
If `wd` is a scalar, then that value is used for all data points
|
213 |
+
(and all dimensions of the input variable). If `wd` = 0, then the
|
214 |
+
covariant weighting matrix for each observation is set to the identity
|
215 |
+
matrix (so each dimension of each observation has the same weight).
|
216 |
+
If `wd` is a rank-1 array of length m (the dimensionality of the input
|
217 |
+
variable), then this vector is the diagonal of the covariant weighting
|
218 |
+
matrix for all data points.
|
219 |
+
If `wd` is a rank-1 array of length n (the number of data points), then
|
220 |
+
the i'th element is the weight for the ith input variable observation
|
221 |
+
(single-dimensional only).
|
222 |
+
If `wd` is a rank-2 array of shape (m, m), then this is the full
|
223 |
+
covariant weighting matrix broadcast to each observation.
|
224 |
+
If `wd` is a rank-2 array of shape (m, n), then `wd[:,i]` is the
|
225 |
+
diagonal of the covariant weighting matrix for the ith observation.
|
226 |
+
If `wd` is a rank-3 array of shape (m, m, n), then `wd[:,:,i]` is the
|
227 |
+
full specification of the covariant weighting matrix for each
|
228 |
+
observation.
|
229 |
+
fix : array_like of ints, optional
|
230 |
+
The `fix` argument is the same as ifixx in the class ODR. It is an
|
231 |
+
array of integers with the same shape as data.x that determines which
|
232 |
+
input observations are treated as fixed. One can use a sequence of
|
233 |
+
length m (the dimensionality of the input observations) to fix some
|
234 |
+
dimensions for all observations. A value of 0 fixes the observation,
|
235 |
+
a value > 0 makes it free.
|
236 |
+
meta : dict, optional
|
237 |
+
Free-form dictionary for metadata.
|
238 |
+
|
239 |
+
Notes
|
240 |
+
-----
|
241 |
+
Each argument is attached to the member of the instance of the same name.
|
242 |
+
The structures of `x` and `y` are described in the Model class docstring.
|
243 |
+
If `y` is an integer, then the Data instance can only be used to fit with
|
244 |
+
implicit models where the dimensionality of the response is equal to the
|
245 |
+
specified value of `y`.
|
246 |
+
|
247 |
+
The `we` argument weights the effect a deviation in the response variable
|
248 |
+
has on the fit. The `wd` argument weights the effect a deviation in the
|
249 |
+
input variable has on the fit. To handle multidimensional inputs and
|
250 |
+
responses easily, the structure of these arguments has the n'th
|
251 |
+
dimensional axis first. These arguments heavily use the structured
|
252 |
+
arguments feature of ODRPACK to conveniently and flexibly support all
|
253 |
+
options. See the ODRPACK User's Guide for a full explanation of how these
|
254 |
+
weights are used in the algorithm. Basically, a higher value of the weight
|
255 |
+
for a particular data point makes a deviation at that point more
|
256 |
+
detrimental to the fit.
|
257 |
+
|
258 |
+
"""
|
259 |
+
|
260 |
+
def __init__(self, x, y=None, we=None, wd=None, fix=None, meta=None):
|
261 |
+
self.x = _conv(x)
|
262 |
+
|
263 |
+
if not isinstance(self.x, numpy.ndarray):
|
264 |
+
raise ValueError("Expected an 'ndarray' of data for 'x', "
|
265 |
+
f"but instead got data of type '{type(self.x).__name__}'")
|
266 |
+
|
267 |
+
self.y = _conv(y)
|
268 |
+
self.we = _conv(we)
|
269 |
+
self.wd = _conv(wd)
|
270 |
+
self.fix = _conv(fix)
|
271 |
+
self.meta = {} if meta is None else meta
|
272 |
+
|
273 |
+
def set_meta(self, **kwds):
|
274 |
+
""" Update the metadata dictionary with the keywords and data provided
|
275 |
+
by keywords.
|
276 |
+
|
277 |
+
Examples
|
278 |
+
--------
|
279 |
+
::
|
280 |
+
|
281 |
+
data.set_meta(lab="Ph 7; Lab 26", title="Ag110 + Ag108 Decay")
|
282 |
+
"""
|
283 |
+
|
284 |
+
self.meta.update(kwds)
|
285 |
+
|
286 |
+
def __getattr__(self, attr):
|
287 |
+
""" Dispatch attribute access to the metadata dictionary.
|
288 |
+
"""
|
289 |
+
if attr in self.meta:
|
290 |
+
return self.meta[attr]
|
291 |
+
else:
|
292 |
+
raise AttributeError("'%s' not in metadata" % attr)
|
293 |
+
|
294 |
+
|
295 |
+
class RealData(Data):
|
296 |
+
"""
|
297 |
+
The data, with weightings as actual standard deviations and/or
|
298 |
+
covariances.
|
299 |
+
|
300 |
+
Parameters
|
301 |
+
----------
|
302 |
+
x : array_like
|
303 |
+
Observed data for the independent variable of the regression
|
304 |
+
y : array_like, optional
|
305 |
+
If array-like, observed data for the dependent variable of the
|
306 |
+
regression. A scalar input implies that the model to be used on
|
307 |
+
the data is implicit.
|
308 |
+
sx : array_like, optional
|
309 |
+
Standard deviations of `x`.
|
310 |
+
`sx` are standard deviations of `x` and are converted to weights by
|
311 |
+
dividing 1.0 by their squares.
|
312 |
+
sy : array_like, optional
|
313 |
+
Standard deviations of `y`.
|
314 |
+
`sy` are standard deviations of `y` and are converted to weights by
|
315 |
+
dividing 1.0 by their squares.
|
316 |
+
covx : array_like, optional
|
317 |
+
Covariance of `x`
|
318 |
+
`covx` is an array of covariance matrices of `x` and are converted to
|
319 |
+
weights by performing a matrix inversion on each observation's
|
320 |
+
covariance matrix.
|
321 |
+
covy : array_like, optional
|
322 |
+
Covariance of `y`
|
323 |
+
`covy` is an array of covariance matrices and are converted to
|
324 |
+
weights by performing a matrix inversion on each observation's
|
325 |
+
covariance matrix.
|
326 |
+
fix : array_like, optional
|
327 |
+
The argument and member fix is the same as Data.fix and ODR.ifixx:
|
328 |
+
It is an array of integers with the same shape as `x` that
|
329 |
+
determines which input observations are treated as fixed. One can
|
330 |
+
use a sequence of length m (the dimensionality of the input
|
331 |
+
observations) to fix some dimensions for all observations. A value
|
332 |
+
of 0 fixes the observation, a value > 0 makes it free.
|
333 |
+
meta : dict, optional
|
334 |
+
Free-form dictionary for metadata.
|
335 |
+
|
336 |
+
Notes
|
337 |
+
-----
|
338 |
+
The weights `wd` and `we` are computed from provided values as follows:
|
339 |
+
|
340 |
+
`sx` and `sy` are converted to weights by dividing 1.0 by their squares.
|
341 |
+
For example, ``wd = 1./numpy.power(`sx`, 2)``.
|
342 |
+
|
343 |
+
`covx` and `covy` are arrays of covariance matrices and are converted to
|
344 |
+
weights by performing a matrix inversion on each observation's covariance
|
345 |
+
matrix. For example, ``we[i] = numpy.linalg.inv(covy[i])``.
|
346 |
+
|
347 |
+
These arguments follow the same structured argument conventions as wd and
|
348 |
+
we only restricted by their natures: `sx` and `sy` can't be rank-3, but
|
349 |
+
`covx` and `covy` can be.
|
350 |
+
|
351 |
+
Only set *either* `sx` or `covx` (not both). Setting both will raise an
|
352 |
+
exception. Same with `sy` and `covy`.
|
353 |
+
|
354 |
+
"""
|
355 |
+
|
356 |
+
def __init__(self, x, y=None, sx=None, sy=None, covx=None, covy=None,
|
357 |
+
fix=None, meta=None):
|
358 |
+
if (sx is not None) and (covx is not None):
|
359 |
+
raise ValueError("cannot set both sx and covx")
|
360 |
+
if (sy is not None) and (covy is not None):
|
361 |
+
raise ValueError("cannot set both sy and covy")
|
362 |
+
|
363 |
+
# Set flags for __getattr__
|
364 |
+
self._ga_flags = {}
|
365 |
+
if sx is not None:
|
366 |
+
self._ga_flags['wd'] = 'sx'
|
367 |
+
else:
|
368 |
+
self._ga_flags['wd'] = 'covx'
|
369 |
+
if sy is not None:
|
370 |
+
self._ga_flags['we'] = 'sy'
|
371 |
+
else:
|
372 |
+
self._ga_flags['we'] = 'covy'
|
373 |
+
|
374 |
+
self.x = _conv(x)
|
375 |
+
|
376 |
+
if not isinstance(self.x, numpy.ndarray):
|
377 |
+
raise ValueError("Expected an 'ndarray' of data for 'x', "
|
378 |
+
f"but instead got data of type '{type(self.x).__name__}'")
|
379 |
+
|
380 |
+
self.y = _conv(y)
|
381 |
+
self.sx = _conv(sx)
|
382 |
+
self.sy = _conv(sy)
|
383 |
+
self.covx = _conv(covx)
|
384 |
+
self.covy = _conv(covy)
|
385 |
+
self.fix = _conv(fix)
|
386 |
+
self.meta = {} if meta is None else meta
|
387 |
+
|
388 |
+
def _sd2wt(self, sd):
|
389 |
+
""" Convert standard deviation to weights.
|
390 |
+
"""
|
391 |
+
|
392 |
+
return 1./numpy.power(sd, 2)
|
393 |
+
|
394 |
+
def _cov2wt(self, cov):
|
395 |
+
""" Convert covariance matrix(-ices) to weights.
|
396 |
+
"""
|
397 |
+
|
398 |
+
from scipy.linalg import inv
|
399 |
+
|
400 |
+
if len(cov.shape) == 2:
|
401 |
+
return inv(cov)
|
402 |
+
else:
|
403 |
+
weights = numpy.zeros(cov.shape, float)
|
404 |
+
|
405 |
+
for i in range(cov.shape[-1]): # n
|
406 |
+
weights[:,:,i] = inv(cov[:,:,i])
|
407 |
+
|
408 |
+
return weights
|
409 |
+
|
410 |
+
def __getattr__(self, attr):
|
411 |
+
lookup_tbl = {('wd', 'sx'): (self._sd2wt, self.sx),
|
412 |
+
('wd', 'covx'): (self._cov2wt, self.covx),
|
413 |
+
('we', 'sy'): (self._sd2wt, self.sy),
|
414 |
+
('we', 'covy'): (self._cov2wt, self.covy)}
|
415 |
+
|
416 |
+
if attr not in ('wd', 'we'):
|
417 |
+
if attr in self.meta:
|
418 |
+
return self.meta[attr]
|
419 |
+
else:
|
420 |
+
raise AttributeError("'%s' not in metadata" % attr)
|
421 |
+
else:
|
422 |
+
func, arg = lookup_tbl[(attr, self._ga_flags[attr])]
|
423 |
+
|
424 |
+
if arg is not None:
|
425 |
+
return func(*(arg,))
|
426 |
+
else:
|
427 |
+
return None
|
428 |
+
|
429 |
+
|
430 |
+
class Model:
|
431 |
+
"""
|
432 |
+
The Model class stores information about the function you wish to fit.
|
433 |
+
|
434 |
+
It stores the function itself, at the least, and optionally stores
|
435 |
+
functions which compute the Jacobians used during fitting. Also, one
|
436 |
+
can provide a function that will provide reasonable starting values
|
437 |
+
for the fit parameters possibly given the set of data.
|
438 |
+
|
439 |
+
Parameters
|
440 |
+
----------
|
441 |
+
fcn : function
|
442 |
+
fcn(beta, x) --> y
|
443 |
+
fjacb : function
|
444 |
+
Jacobian of fcn wrt the fit parameters beta.
|
445 |
+
|
446 |
+
fjacb(beta, x) --> @f_i(x,B)/@B_j
|
447 |
+
fjacd : function
|
448 |
+
Jacobian of fcn wrt the (possibly multidimensional) input
|
449 |
+
variable.
|
450 |
+
|
451 |
+
fjacd(beta, x) --> @f_i(x,B)/@x_j
|
452 |
+
extra_args : tuple, optional
|
453 |
+
If specified, `extra_args` should be a tuple of extra
|
454 |
+
arguments to pass to `fcn`, `fjacb`, and `fjacd`. Each will be called
|
455 |
+
by `apply(fcn, (beta, x) + extra_args)`
|
456 |
+
estimate : array_like of rank-1
|
457 |
+
Provides estimates of the fit parameters from the data
|
458 |
+
|
459 |
+
estimate(data) --> estbeta
|
460 |
+
implicit : boolean
|
461 |
+
If TRUE, specifies that the model
|
462 |
+
is implicit; i.e `fcn(beta, x)` ~= 0 and there is no y data to fit
|
463 |
+
against
|
464 |
+
meta : dict, optional
|
465 |
+
freeform dictionary of metadata for the model
|
466 |
+
|
467 |
+
Notes
|
468 |
+
-----
|
469 |
+
Note that the `fcn`, `fjacb`, and `fjacd` operate on NumPy arrays and
|
470 |
+
return a NumPy array. The `estimate` object takes an instance of the
|
471 |
+
Data class.
|
472 |
+
|
473 |
+
Here are the rules for the shapes of the argument and return
|
474 |
+
arrays of the callback functions:
|
475 |
+
|
476 |
+
`x`
|
477 |
+
if the input data is single-dimensional, then `x` is rank-1
|
478 |
+
array; i.e., ``x = array([1, 2, 3, ...]); x.shape = (n,)``
|
479 |
+
If the input data is multi-dimensional, then `x` is a rank-2 array;
|
480 |
+
i.e., ``x = array([[1, 2, ...], [2, 4, ...]]); x.shape = (m, n)``.
|
481 |
+
In all cases, it has the same shape as the input data array passed to
|
482 |
+
`~scipy.odr.odr`. `m` is the dimensionality of the input data,
|
483 |
+
`n` is the number of observations.
|
484 |
+
`y`
|
485 |
+
if the response variable is single-dimensional, then `y` is a
|
486 |
+
rank-1 array, i.e., ``y = array([2, 4, ...]); y.shape = (n,)``.
|
487 |
+
If the response variable is multi-dimensional, then `y` is a rank-2
|
488 |
+
array, i.e., ``y = array([[2, 4, ...], [3, 6, ...]]); y.shape =
|
489 |
+
(q, n)`` where `q` is the dimensionality of the response variable.
|
490 |
+
`beta`
|
491 |
+
rank-1 array of length `p` where `p` is the number of parameters;
|
492 |
+
i.e. ``beta = array([B_1, B_2, ..., B_p])``
|
493 |
+
`fjacb`
|
494 |
+
if the response variable is multi-dimensional, then the
|
495 |
+
return array's shape is `(q, p, n)` such that ``fjacb(x,beta)[l,k,i] =
|
496 |
+
d f_l(X,B)/d B_k`` evaluated at the ith data point. If `q == 1`, then
|
497 |
+
the return array is only rank-2 and with shape `(p, n)`.
|
498 |
+
`fjacd`
|
499 |
+
as with fjacb, only the return array's shape is `(q, m, n)`
|
500 |
+
such that ``fjacd(x,beta)[l,j,i] = d f_l(X,B)/d X_j`` at the ith data
|
501 |
+
point. If `q == 1`, then the return array's shape is `(m, n)`. If
|
502 |
+
`m == 1`, the shape is (q, n). If `m == q == 1`, the shape is `(n,)`.
|
503 |
+
|
504 |
+
"""
|
505 |
+
|
506 |
+
def __init__(self, fcn, fjacb=None, fjacd=None,
|
507 |
+
extra_args=None, estimate=None, implicit=0, meta=None):
|
508 |
+
|
509 |
+
self.fcn = fcn
|
510 |
+
self.fjacb = fjacb
|
511 |
+
self.fjacd = fjacd
|
512 |
+
|
513 |
+
if extra_args is not None:
|
514 |
+
extra_args = tuple(extra_args)
|
515 |
+
|
516 |
+
self.extra_args = extra_args
|
517 |
+
self.estimate = estimate
|
518 |
+
self.implicit = implicit
|
519 |
+
self.meta = meta if meta is not None else {}
|
520 |
+
|
521 |
+
def set_meta(self, **kwds):
|
522 |
+
""" Update the metadata dictionary with the keywords and data provided
|
523 |
+
here.
|
524 |
+
|
525 |
+
Examples
|
526 |
+
--------
|
527 |
+
set_meta(name="Exponential", equation="y = a exp(b x) + c")
|
528 |
+
"""
|
529 |
+
|
530 |
+
self.meta.update(kwds)
|
531 |
+
|
532 |
+
def __getattr__(self, attr):
|
533 |
+
""" Dispatch attribute access to the metadata.
|
534 |
+
"""
|
535 |
+
|
536 |
+
if attr in self.meta:
|
537 |
+
return self.meta[attr]
|
538 |
+
else:
|
539 |
+
raise AttributeError("'%s' not in metadata" % attr)
|
540 |
+
|
541 |
+
|
542 |
+
class Output:
|
543 |
+
"""
|
544 |
+
The Output class stores the output of an ODR run.
|
545 |
+
|
546 |
+
Attributes
|
547 |
+
----------
|
548 |
+
beta : ndarray
|
549 |
+
Estimated parameter values, of shape (q,).
|
550 |
+
sd_beta : ndarray
|
551 |
+
Standard deviations of the estimated parameters, of shape (p,).
|
552 |
+
cov_beta : ndarray
|
553 |
+
Covariance matrix of the estimated parameters, of shape (p,p).
|
554 |
+
Note that this `cov_beta` is not scaled by the residual variance
|
555 |
+
`res_var`, whereas `sd_beta` is. This means
|
556 |
+
``np.sqrt(np.diag(output.cov_beta * output.res_var))`` is the same
|
557 |
+
result as `output.sd_beta`.
|
558 |
+
delta : ndarray, optional
|
559 |
+
Array of estimated errors in input variables, of same shape as `x`.
|
560 |
+
eps : ndarray, optional
|
561 |
+
Array of estimated errors in response variables, of same shape as `y`.
|
562 |
+
xplus : ndarray, optional
|
563 |
+
Array of ``x + delta``.
|
564 |
+
y : ndarray, optional
|
565 |
+
Array ``y = fcn(x + delta)``.
|
566 |
+
res_var : float, optional
|
567 |
+
Residual variance.
|
568 |
+
sum_square : float, optional
|
569 |
+
Sum of squares error.
|
570 |
+
sum_square_delta : float, optional
|
571 |
+
Sum of squares of delta error.
|
572 |
+
sum_square_eps : float, optional
|
573 |
+
Sum of squares of eps error.
|
574 |
+
inv_condnum : float, optional
|
575 |
+
Inverse condition number (cf. ODRPACK UG p. 77).
|
576 |
+
rel_error : float, optional
|
577 |
+
Relative error in function values computed within fcn.
|
578 |
+
work : ndarray, optional
|
579 |
+
Final work array.
|
580 |
+
work_ind : dict, optional
|
581 |
+
Indices into work for drawing out values (cf. ODRPACK UG p. 83).
|
582 |
+
info : int, optional
|
583 |
+
Reason for returning, as output by ODRPACK (cf. ODRPACK UG p. 38).
|
584 |
+
stopreason : list of str, optional
|
585 |
+
`info` interpreted into English.
|
586 |
+
|
587 |
+
Notes
|
588 |
+
-----
|
589 |
+
Takes one argument for initialization, the return value from the
|
590 |
+
function `~scipy.odr.odr`. The attributes listed as "optional" above are
|
591 |
+
only present if `~scipy.odr.odr` was run with ``full_output=1``.
|
592 |
+
|
593 |
+
"""
|
594 |
+
|
595 |
+
def __init__(self, output):
|
596 |
+
self.beta = output[0]
|
597 |
+
self.sd_beta = output[1]
|
598 |
+
self.cov_beta = output[2]
|
599 |
+
|
600 |
+
if len(output) == 4:
|
601 |
+
# full output
|
602 |
+
self.__dict__.update(output[3])
|
603 |
+
self.stopreason = _report_error(self.info)
|
604 |
+
|
605 |
+
def pprint(self):
|
606 |
+
""" Pretty-print important results.
|
607 |
+
"""
|
608 |
+
|
609 |
+
print('Beta:', self.beta)
|
610 |
+
print('Beta Std Error:', self.sd_beta)
|
611 |
+
print('Beta Covariance:', self.cov_beta)
|
612 |
+
if hasattr(self, 'info'):
|
613 |
+
print('Residual Variance:',self.res_var)
|
614 |
+
print('Inverse Condition #:', self.inv_condnum)
|
615 |
+
print('Reason(s) for Halting:')
|
616 |
+
for r in self.stopreason:
|
617 |
+
print(' %s' % r)
|
618 |
+
|
619 |
+
|
620 |
+
class ODR:
|
621 |
+
"""
|
622 |
+
The ODR class gathers all information and coordinates the running of the
|
623 |
+
main fitting routine.
|
624 |
+
|
625 |
+
Members of instances of the ODR class have the same names as the arguments
|
626 |
+
to the initialization routine.
|
627 |
+
|
628 |
+
Parameters
|
629 |
+
----------
|
630 |
+
data : Data class instance
|
631 |
+
instance of the Data class
|
632 |
+
model : Model class instance
|
633 |
+
instance of the Model class
|
634 |
+
|
635 |
+
Other Parameters
|
636 |
+
----------------
|
637 |
+
beta0 : array_like of rank-1
|
638 |
+
a rank-1 sequence of initial parameter values. Optional if
|
639 |
+
model provides an "estimate" function to estimate these values.
|
640 |
+
delta0 : array_like of floats of rank-1, optional
|
641 |
+
a (double-precision) float array to hold the initial values of
|
642 |
+
the errors in the input variables. Must be same shape as data.x
|
643 |
+
ifixb : array_like of ints of rank-1, optional
|
644 |
+
sequence of integers with the same length as beta0 that determines
|
645 |
+
which parameters are held fixed. A value of 0 fixes the parameter,
|
646 |
+
a value > 0 makes the parameter free.
|
647 |
+
ifixx : array_like of ints with same shape as data.x, optional
|
648 |
+
an array of integers with the same shape as data.x that determines
|
649 |
+
which input observations are treated as fixed. One can use a sequence
|
650 |
+
of length m (the dimensionality of the input observations) to fix some
|
651 |
+
dimensions for all observations. A value of 0 fixes the observation,
|
652 |
+
a value > 0 makes it free.
|
653 |
+
job : int, optional
|
654 |
+
an integer telling ODRPACK what tasks to perform. See p. 31 of the
|
655 |
+
ODRPACK User's Guide if you absolutely must set the value here. Use the
|
656 |
+
method set_job post-initialization for a more readable interface.
|
657 |
+
iprint : int, optional
|
658 |
+
an integer telling ODRPACK what to print. See pp. 33-34 of the
|
659 |
+
ODRPACK User's Guide if you absolutely must set the value here. Use the
|
660 |
+
method set_iprint post-initialization for a more readable interface.
|
661 |
+
errfile : str, optional
|
662 |
+
string with the filename to print ODRPACK errors to. If the file already
|
663 |
+
exists, an error will be thrown. The `overwrite` argument can be used to
|
664 |
+
prevent this. *Do Not Open This File Yourself!*
|
665 |
+
rptfile : str, optional
|
666 |
+
string with the filename to print ODRPACK summaries to. If the file
|
667 |
+
already exists, an error will be thrown. The `overwrite` argument can be
|
668 |
+
used to prevent this. *Do Not Open This File Yourself!*
|
669 |
+
ndigit : int, optional
|
670 |
+
integer specifying the number of reliable digits in the computation
|
671 |
+
of the function.
|
672 |
+
taufac : float, optional
|
673 |
+
float specifying the initial trust region. The default value is 1.
|
674 |
+
The initial trust region is equal to taufac times the length of the
|
675 |
+
first computed Gauss-Newton step. taufac must be less than 1.
|
676 |
+
sstol : float, optional
|
677 |
+
float specifying the tolerance for convergence based on the relative
|
678 |
+
change in the sum-of-squares. The default value is eps**(1/2) where eps
|
679 |
+
is the smallest value such that 1 + eps > 1 for double precision
|
680 |
+
computation on the machine. sstol must be less than 1.
|
681 |
+
partol : float, optional
|
682 |
+
float specifying the tolerance for convergence based on the relative
|
683 |
+
change in the estimated parameters. The default value is eps**(2/3) for
|
684 |
+
explicit models and ``eps**(1/3)`` for implicit models. partol must be less
|
685 |
+
than 1.
|
686 |
+
maxit : int, optional
|
687 |
+
integer specifying the maximum number of iterations to perform. For
|
688 |
+
first runs, maxit is the total number of iterations performed and
|
689 |
+
defaults to 50. For restarts, maxit is the number of additional
|
690 |
+
iterations to perform and defaults to 10.
|
691 |
+
stpb : array_like, optional
|
692 |
+
sequence (``len(stpb) == len(beta0)``) of relative step sizes to compute
|
693 |
+
finite difference derivatives wrt the parameters.
|
694 |
+
stpd : optional
|
695 |
+
array (``stpd.shape == data.x.shape`` or ``stpd.shape == (m,)``) of relative
|
696 |
+
step sizes to compute finite difference derivatives wrt the input
|
697 |
+
variable errors. If stpd is a rank-1 array with length m (the
|
698 |
+
dimensionality of the input variable), then the values are broadcast to
|
699 |
+
all observations.
|
700 |
+
sclb : array_like, optional
|
701 |
+
sequence (``len(stpb) == len(beta0)``) of scaling factors for the
|
702 |
+
parameters. The purpose of these scaling factors are to scale all of
|
703 |
+
the parameters to around unity. Normally appropriate scaling factors
|
704 |
+
are computed if this argument is not specified. Specify them yourself
|
705 |
+
if the automatic procedure goes awry.
|
706 |
+
scld : array_like, optional
|
707 |
+
array (scld.shape == data.x.shape or scld.shape == (m,)) of scaling
|
708 |
+
factors for the *errors* in the input variables. Again, these factors
|
709 |
+
are automatically computed if you do not provide them. If scld.shape ==
|
710 |
+
(m,), then the scaling factors are broadcast to all observations.
|
711 |
+
work : ndarray, optional
|
712 |
+
array to hold the double-valued working data for ODRPACK. When
|
713 |
+
restarting, takes the value of self.output.work.
|
714 |
+
iwork : ndarray, optional
|
715 |
+
array to hold the integer-valued working data for ODRPACK. When
|
716 |
+
restarting, takes the value of self.output.iwork.
|
717 |
+
overwrite : bool, optional
|
718 |
+
If it is True, output files defined by `errfile` and `rptfile` are
|
719 |
+
overwritten. The default is False.
|
720 |
+
|
721 |
+
Attributes
|
722 |
+
----------
|
723 |
+
data : Data
|
724 |
+
The data for this fit
|
725 |
+
model : Model
|
726 |
+
The model used in fit
|
727 |
+
output : Output
|
728 |
+
An instance if the Output class containing all of the returned
|
729 |
+
data from an invocation of ODR.run() or ODR.restart()
|
730 |
+
|
731 |
+
"""
|
732 |
+
|
733 |
+
def __init__(self, data, model, beta0=None, delta0=None, ifixb=None,
|
734 |
+
ifixx=None, job=None, iprint=None, errfile=None, rptfile=None,
|
735 |
+
ndigit=None, taufac=None, sstol=None, partol=None, maxit=None,
|
736 |
+
stpb=None, stpd=None, sclb=None, scld=None, work=None, iwork=None,
|
737 |
+
overwrite=False):
|
738 |
+
|
739 |
+
self.data = data
|
740 |
+
self.model = model
|
741 |
+
|
742 |
+
if beta0 is None:
|
743 |
+
if self.model.estimate is not None:
|
744 |
+
self.beta0 = _conv(self.model.estimate(self.data))
|
745 |
+
else:
|
746 |
+
raise ValueError(
|
747 |
+
"must specify beta0 or provide an estimator with the model"
|
748 |
+
)
|
749 |
+
else:
|
750 |
+
self.beta0 = _conv(beta0)
|
751 |
+
|
752 |
+
if ifixx is None and data.fix is not None:
|
753 |
+
ifixx = data.fix
|
754 |
+
|
755 |
+
if overwrite:
|
756 |
+
# remove output files for overwriting.
|
757 |
+
if rptfile is not None and os.path.exists(rptfile):
|
758 |
+
os.remove(rptfile)
|
759 |
+
if errfile is not None and os.path.exists(errfile):
|
760 |
+
os.remove(errfile)
|
761 |
+
|
762 |
+
self.delta0 = _conv(delta0)
|
763 |
+
# These really are 32-bit integers in FORTRAN (gfortran), even on 64-bit
|
764 |
+
# platforms.
|
765 |
+
# XXX: some other FORTRAN compilers may not agree.
|
766 |
+
self.ifixx = _conv(ifixx, dtype=numpy.int32)
|
767 |
+
self.ifixb = _conv(ifixb, dtype=numpy.int32)
|
768 |
+
self.job = job
|
769 |
+
self.iprint = iprint
|
770 |
+
self.errfile = errfile
|
771 |
+
self.rptfile = rptfile
|
772 |
+
self.ndigit = ndigit
|
773 |
+
self.taufac = taufac
|
774 |
+
self.sstol = sstol
|
775 |
+
self.partol = partol
|
776 |
+
self.maxit = maxit
|
777 |
+
self.stpb = _conv(stpb)
|
778 |
+
self.stpd = _conv(stpd)
|
779 |
+
self.sclb = _conv(sclb)
|
780 |
+
self.scld = _conv(scld)
|
781 |
+
self.work = _conv(work)
|
782 |
+
self.iwork = _conv(iwork)
|
783 |
+
|
784 |
+
self.output = None
|
785 |
+
|
786 |
+
self._check()
|
787 |
+
|
788 |
+
def _check(self):
|
789 |
+
""" Check the inputs for consistency, but don't bother checking things
|
790 |
+
that the builtin function odr will check.
|
791 |
+
"""
|
792 |
+
|
793 |
+
x_s = list(self.data.x.shape)
|
794 |
+
|
795 |
+
if isinstance(self.data.y, numpy.ndarray):
|
796 |
+
y_s = list(self.data.y.shape)
|
797 |
+
if self.model.implicit:
|
798 |
+
raise OdrError("an implicit model cannot use response data")
|
799 |
+
else:
|
800 |
+
# implicit model with q == self.data.y
|
801 |
+
y_s = [self.data.y, x_s[-1]]
|
802 |
+
if not self.model.implicit:
|
803 |
+
raise OdrError("an explicit model needs response data")
|
804 |
+
self.set_job(fit_type=1)
|
805 |
+
|
806 |
+
if x_s[-1] != y_s[-1]:
|
807 |
+
raise OdrError("number of observations do not match")
|
808 |
+
|
809 |
+
n = x_s[-1]
|
810 |
+
|
811 |
+
if len(x_s) == 2:
|
812 |
+
m = x_s[0]
|
813 |
+
else:
|
814 |
+
m = 1
|
815 |
+
if len(y_s) == 2:
|
816 |
+
q = y_s[0]
|
817 |
+
else:
|
818 |
+
q = 1
|
819 |
+
|
820 |
+
p = len(self.beta0)
|
821 |
+
|
822 |
+
# permissible output array shapes
|
823 |
+
|
824 |
+
fcn_perms = [(q, n)]
|
825 |
+
fjacd_perms = [(q, m, n)]
|
826 |
+
fjacb_perms = [(q, p, n)]
|
827 |
+
|
828 |
+
if q == 1:
|
829 |
+
fcn_perms.append((n,))
|
830 |
+
fjacd_perms.append((m, n))
|
831 |
+
fjacb_perms.append((p, n))
|
832 |
+
if m == 1:
|
833 |
+
fjacd_perms.append((q, n))
|
834 |
+
if p == 1:
|
835 |
+
fjacb_perms.append((q, n))
|
836 |
+
if m == q == 1:
|
837 |
+
fjacd_perms.append((n,))
|
838 |
+
if p == q == 1:
|
839 |
+
fjacb_perms.append((n,))
|
840 |
+
|
841 |
+
# try evaluating the supplied functions to make sure they provide
|
842 |
+
# sensible outputs
|
843 |
+
|
844 |
+
arglist = (self.beta0, self.data.x)
|
845 |
+
if self.model.extra_args is not None:
|
846 |
+
arglist = arglist + self.model.extra_args
|
847 |
+
res = self.model.fcn(*arglist)
|
848 |
+
|
849 |
+
if res.shape not in fcn_perms:
|
850 |
+
print(res.shape)
|
851 |
+
print(fcn_perms)
|
852 |
+
raise OdrError("fcn does not output %s-shaped array" % y_s)
|
853 |
+
|
854 |
+
if self.model.fjacd is not None:
|
855 |
+
res = self.model.fjacd(*arglist)
|
856 |
+
if res.shape not in fjacd_perms:
|
857 |
+
raise OdrError(
|
858 |
+
"fjacd does not output %s-shaped array" % repr((q, m, n)))
|
859 |
+
if self.model.fjacb is not None:
|
860 |
+
res = self.model.fjacb(*arglist)
|
861 |
+
if res.shape not in fjacb_perms:
|
862 |
+
raise OdrError(
|
863 |
+
"fjacb does not output %s-shaped array" % repr((q, p, n)))
|
864 |
+
|
865 |
+
# check shape of delta0
|
866 |
+
|
867 |
+
if self.delta0 is not None and self.delta0.shape != self.data.x.shape:
|
868 |
+
raise OdrError(
|
869 |
+
"delta0 is not a %s-shaped array" % repr(self.data.x.shape))
|
870 |
+
|
871 |
+
if self.data.x.size == 0:
|
872 |
+
warn("Empty data detected for ODR instance. "
|
873 |
+
"Do not expect any fitting to occur",
|
874 |
+
OdrWarning, stacklevel=3)
|
875 |
+
|
876 |
+
def _gen_work(self):
|
877 |
+
""" Generate a suitable work array if one does not already exist.
|
878 |
+
"""
|
879 |
+
|
880 |
+
n = self.data.x.shape[-1]
|
881 |
+
p = self.beta0.shape[0]
|
882 |
+
|
883 |
+
if len(self.data.x.shape) == 2:
|
884 |
+
m = self.data.x.shape[0]
|
885 |
+
else:
|
886 |
+
m = 1
|
887 |
+
|
888 |
+
if self.model.implicit:
|
889 |
+
q = self.data.y
|
890 |
+
elif len(self.data.y.shape) == 2:
|
891 |
+
q = self.data.y.shape[0]
|
892 |
+
else:
|
893 |
+
q = 1
|
894 |
+
|
895 |
+
if self.data.we is None:
|
896 |
+
ldwe = ld2we = 1
|
897 |
+
elif len(self.data.we.shape) == 3:
|
898 |
+
ld2we, ldwe = self.data.we.shape[1:]
|
899 |
+
else:
|
900 |
+
we = self.data.we
|
901 |
+
ldwe = 1
|
902 |
+
ld2we = 1
|
903 |
+
if we.ndim == 1 and q == 1:
|
904 |
+
ldwe = n
|
905 |
+
elif we.ndim == 2:
|
906 |
+
if we.shape == (q, q):
|
907 |
+
ld2we = q
|
908 |
+
elif we.shape == (q, n):
|
909 |
+
ldwe = n
|
910 |
+
|
911 |
+
if self.job % 10 < 2:
|
912 |
+
# ODR not OLS
|
913 |
+
lwork = (18 + 11*p + p*p + m + m*m + 4*n*q + 6*n*m + 2*n*q*p +
|
914 |
+
2*n*q*m + q*q + 5*q + q*(p+m) + ldwe*ld2we*q)
|
915 |
+
else:
|
916 |
+
# OLS not ODR
|
917 |
+
lwork = (18 + 11*p + p*p + m + m*m + 4*n*q + 2*n*m + 2*n*q*p +
|
918 |
+
5*q + q*(p+m) + ldwe*ld2we*q)
|
919 |
+
|
920 |
+
if isinstance(self.work, numpy.ndarray) and self.work.shape == (lwork,)\
|
921 |
+
and self.work.dtype.str.endswith('f8'):
|
922 |
+
# the existing array is fine
|
923 |
+
return
|
924 |
+
else:
|
925 |
+
self.work = numpy.zeros((lwork,), float)
|
926 |
+
|
927 |
+
def set_job(self, fit_type=None, deriv=None, var_calc=None,
|
928 |
+
del_init=None, restart=None):
|
929 |
+
"""
|
930 |
+
Sets the "job" parameter is a hopefully comprehensible way.
|
931 |
+
|
932 |
+
If an argument is not specified, then the value is left as is. The
|
933 |
+
default value from class initialization is for all of these options set
|
934 |
+
to 0.
|
935 |
+
|
936 |
+
Parameters
|
937 |
+
----------
|
938 |
+
fit_type : {0, 1, 2} int
|
939 |
+
0 -> explicit ODR
|
940 |
+
|
941 |
+
1 -> implicit ODR
|
942 |
+
|
943 |
+
2 -> ordinary least-squares
|
944 |
+
deriv : {0, 1, 2, 3} int
|
945 |
+
0 -> forward finite differences
|
946 |
+
|
947 |
+
1 -> central finite differences
|
948 |
+
|
949 |
+
2 -> user-supplied derivatives (Jacobians) with results
|
950 |
+
checked by ODRPACK
|
951 |
+
|
952 |
+
3 -> user-supplied derivatives, no checking
|
953 |
+
var_calc : {0, 1, 2} int
|
954 |
+
0 -> calculate asymptotic covariance matrix and fit
|
955 |
+
parameter uncertainties (V_B, s_B) using derivatives
|
956 |
+
recomputed at the final solution
|
957 |
+
|
958 |
+
1 -> calculate V_B and s_B using derivatives from last iteration
|
959 |
+
|
960 |
+
2 -> do not calculate V_B and s_B
|
961 |
+
del_init : {0, 1} int
|
962 |
+
0 -> initial input variable offsets set to 0
|
963 |
+
|
964 |
+
1 -> initial offsets provided by user in variable "work"
|
965 |
+
restart : {0, 1} int
|
966 |
+
0 -> fit is not a restart
|
967 |
+
|
968 |
+
1 -> fit is a restart
|
969 |
+
|
970 |
+
Notes
|
971 |
+
-----
|
972 |
+
The permissible values are different from those given on pg. 31 of the
|
973 |
+
ODRPACK User's Guide only in that one cannot specify numbers greater than
|
974 |
+
the last value for each variable.
|
975 |
+
|
976 |
+
If one does not supply functions to compute the Jacobians, the fitting
|
977 |
+
procedure will change deriv to 0, finite differences, as a default. To
|
978 |
+
initialize the input variable offsets by yourself, set del_init to 1 and
|
979 |
+
put the offsets into the "work" variable correctly.
|
980 |
+
|
981 |
+
"""
|
982 |
+
|
983 |
+
if self.job is None:
|
984 |
+
job_l = [0, 0, 0, 0, 0]
|
985 |
+
else:
|
986 |
+
job_l = [self.job // 10000 % 10,
|
987 |
+
self.job // 1000 % 10,
|
988 |
+
self.job // 100 % 10,
|
989 |
+
self.job // 10 % 10,
|
990 |
+
self.job % 10]
|
991 |
+
|
992 |
+
if fit_type in (0, 1, 2):
|
993 |
+
job_l[4] = fit_type
|
994 |
+
if deriv in (0, 1, 2, 3):
|
995 |
+
job_l[3] = deriv
|
996 |
+
if var_calc in (0, 1, 2):
|
997 |
+
job_l[2] = var_calc
|
998 |
+
if del_init in (0, 1):
|
999 |
+
job_l[1] = del_init
|
1000 |
+
if restart in (0, 1):
|
1001 |
+
job_l[0] = restart
|
1002 |
+
|
1003 |
+
self.job = (job_l[0]*10000 + job_l[1]*1000 +
|
1004 |
+
job_l[2]*100 + job_l[3]*10 + job_l[4])
|
1005 |
+
|
1006 |
+
def set_iprint(self, init=None, so_init=None,
|
1007 |
+
iter=None, so_iter=None, iter_step=None, final=None, so_final=None):
|
1008 |
+
""" Set the iprint parameter for the printing of computation reports.
|
1009 |
+
|
1010 |
+
If any of the arguments are specified here, then they are set in the
|
1011 |
+
iprint member. If iprint is not set manually or with this method, then
|
1012 |
+
ODRPACK defaults to no printing. If no filename is specified with the
|
1013 |
+
member rptfile, then ODRPACK prints to stdout. One can tell ODRPACK to
|
1014 |
+
print to stdout in addition to the specified filename by setting the
|
1015 |
+
so_* arguments to this function, but one cannot specify to print to
|
1016 |
+
stdout but not a file since one can do that by not specifying a rptfile
|
1017 |
+
filename.
|
1018 |
+
|
1019 |
+
There are three reports: initialization, iteration, and final reports.
|
1020 |
+
They are represented by the arguments init, iter, and final
|
1021 |
+
respectively. The permissible values are 0, 1, and 2 representing "no
|
1022 |
+
report", "short report", and "long report" respectively.
|
1023 |
+
|
1024 |
+
The argument iter_step (0 <= iter_step <= 9) specifies how often to make
|
1025 |
+
the iteration report; the report will be made for every iter_step'th
|
1026 |
+
iteration starting with iteration one. If iter_step == 0, then no
|
1027 |
+
iteration report is made, regardless of the other arguments.
|
1028 |
+
|
1029 |
+
If the rptfile is None, then any so_* arguments supplied will raise an
|
1030 |
+
exception.
|
1031 |
+
"""
|
1032 |
+
if self.iprint is None:
|
1033 |
+
self.iprint = 0
|
1034 |
+
|
1035 |
+
ip = [self.iprint // 1000 % 10,
|
1036 |
+
self.iprint // 100 % 10,
|
1037 |
+
self.iprint // 10 % 10,
|
1038 |
+
self.iprint % 10]
|
1039 |
+
|
1040 |
+
# make a list to convert iprint digits to/from argument inputs
|
1041 |
+
# rptfile, stdout
|
1042 |
+
ip2arg = [[0, 0], # none, none
|
1043 |
+
[1, 0], # short, none
|
1044 |
+
[2, 0], # long, none
|
1045 |
+
[1, 1], # short, short
|
1046 |
+
[2, 1], # long, short
|
1047 |
+
[1, 2], # short, long
|
1048 |
+
[2, 2]] # long, long
|
1049 |
+
|
1050 |
+
if (self.rptfile is None and
|
1051 |
+
(so_init is not None or
|
1052 |
+
so_iter is not None or
|
1053 |
+
so_final is not None)):
|
1054 |
+
raise OdrError(
|
1055 |
+
"no rptfile specified, cannot output to stdout twice")
|
1056 |
+
|
1057 |
+
iprint_l = ip2arg[ip[0]] + ip2arg[ip[1]] + ip2arg[ip[3]]
|
1058 |
+
|
1059 |
+
if init is not None:
|
1060 |
+
iprint_l[0] = init
|
1061 |
+
if so_init is not None:
|
1062 |
+
iprint_l[1] = so_init
|
1063 |
+
if iter is not None:
|
1064 |
+
iprint_l[2] = iter
|
1065 |
+
if so_iter is not None:
|
1066 |
+
iprint_l[3] = so_iter
|
1067 |
+
if final is not None:
|
1068 |
+
iprint_l[4] = final
|
1069 |
+
if so_final is not None:
|
1070 |
+
iprint_l[5] = so_final
|
1071 |
+
|
1072 |
+
if iter_step in range(10):
|
1073 |
+
# 0..9
|
1074 |
+
ip[2] = iter_step
|
1075 |
+
|
1076 |
+
ip[0] = ip2arg.index(iprint_l[0:2])
|
1077 |
+
ip[1] = ip2arg.index(iprint_l[2:4])
|
1078 |
+
ip[3] = ip2arg.index(iprint_l[4:6])
|
1079 |
+
|
1080 |
+
self.iprint = ip[0]*1000 + ip[1]*100 + ip[2]*10 + ip[3]
|
1081 |
+
|
1082 |
+
def run(self):
|
1083 |
+
""" Run the fitting routine with all of the information given and with ``full_output=1``.
|
1084 |
+
|
1085 |
+
Returns
|
1086 |
+
-------
|
1087 |
+
output : Output instance
|
1088 |
+
This object is also assigned to the attribute .output .
|
1089 |
+
""" # noqa: E501
|
1090 |
+
|
1091 |
+
args = (self.model.fcn, self.beta0, self.data.y, self.data.x)
|
1092 |
+
kwds = {'full_output': 1}
|
1093 |
+
kwd_l = ['ifixx', 'ifixb', 'job', 'iprint', 'errfile', 'rptfile',
|
1094 |
+
'ndigit', 'taufac', 'sstol', 'partol', 'maxit', 'stpb',
|
1095 |
+
'stpd', 'sclb', 'scld', 'work', 'iwork']
|
1096 |
+
|
1097 |
+
if self.delta0 is not None and (self.job // 10000) % 10 == 0:
|
1098 |
+
# delta0 provided and fit is not a restart
|
1099 |
+
self._gen_work()
|
1100 |
+
|
1101 |
+
d0 = numpy.ravel(self.delta0)
|
1102 |
+
|
1103 |
+
self.work[:len(d0)] = d0
|
1104 |
+
|
1105 |
+
# set the kwds from other objects explicitly
|
1106 |
+
if self.model.fjacb is not None:
|
1107 |
+
kwds['fjacb'] = self.model.fjacb
|
1108 |
+
if self.model.fjacd is not None:
|
1109 |
+
kwds['fjacd'] = self.model.fjacd
|
1110 |
+
if self.data.we is not None:
|
1111 |
+
kwds['we'] = self.data.we
|
1112 |
+
if self.data.wd is not None:
|
1113 |
+
kwds['wd'] = self.data.wd
|
1114 |
+
if self.model.extra_args is not None:
|
1115 |
+
kwds['extra_args'] = self.model.extra_args
|
1116 |
+
|
1117 |
+
# implicitly set kwds from self's members
|
1118 |
+
for attr in kwd_l:
|
1119 |
+
obj = getattr(self, attr)
|
1120 |
+
if obj is not None:
|
1121 |
+
kwds[attr] = obj
|
1122 |
+
|
1123 |
+
self.output = Output(odr(*args, **kwds))
|
1124 |
+
|
1125 |
+
return self.output
|
1126 |
+
|
1127 |
+
def restart(self, iter=None):
|
1128 |
+
""" Restarts the run with iter more iterations.
|
1129 |
+
|
1130 |
+
Parameters
|
1131 |
+
----------
|
1132 |
+
iter : int, optional
|
1133 |
+
ODRPACK's default for the number of new iterations is 10.
|
1134 |
+
|
1135 |
+
Returns
|
1136 |
+
-------
|
1137 |
+
output : Output instance
|
1138 |
+
This object is also assigned to the attribute .output .
|
1139 |
+
"""
|
1140 |
+
|
1141 |
+
if self.output is None:
|
1142 |
+
raise OdrError("cannot restart: run() has not been called before")
|
1143 |
+
|
1144 |
+
self.set_job(restart=1)
|
1145 |
+
self.work = self.output.work
|
1146 |
+
self.iwork = self.output.iwork
|
1147 |
+
|
1148 |
+
self.maxit = iter
|
1149 |
+
|
1150 |
+
return self.run()
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/models.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
2 |
+
# Use the `scipy.odr` namespace for importing the functions
|
3 |
+
# included below.
|
4 |
+
|
5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
6 |
+
|
7 |
+
__all__ = [ # noqa: F822
|
8 |
+
'Model', 'exponential', 'multilinear', 'unilinear',
|
9 |
+
'quadratic', 'polynomial'
|
10 |
+
]
|
11 |
+
|
12 |
+
|
13 |
+
def __dir__():
|
14 |
+
return __all__
|
15 |
+
|
16 |
+
|
17 |
+
def __getattr__(name):
|
18 |
+
return _sub_module_deprecation(sub_package="odr", module="models",
|
19 |
+
private_modules=["_models"], all=__all__,
|
20 |
+
attribute=name)
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/odrpack.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This file is not meant for public use and will be removed in SciPy v2.0.0.
|
2 |
+
# Use the `scipy.odr` namespace for importing the functions
|
3 |
+
# included below.
|
4 |
+
|
5 |
+
from scipy._lib.deprecation import _sub_module_deprecation
|
6 |
+
|
7 |
+
__all__ = [ # noqa: F822
|
8 |
+
'odr', 'OdrWarning', 'OdrError', 'OdrStop',
|
9 |
+
'Data', 'RealData', 'Model', 'Output', 'ODR',
|
10 |
+
'odr_error', 'odr_stop'
|
11 |
+
]
|
12 |
+
|
13 |
+
|
14 |
+
def __dir__():
|
15 |
+
return __all__
|
16 |
+
|
17 |
+
|
18 |
+
def __getattr__(name):
|
19 |
+
return _sub_module_deprecation(sub_package="odr", module="odrpack",
|
20 |
+
private_modules=["_odrpack"], all=__all__,
|
21 |
+
attribute=name)
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (180 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/__pycache__/test_odr.cpython-310.pyc
ADDED
Binary file (18.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/odr/tests/test_odr.py
ADDED
@@ -0,0 +1,565 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import shutil
|
3 |
+
import os
|
4 |
+
|
5 |
+
import numpy as np
|
6 |
+
from numpy import pi
|
7 |
+
from numpy.testing import (assert_array_almost_equal,
|
8 |
+
assert_equal, assert_warns,
|
9 |
+
assert_allclose)
|
10 |
+
import pytest
|
11 |
+
from pytest import raises as assert_raises
|
12 |
+
|
13 |
+
from scipy.odr import (Data, Model, ODR, RealData, OdrStop, OdrWarning,
|
14 |
+
multilinear, exponential, unilinear, quadratic,
|
15 |
+
polynomial)
|
16 |
+
|
17 |
+
|
18 |
+
class TestODR:
|
19 |
+
|
20 |
+
# Bad Data for 'x'
|
21 |
+
|
22 |
+
def test_bad_data(self):
|
23 |
+
assert_raises(ValueError, Data, 2, 1)
|
24 |
+
assert_raises(ValueError, RealData, 2, 1)
|
25 |
+
|
26 |
+
# Empty Data for 'x'
|
27 |
+
def empty_data_func(self, B, x):
|
28 |
+
return B[0]*x + B[1]
|
29 |
+
|
30 |
+
def test_empty_data(self):
|
31 |
+
beta0 = [0.02, 0.0]
|
32 |
+
linear = Model(self.empty_data_func)
|
33 |
+
|
34 |
+
empty_dat = Data([], [])
|
35 |
+
assert_warns(OdrWarning, ODR,
|
36 |
+
empty_dat, linear, beta0=beta0)
|
37 |
+
|
38 |
+
empty_dat = RealData([], [])
|
39 |
+
assert_warns(OdrWarning, ODR,
|
40 |
+
empty_dat, linear, beta0=beta0)
|
41 |
+
|
42 |
+
# Explicit Example
|
43 |
+
|
44 |
+
def explicit_fcn(self, B, x):
|
45 |
+
ret = B[0] + B[1] * np.power(np.exp(B[2]*x) - 1.0, 2)
|
46 |
+
return ret
|
47 |
+
|
48 |
+
def explicit_fjd(self, B, x):
|
49 |
+
eBx = np.exp(B[2]*x)
|
50 |
+
ret = B[1] * 2.0 * (eBx-1.0) * B[2] * eBx
|
51 |
+
return ret
|
52 |
+
|
53 |
+
def explicit_fjb(self, B, x):
|
54 |
+
eBx = np.exp(B[2]*x)
|
55 |
+
res = np.vstack([np.ones(x.shape[-1]),
|
56 |
+
np.power(eBx-1.0, 2),
|
57 |
+
B[1]*2.0*(eBx-1.0)*eBx*x])
|
58 |
+
return res
|
59 |
+
|
60 |
+
def test_explicit(self):
|
61 |
+
explicit_mod = Model(
|
62 |
+
self.explicit_fcn,
|
63 |
+
fjacb=self.explicit_fjb,
|
64 |
+
fjacd=self.explicit_fjd,
|
65 |
+
meta=dict(name='Sample Explicit Model',
|
66 |
+
ref='ODRPACK UG, pg. 39'),
|
67 |
+
)
|
68 |
+
explicit_dat = Data([0.,0.,5.,7.,7.5,10.,16.,26.,30.,34.,34.5,100.],
|
69 |
+
[1265.,1263.6,1258.,1254.,1253.,1249.8,1237.,1218.,1220.6,
|
70 |
+
1213.8,1215.5,1212.])
|
71 |
+
explicit_odr = ODR(explicit_dat, explicit_mod, beta0=[1500.0, -50.0, -0.1],
|
72 |
+
ifixx=[0,0,1,1,1,1,1,1,1,1,1,0])
|
73 |
+
explicit_odr.set_job(deriv=2)
|
74 |
+
explicit_odr.set_iprint(init=0, iter=0, final=0)
|
75 |
+
|
76 |
+
out = explicit_odr.run()
|
77 |
+
assert_array_almost_equal(
|
78 |
+
out.beta,
|
79 |
+
np.array([1.2646548050648876e+03, -5.4018409956678255e+01,
|
80 |
+
-8.7849712165253724e-02]),
|
81 |
+
)
|
82 |
+
assert_array_almost_equal(
|
83 |
+
out.sd_beta,
|
84 |
+
np.array([1.0349270280543437, 1.583997785262061, 0.0063321988657267]),
|
85 |
+
)
|
86 |
+
assert_array_almost_equal(
|
87 |
+
out.cov_beta,
|
88 |
+
np.array([[4.4949592379003039e-01, -3.7421976890364739e-01,
|
89 |
+
-8.0978217468468912e-04],
|
90 |
+
[-3.7421976890364739e-01, 1.0529686462751804e+00,
|
91 |
+
-1.9453521827942002e-03],
|
92 |
+
[-8.0978217468468912e-04, -1.9453521827942002e-03,
|
93 |
+
1.6827336938454476e-05]]),
|
94 |
+
)
|
95 |
+
|
96 |
+
# Implicit Example
|
97 |
+
|
98 |
+
def implicit_fcn(self, B, x):
|
99 |
+
return (B[2]*np.power(x[0]-B[0], 2) +
|
100 |
+
2.0*B[3]*(x[0]-B[0])*(x[1]-B[1]) +
|
101 |
+
B[4]*np.power(x[1]-B[1], 2) - 1.0)
|
102 |
+
|
103 |
+
def test_implicit(self):
|
104 |
+
implicit_mod = Model(
|
105 |
+
self.implicit_fcn,
|
106 |
+
implicit=1,
|
107 |
+
meta=dict(name='Sample Implicit Model',
|
108 |
+
ref='ODRPACK UG, pg. 49'),
|
109 |
+
)
|
110 |
+
implicit_dat = Data([
|
111 |
+
[0.5,1.2,1.6,1.86,2.12,2.36,2.44,2.36,2.06,1.74,1.34,0.9,-0.28,
|
112 |
+
-0.78,-1.36,-1.9,-2.5,-2.88,-3.18,-3.44],
|
113 |
+
[-0.12,-0.6,-1.,-1.4,-2.54,-3.36,-4.,-4.75,-5.25,-5.64,-5.97,-6.32,
|
114 |
+
-6.44,-6.44,-6.41,-6.25,-5.88,-5.5,-5.24,-4.86]],
|
115 |
+
1,
|
116 |
+
)
|
117 |
+
implicit_odr = ODR(implicit_dat, implicit_mod,
|
118 |
+
beta0=[-1.0, -3.0, 0.09, 0.02, 0.08])
|
119 |
+
|
120 |
+
out = implicit_odr.run()
|
121 |
+
assert_array_almost_equal(
|
122 |
+
out.beta,
|
123 |
+
np.array([-0.9993809167281279, -2.9310484652026476, 0.0875730502693354,
|
124 |
+
0.0162299708984738, 0.0797537982976416]),
|
125 |
+
)
|
126 |
+
assert_array_almost_equal(
|
127 |
+
out.sd_beta,
|
128 |
+
np.array([0.1113840353364371, 0.1097673310686467, 0.0041060738314314,
|
129 |
+
0.0027500347539902, 0.0034962501532468]),
|
130 |
+
)
|
131 |
+
assert_allclose(
|
132 |
+
out.cov_beta,
|
133 |
+
np.array([[2.1089274602333052e+00, -1.9437686411979040e+00,
|
134 |
+
7.0263550868344446e-02, -4.7175267373474862e-02,
|
135 |
+
5.2515575927380355e-02],
|
136 |
+
[-1.9437686411979040e+00, 2.0481509222414456e+00,
|
137 |
+
-6.1600515853057307e-02, 4.6268827806232933e-02,
|
138 |
+
-5.8822307501391467e-02],
|
139 |
+
[7.0263550868344446e-02, -6.1600515853057307e-02,
|
140 |
+
2.8659542561579308e-03, -1.4628662260014491e-03,
|
141 |
+
1.4528860663055824e-03],
|
142 |
+
[-4.7175267373474862e-02, 4.6268827806232933e-02,
|
143 |
+
-1.4628662260014491e-03, 1.2855592885514335e-03,
|
144 |
+
-1.2692942951415293e-03],
|
145 |
+
[5.2515575927380355e-02, -5.8822307501391467e-02,
|
146 |
+
1.4528860663055824e-03, -1.2692942951415293e-03,
|
147 |
+
2.0778813389755596e-03]]),
|
148 |
+
rtol=1e-6, atol=2e-6,
|
149 |
+
)
|
150 |
+
|
151 |
+
# Multi-variable Example
|
152 |
+
|
153 |
+
def multi_fcn(self, B, x):
|
154 |
+
if (x < 0.0).any():
|
155 |
+
raise OdrStop
|
156 |
+
theta = pi*B[3]/2.
|
157 |
+
ctheta = np.cos(theta)
|
158 |
+
stheta = np.sin(theta)
|
159 |
+
omega = np.power(2.*pi*x*np.exp(-B[2]), B[3])
|
160 |
+
phi = np.arctan2((omega*stheta), (1.0 + omega*ctheta))
|
161 |
+
r = (B[0] - B[1]) * np.power(np.sqrt(np.power(1.0 + omega*ctheta, 2) +
|
162 |
+
np.power(omega*stheta, 2)), -B[4])
|
163 |
+
ret = np.vstack([B[1] + r*np.cos(B[4]*phi),
|
164 |
+
r*np.sin(B[4]*phi)])
|
165 |
+
return ret
|
166 |
+
|
167 |
+
def test_multi(self):
|
168 |
+
multi_mod = Model(
|
169 |
+
self.multi_fcn,
|
170 |
+
meta=dict(name='Sample Multi-Response Model',
|
171 |
+
ref='ODRPACK UG, pg. 56'),
|
172 |
+
)
|
173 |
+
|
174 |
+
multi_x = np.array([30.0, 50.0, 70.0, 100.0, 150.0, 200.0, 300.0, 500.0,
|
175 |
+
700.0, 1000.0, 1500.0, 2000.0, 3000.0, 5000.0, 7000.0, 10000.0,
|
176 |
+
15000.0, 20000.0, 30000.0, 50000.0, 70000.0, 100000.0, 150000.0])
|
177 |
+
multi_y = np.array([
|
178 |
+
[4.22, 4.167, 4.132, 4.038, 4.019, 3.956, 3.884, 3.784, 3.713,
|
179 |
+
3.633, 3.54, 3.433, 3.358, 3.258, 3.193, 3.128, 3.059, 2.984,
|
180 |
+
2.934, 2.876, 2.838, 2.798, 2.759],
|
181 |
+
[0.136, 0.167, 0.188, 0.212, 0.236, 0.257, 0.276, 0.297, 0.309,
|
182 |
+
0.311, 0.314, 0.311, 0.305, 0.289, 0.277, 0.255, 0.24, 0.218,
|
183 |
+
0.202, 0.182, 0.168, 0.153, 0.139],
|
184 |
+
])
|
185 |
+
n = len(multi_x)
|
186 |
+
multi_we = np.zeros((2, 2, n), dtype=float)
|
187 |
+
multi_ifixx = np.ones(n, dtype=int)
|
188 |
+
multi_delta = np.zeros(n, dtype=float)
|
189 |
+
|
190 |
+
multi_we[0,0,:] = 559.6
|
191 |
+
multi_we[1,0,:] = multi_we[0,1,:] = -1634.0
|
192 |
+
multi_we[1,1,:] = 8397.0
|
193 |
+
|
194 |
+
for i in range(n):
|
195 |
+
if multi_x[i] < 100.0:
|
196 |
+
multi_ifixx[i] = 0
|
197 |
+
elif multi_x[i] <= 150.0:
|
198 |
+
pass # defaults are fine
|
199 |
+
elif multi_x[i] <= 1000.0:
|
200 |
+
multi_delta[i] = 25.0
|
201 |
+
elif multi_x[i] <= 10000.0:
|
202 |
+
multi_delta[i] = 560.0
|
203 |
+
elif multi_x[i] <= 100000.0:
|
204 |
+
multi_delta[i] = 9500.0
|
205 |
+
else:
|
206 |
+
multi_delta[i] = 144000.0
|
207 |
+
if multi_x[i] == 100.0 or multi_x[i] == 150.0:
|
208 |
+
multi_we[:,:,i] = 0.0
|
209 |
+
|
210 |
+
multi_dat = Data(multi_x, multi_y, wd=1e-4/np.power(multi_x, 2),
|
211 |
+
we=multi_we)
|
212 |
+
multi_odr = ODR(multi_dat, multi_mod, beta0=[4.,2.,7.,.4,.5],
|
213 |
+
delta0=multi_delta, ifixx=multi_ifixx)
|
214 |
+
multi_odr.set_job(deriv=1, del_init=1)
|
215 |
+
|
216 |
+
out = multi_odr.run()
|
217 |
+
assert_array_almost_equal(
|
218 |
+
out.beta,
|
219 |
+
np.array([4.3799880305938963, 2.4333057577497703, 8.0028845899503978,
|
220 |
+
0.5101147161764654, 0.5173902330489161]),
|
221 |
+
)
|
222 |
+
assert_array_almost_equal(
|
223 |
+
out.sd_beta,
|
224 |
+
np.array([0.0130625231081944, 0.0130499785273277, 0.1167085962217757,
|
225 |
+
0.0132642749596149, 0.0288529201353984]),
|
226 |
+
)
|
227 |
+
assert_array_almost_equal(
|
228 |
+
out.cov_beta,
|
229 |
+
np.array([[0.0064918418231375, 0.0036159705923791, 0.0438637051470406,
|
230 |
+
-0.0058700836512467, 0.011281212888768],
|
231 |
+
[0.0036159705923791, 0.0064793789429006, 0.0517610978353126,
|
232 |
+
-0.0051181304940204, 0.0130726943624117],
|
233 |
+
[0.0438637051470406, 0.0517610978353126, 0.5182263323095322,
|
234 |
+
-0.0563083340093696, 0.1269490939468611],
|
235 |
+
[-0.0058700836512467, -0.0051181304940204, -0.0563083340093696,
|
236 |
+
0.0066939246261263, -0.0140184391377962],
|
237 |
+
[0.011281212888768, 0.0130726943624117, 0.1269490939468611,
|
238 |
+
-0.0140184391377962, 0.0316733013820852]]),
|
239 |
+
)
|
240 |
+
|
241 |
+
# Pearson's Data
|
242 |
+
# K. Pearson, Philosophical Magazine, 2, 559 (1901)
|
243 |
+
|
244 |
+
def pearson_fcn(self, B, x):
|
245 |
+
return B[0] + B[1]*x
|
246 |
+
|
247 |
+
def test_pearson(self):
|
248 |
+
p_x = np.array([0.,.9,1.8,2.6,3.3,4.4,5.2,6.1,6.5,7.4])
|
249 |
+
p_y = np.array([5.9,5.4,4.4,4.6,3.5,3.7,2.8,2.8,2.4,1.5])
|
250 |
+
p_sx = np.array([.03,.03,.04,.035,.07,.11,.13,.22,.74,1.])
|
251 |
+
p_sy = np.array([1.,.74,.5,.35,.22,.22,.12,.12,.1,.04])
|
252 |
+
|
253 |
+
p_dat = RealData(p_x, p_y, sx=p_sx, sy=p_sy)
|
254 |
+
|
255 |
+
# Reverse the data to test invariance of results
|
256 |
+
pr_dat = RealData(p_y, p_x, sx=p_sy, sy=p_sx)
|
257 |
+
|
258 |
+
p_mod = Model(self.pearson_fcn, meta=dict(name='Uni-linear Fit'))
|
259 |
+
|
260 |
+
p_odr = ODR(p_dat, p_mod, beta0=[1.,1.])
|
261 |
+
pr_odr = ODR(pr_dat, p_mod, beta0=[1.,1.])
|
262 |
+
|
263 |
+
out = p_odr.run()
|
264 |
+
assert_array_almost_equal(
|
265 |
+
out.beta,
|
266 |
+
np.array([5.4767400299231674, -0.4796082367610305]),
|
267 |
+
)
|
268 |
+
assert_array_almost_equal(
|
269 |
+
out.sd_beta,
|
270 |
+
np.array([0.3590121690702467, 0.0706291186037444]),
|
271 |
+
)
|
272 |
+
assert_array_almost_equal(
|
273 |
+
out.cov_beta,
|
274 |
+
np.array([[0.0854275622946333, -0.0161807025443155],
|
275 |
+
[-0.0161807025443155, 0.003306337993922]]),
|
276 |
+
)
|
277 |
+
|
278 |
+
rout = pr_odr.run()
|
279 |
+
assert_array_almost_equal(
|
280 |
+
rout.beta,
|
281 |
+
np.array([11.4192022410781231, -2.0850374506165474]),
|
282 |
+
)
|
283 |
+
assert_array_almost_equal(
|
284 |
+
rout.sd_beta,
|
285 |
+
np.array([0.9820231665657161, 0.3070515616198911]),
|
286 |
+
)
|
287 |
+
assert_array_almost_equal(
|
288 |
+
rout.cov_beta,
|
289 |
+
np.array([[0.6391799462548782, -0.1955657291119177],
|
290 |
+
[-0.1955657291119177, 0.0624888159223392]]),
|
291 |
+
)
|
292 |
+
|
293 |
+
# Lorentz Peak
|
294 |
+
# The data is taken from one of the undergraduate physics labs I performed.
|
295 |
+
|
296 |
+
def lorentz(self, beta, x):
|
297 |
+
return (beta[0]*beta[1]*beta[2] / np.sqrt(np.power(x*x -
|
298 |
+
beta[2]*beta[2], 2.0) + np.power(beta[1]*x, 2.0)))
|
299 |
+
|
300 |
+
def test_lorentz(self):
|
301 |
+
l_sy = np.array([.29]*18)
|
302 |
+
l_sx = np.array([.000972971,.000948268,.000707632,.000706679,
|
303 |
+
.000706074, .000703918,.000698955,.000456856,
|
304 |
+
.000455207,.000662717,.000654619,.000652694,
|
305 |
+
.000000859202,.00106589,.00106378,.00125483, .00140818,.00241839])
|
306 |
+
|
307 |
+
l_dat = RealData(
|
308 |
+
[3.9094, 3.85945, 3.84976, 3.84716, 3.84551, 3.83964, 3.82608,
|
309 |
+
3.78847, 3.78163, 3.72558, 3.70274, 3.6973, 3.67373, 3.65982,
|
310 |
+
3.6562, 3.62498, 3.55525, 3.41886],
|
311 |
+
[652, 910.5, 984, 1000, 1007.5, 1053, 1160.5, 1409.5, 1430, 1122,
|
312 |
+
957.5, 920, 777.5, 709.5, 698, 578.5, 418.5, 275.5],
|
313 |
+
sx=l_sx,
|
314 |
+
sy=l_sy,
|
315 |
+
)
|
316 |
+
l_mod = Model(self.lorentz, meta=dict(name='Lorentz Peak'))
|
317 |
+
l_odr = ODR(l_dat, l_mod, beta0=(1000., .1, 3.8))
|
318 |
+
|
319 |
+
out = l_odr.run()
|
320 |
+
assert_array_almost_equal(
|
321 |
+
out.beta,
|
322 |
+
np.array([1.4306780846149925e+03, 1.3390509034538309e-01,
|
323 |
+
3.7798193600109009e+00]),
|
324 |
+
)
|
325 |
+
assert_array_almost_equal(
|
326 |
+
out.sd_beta,
|
327 |
+
np.array([7.3621186811330963e-01, 3.5068899941471650e-04,
|
328 |
+
2.4451209281408992e-04]),
|
329 |
+
)
|
330 |
+
assert_array_almost_equal(
|
331 |
+
out.cov_beta,
|
332 |
+
np.array([[2.4714409064597873e-01, -6.9067261911110836e-05,
|
333 |
+
-3.1236953270424990e-05],
|
334 |
+
[-6.9067261911110836e-05, 5.6077531517333009e-08,
|
335 |
+
3.6133261832722601e-08],
|
336 |
+
[-3.1236953270424990e-05, 3.6133261832722601e-08,
|
337 |
+
2.7261220025171730e-08]]),
|
338 |
+
)
|
339 |
+
|
340 |
+
def test_ticket_1253(self):
|
341 |
+
def linear(c, x):
|
342 |
+
return c[0]*x+c[1]
|
343 |
+
|
344 |
+
c = [2.0, 3.0]
|
345 |
+
x = np.linspace(0, 10)
|
346 |
+
y = linear(c, x)
|
347 |
+
|
348 |
+
model = Model(linear)
|
349 |
+
data = Data(x, y, wd=1.0, we=1.0)
|
350 |
+
job = ODR(data, model, beta0=[1.0, 1.0])
|
351 |
+
result = job.run()
|
352 |
+
assert_equal(result.info, 2)
|
353 |
+
|
354 |
+
# Verify fix for gh-9140
|
355 |
+
|
356 |
+
def test_ifixx(self):
|
357 |
+
x1 = [-2.01, -0.99, -0.001, 1.02, 1.98]
|
358 |
+
x2 = [3.98, 1.01, 0.001, 0.998, 4.01]
|
359 |
+
fix = np.vstack((np.zeros_like(x1, dtype=int), np.ones_like(x2, dtype=int)))
|
360 |
+
data = Data(np.vstack((x1, x2)), y=1, fix=fix)
|
361 |
+
model = Model(lambda beta, x: x[1, :] - beta[0] * x[0, :]**2., implicit=True)
|
362 |
+
|
363 |
+
odr1 = ODR(data, model, beta0=np.array([1.]))
|
364 |
+
sol1 = odr1.run()
|
365 |
+
odr2 = ODR(data, model, beta0=np.array([1.]), ifixx=fix)
|
366 |
+
sol2 = odr2.run()
|
367 |
+
assert_equal(sol1.beta, sol2.beta)
|
368 |
+
|
369 |
+
# verify bugfix for #11800 in #11802
|
370 |
+
def test_ticket_11800(self):
|
371 |
+
# parameters
|
372 |
+
beta_true = np.array([1.0, 2.3, 1.1, -1.0, 1.3, 0.5])
|
373 |
+
nr_measurements = 10
|
374 |
+
|
375 |
+
std_dev_x = 0.01
|
376 |
+
x_error = np.array([[0.00063445, 0.00515731, 0.00162719, 0.01022866,
|
377 |
+
-0.01624845, 0.00482652, 0.00275988, -0.00714734, -0.00929201, -0.00687301],
|
378 |
+
[-0.00831623, -0.00821211, -0.00203459, 0.00938266, -0.00701829,
|
379 |
+
0.0032169, 0.00259194, -0.00581017, -0.0030283, 0.01014164]])
|
380 |
+
|
381 |
+
std_dev_y = 0.05
|
382 |
+
y_error = np.array([[0.05275304, 0.04519563, -0.07524086, 0.03575642,
|
383 |
+
0.04745194, 0.03806645, 0.07061601, -0.00753604, -0.02592543, -0.02394929],
|
384 |
+
[0.03632366, 0.06642266, 0.08373122, 0.03988822, -0.0092536,
|
385 |
+
-0.03750469, -0.03198903, 0.01642066, 0.01293648, -0.05627085]])
|
386 |
+
|
387 |
+
beta_solution = np.array([
|
388 |
+
2.62920235756665876536e+00, -1.26608484996299608838e+02,
|
389 |
+
1.29703572775403074502e+02, -1.88560985401185465804e+00,
|
390 |
+
7.83834160771274923718e+01, -7.64124076838087091801e+01])
|
391 |
+
|
392 |
+
# model's function and Jacobians
|
393 |
+
def func(beta, x):
|
394 |
+
y0 = beta[0] + beta[1] * x[0, :] + beta[2] * x[1, :]
|
395 |
+
y1 = beta[3] + beta[4] * x[0, :] + beta[5] * x[1, :]
|
396 |
+
|
397 |
+
return np.vstack((y0, y1))
|
398 |
+
|
399 |
+
def df_dbeta_odr(beta, x):
|
400 |
+
nr_meas = np.shape(x)[1]
|
401 |
+
zeros = np.zeros(nr_meas)
|
402 |
+
ones = np.ones(nr_meas)
|
403 |
+
|
404 |
+
dy0 = np.array([ones, x[0, :], x[1, :], zeros, zeros, zeros])
|
405 |
+
dy1 = np.array([zeros, zeros, zeros, ones, x[0, :], x[1, :]])
|
406 |
+
|
407 |
+
return np.stack((dy0, dy1))
|
408 |
+
|
409 |
+
def df_dx_odr(beta, x):
|
410 |
+
nr_meas = np.shape(x)[1]
|
411 |
+
ones = np.ones(nr_meas)
|
412 |
+
|
413 |
+
dy0 = np.array([beta[1] * ones, beta[2] * ones])
|
414 |
+
dy1 = np.array([beta[4] * ones, beta[5] * ones])
|
415 |
+
return np.stack((dy0, dy1))
|
416 |
+
|
417 |
+
# do measurements with errors in independent and dependent variables
|
418 |
+
x0_true = np.linspace(1, 10, nr_measurements)
|
419 |
+
x1_true = np.linspace(1, 10, nr_measurements)
|
420 |
+
x_true = np.array([x0_true, x1_true])
|
421 |
+
|
422 |
+
y_true = func(beta_true, x_true)
|
423 |
+
|
424 |
+
x_meas = x_true + x_error
|
425 |
+
y_meas = y_true + y_error
|
426 |
+
|
427 |
+
# estimate model's parameters
|
428 |
+
model_f = Model(func, fjacb=df_dbeta_odr, fjacd=df_dx_odr)
|
429 |
+
|
430 |
+
data = RealData(x_meas, y_meas, sx=std_dev_x, sy=std_dev_y)
|
431 |
+
|
432 |
+
odr_obj = ODR(data, model_f, beta0=0.9 * beta_true, maxit=100)
|
433 |
+
#odr_obj.set_iprint(init=2, iter=0, iter_step=1, final=1)
|
434 |
+
odr_obj.set_job(deriv=3)
|
435 |
+
|
436 |
+
odr_out = odr_obj.run()
|
437 |
+
|
438 |
+
# check results
|
439 |
+
assert_equal(odr_out.info, 1)
|
440 |
+
assert_array_almost_equal(odr_out.beta, beta_solution)
|
441 |
+
|
442 |
+
def test_multilinear_model(self):
|
443 |
+
x = np.linspace(0.0, 5.0)
|
444 |
+
y = 10.0 + 5.0 * x
|
445 |
+
data = Data(x, y)
|
446 |
+
odr_obj = ODR(data, multilinear)
|
447 |
+
output = odr_obj.run()
|
448 |
+
assert_array_almost_equal(output.beta, [10.0, 5.0])
|
449 |
+
|
450 |
+
def test_exponential_model(self):
|
451 |
+
x = np.linspace(0.0, 5.0)
|
452 |
+
y = -10.0 + np.exp(0.5*x)
|
453 |
+
data = Data(x, y)
|
454 |
+
odr_obj = ODR(data, exponential)
|
455 |
+
output = odr_obj.run()
|
456 |
+
assert_array_almost_equal(output.beta, [-10.0, 0.5])
|
457 |
+
|
458 |
+
def test_polynomial_model(self):
|
459 |
+
x = np.linspace(0.0, 5.0)
|
460 |
+
y = 1.0 + 2.0 * x + 3.0 * x ** 2 + 4.0 * x ** 3
|
461 |
+
poly_model = polynomial(3)
|
462 |
+
data = Data(x, y)
|
463 |
+
odr_obj = ODR(data, poly_model)
|
464 |
+
output = odr_obj.run()
|
465 |
+
assert_array_almost_equal(output.beta, [1.0, 2.0, 3.0, 4.0])
|
466 |
+
|
467 |
+
def test_unilinear_model(self):
|
468 |
+
x = np.linspace(0.0, 5.0)
|
469 |
+
y = 1.0 * x + 2.0
|
470 |
+
data = Data(x, y)
|
471 |
+
odr_obj = ODR(data, unilinear)
|
472 |
+
output = odr_obj.run()
|
473 |
+
assert_array_almost_equal(output.beta, [1.0, 2.0])
|
474 |
+
|
475 |
+
def test_quadratic_model(self):
|
476 |
+
x = np.linspace(0.0, 5.0)
|
477 |
+
y = 1.0 * x ** 2 + 2.0 * x + 3.0
|
478 |
+
data = Data(x, y)
|
479 |
+
odr_obj = ODR(data, quadratic)
|
480 |
+
output = odr_obj.run()
|
481 |
+
assert_array_almost_equal(output.beta, [1.0, 2.0, 3.0])
|
482 |
+
|
483 |
+
def test_work_ind(self):
|
484 |
+
|
485 |
+
def func(par, x):
|
486 |
+
b0, b1 = par
|
487 |
+
return b0 + b1 * x
|
488 |
+
|
489 |
+
# generate some data
|
490 |
+
n_data = 4
|
491 |
+
x = np.arange(n_data)
|
492 |
+
y = np.where(x % 2, x + 0.1, x - 0.1)
|
493 |
+
x_err = np.full(n_data, 0.1)
|
494 |
+
y_err = np.full(n_data, 0.1)
|
495 |
+
|
496 |
+
# do the fitting
|
497 |
+
linear_model = Model(func)
|
498 |
+
real_data = RealData(x, y, sx=x_err, sy=y_err)
|
499 |
+
odr_obj = ODR(real_data, linear_model, beta0=[0.4, 0.4])
|
500 |
+
odr_obj.set_job(fit_type=0)
|
501 |
+
out = odr_obj.run()
|
502 |
+
|
503 |
+
sd_ind = out.work_ind['sd']
|
504 |
+
assert_array_almost_equal(out.sd_beta,
|
505 |
+
out.work[sd_ind:sd_ind + len(out.sd_beta)])
|
506 |
+
|
507 |
+
@pytest.mark.skipif(True, reason="Fortran I/O prone to crashing so better "
|
508 |
+
"not to run this test, see gh-13127")
|
509 |
+
def test_output_file_overwrite(self):
|
510 |
+
"""
|
511 |
+
Verify fix for gh-1892
|
512 |
+
"""
|
513 |
+
def func(b, x):
|
514 |
+
return b[0] + b[1] * x
|
515 |
+
|
516 |
+
p = Model(func)
|
517 |
+
data = Data(np.arange(10), 12 * np.arange(10))
|
518 |
+
tmp_dir = tempfile.mkdtemp()
|
519 |
+
error_file_path = os.path.join(tmp_dir, "error.dat")
|
520 |
+
report_file_path = os.path.join(tmp_dir, "report.dat")
|
521 |
+
try:
|
522 |
+
ODR(data, p, beta0=[0.1, 13], errfile=error_file_path,
|
523 |
+
rptfile=report_file_path).run()
|
524 |
+
ODR(data, p, beta0=[0.1, 13], errfile=error_file_path,
|
525 |
+
rptfile=report_file_path, overwrite=True).run()
|
526 |
+
finally:
|
527 |
+
# remove output files for clean up
|
528 |
+
shutil.rmtree(tmp_dir)
|
529 |
+
|
530 |
+
def test_odr_model_default_meta(self):
|
531 |
+
def func(b, x):
|
532 |
+
return b[0] + b[1] * x
|
533 |
+
|
534 |
+
p = Model(func)
|
535 |
+
p.set_meta(name='Sample Model Meta', ref='ODRPACK')
|
536 |
+
assert_equal(p.meta, {'name': 'Sample Model Meta', 'ref': 'ODRPACK'})
|
537 |
+
|
538 |
+
def test_work_array_del_init(self):
|
539 |
+
"""
|
540 |
+
Verify fix for gh-18739 where del_init=1 fails.
|
541 |
+
"""
|
542 |
+
def func(b, x):
|
543 |
+
return b[0] + b[1] * x
|
544 |
+
|
545 |
+
# generate some data
|
546 |
+
n_data = 4
|
547 |
+
x = np.arange(n_data)
|
548 |
+
y = np.where(x % 2, x + 0.1, x - 0.1)
|
549 |
+
x_err = np.full(n_data, 0.1)
|
550 |
+
y_err = np.full(n_data, 0.1)
|
551 |
+
|
552 |
+
linear_model = Model(func)
|
553 |
+
# Try various shapes of the `we` array from various `sy` and `covy`
|
554 |
+
rd0 = RealData(x, y, sx=x_err, sy=y_err)
|
555 |
+
rd1 = RealData(x, y, sx=x_err, sy=0.1)
|
556 |
+
rd2 = RealData(x, y, sx=x_err, sy=[0.1])
|
557 |
+
rd3 = RealData(x, y, sx=x_err, sy=np.full((1, n_data), 0.1))
|
558 |
+
rd4 = RealData(x, y, sx=x_err, covy=[[0.01]])
|
559 |
+
rd5 = RealData(x, y, sx=x_err, covy=np.full((1, 1, n_data), 0.01))
|
560 |
+
for rd in [rd0, rd1, rd2, rd3, rd4, rd5]:
|
561 |
+
odr_obj = ODR(rd, linear_model, beta0=[0.4, 0.4],
|
562 |
+
delta0=np.full(n_data, -0.1))
|
563 |
+
odr_obj.set_job(fit_type=0, del_init=1)
|
564 |
+
# Just make sure that it runs without raising an exception.
|
565 |
+
odr_obj.run()
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (13.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_basinhopping.cpython-310.pyc
ADDED
Binary file (26.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_bracket.cpython-310.pyc
ADDED
Binary file (18.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_chandrupatla.cpython-310.pyc
ADDED
Binary file (17.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_constraints.cpython-310.pyc
ADDED
Binary file (21.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_dcsrch.cpython-310.pyc
ADDED
Binary file (16.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_differentiable_functions.cpython-310.pyc
ADDED
Binary file (19.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_differentialevolution.cpython-310.pyc
ADDED
Binary file (62.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_direct_py.cpython-310.pyc
ADDED
Binary file (10.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_dual_annealing.cpython-310.pyc
ADDED
Binary file (22.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_isotonic.cpython-310.pyc
ADDED
Binary file (5.77 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_lbfgsb_py.cpython-310.pyc
ADDED
Binary file (16.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linesearch.cpython-310.pyc
ADDED
Binary file (21 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog.cpython-310.pyc
ADDED
Binary file (27.8 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_doc.cpython-310.pyc
ADDED
Binary file (61.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_highs.cpython-310.pyc
ADDED
Binary file (14.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_ip.cpython-310.pyc
ADDED
Binary file (37.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_simplex.cpython-310.pyc
ADDED
Binary file (22 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_linprog_util.cpython-310.pyc
ADDED
Binary file (46.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_milp.cpython-310.pyc
ADDED
Binary file (12.9 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_minimize.cpython-310.pyc
ADDED
Binary file (40.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_minpack_py.cpython-310.pyc
ADDED
Binary file (38 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_nnls.cpython-310.pyc
ADDED
Binary file (4.64 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_nonlin.cpython-310.pyc
ADDED
Binary file (46.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_numdiff.cpython-310.pyc
ADDED
Binary file (23.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_optimize.cpython-310.pyc
ADDED
Binary file (114 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_qap.cpython-310.pyc
ADDED
Binary file (23.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_remove_redundancy.cpython-310.pyc
ADDED
Binary file (10.3 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/scipy/optimize/__pycache__/_shgo.cpython-310.pyc
ADDED
Binary file (44.4 kB). View file
|
|