Note
Go to the end to download the full example code
1D ndsplines vs. scipy.interpolateΒΆ
7 import numpy as np
8 import gc
9 import time
10
11 from scipy import interpolate
12 import ndsplines
13
14 import matplotlib.pyplot as plt
15
16 # number of time measurements per input/query size
17 n_iter = 10
18
19
20 def timeit(func, n_iter=1, return_samps=True, **func_kwargs):
21 results = np.empty(n_iter, dtype=np.double)
22 for i in range(n_iter):
23 # gc.collect()
24
25 tstart = time.time()
26 func(**func_kwargs)
27 delta = time.time() - tstart
28
29 results[i] = delta
30
31 if return_samps:
32 return results
33 else:
34 return np.mean(results)
35
36
37 def gen_xy(size):
38 x = np.pi * np.linspace(-1, 1, size)
39 y = np.sin(x)
40 return x, y
41
42
43 def gen_xx(size):
44 return 3 * np.pi * np.linspace(-1, 1, size)
45
46
47 # make_interp_spline timing
48 x_sizes = np.logspace(1, 3, 10, dtype=int)
49 t_scipy_build = np.empty((2, x_sizes.size))
50 t_ndspl_build = np.empty((2, x_sizes.size))
51 for i, size in enumerate(x_sizes):
52 x, y = gen_xy(size)
53 t_scipy = 10e3 * timeit(interpolate.make_interp_spline, x=x.copy(), y=y,
54 n_iter=n_iter)
55 t_ndspl = 10e3 * timeit(ndsplines.make_interp_spline, x=x.copy(), y=y,
56 n_iter=n_iter)
57 t_scipy_build[:, i] = np.mean(t_scipy), np.std(t_scipy)
58 t_ndspl_build[:, i] = np.mean(t_ndspl), np.std(t_ndspl)
59
60 # spline query timing
61 x, y = gen_xy(7)
62 xx_sizes = np.logspace(0, 3, 10, dtype=int)
63 t_scipy_call = np.empty((2, xx_sizes.size))
64 t_ndspl_npy_call = np.empty((2, xx_sizes.size))
65 t_ndspl_pyx_call = np.empty((2, xx_sizes.size))
66 for i, size in enumerate(xx_sizes):
67 xx = gen_xx(size)
68 spl_scipy = interpolate.make_interp_spline(x.copy(), y)
69 spl_ndspl = ndsplines.make_interp_spline(x.copy(), y)
70 spl_ndspl.allocate_workspace_arrays(size)
71 t_scipy = 10e3 * timeit(spl_scipy, x=xx.copy(), n_iter=n_iter)
72 ndsplines.set_impl('cython')
73 t_ndspl_pyx = 10e3 * timeit(spl_ndspl, x=xx.copy(), n_iter=n_iter)
74 ndsplines.set_impl('numpy')
75 t_ndspl_npy = 10e3 * timeit(spl_ndspl, x=xx.copy(), n_iter=n_iter)
76 t_scipy_call[:, i] = np.mean(t_scipy), np.std(t_scipy)
77 t_ndspl_npy_call[:, i] = np.mean(t_ndspl_npy), np.std(t_ndspl_npy)
78 t_ndspl_pyx_call[:, i] = np.mean(t_ndspl_pyx), np.std(t_ndspl_pyx)
79
80 # plot results
81 fig, axes = plt.subplots(nrows=2)
82
83 axes[0].errorbar(x_sizes, t_scipy_build[0], capsize=3, yerr=t_scipy_build[1],
84 label='scipy')
85 axes[0].errorbar(x_sizes, t_ndspl_build[0], capsize=3, yerr=t_ndspl_build[1],
86 label='ndsplines')
87 axes[0].set_title('make_interp_spline')
88
89 axes[1].errorbar(xx_sizes, t_scipy_call[0], capsize=3, yerr=t_scipy_call[1],
90 label='scipy.interpolate')
91 axes[1].errorbar(xx_sizes, t_ndspl_npy_call[0], capsize=3, yerr=t_ndspl_npy_call[1],
92 label='ndsplines npy')
93 axes[1].errorbar(xx_sizes, t_ndspl_pyx_call[0], capsize=3, yerr=t_ndspl_pyx_call[1],
94 label='ndsplines pyx')
95 axes[1].set_title('spline.__call__')
96
97 for ax in axes:
98 ax.set_xlabel('input array size')
99 ax.set_ylabel('time [ms]')
100 ax.set_xscale('log')
101 ax.grid()
102
103 axes[-1].legend()
104 fig.tight_layout()
105
106 plt.show()
Total running time of the script: (0 minutes 0.654 seconds)