|
1 | 1 | import numpy as np |
2 | 2 | import scipy.signal |
| 3 | +from warnings import warn |
3 | 4 |
|
4 | 5 | # included code |
5 | 6 | from pynumdiff.finite_difference import first_order as finite_difference |
|
9 | 10 | ################################ |
10 | 11 | # Smoothing finite differences # |
11 | 12 | ################################ |
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 |
31 | 15 |
|
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 |
36 | 22 |
|
37 | 23 |
|
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 |
39 | 27 | """ |
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] |
49 | 34 |
|
50 | 35 | 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 |
52 | 37 |
|
53 | 38 | x_hat = x |
54 | | - for _ in range(iterations): |
| 39 | + for _ in range(num_iterations): |
55 | 40 | x_hat = scipy.signal.medfilt(x_hat, window_size) |
56 | 41 | x_hat, dxdt_hat = finite_difference(x_hat, dt) |
57 | 42 |
|
58 | 43 | return x_hat, dxdt_hat |
59 | 44 |
|
60 | 45 |
|
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 |
65 | 48 |
|
66 | 49 | :param np.ndarray[float] x: array of time series to differentiate |
67 | 50 | :param float dt: time step size |
68 | 51 |
|
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 |
85 | 57 |
|
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 |
87 | 61 | """ |
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] |
97 | 68 |
|
98 | 69 | 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) |
100 | 71 | x_hat, dxdt_hat = finite_difference(x_hat, dt) |
101 | 72 |
|
102 | 73 | return x_hat, dxdt_hat |
103 | 74 |
|
104 | 75 |
|
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 |
132 | 78 |
|
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 |
134 | 90 | """ |
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] |
143 | 97 |
|
144 | 98 | 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) |
146 | 100 | x_hat, dxdt_hat = finite_difference(x_hat, dt) |
147 | 101 |
|
148 | 102 | return x_hat, dxdt_hat |
|
0 commit comments