The Brain Electrophysiological recording & STimulation (BEST) toolbox

Non-invasive brain stimulation (NIBS) experiments involve many recurring procedures that are not sufficiently standardized in the community. Given the diversity in experimental design and experience of the investigators, automated but yet flexible data collection and analysis tools are needed to increase objectivity, reliability, and reproducibility of NIBS experiments. The Brain Electrophysiological recording and STimulation (BEST) Toolbox is a MATLAB-based, open-source software with graphical user interface that allows users to design, run, and share freely configurable multi-protocol, multi-session NIBS studies, including transcranial magnetic, electric, and ultrasound stimulation (TMS, tES, TUS). Interfacing with a variety of recording and stimulation devices, the BEST toolbox analyzes EMG and EEG data, and configures stimulation parameters on-the-fly to facilitate closed-loop protocols and real-time applications. Its functionality is continuously expanded and includes e.g., TMS motor hotspot search, threshold estimation, motor evoked potential (MEP) and TMS-evoked EEG potential (TEP) measurements, dose-response curves, paired-pulse and dual-coil TMS, rTMS interventions.


Introduction
Non-Invasive brain stimulation (NIBS) has been used widely used in basic and clinical research for over 30 years. A growing number of stimulation techniques including transcranial magnetic, electric, and ultrasound stimulation (TMS, tES, TUS) as well as peripheral electric stimulation (PES), and a variety of specific measurement and intervention protocols have been developed. Nonetheless, many of the experimental procedures are not well standardized, compromising comparability and reproducibility across labs and contributing to replicability problems in the field. At the same time, the increasing combination of NIBS with noninvasive electrophysiological and neuroimaging measures, such as electromyography, electroencephalography, and functional magnetic resonance imaging (EMG, EEG, fMRI) [1], and the introduction of novel approaches, such as brain state-dependent brain stimulation [2] has led to increasing demands on NIBS users when designing and running experiments, often requiring customized hardware and software solutions. To increase the objectivity and reproducibility of NIBS studies, encourage open science, and support users with less technical expertise in running modern NIBS studies according to the highest standards, a powerful but easily accessible tool was needed. The Brain Electrophysiological recording and STimulation (BEST) Toolbox is open-source software with a graphical user interface (GUI) which enables its users to comfortably design and run experiments with multiple sessions and a variety of different stimulation and recording protocols in a flexible but standardized and automated manner. Experimental files can be easily saved and shared with colleagues or distributed together with publications. Interfacing with numerous recording and stimulation devices, the BEST toolbox can analyze and visualize incoming electrophysiological and behavioral data online and automatically adjust stimulation parameters on-the-fly to facilitate data quality monitoring and closed-loop and real-time applications.

Technical specifications
The BEST toolbox is an open-source cross-platform (Windows, Mac, Linux) application developed in MATLAB release 2017b with Java-MATLAB Interface utilities for its advanced GUI, interacting with other established MATLAB toolboxes such as FieldTrip [3], SPM [4], and MAGIC [5]. The BEST toolbox runs smoothly on a standard computer with 1.5 GHz dual-core processor and 8 GB RAM, allowing for~4 GB maximum array size and total memory to be used by MATLAB, which is sufficient for all operations of the BEST toolbox within the MATAB environment, due to its memory efficient object-oriented software architecture and trial-wise data processing procedures. Its low-and high-level functions can also be used in isolation in custom MATLAB scripts for maximal flexibility. The open-source repository is hosted at GitHub (github.com/umairhassan/best-toolbox) and released under the GNU General Public License (v3). A detailed technical documentation including software dependencies, hardware configuration, a user guide, and video tutorials is available at best-toolbox.org. Users are invited to file bug reports and request additional functionality via the GitHub repository page and sign up for a newsletter to be informed about bug fixes, new features, and usability surveys. Individual support is provided wherever possible.

Input/output interfaces & timing issues
The BEST toolbox interfaces not only with a variety of legacy hardware devices (Fig. 1A) that either provide input to (e.g., EMG and EEG systems, or button boxes) or receive outputs from the BEST toolbox (e.g., to TMS, tES, TUS, or PES devices), but also with opensource signal processing software and micro-controlling hardware, as well as dedicated real-time computer systems. The automatic evaluation of inputs resulting from previous output parameters and the possibility for iterative online parameter adjustments enables the BEST toolbox to provide closed-loop modes for a variety of protocols. In this section, we discuss currently supported input and output interfaces, the integration with neuronavigation, and the time complexity for running brain stimulation experiments with the BEST toolbox.

