From 7264fc29d7db11d1bd9bfb6f3b73c50c46617052 Mon Sep 17 00:00:00 2001 From: Richard Hartmann Date: Mon, 5 Dec 2016 14:47:36 +0100 Subject: [PATCH] minor changes --- .travis.yml | 5 +++++ stocproc/method_fft.py | 8 +++++++- stocproc/method_kle.py | 2 +- stocproc/stocproc.py | 18 ++++++++++++++---- tests/test_stocproc.py | 2 +- 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 595d82f..56f580d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,6 +22,11 @@ install: - pip install cython - pip install numpy scipy - pip freeze +- git clone https://github.com/cimatosa/fcSpline.git fcs_pack +- cd fcs_pack +- python setup.py build_ext --inplace +- cd .. +- ln -s fcs_pack/fcSpline - python setup.py build_ext --inplace script: diff --git a/stocproc/method_fft.py b/stocproc/method_fft.py index feb93aa..f5e1c89 100644 --- a/stocproc/method_fft.py +++ b/stocproc/method_fft.py @@ -178,12 +178,18 @@ def _f_opt(x, integrand, a, b, N, t_max, ft_ref, diff_method, _f_opt_cache, b_on else: a_ = find_integral_boundary(integrand, tol=tol, ref_val=a, max_val=1e6, x0=-1) b_ = find_integral_boundary(integrand, tol=tol, ref_val=b, max_val=1e6, x0=1) - except RuntimeError: + except Exception as e: + log.debug("Exception {} ({}) in _f_opt".format(type(e), e)) # in case 'find_integral_boundary' failes d = 300 _f_opt_cache[key] = d, None, None return d + if a_ == b_: + d = 300 + _f_opt_cache[key] = d, None, None + return d + tau, ft_tau = fourier_integral_midpoint(integrand, a_, b_, N) idx = np.where(tau <= t_max) diff --git a/stocproc/method_kle.py b/stocproc/method_kle.py index c893fd3..6c65f75 100644 --- a/stocproc/method_kle.py +++ b/stocproc/method_kle.py @@ -471,7 +471,7 @@ def auto_ng(corr, t_max, ngfac=2, meth=get_mid_point_weights_times, tol=1e-3, di if not is_equi: sqrt_lambda_ui_spl = tools.ComplexInterpolatedUnivariateSpline(tfine, sqrt_lambda_ui_fine, noWarning=True) else: - sqrt_lambda_ui_spl = fcSpline.FCS(x_low=0, x_high=t_max, y=sqrt_lambda_ui_fine) + sqrt_lambda_ui_spl = fcSpline.FCS(x_low=0, x_high=t_max, y=sqrt_lambda_ui_fine, ord_bound_apprx=2) time_spline += (time.time() - t0) # calculate the max deviation diff --git a/stocproc/stocproc.py b/stocproc/stocproc.py index 3f3ff4b..f407097 100644 --- a/stocproc/stocproc.py +++ b/stocproc/stocproc.py @@ -86,6 +86,7 @@ class _absStocProc(abc.ABC): np.random.seed(seed) self._one_over_sqrt_2 = 1/np.sqrt(2) self._proc_cnt = 0 + self.sqrt_scale = 1. log.debug("init StocProc with t_max {} and {} grid points".format(t_max, num_grid_points)) def __call__(self, t=None): @@ -117,6 +118,10 @@ class _absStocProc(abc.ABC): :return: the stochastic process, array of complex numbers """ pass + + def _calc_scaled_z(self, y): + r"""scaled the discrete process z with sqrt(scale), such that = scale bcf(i,j)""" + return self.sqrt_scale * self.calc_z(y) @abc.abstractmethod def get_num_y(self): @@ -154,8 +159,11 @@ class _absStocProc(abc.ABC): if y is None: #random complex normal samples y = np.random.normal(scale=self._one_over_sqrt_2, size = 2*self.get_num_y()).view(np.complex) - self._z = self.calc_z(y) + self._z = self._calc_scaled_z(y) log.debug("proc_cnt:{} new process generated [{:.2e}s]".format(self._proc_cnt, time.time() - t0)) + + def set_scale(self, scale): + self.sqrt_scale = np.sqrt(scale) class StocProc_KLE(_absStocProc): @@ -221,7 +229,8 @@ class StocProc_KLE(_absStocProc): Details on the error estimation and further clarification of the parameters ng_fac, meth, diff_method, dm_random_samples can be found at :py:func:`stocproc.method_kle.auto_ng`. """ - + self.key = r_tau, t_max, tol + sqrt_lambda_ui_fine, t = method_kle.auto_ng(corr=r_tau, t_max=t_max, ngfac=ng_fac, @@ -236,7 +245,7 @@ class StocProc_KLE(_absStocProc): state = sqrt_lambda_ui_fine, t, seed self.__setstate__(state) - self.key = r_tau, t_max, tol + def get_key(self): """Returns the tuple (r_tau, t_max, tol) which should suffice to identify the process in order to load/dump @@ -336,6 +345,8 @@ class StocProc_FFT(_absStocProc): """ def __init__(self, spectral_density, t_max, bcf_ref, intgr_tol=1e-2, intpl_tol=1e-2, seed=None, negative_frequencies=False): + self.key = bcf_ref, t_max, intgr_tol, intpl_tol + if not negative_frequencies: log.info("non neg freq only") # assume the spectral_density is 0 for w<0 @@ -393,7 +404,6 @@ class StocProc_FFT(_absStocProc): self.yl = spectral_density(omega + a + dx/2) * dx / np.pi self.yl = np.sqrt(self.yl) self.omega_min_correction = np.exp(-1j*(a+dx/2)*self.t) #self.t is from the parent class - self.key = bcf_ref, t_max, intgr_tol, intpl_tol def __getstate__(self): return self.yl, self.num_grid_points, self.omega_min_correction, self.t_max, self._seed diff --git a/tests/test_stocproc.py b/tests/test_stocproc.py index 6c7091a..0b99575 100644 --- a/tests/test_stocproc.py +++ b/tests/test_stocproc.py @@ -239,7 +239,7 @@ def test_many(plot=False): if __name__ == "__main__": import logging - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(level=logging.INFO) # test_stochastic_process_KLE_correlation_function(plot=False) # test_stochastic_process_FFT_correlation_function(plot=False) # test_stocproc_dump_load()