Skip to content

Commit ab43990

Browse files
committed
halfway through the docstrings, and then gotta add them to the new unittest structure. Should be easy.
1 parent 14751af commit ab43990

1 file changed

Lines changed: 57 additions & 103 deletions

File tree

pynumdiff/smooth_finite_difference/_smooth_finite_difference.py

Lines changed: 57 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
import scipy.signal
3+
from warnings import warn
34

45
# included code
56
from pynumdiff.finite_difference import first_order as finite_difference
@@ -9,140 +10,93 @@
910
################################
1011
# Smoothing finite differences #
1112
################################
12-
def mediandiff(x, dt, params, options={}):
13-
"""
14-
Perform median smoothing using scipy.signal.medfilt
15-
followed by first order finite difference
16-
17-
:param x: array of time series to differentiate
18-
:type x: np.array (float)
19-
20-
:param dt: time step size
21-
:type dt: float
22-
23-
:param params: filter window size
24-
:type params: list (int) or int
25-
26-
:param options: an empty dictionary or a dictionary with 1 key value pair
27-
28-
- 'iterate': whether to run multiple iterations of the smoother. Note: iterate does nothing for median smoother.
29-
30-
:type options: dict {'iterate': (boolean)}
13+
def mediandiff(x, dt, params=None, options={}, window_size=5, num_iterations=1):
14+
"""Perform median smoothing using scipy.signal.medfilt followed by first order finite difference
3115
32-
:return: a tuple consisting of:
33-
34-
- x_hat: estimated (smoothed) x
35-
- dxdt_hat: estimated derivative of x
16+
:param np.array[float] x: array of time series to differentiate
17+
:param float dt: time step size
18+
:param list[int] params: (**deprecated**, prefer :code:`window_size` and :code:`num_iterations`)
19+
:param dict options: (**deprecated**, prefer :code:`num_iterations`) an empty dictionary or {'iterate': (bool)}
20+
:param int window_size: filter window size
21+
:param int num_iterations: how many times to apply median smoothing
3622
3723
38-
:rtype: tuple -> (np.array, np.array)
24+
:return: tuple[np.array, np.array] of\n
25+
- **x_hat** -- estimated (smoothed) x
26+
- **dxdt_hat** -- estimated derivative of x
3927
"""
40-
41-
if 'iterate' in options.keys() and options['iterate'] is True:
42-
window_size, iterations = params
43-
else:
44-
iterations = 1
45-
if isinstance(params, list):
46-
window_size = params[0]
47-
else:
48-
window_size = params
28+
if params != None: # Warning to support old interface for a while. Remove these lines along with params in a future release.
29+
warn("""`params` and `options` parameters will be removed in a future version. Use `window_size`
30+
and `num_iterations` instead.""", DeprecationWarning)
31+
window_size = params[0] if isinstance(params, list) else params
32+
if 'iterate' in options and options['iterate']:
33+
num_iterations = params[1]
4934

5035
if not window_size % 2:
51-
window_size += 1 # assert window_size % 2 == 1 # is odd
36+
window_size += 1 # make sure window_size is odd
5237

5338
x_hat = x
54-
for _ in range(iterations):
39+
for _ in range(num_iterations):
5540
x_hat = scipy.signal.medfilt(x_hat, window_size)
5641
x_hat, dxdt_hat = finite_difference(x_hat, dt)
5742

5843
return x_hat, dxdt_hat
5944

6045

61-
def meandiff(x, dt, params, options={}):
62-
"""
63-
Perform mean smoothing by convolving mean kernel with x
64-
followed by first order finite difference
46+
def meandiff(x, dt, params=None, options={}, window_size=5, num_iterations=1):
47+
"""Perform mean smoothing by convolving mean kernel with x followed by first order finite difference
6548
6649
:param np.ndarray[float] x: array of time series to differentiate
6750
:param float dt: time step size
6851
69-
:param params: [filter_window_size] or if 'iterate' in options:
70-
[filter_window_size, num_iterations]
71-
72-
:type params: list (int)
73-
74-
:param options: an empty dictionary or a dictionary with 1 key value pair
75-
76-
- 'iterate': whether to run multiple iterations of the smoother. Note: iterate does nothing for median smoother.
77-
78-
:type options: dict {'iterate': (boolean)}
79-
80-
:return: a tuple consisting of:
81-
82-
- x_hat: estimated (smoothed) x
83-
- dxdt_hat: estimated derivative of x
84-
52+
:param list[int] params: (**deprecated**, prefer :code:`window_size` and :code:`num_iterations`)
53+
:code:`[window_size]` or, :code:`if 'iterate' in options`, :code:`[window_size, num_iterations]`
54+
:param dict options: (**deprecated**, prefer :code:`num_iterations`) an empty dictionary or {'iterate': (bool)}
55+
:param int window_size: filter window size
56+
:param int num_iterations: how many times to apply mean smoothing
8557
86-
:rtype: tuple -> (np.array, np.array)
58+
:return: tuple[np.array, np.array] of\n
59+
- **x_hat** -- estimated (smoothed) x
60+
- **dxdt_hat** -- estimated derivative of x
8761
"""
88-
89-
if 'iterate' in options.keys() and options['iterate'] is True:
90-
window_size, iterations = params
91-
else:
92-
iterations = 1
93-
if isinstance(params, list):
94-
window_size = params[0]
95-
else:
96-
window_size = params
62+
if params != None: # Warning to support old interface for a while. Remove these lines along with params in a future release.
63+
warn("""`params` and `options` parameters will be removed in a future version. Use `window_size`
64+
and `num_iterations` instead.""", DeprecationWarning)
65+
window_size = params[0] if isinstance(params, list) else params
66+
if 'iterate' in options and options['iterate']:
67+
num_iterations = params[1]
9768

