55from pynumdiff .utils import utility
66
77
8- def splinediff (x , dt , params = None , options = {}, deg = 3 , s = None , num_iterations = 1 ):
8+ def splinediff (x , dt , params = None , options = {}, degree = 3 , s = None , num_iterations = 1 ):
99 """Find smoothed data and derivative estimates by fitting a smoothing spline to the data with
1010 scipy.interpolate.UnivariateSpline.
1111
1212 :param np.array[float] x: data to differentiate
1313 :param float dt: step size
14- :param list params: (**deprecated**, prefer :code:`deg `, :code:`cutoff_freq`, and :code:`num_iterations`)
14+ :param list params: (**deprecated**, prefer :code:`degree `, :code:`cutoff_freq`, and :code:`num_iterations`)
1515 :param dict options: (**deprecated**, prefer :code:`num_iterations`) an empty dictionary or {'iterate': (bool)}
16- :param int deg : polynomial degree of the spline. A kth degree spline can be differentiated k times.
16+ :param int degree : polynomial degree of the spline. A kth degree spline can be differentiated k times.
1717 :param float s: positive smoothing factor used to choose the number of knots. Number of knots will be increased
1818 until the smoothing condition is satisfied: :math:`\\ sum_t (x[t] - \\ text{spline}[t])^2 \\ leq s`
1919 :param int num_iterations: how many times to apply smoothing
@@ -25,15 +25,15 @@ def splinediff(x, dt, params=None, options={}, deg=3, s=None, num_iterations=1):
2525 if params != None : # Warning to support old interface for a while. Remove these lines along with params in a future release.
2626 warn ("`params` and `options` parameters will be removed in a future version. Use `order`, `s`, and " +
2727 "`num_iterations` instead." , DeprecationWarning )
28- deg , s = params [0 :2 ]
28+ degree , s = params [0 :2 ]
2929 if 'iterate' in options and options ['iterate' ]:
3030 num_iterations = params [2 ]
3131
3232 t = np .arange (len (x ))* dt
3333
3434 x_hat = x
3535 for _ in range (num_iterations ):
36- spline = scipy .interpolate .UnivariateSpline (t , x_hat , k = deg , s = s )
36+ spline = scipy .interpolate .UnivariateSpline (t , x_hat , k = degree , s = s )
3737 x_hat = spline (t )
3838
3939 dspline = spline .derivative ()
@@ -42,16 +42,16 @@ def splinediff(x, dt, params=None, options={}, deg=3, s=None, num_iterations=1):
4242 return x_hat , dxdt_hat
4343
4444
45- def polydiff (x , dt , params = None , options = None , poly_deg = None , window_size = None , step_size = 1 ,
45+ def polydiff (x , dt , params = None , options = None , degree = None , window_size = None , step_size = 1 ,
4646 kernel = 'friedrichs' ):
4747 """Fit polynomials to the data, and differentiate the polynomials.
4848
4949 :param np.array[float] x: data to differentiate
5050 :param float dt: step size
51- :param list[int] params: (**deprecated**, prefer :code:`poly_deg ` and :code:`window_size`)
51+ :param list[int] params: (**deprecated**, prefer :code:`degree ` and :code:`window_size`)
5252 :param dict options: (**deprecated**, prefer :code:`step_size` and :code:`kernel`)
5353 a dictionary consisting of {'sliding': (bool), 'step_size': (int), 'kernel_name': (str)}
54- :param int poly_deg : degree of the polynomial
54+ :param int degree : degree of the polynomial
5555 :param int window_size: size of the sliding window, if not given no sliding
5656 :param int step_size: step size for sliding
5757 :param str kernel: name of kernel to use for weighting and smoothing windows ('gaussian' or 'friedrichs')
@@ -61,27 +61,27 @@ def polydiff(x, dt, params=None, options=None, poly_deg=None, window_size=None,
6161 - **dxdt_hat** -- estimated derivative of x
6262 """
6363 if params != None :
64- warn ("`params` and `options` parameters will be removed in a future version. Use `poly_deg ` " +
64+ warn ("`params` and `options` parameters will be removed in a future version. Use `degree ` " +
6565 "and `window_size` instead." , DeprecationWarning )
66- poly_deg = params [0 ]
66+ degree = params [0 ]
6767 if len (params ) > 1 : window_size = params [1 ]
6868 if options != None :
6969 if 'sliding' in options and not options ['sliding' ]: window_size = None
7070 if 'step_size' in options : step_size = options ['step_size' ]
7171 if 'kernel_name' in options : kernel = options ['kernel_name' ]
72- elif poly_deg == None or window_size == None :
73- raise ValueError ("`poly_deg ` and `window_size` must be given." )
72+ elif degree == None or window_size == None :
73+ raise ValueError ("`degree ` and `window_size` must be given." )
7474
75- if window_size < poly_deg * 3 :
76- window_size = poly_deg * 3 + 1
75+ if window_size < degree * 3 :
76+ window_size = degree * 3 + 1
7777 if window_size % 2 == 0 :
7878 window_size += 1
7979 warn ("Kernel window size should be odd. Added 1 to length." )
8080
81- def _polydiff (x , dt , poly_deg , weights = None ):
81+ def _polydiff (x , dt , degree , weights = None ):
8282 t = np .arange (len (x ))* dt
8383
84- r = np .polyfit (t , x , poly_deg , w = weights ) # polyfit returns highest order first
84+ r = np .polyfit (t , x , degree , w = weights ) # polyfit returns highest order first
8585 dr = np .polyder (r ) # power rule already implemented for us
8686
8787 dxdt_hat = np .polyval (dr , t ) # evaluate the derivative and original polynomials at points t
@@ -90,22 +90,22 @@ def _polydiff(x, dt, poly_deg, weights=None):
9090 return x_hat , dxdt_hat
9191
9292 if not window_size :
93- return _polydiff (x , dt , poly_deg )
93+ return _polydiff (x , dt , degree )
9494
9595 kernel = {'gaussian' :utility .gaussian_kernel , 'friedrichs' :utility .friedrichs_kernel }[kernel ](window_size )
96- return utility .slide_function (_polydiff , x , dt , kernel , poly_deg , stride = step_size , pass_weights = True )
96+ return utility .slide_function (_polydiff , x , dt , kernel , degree , stride = step_size , pass_weights = True )
9797
9898
99- def savgoldiff (x , dt , params = None , options = None , poly_deg = None , window_size = None , smoothing_win = None ):
99+ def savgoldiff (x , dt , params = None , options = None , degree = None , window_size = None , smoothing_win = None ):
100100 """Use the Savitzky-Golay to smooth the data and calculate the first derivative. It uses
101101 scipy.signal.savgol_filter. The Savitzky-Golay is very similar to the sliding polynomial fit,
102102 but slightly noisier, and much faster.
103103
104104 :param np.array[float] x: data to differentiate
105105 :param float dt: step size
106- :param list params: (**deprecated**, prefer :code:`poly_deg `, :code:`window_size`, and :code:`smoothing_win`)
106+ :param list params: (**deprecated**, prefer :code:`degree `, :code:`window_size`, and :code:`smoothing_win`)
107107 :param dict options: (**deprecated**)
108- :param int poly_deg : degree of the polynomial
108+ :param int degree : degree of the polynomial
109109 :param int window_size: size of the sliding window, must be odd (if not, 1 is added)
110110 :param int smoothing_win: size of the window used for gaussian smoothing, a good default is
111111 window_size, but smaller for high frequnecy data
@@ -115,19 +115,19 @@ def savgoldiff(x, dt, params=None, options=None, poly_deg=None, window_size=None
115115 - **dxdt_hat** -- estimated derivative of x
116116 """
117117 if params != None : # Warning to support old interface for a while. Remove these lines along with params in a future release.
118- warn ("`params` and `options` parameters will be removed in a future version. Use `poly_deg `, " +
118+ warn ("`params` and `options` parameters will be removed in a future version. Use `degree `, " +
119119 "`window_size`, and `smoothing_win` instead." , DeprecationWarning )
120- poly_deg , window_size , smoothing_win = params
121- elif poly_deg == None or window_size == None or smoothing_win == None :
122- raise ValueError ("`poly_deg `, `window_size`, and `smoothing_win` must be given." )
120+ degree , window_size , smoothing_win = params
121+ elif degree == None or window_size == None or smoothing_win == None :
122+ raise ValueError ("`degree `, `window_size`, and `smoothing_win` must be given." )
123123
124- window_size = np .clip (window_size , poly_deg + 1 , len (x )- 1 )
124+ window_size = np .clip (window_size , degree + 1 , len (x )- 1 )
125125 if window_size % 2 == 0 :
126126 window_size += 1 # window_size needs to be odd
127127 warn ("Kernel window size should be odd. Added 1 to length." )
128128 smoothing_win = min (smoothing_win , len (x )- 1 )
129129
130- dxdt_hat = scipy .signal .savgol_filter (x , window_size , poly_deg , deriv = 1 )/ dt
130+ dxdt_hat = scipy .signal .savgol_filter (x , window_size , degree , deriv = 1 )/ dt
131131
132132 kernel = utility .gaussian_kernel (smoothing_win )
133133 dxdt_hat = utility .convolutional_smoother (dxdt_hat , kernel )
0 commit comments