Inputs (recording)
The BEST toolbox integrates multiple APIs (Application Programming Interfaces) to receive time series data in a time-locked manner from different devices (Fig. 1) via the following options. The FieldTrip [3] real-time buffer can receive input from a large variety of sources, providing it to the BEST toolbox via a TCP/IP protocol, whether running on the same or a different kernel. Conversely, the control software of the widely used CED Power1401 or Micro1401 microcontroller units (CED, UK) provides data access if running on the same kernel as the BEST toolbox, and also responses from button boxes, keyboards, or other auxiliary devices can be collected via serial (COM) or parallel (LPT) connection(s) with the BEST toolbox host computer. Dedicated real-time systems with digital and analog input and output channels, such as the bossdevice (sync2brain, Germany), can receive and process digitized EEG and EMG data in real-time from specific EEG amplifiers such as NeurOne Tesla (Bittium, Finland) or actiCHamp Plus (Brain Products, Germany). Loading the bossdevice Simulink Real-Time© firmware on the same kernel as the BEST toolbox, it provides real-time access to raw and processed time series data via a MATLAB API, enabling quasi-continuous data-streaming for real-time applications (e.g., EEG-triggered TMS).

Outputs (stimulation)
The BEST toolbox uses multiple APIs for interfacing with numerous output devices ( Fig. 1) to remotely configure stimulation parameters (e.g., intensity, inter-stimulus intervals, etc.) and trigger the stimulation (single pulse or predefined stimulation sequence). The MAGIC toolbox ( [5]; https://github.com/nigelrogasch/MAGIC) is used to set stimulation parameters of TMS devices via a serial connection, currently supporting MagVenture (Denmark), MagStim (UK), and Duomag (Czech Republic) devices, and the NeuroFUSAPI (https://github.com/umair-hassan/neurofus-api) allows configuration of the NeuroFUS Pro TUS device (BrainBox, UK). Serial connections (COM, USB) are established directly with the BEST toolbox host computer, and TTL (RS232) trigger pulses can be sent via its parallel ports or dedicated I/O cards. Alternatively, open-source microcontrollers (e.g., Arduino, Raspberry-Pi) can be used as an effective low-cost solution to generate arbitrary serial control commands and TTL trigger pulses, facilitating customized solutions (e.g., remote control of a DS7A constant current stimulator (Digitimer, UK) by generating a pulse width modulation control signal for a stepper motor that mechanically turns the device's intensity dial). The bossdevice, connected to the BEST toolbox via a MATLAB API, uses a state-of-the-art FGPIO system to generate highprecision TTL pulses (or pulse sequences).

Neuronavigation
In addition to interfaces with recording and stimulation hardware, bidirectional network communication with MR-based frameless stereotactic neuronavigation systems (e.g., TMS-Navigator, Localite, Germany) provides the BEST toolbox with the current position of the navigated stimulation device (e.g., TMS coil or TUS transducer) for on-target control and trial-by-trial documentation of targeting success. It also enables the BEST toolbox to select from a list of pre-defined targets, guiding the user or a compatible robotic system (e.g., TMS-Cobot, Axilum, France) where to next position the stimulation device. This facilitates the development of semi-or fully-automated closed-loop hotspot searches (with algorithms evaluating the quality of the current target and intelligently determining the next one) as well as trial-by-trial intermingling of stimulation targets. Open-source systems (e.g., InVesalius Navigator [6]) will be included in future releases.

Time complexity
The overall time required for a single experimental trial controlled via the BEST toolbox is composed of the processing time of several software components (device firmware, APIs, operating system, and the BEST toolbox itself), each contributing its own time complexity (the execution time of an algorithm as a function of its input length), as well as certain hardware-specific delays. The timing accuracy required for an experiment depends on its specific aims, and the user has to provide a hardware setup matching this requirement, while the BEST toolbox is equally compatible with setups of high-accuracy (e.g., real-time EEG system and bossdevice) and low-accuracy (e.g., FieldTrip buffer and host computer serial/ parallel ports). On the input side, a key factor is the time required for providing BEST toolbox access to the digital time series data. It can be as short as~5e10 ms for UDP streaming from recording devices with real-time digital output (e.g., NeurOne Tesla or acti-CHamp Plus) via a real-time system (e.g., bossdvice) to the MATLAB instance running BEST toolbox. Note that for real-time targeting of fast and transient events (e.g., EEG oscillatory phase), relevant data processing must happen completely within the real-time system (receiving data already after~3 ms), while the BEST toolbox only conducts less time critical online analyses and results plotting. Conversely, data reaches the BEST toolbox after~200e500 ms for TCP/IP streaming via the FieldTrip buffer and~400e800 ms when retrieving data from a CED microcontroller. On the output side, considerable delays can be related to the preparation, transmission, and processing of serial commands for changing stimulation parameters or triggering the stimulator (e.g.,~300e500 ms for changing stimulation intensity but~3000e4000 ms for reversing current direction). Shorter delays are typically caused by sending TTL (RS232) pulses from a parallel port or I/O-card to a dedicated trigger input channel of the stimulation device (~10e20 ms). However, all processes running on the host computer of the BEST toolbox eventually depend on the performance of its hardware (CPU, graphics card, memory, etc.) and are subject to interference by other processes of the operating system, causing additional and variable delays. For time critical protocols, requiring high temporal precision of the pulse onset relative to an external event (e.g., realtime EEG-triggered TMS) or precise inter-pulse intervals (e.g., ppTMS or rTMS), triggers should better be generated by dedicated external hardware (e.g., open-source microcontroller or real-time bossdevice), reducing trigger-delays to the sub-millisecond range. To ensure inter-pulse precision, timing control can also be moved (partially or fully) to the stimulator (for some models) if no adequate output hardware is available to provide precise trigger timing. The time complexity of the BEST toolbox itself depends on the time required by the different processing steps in a given experimental protocol. The closed-loop MEP threshold hunting, for example, includes data gathering for the current trial (varying with e.g., number of channels, epoch length, sampling rate), data processing (e.g., filtering, re-referencing, baseline-correction, artifact rejection, calculation of MEP amplitude/latency), decision making (e.g., calculation of stimulation intensity for the next trial), output preparation (e.g., of serial command to change stimulation parameter configuration), plotting of updated result figures (e.g., time-locked waveform averages, thresholding traces), idling time (waiting for the remaining inter-trial interval, ITI), and eventually sending the TTL trigger for the next trial. These delays add up to (~500e1000 ms, excluding waiting time), limiting the minimum ITI to~1 s. For real-time EEG amplitude-/phase-triggered TMS, similar online analyses and plotting delays accumulate within the BEST toolbox to a minimum ITI of~1 s, even though real-time targeting itself (e.g., via the bossdevice) runs at a measurable loop delay of 3e6 ms, and in principle multiple targets can be stimulated within a BEST toolbox single trial. Note that all time complexity estimates provided are system-dependent, assuming a standard PC (8 GB RAM and 1.5 GHz dual-core processor) and graphics card with no other software running. However, even for the computationally most demanding protocols, ITIs of less than 2 s can usually be ensured.

Toolbox structure & function
The BEST toolbox is organized along the hierarchical structure of Experiments, which consist of multiple Sessions, which contain multiple Protocols, which are configured by setting multiple Parameters. The GUI (Fig. 1B) consists of a single window with tabs that not only allow to Load and Save Experiments, but also to open and close multiple resizable frames that give access to the highlevel functions (modules) Experimental Control, Protocol Design, Results, Hardware Configuration, General Settings, and Lab Book.

Experimental control
NIBS experiments often involve several stimulation protocols and repeated measurements, sometimes distributed across multiple sessions with identical or varying parameters. The Experimental Control module (Fig. 1B) allows the user to setup new Experiments, organize multiple Protocols into multiple Sessions and save the entire structure as a template. These templates can be loaded and individualized per participant, modified for another study, or shared with a colleague. After an automated plausibility check detecting missing entries and conflicting parameters, Protocols can be run, paused, resumed, and canceled at any time, and their completion status is provided. While raw data recorded by the commercial input systems is stored by default in their native file formats and remains untouched, any data processed by the BEST toolbox are incrementally stored to disc in FieldTrip data format. Results are stored within the Experiment files, making them readily available in combination with experimental design parameters.
Subject-specific Experiment files can be re-opened and continued at any time.

Protocol Design
The BEST toolbox offers a number of high-level functions to configure specific stimulation/recording Protocols. For this purpose, a Protocol is selected in the Experimental Control module from a drop-down menu and then parametrized according to the experimental aims using the Protocol Design module (Fig. 1B). Several standard protocols come largely preconfigured, and merely a few default values in the respective parameter fields merely need to be adjusted (e.g., stimulation intensity, inter-stimulus interval, number of trials, recording channels). However, one can also interactively design completely novel protocols with multiple randomized conditions. Specific parameters can be set per condition in table format or via a graphical editor, which illustrates the stimulation triggers for multiple devices together with the recording channels. Stimulation parameters can be defined in absolute and relative terms and linked to a specific outcome measure of Protocols scheduled earlier in the Experiment (e.g., individual resting or active motor threshold, RMT/AMT). Instead of stimulating at pre-defined randomized inter-trial intervals, most protocols can also be performed in a real-time brain-state dependent manner (e.g., targeting specific phase angles and amplitudes of a certain frequency band extracted from a customized spatial filter, as in real-time EEG-triggered TMS; e.g. [7,8]), or be synchronized to external triggers (e.g., MR volume triggers, as in concurrent TMS-fMRI [9]). Moreover, ad-hoc evaluation of the pre-TMS baseline allows for the automated rejection and replacement of bad trials (e.g., those with EMG muscle pre-innervation) already during the recording session. Custom MATLAB code (incl. functions from other MATLAB toolboxes) can be integrated as callback functions relative to a stimulus trigger, providing full flexibility when including, e.g., behavioral tasks.

Results
To allow online quality control and facilitate piloting, recorded data are processed and visualized on-the-fly by the Results module, with available analyses and plots depending on the respective Protocol (for example results see Fig. 2). Results can also be hidden for experimenter blinding. All results figures are saved and can be opened in separate windows for closer inspection. Reopening an Experiment and selecting a completed Protocol at a later time point will restore all results. Post-hoc offline data analysis functionality within the BEST toolbox is currently not provided.

Hardware configuration
Stimulation and recording devices vary across labs and experiments. The Hardware Configuration module (Fig. 1C) allows users to flexibly configure (e.g., name, ports, channels, etc.) multiple Input (stimulation) and Output (recording) devices, which are saved in the Experiment file and can later be selected from a drop-down menu in the Protocol Design module.

General Settings
The General Settings module allows users to set other Experiment-specific parameters, such directories for experimental configurations and data, as well as settings for Results figure saving, etc.

Lab Book
The Lab Book module automatically logs all significant events while conducting an Experiment (e.g., start and stop of recordings, pauses, successful completions or cancellations) together with a time stamp. It also allows the experimenter to add free text annotations at any time to document unusual events and experimental decisions within the Experiment file.

Protocol Functions
The Protocol Design module provides several high-level functions to configure standard NIBS Protocols (particularly for TMS- EMG), such as hotspots search, threshold estimation, stimulusresponse curves, excitability measurements (e.g., MEPs, ERPs, and TEPs), and both intra-and intercortical interactions (paired-pulse and dual-coil protocols). In the following, we briefly describe a few of the most frequently used Protocol Functions.

Hotspot search
Hotspot search protocols are required when the position and orientation of a stimulation device (e.g., TMS coil or PES electrode block) are adjusted iteratively to optimize the response of interest (e.g., MEP, TEP, motor/sensory perception) with respect to predefined criteria (e.g., amplitude, consistency, waveform shape, or sensory quality). Hotspot searches commonly require subjective decisions to be made by the experimenter evaluating responses and iteratively changing stimulation position manually until satisfied. Future releases will provide an objective closed-loop mode, commanding the user or a robotic positioning system where to next place the coil.

Threshold estimation
Motor or sensory thresholds can be estimated in a closed-loop manner. The protocol configures the stimulation intensity of the device, triggers the stimulation, acquires the response data (e.g., EMG or behavioral input), and runs a threshold estimation method (e.g., PEST [10] or maximum likelihood estimation [11]), to determine and set the next stimulation intensity (cf. RMT estimation in Fig. 2B). Threshold criteria are user-defined (e.g., 50 mV for RMT and 200 mV for AMT) and the above described automated pre-TMS evaluation and trial rejection function together with a maximal muscle contraction EMG calibration run allows to ensure that only trials with the desired pre-contraction are included (e.g., <50 mV for RMT or 10e30% of maximal contraction for AMT).

MEP stimulus-response curve
Stimulus-Response curves are a common means to characterize the relationship between stimulation and response parameters (e.g., TMS intensity and MEP amplitude). The BEST toolbox fits a Boltzmann sigmoid function [12] to the data and provides its threshold, inflection point, and plateau (Fig. 2C). Respective curves can also be generated for paired-pulse and dual-stimulator protocols (e.g., SICI, SICF, LICI, ICF, IHI, SAI, and PAS), systematically varying the intensity of test or conditioning stimulus, interstimulus interval (ISI), or inter-trial interval (ITI), and determining the optimal parameter which produces 50% of maximum test pulse modulation.

MEP measurement
MEPs can be measured from multiple muscles, with peak-topeak amplitudes and latencies evaluated and stored on a trial-bytrial basis, and descriptive measures (e.g., mean, median, SD, SEM) being provided (cf. Fig. 2A). Paired-pulse and dual-stimulator protocols can also be created with this function.

TEP measurement
TEPs can be measured from multi-channel EEG, visualizing single-trial and averaged time-locked EEG data for specific montages as well as the topographies of different TEP components from specific time windows of interest (Fig. 2E).

rTMS interventions
Repetitive TMS (rTMS) interventions can be flexibly designed for conducting established fixed frequency rTMS (e.g., 1 or 5 Hz) or patterned protocols (e.g., theta bursts stimulation) as well as novel protocols with arbitrary pulse sequences. Depending on the timing accuracy of the trigger output hardware and the configuration possibilities provided by the stimulator, the user can choose whether single pulses, partial burst/trains, or entire pulse sequences are released by the stimulator upon a single trigger.

ERP measurement
Generic ERPs can be recorded from multi-channel EEG, visualizing the averaged time-locked responses for specific montages and topographical maps for different time windows of interest. Amplitude and latency of ERP components (e.g., N20 latency of the somatosensory evoked potential, SEP) are automatically extracted and provided to subsequent protocols (e.g., SAI, PAS, etc.).

Resting-state EEG measurement
For resting-state EEG measurements, a few minutes of multichannel continuous EEG data are recorded. Power spectral densities are estimated using multi-tapered windowed fast Fourier transforms via FieldTrip [3], and 1/f correction can be performed using IRASA [13] or FOOOF [14] (the latter via FieldTrip and BrainStorm toolboxes). Original and 1/f-corrected power spectra are visualized, and the individual peak frequency and signal-tonoise-ratio (SNR) are provided (Fig. 2D) for a pre-defined target oscillation to support real-time EEG-triggered TMS protocols.

Concurrent TMS-fMRI
To provide the temporally precise interleaving of TMS pulses and fMRI volume acquisition required for concurrent TMS-fMRI ( [8]), the BEST toolbox controls an external microcontroller and LM555 Timer Integrated Circuit (http://best-toolbox.org/14_ TMSfMRIMeasurement), which receives MR volume triggers and triggers the TMS machine when applicable.

TUS parameter planning
The BEST toolbox supports the user in determining and correctly setting the multiple required and interdependent sonication parameters for focused transcranial ultrasound (TUS) experiments and configures the NeuroFUS Pro system (BrainBox, UK) accordingly.

Conclusion & outlook
The BEST toolbox provides a powerful and easily accessible tool for researchers and clinicians to design, run, and perform online analysis of state-of-the-art multimodal NIBS studies. It is designed to promote objectivity, reproducibility, and transparency in NIBS research, raise the standards for experimental control and data quality, and facilitate the use of real-time and closed-loop applications, e.g., for brain state-dependent brain stimulation. The BEST toolbox functionality is continuously expanded, and future releases will provide full integration with (robotized) MR-based neuronavigation to fully automate hotspot search and mapping studies.

CRediT authorship contribution statement
Umair Hassan: Conceptualization, the toolbox, designed and developed the toolbox, tested the toolbox, wrote the manuscript. Steven Pillen: tested the toolbox. Christoph Zrenner: tested the toolbox. Til Ole Bergmann: Conceptualization, the toolbox, tested the toolbox, wrote the manuscript, all authors jointly discussed and revised the manuscript.

Declaration of competing interest
U.H. is head of software development at sync2brain GmbH, Germany. C.Z. has an interest in sync2brain GmbH. The other authors declare no conflict of interest.