9869
kernel = utility._mean_kernel(window_size)
99-
x_hat = utility.convolutional_smoother(x, kernel, iterations)
70+
x_hat = utility.convolutional_smoother(x, kernel, num_iterations)
10071
x_hat, dxdt_hat = finite_difference(x_hat, dt)
10172

10273
return x_hat, dxdt_hat
10374

10475

105-
def gaussiandiff(x, dt, params, options={}):
106-
"""
107-
Perform gaussian smoothing by convolving gaussian kernel with x
108-
followed by first order finite difference
109-
110-
:param x: array of time series to differentiate
111-
:type x: np.array (float)
112-
113-
:param dt: time step size
114-
:type dt: float
115-
116-
:param params: [filter_window_size] or if 'iterate' in options:
117-
[filter_window_size, num_iterations]
118-
119-
:type params: list (int)
120-
121-
:param options: an empty dictionary or a dictionary with 1 key value pair
122-
123-
- 'iterate': whether to run multiple iterations of the smoother. Note: iterate does nothing for median smoother.
124-
125-
:type options: dict {'iterate': (boolean)}
126-
127-
:return: a tuple consisting of:
128-
129-
- x_hat: estimated (smoothed) x
130-
- dxdt_hat: estimated derivative of x
131-
76+
def gaussiandiff(x, dt, params=None, options={}, window_size=5, num_iterations=1):
77+
"""Perform gaussian smoothing by convolving gaussian kernel with x followed by first order finite difference
13278
133-
:rtype: tuple -> (np.array, np.array)
79+
:param np.array[float] x: array of time series to differentiate
80+
:param float dt: time step size
81+
:param list[int] params: (**deprecated**, prefer :code:`window_size` and :code:`num_iterations`)
82+
:code:`[window_size]` or, :code:`if 'iterate' in options`, :code:`[window_size, num_iterations]`
83+
:param dict options: (**deprecated**, prefer :code:`num_iterations`) an empty dictionary or {'iterate': (bool)}
84+
:param int window_size: filter window size
85+
:param int num_iterations: how many times to apply gaussian smoothing
86+
87+
:return: tuple[np.array, np.array] of\n
88+
- **x_hat** -- estimated (smoothed) x
89+
- **dxdt_hat** -- estimated derivative of x
13490
"""
135-
if 'iterate' in options.keys() and options['iterate'] is True:
136-
window_size, iterations = params
137-
else:
138-
iterations = 1
139-
if isinstance(params, list):
140-
window_size = params[0]
141-
else:
142-
window_size = params
91+
if params != None: # Warning to support old interface for a while. Remove these lines along with params in a future release.
92+
warn("""`params` and `options` parameters will be removed in a future version. Use `window_size`
93+
and `num_iterations` instead.""", DeprecationWarning)
94+
window_size = params[0] if isinstance(params, list) else params
95+
if 'iterate' in options and options['iterate']:
96+
num_iterations = params[1]
14397

14498
kernel = utility._gaussian_kernel(window_size)
145-
x_hat = utility.convolutional_smoother(x, kernel, iterations)
99+
x_hat = utility.convolutional_smoother(x, kernel, num_iterations)
146100
x_hat, dxdt_hat = finite_difference(x_hat, dt)
147101

148102
return x_hat, dxdt_hat

0 commit comments

Comments
 (0)