Skip to content

Commit

Permalink
Merge pull request #8 from dfki-ric/refactor/cleanup_cython
Browse files Browse the repository at this point in the history
Clean up cython module
  • Loading branch information
AlexanderFabisch authored Dec 22, 2021
2 parents 16c9c49 + 78c94a4 commit fca7487
Show file tree
Hide file tree
Showing 14 changed files with 1,077 additions and 555 deletions.
26 changes: 26 additions & 0 deletions benchmarks/benchmark_cartesian_dmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from functools import partial
import numpy as np
from movement_primitives.dmp import CartesianDMP
import timeit


start_y = np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0])
goal_y = np.array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
dt = 0.01
int_dt = 0.001

dmp = CartesianDMP(execution_time=1.0, dt=dt, n_weights_per_dim=6, int_dt=int_dt)
dmp.configure(start_y=start_y, goal_y=goal_y)
dmp.set_weights(1000 * np.random.randn(*dmp.get_weights().shape))

times = timeit.repeat(partial(dmp.open_loop, quaternion_step_function="cython"), repeat=10, number=1)
print("RK4 + Cython")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, quaternion_step_function="python"), repeat=10, number=1)
print("RK4 + Python")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, step_function="euler", quaternion_step_function="python"), repeat=10, number=1)
print("Euler + Python")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))
15 changes: 14 additions & 1 deletion benchmarks/benchmark_dmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@

dmp = DMP(n_dims=n_dims, execution_time=1.0, dt=dt, n_weights_per_dim=6, int_dt=int_dt)
dmp.configure(start_y=start_y, goal_y=goal_y)
dmp.forcing_term.weights = 1000 * np.random.randn(*dmp.forcing_term.weights.shape)
dmp.set_weights(1000 * np.random.randn(*dmp.get_weights().shape))

times = timeit.repeat(partial(dmp.open_loop, step_function="rk4"), repeat=10, number=1)
print("RK4")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, step_function="euler"), repeat=10, number=1)
print("Euler (Python)")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, step_function="euler-cython"), repeat=10, number=1)
print("Euler (Cython)")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, step_function="rk4-cython"), repeat=10, number=1)
print("RK4 (Cython)")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))
16 changes: 16 additions & 0 deletions benchmarks/benchmark_dmp_phase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from functools import partial
import numpy as np
from movement_primitives.dmp._canonical_system import canonical_system_alpha
from movement_primitives.dmp._canonical_system import phase as phase_python
from movement_primitives.dmp_fast import phase as phase_cython
import timeit


goal_t = 1.0
start_t = 0.0
int_dt = 0.001
alpha = canonical_system_alpha(0.01, goal_t, start_t, int_dt)
times = timeit.repeat(partial(phase_python, 0.5, alpha, goal_t, start_t, int_dt), repeat=1000, number=1000)
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))
times = timeit.repeat(partial(phase_cython, 0.5, alpha, goal_t, start_t, int_dt), repeat=1000, number=1000)
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))
16 changes: 10 additions & 6 deletions benchmarks/benchmark_dual_dmp.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from functools import partial
import numpy as np
from movement_primitives.dmp import DualCartesianDMP
import timeit


start_y = np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0])
goal_y = np.array([1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
dt = 0.001
int_dt = 0.0001
dt = 0.01
int_dt = 0.001

dmp = DualCartesianDMP(execution_time=1.0, dt=dt, n_weights_per_dim=6, int_dt=int_dt)
dmp.configure(start_y=start_y, goal_y=goal_y)
dmp.forcing_term.weights = 1000 * np.random.randn(*dmp.forcing_term.weights.shape)
dmp.set_weights(1000 * np.random.randn(*dmp.get_weights().shape))

times = timeit.repeat(dmp.open_loop, repeat=10, number=1)
times = timeit.repeat(partial(dmp.open_loop, step_function="cython"), repeat=10, number=1)
print("Cython")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))

times = timeit.repeat(partial(dmp.open_loop, step_function="python"), repeat=10, number=1)
print("Python")
print("Mean: %.5f; Std. dev.: %.5f" % (np.mean(times), np.std(times)))
# Pure python
# Mean: 0.58188; Std. dev.: 0.00225
4 changes: 0 additions & 4 deletions movement_primitives/dmp/_canonical_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,3 @@ def phase(t, alpha, goal_t, start_t, int_dt=0.001, eps=1e-10):
execution_time = goal_t - start_t
b = max(1.0 - alpha * int_dt / execution_time, eps)
return b ** ((t - start_t) / int_dt)


# uncomment to overwrite with Cython implementation
#from ..dmp_fast import phase
Loading

0 comments on commit fca7487

Please sign in to comment.