22ND-Gridded cubic smoothing spline implementation
33"""
44
5- from typing import Optional , Sequence , Tuple , Union
5+ from typing import Optional , Sequence , Tuple , Union , cast
66import collections .abc as c_abc
7- from numbers import Number
87
98import numpy as np
109from scipy .interpolate import NdPPoly , PPoly
1817 umv_coeffs_to_flatten ,
1918)
2019from ._sspumv import CubicSmoothingSpline
21- from ._types import NdGridDataType , UnivariateDataType
20+ from ._types import SequenceUnivariateDataType , FloatNDArrayType , Float1DArrayTupe
2221
2322
24- def ndgrid_prepare_data_vectors (data , name , min_size : int = 2 ) -> Tuple [np .ndarray , ...]:
23+ def ndgrid_prepare_data_vectors (
24+ data : SequenceUnivariateDataType ,
25+ name : str ,
26+ min_size : int = 2 ,
27+ ) -> Tuple [Float1DArrayTupe , ...]:
2528 if not isinstance (data , c_abc .Sequence ):
2629 raise TypeError (f"'{ name } ' must be a sequence of 1-d array-like (vectors) or scalars." )
2730
28- data = list ( data )
31+ data_ : list [ Float1DArrayTupe ] = []
2932
3033 for axis , d in enumerate (data ):
3134 d = np .asarray (d , dtype = np .float64 )
3235 if d .ndim > 1 :
3336 raise ValueError (f"All '{ name } ' elements must be a vector for axis { axis } ." )
3437 if d .size < min_size :
3538 raise ValueError (f"'{ name } ' must contain at least { min_size } data points for axis { axis } ." )
36- data [ axis ] = d
39+ data_ . append ( d )
3740
38- return tuple (data )
41+ return tuple (data_ )
3942
4043
4144class NdGridSplinePPForm (ISplinePPForm [Tuple [np .ndarray , ...], Tuple [int , ...]], NdPPoly ):
@@ -76,9 +79,9 @@ def ndim(self) -> int:
7679 def shape (self ) -> Tuple [int , ...]:
7780 return tuple (len (xi ) for xi in self .x )
7881
79- def __call__ (
82+ def __call__ ( # type: ignore[override]
8083 self ,
81- x : Sequence [ UnivariateDataType ] ,
84+ x : SequenceUnivariateDataType ,
8285 nu : Optional [Tuple [int , ...]] = None ,
8386 extrapolate : Optional [bool ] = None ,
8487 ) -> np .ndarray :
@@ -87,8 +90,8 @@ def __call__(
8790 Parameters
8891 ----------
8992
90- x : tuple of 1-d array-like
91- The tuple of point values for each dimension to evaluate the spline at.
93+ x : Sequence of 1-d array-like
94+ The sequence of point values for each dimension to evaluate the spline at.
9295
9396 nu : [*Optional*] tuple of int
9497 Orders of derivatives to evaluate. Each must be non-negative.
@@ -158,7 +161,7 @@ class NdGridCubicSmoothingSpline(
158161 ISmoothingSpline [
159162 NdGridSplinePPForm ,
160163 Tuple [float , ...],
161- NdGridDataType ,
164+ SequenceUnivariateDataType ,
162165 Tuple [int , ...],
163166 bool ,
164167 ]
@@ -204,31 +207,29 @@ class NdGridCubicSmoothingSpline(
204207
205208 def __init__ (
206209 self ,
207- xdata : NdGridDataType ,
210+ xdata : SequenceUnivariateDataType ,
208211 ydata : np .ndarray ,
209- weights : Optional [Union [ UnivariateDataType , NdGridDataType ] ] = None ,
212+ weights : Optional [SequenceUnivariateDataType ] = None ,
210213 smooth : Optional [Union [float , Sequence [Optional [float ]]]] = None ,
211214 normalizedsmooth : bool = False ,
212215 ) -> None :
213216 x , y , w , s = self ._prepare_data (xdata , ydata , weights , smooth )
214- coeffs , smooth = self ._make_spline (x , y , w , s , normalizedsmooth )
215-
216- self ._spline = NdGridSplinePPForm .construct_fast (coeffs , x )
217- self ._smooth = smooth
217+ coeffs , self ._smooth = self ._make_spline (x , y , w , s , normalizedsmooth )
218+ self ._spline = cast (NdGridSplinePPForm , NdGridSplinePPForm .construct_fast (coeffs , x ))
218219
219220 def __call__ (
220221 self ,
221- x : Union [ NdGridDataType , Sequence [ Number ]] ,
222+ x : SequenceUnivariateDataType ,
222223 nu : Optional [Tuple [int , ...]] = None ,
223224 extrapolate : Optional [bool ] = None ,
224- ) -> np . ndarray :
225+ ) -> FloatNDArrayType :
225226 """Evaluate the spline for given data
226227
227228 Parameters
228229 ----------
229230
230- x : tuple of 1-d array-like
231- The tuple of point values for each dimension to evaluate the spline at.
231+ x : Sequence of 1-d array-like
232+ The sequence of point values for each dimension to evaluate the spline at.
232233
233234 nu : [*Optional*] tuple of int
234235 Orders of derivatives to evaluate. Each must be non-negative.
0 commit comments