using NeuroAnalyzer
using Statistics
NeuroAnalyzer.verbose = false
NeuroAnalyzer.progress_bar = false
const FILE = "files/eeg/ds004504_sub-044_eyesclosed.set"
const CH = ["Fp1", "Fp2", "F3", "F4", "C3", "C4", "P3", "P4", "O1", "O2",
"F7", "F8", "T3", "T4", "T5", "T6", "Fz", "Cz", "Pz"]
function workflow()
t = Dict{String, Float64}()
t["import"] = @elapsed eeg = import_set(FILE)
t["channels"] = @elapsed begin
keep_channel!(eeg; ch = CH)
load_locs!(eeg;
file_name = "files/locs/standard-10-20-cap19-elmiko.locs")
end
t["filter"] = @elapsed begin
# cutoff = -6 dB point (MNE: pass-band edge ± half of the transition band)
NeuroAnalyzer.filter!(eeg; ch = "eeg", fprototype = :fir,
ftype = :hp, cutoff = 0.25, bw = 0.5)
NeuroAnalyzer.filter!(eeg; ch = "eeg", fprototype = :fir,
ftype = :lp, cutoff = 50.625, bw = 11.25)
end
t["reference"] = @elapsed reference_avg!(eeg)
t["resample"] = @elapsed resample!(eeg; new_sr = 250)
t["epoch"] = @elapsed ep = epoch(eeg; ep_len = 2)
t["psd"] = @elapsed bp = band_power(ep; ch = "eeg", flim = (8, 12),
method = :welch,
wlen = 250, woverlap = 125)
# mean alpha power over epochs, μV²
return t, vec(mean(bp; dims = 2))
end
steps = ["import", "channels", "filter", "reference",
"resample", "epoch", "psd"]
t_first = @elapsed workflow() # includes compilation
times = Dict(s => Float64[] for s in steps)
for _ in 1:10
t, _ = workflow()
for s in steps
push!(times[s], t[s])
end
end
for s in steps
println(rpad(s, 10), round(median(times[s]); digits = 3))
end
println(rpad("total", 10),
round(sum(median(times[s]) for s in steps); digits = 3))
println(rpad("first run", 10), round(t_first; digits = 1))NeuroAnalyzer vs MNE
Resting-state EEG workflow
The same recording processed with the same steps in NeuroAnalyzer and MNE-Python, with filter and spectral parameters matched so that the two pipelines give the same numbers.
Data: ds004504_sub-044_eyesclosed.set: participant sub-044 (healthy control, 64 years, MMSE 30) of the OpenNeuro dataset ds004504 (Miltiadous et al., 2023, doi:10.3390/data8060095): a 14.7-minute eyes-closed resting-state recording from a clinical EEG system (Nihon Kohden), 19 channels of the 10–20 system at 500 Hz, EEGLAB format (CC0 license). The recording has a clear posterior alpha rhythm (spectral peak at 11 Hz) and strong 50 Hz line noise.
Workflow:
| Step | Operation |
|---|---|
| import | read the EEGLAB (.set) file |
| channels | select the 19 EEG channels, load the electrode locations |
| filter | FIR (Hamming-windowed sinc) high-pass 0.5 Hz and low-pass 45 Hz |
| reference | common average reference |
| resample | 500 → 250 Hz |
| epoch | 2-second epochs (440 epochs) |
| psd | Welch PSD per epoch (250-sample Hann windows, 50% overlap), alpha (8–12 Hz) band power |
The filters are the same in both libraries:
- transition bands of MNE’s defaults: 0.5 Hz for the 0.5 Hz high-pass and 11.25 Hz for the 45 Hz low-pass (both designs give 3301 and 147 taps);
- cut-off frequencies: MNE’s
l_freqandh_freqare the edges of the pass band, NeuroAnalyzer’scutoffis the middle of the transition band (-6 dB), so NeuroAnalyzer’s cut-offs are set to 0.5 - 0.5/2 = 0.25 Hz and 45 + 11.25/2 = 50.625 Hz; - direction: NeuroAnalyzer filters forward and backward (
dir = :twopass, the default), MNE withphase = "zero-double"; both are zero-phase, with the squared magnitude response of the filter.
Band power is the sum of the PSD bins in 8–12 Hz × the frequency resolution (1 Hz) in both libraries.
Each step is timed separately; the table shows medians of 10 runs, after a first (warm-up) run.
Run with julia -t auto (multi-threaded) or julia -t 1 (single thread).
import time
import numpy as np
import mne
mne.set_log_level("WARNING")
CH = ["Fp1", "Fp2", "F3", "F4", "C3", "C4", "P3", "P4", "O1", "O2",
"F7", "F8", "T3", "T4", "T5", "T6", "Fz", "Cz", "Pz"]
def workflow():
t = {}
t0 = time.perf_counter()
raw = mne.io.read_raw_eeglab("files/eeg/ds004504_sub-044_eyesclosed.set",
preload=True)
t["import"] = time.perf_counter() - t0
t0 = time.perf_counter()
raw.pick(CH)
raw.set_montage(mne.channels.read_custom_montage(
"files/locs/standard-10-20-cap19-elmiko.locs"))
t["channels"] = time.perf_counter() - t0
t0 = time.perf_counter()
# forward-backward filters, as in NeuroAnalyzer
raw.filter(0.5, None, fir_design="firwin", # transition 0.5 Hz
phase="zero-double")
raw.filter(None, 45.0, fir_design="firwin", # transition 11.25 Hz
phase="zero-double")
t["filter"] = time.perf_counter() - t0
t0 = time.perf_counter()
raw.set_eeg_reference("average")
t["reference"] = time.perf_counter() - t0
t0 = time.perf_counter()
raw.resample(250)
t["resample"] = time.perf_counter() - t0
t0 = time.perf_counter()
epochs = mne.make_fixed_length_epochs(raw, duration=2.0, preload=True)
t["epoch"] = time.perf_counter() - t0
t0 = time.perf_counter()
psd = epochs.compute_psd(method="welch", n_fft=250, n_per_seg=250,
n_overlap=125, window="hann")
p, f = psd.get_data(return_freqs=True)
band = (f >= 8) & (f <= 12)
bp = p[:, :, band].sum(axis=2) * (f[1] - f[0])
t["psd"] = time.perf_counter() - t0
# mean alpha power over epochs, V² → μV²
return t, bp.mean(axis=0) * 1e12
steps = ["import", "channels", "filter", "reference",
"resample", "epoch", "psd"]
t0 = time.perf_counter()
workflow()
t_first = time.perf_counter() - t0
times = {s: [] for s in steps}
for _ in range(10):
t, _ = workflow()
for s in steps:
times[s].append(t[s])
for s in steps:
print(f"{s:<10}{np.median(times[s]):.3f}")
print(f"{'total':<10}{sum(np.median(times[s]) for s in steps):.3f}")
print(f"{'first run':<10}{t_first:.1f}")Test system (both workflows): AMD Ryzen Threadripper 3960X (24 cores, 48 threads), Debian Linux (7.3.4 kernel); Julia 1.13.4, NeuroAnalyzer 0.26.12-dev; Python 3.14.10, MNE 1.13.2, MNE-NIRS 0.7.3, NumPy 2.5.5, SciPy 1.18.1.
Results (seconds, median of 10 runs):
| Step | NeuroAnalyzer (48 threads) | NeuroAnalyzer (1 thread) | MNE |
|---|---|---|---|
| import | 0.041 | 0.039 | 0.066 |
| channels | 0.001 | 0.001 | 0.014 |
| filter | 0.208 | 0.975 | 0.417 |
| reference | 0.033 | 0.130 | 0.041 |
| resample | 0.050 | 0.188 | 0.168 |
| epoch | 0.009 | 0.007 | 0.022 |
| psd | 0.235 | 0.198 | 1.387 |
| total | 0.578 | 1.538 | 2.113 |
| first run (incl. compilation) | 13.7 | 17.1 | 3.0 |
- Once compiled, the NeuroAnalyzer workflow is ≈ 3.7× faster than MNE with multiple threads and ≈ 1.4× faster on a single thread. Most of the difference comes from the PSD (MNE: ≈ 1.4 s, NeuroAnalyzer: ≈ 0.20–0.24 s).
- On a single thread, filtering takes ≈ 2.3× and re-referencing ≈ 3× longer than in MNE, and resampling about as long; with 48 threads, filtering is ≈ 2× and resampling ≈ 3× faster than in MNE (channels are processed in parallel) and re-referencing is as fast as in MNE (samples are processed in parallel). NeuroAnalyzer imports the EEGLAB file ≈ 1.7× faster.
- The PSD is calculated per epoch in parallel; for 440 short (2-s) epochs, the overhead of multithreading outweighs the gain.
- The first run in Julia includes just-in-time compilation of all the functions used (≈ 14–17 s), which is paid once per session. For a single file MNE is faster; for batch processing of many files, NeuroAnalyzer is faster.
Validation
Mean alpha power (μV²) per channel, averaged over epochs:
| Channel | NeuroAnalyzer | MNE | Ratio |
|---|---|---|---|
| Fp1 | 33.010 | 33.018 | 0.9998 |
| Fp2 | 46.374 | 46.383 | 0.9998 |
| F3 | 24.073 | 24.076 | 0.9999 |
| F4 | 46.234 | 46.237 | 0.9999 |
| C3 | 8.010 | 8.011 | 0.9998 |
| C4 | 13.681 | 13.683 | 0.9999 |
| P3 | 26.652 | 26.654 | 0.9999 |
| P4 | 18.929 | 18.934 | 0.9997 |
| O1 | 99.652 | 99.664 | 0.9999 |
| O2 | 85.406 | 85.422 | 0.9998 |
| F7 | 19.857 | 19.861 | 0.9998 |
| F8 | 32.500 | 32.504 | 0.9999 |
| T3 | 16.717 | 16.716 | 1.0000 |
| T4 | 18.638 | 18.638 | 1.0000 |
| T5 | 64.309 | 64.306 | 1.0000 |
| T6 | 33.069 | 33.073 | 0.9999 |
| Fz | 35.981 | 35.983 | 1.0000 |
| Cz | 18.010 | 18.010 | 1.0000 |
| Pz | 28.738 | 28.745 | 0.9998 |
The results agree within 0.03% (median ratio 0.9999, Pearson’s r = 1.000). The NeuroAnalyzer results are identical with 48 threads and with one thread. In a previous version of the scripts, the differences came from the filters, which were not matched:
- NeuroAnalyzer filtered in two passes (forward and backward,
dir = :twopass), so the magnitude response is squared; MNE applied the filter once and compensated its delay. Both are zero-phase, but the two-pass filter attenuates more in the transition bands. - NeuroAnalyzer’s
cutoffis the middle of the transition band, MNE’sh_freqthe edge of the pass band: withcutoff = 45the -6 dB point of NeuroAnalyzer’s low-pass filter is at 45 Hz, MNE’s at 50.6 Hz. - The recording contains strong 50 Hz line noise. With a nominal 45 Hz low-pass filter and the same transition band (11.25 Hz), MNE (single pass) keeps ≈ 36% of its power, NeuroAnalyzer < 0.01%. This does not affect the alpha band, but it should be considered when comparing broadband measures; a notch filter (see Power line noise) removes it in both libraries.
fNIRS workflow
The same NIRx recording processed with the same steps in NeuroAnalyzer and MNE-Python with MNE-NIRS, with parameters matched so that the two pipelines give the same numbers.
Data: the MNE fNIRS motor dataset (mne.datasets.fnirs_motor, participant 1): a 50-minute finger tapping experiment recorded with a NIRx NIRScout system, 28 source-detector pairs (8 sources, 16 detectors, 8 of them short-separation) at 760 and 850 nm (56 channels), 7.8125 Hz; 90 blocks of three conditions (1: control, 2: left-hand tapping, 3: right-hand tapping; 30 blocks each).
Workflow:
| Step | Operation |
|---|---|
| import | read the NIRx recording (raw light intensity) |
| od | intensity → optical density |
| sci | scalp coupling index of each pair (0.7–1.5 Hz) |
| tddr | motion correction: temporal derivative distribution repair (TDDR) |
| hb | modified Beer-Lambert law (partial path length factor 6) → HbO, HbR |
| glm | GLM of the unfiltered HbO and HbR: canonical HRF, 5-s blocks, cosine drift regressors (0.01 Hz), AR prewhitening |
| filter | FIR band-pass 0.05–0.7 Hz (transition bands 0.02 and 0.2 Hz), HbO and HbR |
| epoch | epochs -5…15 s around the blocks, baseline -5…0 s, average of each condition |
NeuroAnalyzer stores integer sampling rates only: the 7.8125 Hz recording is upsampled ×16 to 125 Hz when it is imported (exactly: every 16th sample is the original sample), so NeuroAnalyzer processes 16 times more samples than MNE. The filters are matched as in the EEG workflow (NeuroAnalyzer’s cut-offs: 0.05 - 0.02/2 = 0.04 Hz and 0.7 + 0.2/2 = 0.8 Hz; forward-backward filtering in both). MNE-NIRS prewhitens the GLM residuals with an AR(5) model, which spans 0.64 s at 7.8125 Hz; NeuroAnalyzer selects the AR order by BIC up to the same span (80 samples at 125 Hz).
Each step is timed separately; the table shows medians of 10 runs, after a first (warm-up) run.
using NeuroAnalyzer
using Statistics
NeuroAnalyzer.verbose = false
NeuroAnalyzer.progress_bar = false
const COND = ["1", "2", "3"]
function workflow()
t = Dict{String, Float64}()
t["import"] = @elapsed n = import_nirx("files/nirs/nirx_motor/NIRS-2019-11-02_002.hdr")
t["od"] = @elapsed intensity2od!(n)
t["sci"] = @elapsed sci = nirs_sci(n; flim = (0.7, 1.5))
t["tddr"] = @elapsed tddr!(n)
t["hb"] = @elapsed od2conc!(n; ppf = [6.0, 6.0])
hb = get_channel(n; type = ["nirs_hbo", "nirs_hbr"])
# GLM of the unfiltered signals: canonical HRF, 5-s blocks, cosine drift
# (0.01 Hz), AR prewhitening (order ≤ 80 = 0.64 s at 125 Hz, as AR(5) at 7.8 Hz)
t["glm"] = @elapsed glm = nirs_glm(n; ch = hb, cond = COND, duration = 5,
drift = 0.01, ar = 80)
# cutoff = -6 dB point (MNE: pass-band edge ± half of the transition band)
t["filter"] = @elapsed begin
NeuroAnalyzer.filter!(n; ch = hb, fprototype = :fir,
ftype = :hp, cutoff = 0.04, bw = 0.02)
NeuroAnalyzer.filter!(n; ch = hb, fprototype = :fir,
ftype = :lp, cutoff = 0.8, bw = 0.2)
end
# block averages -5…15 s, baseline -5…0 s
t["epoch"] = @elapsed avg = [
average_epochs(epoch(n; marker_value = c, offset = 5, ep_len = 20);
bl = (-5, 0))
for c in COND]
return t, (; sci, glm, avg)
end
steps = ["import", "od", "sci", "tddr", "hb", "glm", "filter", "epoch"]
t_first = @elapsed workflow() # includes compilation
times = Dict(s => Float64[] for s in steps)
for _ in 1:10
t, _ = workflow()
for s in steps
push!(times[s], t[s])
end
end
for s in steps
println(rpad(s, 10), round(median(times[s]); digits = 3))
end
println(rpad("total", 10),
round(sum(median(times[s]) for s in steps); digits = 3))
println(rpad("first run", 10), round(t_first; digits = 1))Run with julia -t auto (multi-threaded) or julia -t 1 (single thread).
import time
import numpy as np
import mne
from mne.preprocessing.nirs import (beer_lambert_law, optical_density,
scalp_coupling_index,
temporal_derivative_distribution_repair)
from mne_nirs.experimental_design import make_first_level_design_matrix
from mne_nirs.statistics import run_glm
mne.set_log_level("WARNING")
def workflow():
t = {}
t0 = time.perf_counter()
raw = mne.io.read_raw_nirx("files/nirs/nirx_motor", preload=True)
# tapping blocks last 5 s; marker 15 (start/end of the run) is not a condition
raw.annotations.set_durations(5)
raw.annotations.delete(np.where(raw.annotations.description == "15.0")[0])
t["import"] = time.perf_counter() - t0
t0 = time.perf_counter()
od = optical_density(raw)
t["od"] = time.perf_counter() - t0
t0 = time.perf_counter()
sci = scalp_coupling_index(od, l_freq=0.7, h_freq=1.5)
t["sci"] = time.perf_counter() - t0
t0 = time.perf_counter()
od = temporal_derivative_distribution_repair(od)
t["tddr"] = time.perf_counter() - t0
t0 = time.perf_counter()
hb = beer_lambert_law(od, ppf=6.0)
t["hb"] = time.perf_counter() - t0
# GLM of the unfiltered signals: canonical (SPM) HRF, 5-s blocks,
# cosine drift (0.01 Hz), AR(5) prewhitening
t0 = time.perf_counter()
dm = make_first_level_design_matrix(hb, drift_model="cosine", high_pass=0.01,
hrf_model="spm", stim_dur=5.0)
glm = run_glm(hb, dm, noise_model="ar5")
t["glm"] = time.perf_counter() - t0
# forward-backward filter, as in NeuroAnalyzer
t0 = time.perf_counter()
hb.filter(0.05, 0.7, l_trans_bandwidth=0.02, h_trans_bandwidth=0.2,
phase="zero-double")
t["filter"] = time.perf_counter() - t0
# block averages -5…15 s, baseline -5…0 s
t0 = time.perf_counter()
events, event_id = mne.events_from_annotations(hb)
epochs = mne.Epochs(hb, events, event_id=event_id, tmin=-5, tmax=15,
baseline=(None, 0), reject=None, preload=True)
avg = {c: epochs[c].average(picks="all") for c in event_id}
t["epoch"] = time.perf_counter() - t0
return t, (sci, glm, avg)
steps = ["import", "od", "sci", "tddr", "hb", "glm", "filter", "epoch"]
t0 = time.perf_counter()
workflow()
t_first = time.perf_counter() - t0
times = {s: [] for s in steps}
for _ in range(10):
t, _ = workflow()
for s in steps:
times[s].append(t[s])
for s in steps:
print(f"{s:<10}{np.median(times[s]):.3f}")
print(f"{'total':<10}{sum(np.median(times[s]) for s in steps):.3f}")
print(f"{'first run':<10}{t_first:.1f}")Results (seconds, median of 10 runs):
| Step | NeuroAnalyzer (48 threads) | NeuroAnalyzer (1 thread) | MNE |
|---|---|---|---|
| import | 2.129 | 2.559 | 0.835 |
| od | 0.865 | 0.854 | 0.018 |
| sci | 0.702 | 1.212 | 0.104 |
| tddr | 2.327 | 3.780 | 0.235 |
| hb | 1.515 | 1.877 | 0.015 |
| glm | 6.522 | 19.669 | 61.582 |
| filter | 0.939 | 2.460 | 0.043 |
| epoch | 0.850 | 1.119 | 0.031 |
| total | 15.850 | 33.531 | 62.861 |
| first run (incl. compilation) | 30.6 | 48.7 | 61.3 |
- The whole workflow is ≈ 4× faster in NeuroAnalyzer with multiple threads and ≈ 1.9× faster on a single thread. This is due to the GLM, which takes most of the time in MNE-NIRS (≈ 62 s for the AR(5) prewhitened GLM of 56 channels; NeuroAnalyzer: 6.5 s with 48 threads, 20 s on a single thread).
- All other steps are faster in MNE (≈ 1.3 s in total, NeuroAnalyzer: ≈ 9 s with 48 threads). NeuroAnalyzer processes 16 times more samples (the recording is upsampled to 125 Hz on import), which accounts for most of the difference.
- With the MNE recording also upsampled to 125 Hz after import (
raw.resample(125); import and resampling: 1.2 s), a single run of the MNE-NIRS GLM takes 232 s with AR(5) and 746 s with AR(80), which spans the same 0.64 s as the AR model of NeuroAnalyzer (NeuroAnalyzer: 6.5 s with 48 threads, 20 s on a single thread). - In MNE, the first run takes as long as the following ones (the GLM dominates); in NeuroAnalyzer, the first run includes ≈ 15 s of compilation.
Validation
The results of each step compared between the libraries (NeuroAnalyzer signals at the time points of the MNE samples; concentrations in μM in both):
| Step | Agreement |
|---|---|
| import | identical intensities (max. relative difference 3 × 10⁻¹⁵) and event times |
| od | max. relative difference 3 × 10⁻⁵ |
| sci | r = 0.9995, max. difference 0.057; pairs with SCI < 0.75: 5 (NeuroAnalyzer), 7 (MNE) |
| tddr | median relative difference 5% (max. 18%) |
| hb | r ≥ 0.98, SD ratio NeuroAnalyzer/MNE: median 0.984 (0.90–1.02) |
| filter | r: median 0.997 (min. 0.984), SD ratio: median 0.980 |
| epoch (block averages) | r: median 0.998–0.999 (tapping) and 0.999 (control); peak ratio 0.98–0.99 |
| glm (β, 28 pairs × 2 × 3 conditions) | r = 0.989, ratio NeuroAnalyzer/MNE: median 0.976; same sign in 156 of 168 |
- TDDR is applied to the derivative of the signal, which differs between the 125 Hz (upsampled) and the 7.8125 Hz signals; the differences propagate to the following steps.
- The scalp coupling index is calculated with different band-pass filters (NeuroAnalyzer: IIR, MNE: FIR), which gives slightly different values for pairs with poor coupling, near the 0.75 threshold.
- The GLMs differ in the AR model (order selected by BIC vs fixed AR(5)) and in the sampling of the regressors (125 Hz vs 7.8125 Hz). The 12 β values with different signs are small in both libraries (|β| ≤ 0.028 μM in NeuroAnalyzer and ≤ 0.016 μM in MNE; median |β| of all channels: 0.033 μM).