Speed measurement

Here we will compare the speed of plotting UnfoldMakie with MNE (Python) and EEGLAB (MATLAB).

Three cases are measured:

  • Single topoplot
  • Topoplot series with 50 topoplots
  • Topoplott animation with 50 timestamps

Note that the results of benchmarking on your computer and on Github may differ.

using UnfoldMakie
using TopoPlots
using BenchmarkTools
using Observables
using CairoMakie
using PythonPlot;
using PyMNE;
    CondaPkg Found dependencies: /home/runner/.julia/packages/PyMNE/cNGDN/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PythonCall/L4cjh/CondaPkg.toml
    CondaPkg Found dependencies: /home/runner/.julia/packages/PythonPlot/oS8x4/CondaPkg.toml
    CondaPkg Resolving changes
             + libstdcxx-ng
             + matplotlib
             + mne (pip)
             + openssl
             + python
             + uv
    CondaPkg Initialising pixi
             │ /home/runner/.julia/artifacts/cefba4912c2b400756d043a2563ef77a0088866b/bin/pixi
             │ init
             │ --format pixi
             └ /home/runner/work/UnfoldMakie.jl/UnfoldMakie.jl/docs/.CondaPkg
✔ Created /home/runner/work/UnfoldMakie.jl/UnfoldMakie.jl/docs/.CondaPkg/pixi.toml
    CondaPkg Wrote /home/runner/work/UnfoldMakie.jl/UnfoldMakie.jl/docs/.CondaPkg/pixi.toml
             │ [dependencies]
             │ openssl = ">=3, <3.6"
             │ uv = ">=0.4"
             │ libstdcxx-ng = ">=3.4,<15.0"
             │ matplotlib = ">=1"
             │
             │     [dependencies.python]
             │     channel = "conda-forge"
             │     build = "*cpython*"
             │     version = ">=3.8,<4, >=3.4,<4"
             │
             │ [project]
             │ name = ".CondaPkg"
             │ platforms = ["linux-64"]
             │ channels = ["conda-forge", "anaconda"]
             │ channel-priority = "strict"
             │ description = "automatically generated by CondaPkg.jl"
             │
             │ [pypi-dependencies]
             └ mne = ">=1.4"
    CondaPkg Installing packages
             │ /home/runner/.julia/artifacts/cefba4912c2b400756d043a2563ef77a0088866b/bin/pixi
             │ install
             └ --manifest-path /home/runner/work/UnfoldMakie.jl/UnfoldMakie.jl/docs/.CondaPkg/pixi.toml
✔ The default environment has been installed.

Data input

dat, positions = TopoPlots.example_data()
df = UnfoldMakie.eeg_array_to_dataframe(dat[:, :, 1], string.(1:length(positions)));

Topoplots

UnfoldMakie.jl

@benchmark plot_topoplot(dat[:, 320, 1]; positions = positions)
BenchmarkTools.Trial: 142 samples with 1 evaluation per sample.
 Range (minmax):  31.601 ms141.646 ms   GC (min … max): 0.00% … 56.56%
 Time  (median):     32.249 ms                GC (median):    0.00%
 Time  (mean ± σ):   35.243 ms ±  15.883 ms   GC (mean ± σ):  5.56% ±  9.08%

                                                              
  ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▁▄▁▁▄ ▄
  31.6 ms       Histogram: log(frequency) by time       133 ms <

 Memory estimate: 8.94 MiB, allocs estimate: 134979.

UnfoldMakie.jl with DelaunayMesh

@benchmark plot_topoplot(
    dat[:, 320, 1];
    positions = positions,
    topo_interpolation = (; interpolation = DelaunayMesh()),
)
BenchmarkTools.Trial: 142 samples with 1 evaluation per sample.
 Range (minmax):  31.482 ms153.396 ms   GC (min … max): 0.00% … 57.10%
 Time  (median):     32.128 ms                GC (median):    0.00%
 Time  (mean ± σ):   35.618 ms ±  18.275 ms   GC (mean ± σ):  6.28% ±  9.28%

                                                              
  ▁▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▁▁▁▁▁▁▁▁▁▁▅ ▄
  31.5 ms       Histogram: log(frequency) by time       146 ms <

 Memory estimate: 8.94 MiB, allocs estimate: 134984.

MNE

