Skip to content

Commit d06004e

Browse files
committed
add ArrayLike types for input args
1 parent 68b710b commit d06004e

File tree

2 files changed

+114
-112
lines changed

2 files changed

+114
-112
lines changed

src/odrpack/odr_scipy.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@
55
from odrpack.__odrpack import loc_iwork, loc_rwork
66
from odrpack.__odrpack import odr as _odr
77
from odrpack.__odrpack import workspace_dimensions, stop_message
8-
from odrpack.result import BoolArray, F64Array, OdrResult
8+
from odrpack.result import BoolArrayLike, F64Array, F64ArrayLike, OdrResult
99

1010
__all__ = ['odr_fit']
1111

1212

1313
def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
14-
xdata: F64Array,
15-
ydata: F64Array,
16-
beta0: F64Array,
14+
xdata: F64ArrayLike,
15+
ydata: F64ArrayLike,
16+
beta0: F64ArrayLike,
1717
*,
18-
weight_x: float | F64Array | None = None,
19-
weight_y: float | F64Array | None = None,
20-
bounds: tuple[F64Array | None, F64Array | None] | None = None,
18+
weight_x: float | F64ArrayLike | None = None,
19+
weight_y: float | F64ArrayLike | None = None,
20+
bounds: tuple[F64ArrayLike | None, F64ArrayLike | None] | None = None,
2121
task: Literal['explicit-ODR', 'implicit-ODR', 'OLS'] = 'explicit-ODR',
22-
fix_beta: BoolArray | None = None,
23-
fix_x: BoolArray | None = None,
22+
fix_beta: BoolArrayLike | None = None,
23+
fix_x: BoolArrayLike | None = None,
2424
jac_beta: Callable[[F64Array, F64Array], F64Array] | None = None,
2525
jac_x: Callable[[F64Array, F64Array], F64Array] | None = None,
26-
delta0: F64Array | None = None,
26+
delta0: F64ArrayLike | None = None,
2727
diff_scheme: Literal['forward', 'central'] = 'forward',
2828
report: Literal['none', 'short', 'long', 'iteration'] = 'none',
2929
maxit: int = 50,
3030
ndigit: int | None = None,
3131
taufac: float | None = None,
3232
sstol: float | None = None,
3333
partol: float | None = None,
34-
step_beta: F64Array | None = None,
35-
step_delta: F64Array | None = None,
36-
scale_beta: F64Array | None = None,
37-
scale_delta: F64Array | None = None,
34+
step_beta: F64ArrayLike | None = None,
35+
step_delta: F64ArrayLike | None = None,
36+
scale_beta: F64ArrayLike | None = None,
37+
scale_delta: F64ArrayLike | None = None,
3838
rptfile: str | None = None,
3939
errfile: str | None = None,
4040
) -> OdrResult:
@@ -46,19 +46,19 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
4646
f : Callable[[F64Array, F64Array], F64Array]
4747
Function to be fitted, with the signature `f(x, beta)`. It must return
4848
an array with the same shape as `y`.
49-
xdata : F64Array
49+
xdata : F64ArrayLike
5050
Array of shape `(n,)` or `(m, n)` containing the observed values of the
5151
explanatory variable(s).
52-
ydata : F64Array
52+
ydata : F64ArrayLike
5353
Array of shape `(n,)` or `(q, n)` containing the observed values of the
5454
response variable(s). When the model is explicit, `ydata` must contain
5555
a value for each observation. To ignore specific values (e.g., missing
5656
data), set the corresponding entry in `weight_y` to zero. When the model
5757
is implicit, `ydata` is not used (but must be defined).
58-
beta0 : F64Array
58+
beta0 : F64ArrayLike
5959
Array of shape `(npar,)` with the initial guesses of the model parameters,
6060
within the optional bounds specified by `bounds`.
61-
weight_x : float | F64Array | None
61+
weight_x : float | F64ArrayLike | None
6262
Scalar or array specifying how the errors on `xdata` are to be weighted.
6363
If `weight_x` is a scalar, then it is used for all data points. If
6464
`weight_x` is an array of shape `(n,)` and `m==1`, then `weight_x[i]`
@@ -73,7 +73,7 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
7373
comprehensive description of the options, refer to page 26 of the
7474
ODRPACK95 guide. By default, `weight_x` is set to one for all `xdata`
7575
points.
76-
weight_y : float | F64Array | None
76+
weight_y : float | F64ArrayLike | None
7777
Scalar or array specifying how the errors on `ydata` are to be weighted.
7878
If `weight_y` is a scalar, then it is used for all data points. If
7979
`weight_y` is an array of shape `(n,)` and `q==1`, then `weight_y[i]`
@@ -88,7 +88,7 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
8888
comprehensive description of the options, refer to page 25 of the
8989
ODRPACK95 guide. By default, `weight_y` is set to one for all `ydata`
9090
points.
91-
bounds : tuple[F64Array | None, F64Array | None] | None
91+
bounds : tuple[F64ArrayLike | None, F64ArrayLike | None] | None
9292
Tuple of arrays with the same shape as `beta0`, specifying the lower and
9393
upper bounds of the model parameters. The first array contains the lower
9494
bounds, and the second contains the upper bounds. By default, the bounds
@@ -99,11 +99,11 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
9999
an orthogonal distance regression problem with an explicit model.
100100
`'implicit-ODR'` handles models defined implicitly. `'OLS'` performs
101101
ordinary least squares fitting.
102-
fix_beta : BoolArray | None
102+
fix_beta : BoolArrayLike | None
103103
Array with the same shape as `beta0`, specifying which elements of `beta`
104104
are to be held fixed. `True` means the parameter is fixed; `False` means
105105
it is adjustable. By default, all elements of `beta` are set to `False`.
106-
fix_x : BoolArray | None
106+
fix_x : BoolArrayLike | None
107107
Array with the same shape as `xdata`, specifying which elements of `xdata`
108108
are to be held fixed. Alternatively, it can be a rank-1 array of shape
109109
`(m,)` or `(n,)`, in which case it will be broadcast along the other
@@ -122,7 +122,7 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
122122
signature `jac_x(x, beta)`. It must return an array with shape
123123
`(q, m, n)` or a compatible shape. By default, the Jacobian is approximated
124124
numerically using the finite difference scheme specified by `diff_scheme`.
125-
delta0 : F64Array | None
125+
delta0 : F64ArrayLike | None
126126
Array with the same shape as `xdata`, containing the initial guesses of
127127
the errors in the explanatory variable. By default, `delta0` is set to
128128
zero for all elements of `xdata`.
@@ -159,30 +159,30 @@ def odr_fit(f: Callable[[F64Array, F64Array], F64Array],
159159
explicit, the default value is `eps**(2/3)`, and when the model is
160160
implicit, the default value is `eps**(1/3)`, where `eps` is the machine
161161
precision in `float64`.
162-
step_beta : F64Array | None
162+
step_beta : F64ArrayLike | None
163163
Array with the same shape as `beta0` containing the _relative_ step
164164
sizes used to compute the finite difference derivatives with respect
165165
to the model parameters. By default, the step size is set internally
166166
based on the value of `ndigit` and the type of finite differences
167167
specified by `diff_scheme`. For additional details, refer to pages 31
168168
and 78 of the ODRPACK95 guide.
169-
step_delta : F64Array | None
169+
step_delta : F64ArrayLike | None
170170
Array with the same shape as `xdata`, containing the _relative_ step
171171
sizes used to compute the finite difference derivatives with respect to
172172
the errors in the explanatory variable. Alternatively, it can be a rank-1
173173
array of shape `(m,)` or `(n,)`, in which case it will be broadcast along
174174
the other axis. By default, step size is set internally based on the value
175175
of `ndigit` and the type of finite differences specified by `diff_scheme`.
176176
For additional details, refer to pages 31 and 78 of the ODRPACK95 guide.
177-
scale_beta : F64Array | None
177+
scale_beta : F64ArrayLike | None
178178
Array with the same shape as `beta0` containing the scale values of the
179179
model parameters. Scaling is used to improve the numerical stability
180180
of the regression, but does not affect the problem specification. Scaling
181181
should not be confused with the weighting matrices `weight_x` and
182182
`weight_y`. By default, the scale is set internally based on the relative
183183
magnitudes of `beta`. For further details, refer to pages 32 and 84 of
184184
the ODRPACK95 guide.
185-
scale_delta : F64Array | None
185+
scale_delta : F64ArrayLike | None
186186
Array with the same shape as `xdata`, containing the scale values of the
187187
errors in the explanatory variable. Alternatively, it can be a rank-1
188188
array of shape `(m,)` or `(n,)`, in which case it will be broadcast along

src/odrpack/result.py

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,87 @@
1-
from dataclasses import dataclass
2-
3-
import numpy as np
4-
from numpy.typing import NDArray
5-
6-
__all__ = ['OdrResult']
7-
8-
F64Array = NDArray[np.float64]
9-
I32Array = NDArray[np.int32]
10-
BoolArray = NDArray[np.bool_]
11-
12-
13-
@dataclass(frozen=True)
14-
class OdrResult():
15-
"""
16-
Results of an Orthogonal Distance Regression (ODR) computation.
17-
18-
Attributes
19-
----------
20-
beta : F64Array
21-
Estimated parameters of the model.
22-
delta : F64Array
23-
Differences between the observed and fitted `x` values.
24-
eps : F64Array
25-
Differences between the observed and fitted `y` values.
26-
xplusd : F64Array
27-
Adjusted `x` values after fitting, `x + delta`.
28-
yest : F64Array
29-
Estimated `y` values corresponding to the fitted model, `y + eps`.
30-
sd_beta : F64Array
31-
Standard deviations of the estimated parameters.
32-
cov_beta : F64Array
33-
Covariance matrix of the estimated parameters.
34-
res_var : float
35-
Residual variance, indicating the variance of the residuals.
36-
nfev : int
37-
Number of function evaluations during the fitting process.
38-
njev : int
39-
Number of Jacobian evaluations during the fitting process.
40-
niter : int
41-
Number of iterations performed in the optimization process.
42-
irank : int
43-
Rank of the Jacobian matrix at the solution.
44-
inv_condnum : float
45-
Inverse of the condition number of the Jacobian matrix.
46-
info : int
47-
Status code of the fitting process (e.g., success or failure).
48-
stopreason : str
49-
Message indicating the reason for stopping.
50-
success : bool
51-
Whether the fitting process was successful.
52-
sum_square : float
53-
Sum of squared residuals (including both `delta` and `eps`).
54-
sum_square_delta : float
55-
Sum of squared differences between observed and fitted `x` values.
56-
sum_square_eps : float
57-
Sum of squared differences between observed and fitted `y` values.
58-
iwork : I32Array
59-
Integer workspace array used internally by `odrpack`. Typically for
60-
advanced debugging.
61-
rwork : F64Array
62-
Floating-point workspace array used internally by `odrpack`. Typically
63-
for advanced debugging.
64-
"""
65-
beta: F64Array
66-
delta: F64Array
67-
eps: F64Array
68-
xplusd: F64Array
69-
yest: F64Array
70-
sd_beta: F64Array
71-
cov_beta: F64Array
72-
res_var: float
73-
nfev: int
74-
njev: int
75-
niter: int
76-
irank: int
77-
inv_condnum: float
78-
info: int
79-
stopreason: str
80-
success: bool
81-
sum_square: float
82-
sum_square_delta: float
83-
sum_square_eps: float
84-
iwork: I32Array
85-
rwork: F64Array
1+
from dataclasses import dataclass
2+
3+
import numpy as np
4+
from numpy.typing import NDArray, ArrayLike
5+
6+
__all__ = ['OdrResult']
7+
8+
F64Array = NDArray[np.float64]
9+
F64ArrayLike = ArrayLike
10+
I32Array = NDArray[np.int32]
11+
BoolArray = NDArray[np.bool_]
12+
BoolArrayLike = ArrayLike
13+
14+
15+
@dataclass(frozen=True, slots=False)
16+
class OdrResult():
17+
"""
18+
Results of an Orthogonal Distance Regression (ODR) computation.
19+
20+
Attributes
21+
----------
22+
beta : F64Array
23+
Estimated parameters of the model.
24+
delta : F64Array
25+
Differences between the observed and fitted `x` values.
26+
eps : F64Array
27+
Differences between the observed and fitted `y` values.
28+
xplusd : F64Array
29+
Adjusted `x` values after fitting, `x + delta`.
30+
yest : F64Array
31+
Estimated `y` values corresponding to the fitted model, `y + eps`.
32+
sd_beta : F64Array
33+
Standard deviations of the estimated parameters.
34+
cov_beta : F64Array
35+
Covariance matrix of the estimated parameters.
36+
res_var : float
37+
Residual variance, indicating the variance of the residuals.
38+
nfev : int
39+
Number of function evaluations during the fitting process.
40+
njev : int
41+
Number of Jacobian evaluations during the fitting process.
42+
niter : int
43+
Number of iterations performed in the optimization process.
44+
irank : int
45+
Rank of the Jacobian matrix at the solution.
46+
inv_condnum : float
47+
Inverse of the condition number of the Jacobian matrix.
48+
info : int
49+
Status code of the fitting process (e.g., success or failure).
50+
stopreason : str
51+
Message indicating the reason for stopping.
52+
success : bool
53+
Whether the fitting process was successful.
54+
sum_square : float
55+
Sum of squared residuals (including both `delta` and `eps`).
56+
sum_square_delta : float
57+
Sum of squared differences between observed and fitted `x` values.
58+
sum_square_eps : float
59+
Sum of squared differences between observed and fitted `y` values.
60+
iwork : I32Array
61+
Integer workspace array used internally by `odrpack`. Typically for
62+
advanced debugging.
63+
rwork : F64Array
64+
Floating-point workspace array used internally by `odrpack`. Typically
65+
for advanced debugging.
66+
"""
67+
beta: F64Array
68+
delta: F64Array
69+
eps: F64Array
70+
xplusd: F64Array
71+
yest: F64Array
72+
sd_beta: F64Array
73+
cov_beta: F64Array
74+
res_var: float
75+
nfev: int
76+
njev: int
77+
niter: int
78+
irank: int
79+
inv_condnum: float
80+
info: int
81+
stopreason: str
82+
success: bool
83+
sum_square: float
84+
sum_square_delta: float
85+
sum_square_eps: float
86+
iwork: I32Array
87+
rwork: F64Array

0 commit comments

Comments
 (0)