@@ -135,52 +135,40 @@ def integrate_dxdt_hat(dxdt_hat, dt):
135135
136136 :return: **x_hat** (np.array[float]) -- integral of dxdt_hat
137137 """
138- x = scipy .integrate .cumulative_trapezoid (dxdt_hat )
139- first_value = x [0 ] - dxdt_hat [0 ]
140- return np .hstack ((first_value , x ))* dt
138+ return np .hstack ((0 , scipy .integrate .cumulative_trapezoid (dxdt_hat )))* dt
141139
142140
143141# Optimization routine to estimate the integration constant.
144142def estimate_initial_condition (x , x_hat ):
145- """
146- Integration leaves an unknown integration constant. This function finds a best fit integration constant given x, and x_hat (the integral of dxdt_hat)
147-
148- :param x: timeseries of measurements
149- :type x: np.array
143+ """Integration leaves an unknown integration constant. This function finds a best fit integration constant given x and
144+ x_hat (the integral of dxdt_hat) by optimizing :math:`\\ min_c ||x - \\ hat{x} + c||_2`.
150145
151- :param x_hat: smoothed estiamte of x, for the purpose of this function this should have been determined by integrate_dxdt_hat
152- :type x_hat: np.array
146+ :param np.array[float] x: timeseries of measurements
147+ :param np.array[float] x_hat: smoothed estiamte of x, for the purpose of this function this should have been determined
148+ by integrate_dxdt_hat
153149
154- :return: integration constant (i.e. initial condition) that best aligns x_hat with x
155- :rtype: float
150+ :return: **integration constant** (float) -- initial condition that best aligns x_hat with x
156151 """
157- def f (x0 , * args ):
158- x , x_hat = args [0 ]
159- error = np .linalg .norm (x - (x_hat + x0 ))
160- return error
161- result = scipy .optimize .minimize (f , [0 ], args = [x , x_hat ], method = 'SLSQP' )
162- return result .x
152+ return scipy .optimize .minimize (lambda x0 , x , xhat : np .linalg .norm (x - (x_hat + x0 )), # fn to minimize in 1st argument
153+ 0 , args = (x , x_hat ), method = 'SLSQP' ).x [0 ] # result is a vector, even if initial guess is just a scalar
163154
164155
165156# kernels
166157def _mean_kernel (window_size ):
167- """A uniform boxcar of total integral 1
168- """
158+ """A uniform boxcar of total integral 1"""
169159 return np .ones (window_size )/ window_size
170160
171161
172162def _gaussian_kernel (window_size ):
173- """A truncated gaussian
174- """
163+ """A truncated gaussian"""
175164 sigma = window_size / 6.
176165 t = np .linspace (- 2.7 * sigma , 2.7 * sigma , window_size )
177166 ker = 1 / np .sqrt (2 * np .pi * sigma ** 2 ) * np .exp (- (t ** 2 )/ (2 * sigma ** 2 )) # gaussian function itself
178167 return ker / np .sum (ker )
179168
180169
181170def _friedrichs_kernel (window_size ):
182- """A bump function
183- """
171+ """A bump function"""
184172 x = np .linspace (- 0.999 , 0.999 , window_size )
185173 ker = np .exp (- 1 / (1 - x ** 2 ))
186174 return ker / np .sum (ker )
0 commit comments