posmat = collect(reduce(hcat, [[p[1], p[2]] for p in positions])')
pypos = Py(posmat).to_numpy()
pydat = Py(dat[:, 320, 1])

@benchmark begin
    f = PythonPlot.figure()
    PyMNE.viz.plot_topomap(
        pydat,
        pypos,
        sphere = 1.1,
        extrapolate = "box",
        cmap = "RdBu_r",
        sensors = false,
        contours = 6,
    )
    f.show()
end
BenchmarkTools.Trial: 286 samples with 1 evaluation per sample.
 Range (minmax):  11.795 ms601.880 ms   GC (min … max): 0.00% … 0.00%
 Time  (median):     12.111 ms                GC (median):    0.00%
 Time  (mean ± σ):   17.489 ms ±  51.582 ms   GC (mean ± σ):  0.00% ± 0.00%

        ▄▄▇█▆▃                  ▁                              
  ▃▄▄▆▆███████▆▅▅▃▄▃▄▁▄▃▃▃▄▅▅▆█▅█▅▆▃▁▃▃▁▃▃▃▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▃ ▃
  11.8 ms         Histogram: frequency by time         13.5 ms <

 Memory estimate: 4.12 KiB, allocs estimate: 124.

Topoplot series

Note that UnfoldMakie and MNE have different defaults for displaying topoplot series. UnfoldMakie in plot_topoplot averages over time samples. MNE in plot_topopmap displays single samples without averaging.

UnfoldMakie.jl

@benchmark begin
    plot_topoplotseries(
        df;
        bin_num = 50,
        positions = positions,
        axis = (; xlabel = "Time windows [s]"),
    )
end
BenchmarkTools.Trial: 3 samples with 1 evaluation per sample.
 Range (minmax):  1.839 s  1.861 s   GC (min … max): 3.35% … 5.13%
 Time  (median):     1.846 s               GC (median):    5.17%
 Time  (mean ± σ):   1.849 s ± 11.034 ms   GC (mean ± σ):  4.62% ± 1.10%

   ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁
  1.84 s         Histogram: frequency by time        1.86 s <

 Memory estimate: 386.48 MiB, allocs estimate: 5519586.

MNE

easycap_montage = PyMNE.channels.make_standard_montage("standard_1020")
ch_names = pyconvert(Vector{String}, easycap_montage.ch_names)[1:64]
info = PyMNE.create_info(PyList(ch_names), ch_types = "eeg", sfreq = 1)
info.set_montage(easycap_montage)
simulated_epochs = PyMNE.EvokedArray(Py(dat[:, :, 1]), info)

@benchmark simulated_epochs.plot_topomap(1:50)
BenchmarkTools.Trial: 6 samples with 1 evaluation per sample.
 Range (minmax):  689.554 ms   1.929 s   GC (min … max): 0.00% … 0.00%
 Time  (median):     690.743 ms                GC (median):    0.00%
 Time  (mean ± σ):   897.430 ms ± 505.385 ms   GC (mean ± σ):  0.00% ± 0.00%

                                                                
  ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅ ▁
  690 ms           Histogram: frequency by time          1.93 s <

 Memory estimate: 3.06 KiB, allocs estimate: 98.

MATLAB

Running MATLAB on a GitHub Action is not easy. So we benchmarked three consecutive executions (on a screenshot) on a server with an AMD EPYC 7452 32-core processor. Note that Github and the server we used for MATLAB benchmarking are two different computers, which can give different timing results.

Animation

The main advantage of Julia is the speed with which the figures are updated.

timestamps = range(1, 50, step = 1)
framerate = 50
50

UnfoldMakie with .gif

@benchmark begin
    f = Makie.Figure()
    dat_obs = Observable(dat[:, 1, 1])
    plot_topoplot!(f[1, 1], dat_obs, positions = positions)
    record(f, "topoplot_animation_UM.gif", timestamps; framerate = framerate) do t
        dat_obs[] = @view(dat[:, t, 1])
    end
end
BenchmarkTools.Trial: 2 samples with 1 evaluation per sample.
 Range (minmax):  3.907 s  4.004 s   GC (min … max): 0.44% … 1.61%
 Time  (median):     3.956 s               GC (median):    1.03%
 Time  (mean ± σ):   3.956 s ± 68.735 ms   GC (mean ± σ):  1.03% ± 0.83%

                              ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁
  3.91 s         Histogram: frequency by time           4 s <

 Memory estimate: 678.78 MiB, allocs estimate: 896288.

MNE with .gif

@benchmark begin
    fig, anim = simulated_epochs.animate_topomap(
        times = Py(timestamps),
        frame_rate = framerate,
        blit = false,
        image_interp = "cubic", # same as CloughTocher
    )
    anim.save("topomap_animation_mne.gif", writer = "ffmpeg", fps = framerate)
end
BenchmarkTools.Trial: 1 sample with 1 evaluation per sample.
 Single result which took 9.165 s (0.00% GC) to evaluate,
 with a memory estimate of 4.07 KiB, over 150 allocations.

Note, that due to some bugs in (probably) PythonCall topoplot is black and white.


This page was generated using Literate.jl.