Skip to content

Note

Click here to download the full example code

convolve

import numpy as np
import pynapple as nap
import jax.numpy as jnp
from time import perf_counter
import matplotlib.pyplot as plt

Machine Configuration

import jax
print(jax.devices())

Out:

[CpuDevice(id=0)]
def get_mean_perf_class(obj, kernel):
    n = 10
    tmp = np.zeros(n)
    for i in range(n):
        t1 = perf_counter()
        out = obj.convolve(kernel)
        t2 = perf_counter()
        tmp[i] = t2 - t1
    return np.mean(tmp), np.std(tmp)

def benchmark_convolve(kernel):
    times = []
    for nd in range(10, 500, 50):
        print("Dimensions ", nd)
        t = np.arange(10000)
        d = np.random.randn(10000, nd)
        tsd = nap.TsdFrame(t=t, d=d)        
        tsd2 = tsd.convolve(kernel) # First call to compile
        m, s = get_mean_perf_class(tsd, kernel)
        times.append([nd, m, s])
    return np.array(times)

Calling with jax

nap.nap_config.set_backend("jax")
jax_times = benchmark_convolve(jnp.ones(11))

Out:

Dimensions  10
/mnt/home/gviejo/pynajax/src/pynajax/utils.py:131: UserWarning: Converting 'd' to jax.ndarray. The provided array was of type 'ndarray'.
  return cast_to_jax(array, array_name, suppress_conversion_warnings)
Dimensions  60
/mnt/home/gviejo/pynajax/src/pynajax/utils.py:131: UserWarning: Converting 'd' to jax.ndarray. The provided array was of type 'ndarray'.
  return cast_to_jax(array, array_name, suppress_conversion_warnings)
Dimensions  110
Dimensions  160
Dimensions  210
Dimensions  260
Dimensions  310
Dimensions  360
Dimensions  410
Dimensions  460

Calling with numba

nap.nap_config.set_backend("numba")
num_times = benchmark_convolve(np.ones(11))

Out:

Dimensions  10
Dimensions  60
Dimensions  110
Dimensions  160
Dimensions  210
Dimensions  260
Dimensions  310
Dimensions  360
Dimensions  410
Dimensions  460

Figure

plt.figure()
for arr, label in zip(
    [num_times, jax_times],
    ["numba backend", "pynajax backend"],
    ):
    plt.plot(arr[:, 0], arr[:, 1], "o-", label=label)
    plt.fill_between(arr[:, 0], arr[:, 1] - arr[:, 2], arr[:, 1] + arr[:, 2], alpha=0.2)
plt.legend()
plt.xlabel("Number of Dimensions")
plt.ylabel("Time (s)")
plt.title("Convolve benchmark")
plt.savefig("../images/convolve_benchmark.png")
plt.show()

Convolve benchmark

Out:

/mnt/home/gviejo/pynajax/docs/examples/plot_benchmark_convolve.py:64: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown
  plt.show()

Saving

Total running time of the script: ( 0 minutes 9.641 seconds)

Download Python source code: plot_benchmark_convolve.py

Download Jupyter notebook: plot_benchmark_convolve.ipynb

Gallery generated by mkdocs-gallery