To download BrainKit, click here to go to its GitHub page. You can also see an earlier post which lays out some of the concepts behind BrainKit here.



Planning BrainKit started about a year ago when I was thinking about the question “now that putting together the hardware and software to make a relatively inexpensive device to stimulate the brain is basically a solved problem, what is the next major obstacle to the use of noninvasive brain stimulation?” The answer was (and still is) figuring out where in the brain to stimulate to achieve some desired effect.

BrainKit was inspired by this idea, a brain stimulator which also is capable of monitoring brain activity and using statistics to understand the neural correlates of mental states and design stimulation montages more intelligently. For instance, BrainKit can find brain regions that show different patterns of activity in fatigued and alert states—and then allow you to stimulate these regions to see if it affects alertness.

BrainKit was also designed to expand upon the capabilities of the aging OpenStim framework, and it offers a lot of new stimulation capabilities, specifically:

* The ability to deliver tDCS or tACS stimulation to 6 independently regulated electrodes (plus additional unregulated electrodes) simultaneously.

* Neurophysiological measurement based on head reactance (and possibly other methods) and sophisticated statistical processing to target brain regions for stimulation based on their association with some function.

* Fixed electrode locations based on the 10-20 system which allows brain regions to be targeted with a minimum of manual electrode placement

* Support for experimental design features including sham blinding, data logging, and automatic logging of cognitive and behavioral data in a standardized file format.



Like the OpenStim, it’s based on an Arduino board (the Arduino Leonardo) plus the AD5206BN10 chip.



I’m planning to write more about some of these (i.e. details on the neurophys stuff) in the coming weeks, but for now here is a basic guide on how to set up and use the device.

Setting up the hardware

BrainKit is a DIY device, which means you build it yourself (don’t worry, it has some self tests that can tell you if you did it right before it fries your brain). The hardware you’ll need consists of a laptop computer, and Arduino Leonardo, an AD5206BN10 chip,6 100 ohm resistors, some wire, electrodes, and a headset to fasten electrodes in.

BrainKit is unusual among tDCS devices, in that unlike most setups which are designed to be hooked up to electrodes strapped to arbitrary locations, it is designed to work with 6 electrodes that are fixed in specific places on a headset, at the 10-20 system locations F3,C3,O1,F4,C4,and O2. You can, of course, use it with electrodes that are in different locations, or even “loose”–but one of the major advantages is that by conforming to this location scheme you can easily cross-reference neurophysiological recording locations and stimulation locations, and use protocols that were written by other people (i.e. if someone sends you a configuration file, your device can use the default 10-20 locations to automatically target the correct areas without needing to manually place electrodes).

The first step in building the BrainKit device is assembling the 6 regulator “cells” that control each electrode. Each cell consists of three parts: a 100-ohm resistor connecting two of the Arduino pins a connection from one pin to the one of the AD5206’s “A” terminals, and a connection from one of the W terminals to the appropriate electrode.

Luckily the layout of the AD5206 is pretty simple. It has 6 internal cells (plus a few other connections used for it to get power and commands) so all that’s needed is to wire the Arduino’s output to the “A” terminal of the appropriate cell and the electrode to the “W” terminal of the same cell.

AD5206 layout. There’s a notch on the chip which indicates the side that is “up” in this schematic

An easy way to create the first part of each output is to simply trim a resistor’s leads to the same length, then solder an “output” wire to close to the resistor body on one end, leaving the ends of the leads free to slide into two adjacent holes on the Arduino’s header (this assumes your resistor leads are the right size to fit snugly in the header)

The BrainKit software requires that the cells and electrodes be connected in a specific way so that it can stimulate the correct locations on the head. In the default software, these mappings are:

Pin A0/A1 → AD5206 cell #1 → Electrode F3

Pin A2/A3 → AD5206 cell #2 → Electrode C3

Pin A4/A5 → AD5206 cell #3 → Electrode O1

Pin 4/6 → AD5206 cell #4 → Electrode F4

Pin 8/9 → AD5206 cell #5 → Electrode C4

Pin 10/12 → AD5206 cell #6 → Electrode O2

What’s with these pin numbers? We need to use pins with analog input capability to read the current values, and on the Leonardo these just happen to be laid out weirdly (the last three pairs are actually ‘supplemental” analog inputs despite not being on the analog header)

The other important part of setting this up is that to read the current properly, the resistor connecting each pair of pins must be placed between the lower numbered pin in that pair and the wire that goes to the AD5206. The opposite arrangement, with the lower numbered pin connected directly to the AD5206 with no resistor in between, will make the current regulation funky, which is not a good attribute for something you’re relying on to not fry your brain.

Once the electrodes are connected to the AD5206 (and from there to their appropriate pins on the Arduino) the next step is to make a few more connections that the AD5206 needs to get power and communications from the Arduino board, which are described here. Note that working with this board involves a few quirks: the chip select line must be wired to pin #2, and the master-out-slave-in and sck pins on the Leonardo are actually on the ICSP header (the row of 6 pins on the edge of the board). Note that the “SCK” pin is the one closest to the USB port.

The Arduino ICSP header. The only pins that need to be connected to the AD5206 are SCK and MOSI

Once these connections are made, it’s time to start it up!

Setting up the software

BrainKit has two components: the firmware which runs on the Arduino board and control software which runs on a laptop connected over USB.

Note:It’s important when using the device that it’s connected only to a laptop running on battery power. Mains power can be subject to electrical surges which could climb through the USB cable into your brain.



Installing the firmware can be done simply using the Arduino IDE (arduino.com), to open the brainkit_firmware.ino file and downloading it to the board.

The computer-side software is designed to run in Windows (other OSes have partial support but will not work completely) and requires Python 2.7 in addition to the following dependencies:

scipy-stack

easygui

pyserial

Once all of these programs are installed, the application can be launched simply by running “brainkit.py”.

Once the software is installed, it’s prudent to do a systems check to make sure everything is working. To do this, select the “run stimulator self tests” option and click “Regulator test”, then follow the prompts on the screen. The software will automatically verify that the device is capable of regulating current

Using the BrainKit device as a stimulator

The most straightforward way to use BrainKit is as a manually controlled stimulator. Like the OpenStim, BrainKit’s stimulation parameters are set using a configuration file, and it includes a way to create one “the “create protocol manually” option which will generate and save a configuration file.

While most of the parameters should be self-explanatory, there are some specific issues that pertain to multi-channel stimulation. The primary one of these is that since the current flowing through each electrode is independently regulated, current must be deliberately balanced. A protocol which enables C3 as an anode carrying 1 milliamp of current, for instance, will not do anything (in fact it will act as an open circuit) without at least one cathode configured to carry at least one mA. Similarly, if the anode is configured for 1.5 mA but the cathode is only set to 1 mA (or vice versa) only 1 mA will flow through the circuit. Current can be balanced across any collection of electrodes. (i.e. an anode can distribute 1 mA of current to two cathodes each carrying 0.5 mA).

What if you want to use an extracephalic electrode, say a cathode placed on the shoulder? While you could move one of the 6 regulated electrodes, there’s a simpler way to do this: using an “unregulated” electrode which is connected directly to the Arduino’s power system without passing through a regulator. As the name implies, it is not possible to control the amount of current passing through an unregulated electrode (it will absorb whatever current is available), but the total amount of current can still be controlled using the regulated electrodes. For instance, you can design a montage where two anodes over F3 and F4 each emit 0.5 mA of current, in which case the current at the unregulated cathode must be 1 mA.

The BrainKit has two attachment points for unregulated electrodes: an unregulated cathode can be attached to pin 3 while an unregulated anode should be attached to pin 13. (For DC stimulation this is essentially equivalent to attaching electrodes to the 5V and GND pins, however using GPIO pins allows the polarity to be switched during AC stimulation).

BrainKit also includes the ability to log protocols and attach “outcome measures” (i.e. scores on a game or brain training task) taken before and after stimulation to a data file associated with each protocol. When creating the protocol. You will be asked to add outcome measures which will be recorded each time the protocol is run.

A special type of outcome measure is the NPHYS measure, which tells BrainKit that it should record and log neurophysiological data either before, after, or before and after running the protocol (see the section on recording for more details)

Running protocols

Once a protocol has been created, it can be run by selecting the “Run protocol” option and the selecting the appropriate protocol file. BrainKit will verify the protocol, collect any pre-stimulation outcome measures, run the protocol, and then collect post-stimulation outcome measures. Data are logged in a file in the same directory as the brainkit.py file, whose name consists of the protocol named prefaced with “log-”

While stimulation or neurophys data recording is running, it can be aborted at any time by pressing the spacebar.

Voltage issues

Because the BrainKit device uses the Arduino’s logic level voltage (5V) directly for output, you may run into issues where the device is unable to generate enough voltage to reach a target current (typically if you are using an electrode placed off the head or the target current is greater than 2 milliamps).

To rectify this issue, it is necessary to reduce the circuit’s resistance. A few ways to do this include:

Increasing the saline concentration in the water. For optimal resistance the solution should be warm and saturated (no more salt can dissolve)

Increasing pressure on the electrodes.

“Kneading” electrodes into the skin to encourage better electrical contact through the hair.

Using custom built electrodes instead of commercial ones (some reports suggest that commercial electrodes may have higher internal resistance). A guide to building electrodes can be found here.

Additionally, if your montage includes a “return” electrode which is not intended to affect the brain, resistance can be decreased by increasing the number of electrodes acting as return electrodes (which effectively increases the area of this electrode)

Neurophysiology recording and analysis

Note: Although the neurophysiology components of BrainKit have been tested (some of the initial data can be found here and here, and I plan to publish some data on the updated algorithm soon) this component is still relatively novel and therefore should still be considered an experimental method of monitoring brain activation.



Background

The current neurophysiology data recording is based on detecting changes in the electrical reactance (resistance to a change in voltage) of the tissue underneath each electrode. In a typical setup, a headset with electrodes at each of the 6 10-20 positions is placed on the head, and a ground lead connected to pin 13 is placed on the left shoulder. Current pulses of a few microamps are then injected into the tissue from each electrode, while the electrical voltage is monitored. By measuring the rate at which the voltage increases, tissue reactance can be inferred.

While this is not a typical method of detecting brain activation, data previously collected show that scalp reactance appears to reflect local brain activity, with a time course suggesting that it detects changes in the local chemistry induced by the hemodynamic response (the same response used to localize brain activity in fMRI). This method also has the advantage of being implemented directly in the stimulator, with no need for additional hardware (in the future, this functionality might be implemented as a group of “plugins” to accommodate other neurophysiological sensing modalities like NIRS or EEG).

Although there appears to be a reliable event-related response of scalp reactance which indicates brain activation, event-related designs require carefully designed stimulus presentation and are generally not suited for capturing brain activity over a long duration or capturing resting and “default mode” activity. Therefore, BrainKit currently uses a blocked experimental model for all neurophysiological data recording, where an entire file is considered to be one “block”.

In contrast to event-related designs where brain activity can be measured as a change in signal amplitude from baseline, brain activity in a blocked design is computed by analyzing the root-mean-square amplitude of a low-pass filtered version of the reactance signal. This method has shown good validity in localizing brain activity (data forthcoming) and likely reflects the fact that the event-related responses observed also increase the RMS of the signal. Quantifying the signal via low-pass filtering and RMS, as opposed to simple amplitude, is necessary because the raw signal is highly vulnerable to slow drift, and the use of RMS allows for the signal’s amplitude to be measured over a period of minutes without the need to specifically define “baseline” or “measurement” periods.

Before computing RMS values for a block, BrainKit also employs a number of pre-processing steps. Data are resampled to a 10 Hz sampling rate (to correct for timing errors caused by delays during data recording) high-pass filtered above 0.05 Hz and then split into “epochs” of several seconds. Epochs containing unusually high “peaks” in data from any electrode are then rejected, as these peaks usually result from interruptions of the contact between the electrode and the subject and would distort RMS calculations. Finally, the mean RMS value of the signal in all of the non-rejected epochs is calculated for each electrode.

Statistics and experiments

The core philosophy of BrainKit is that candidate locations which can be stimulated to affect the performance of a task can be identified by identifying the brain regions in which natural trial-by-trial variation in activity explains natural variation in task performance.

The primary method used by BrainKit to identify these regions is to linear-correlate trial-by-trial activation values from each of the 6 electrodes with variables representing trial-by-trial performance on a task, and identify significant correlations with permutation-based significance testing, a form of non-parametric hypothesis testing that is relatively robust and involves limited assumptions compared to traditional parametric testing. BrainKit also supports experimental designs with “paired” neurophysiological measurements, which provide better power for experiments examining the effect of some manipulation (i.e. introduction of a drug) on the brain (here the t-value is computed for each electrode, with significance again computed with parametric testing)

The information that BrainKit uses to analyze this data is conveyed in an “experiment file”, with the following format, with the columns separated by tabs:

The first row in an experiment file must specify the names of all the variables, while the first column must always contain the neurophysiological measurement data for each trial. Subsequent columns contain the behavioral or cognitive variables recorded for that trial. (note that, in the case of an experiment with “paired” neurophysiological recordings, a variable can also be the name of another neurophysiological recording file). Neurophysiological files should be placed within the same directory as the experiment file. The experiment file should be saved as a normal text file.





Recording neurophysiological data

Neurophysiological data files can be recorded by selecting the “record data” option on the BrainKit main menu, as well as when running a protocol which includes NPHYS as an outcome measure.



To record data, all electrodes should be wet with saline and the headset should be attached in the same manner as for stimulation. An additional sponge electrode should be soaked in saline and connected to pin #13, then attached to the left arm (this acts as a ground connection).



Recording data is usually unnoticeable, but because it involves injecting very small amounts of pulsed current (far less than those encountered during stimulation) some subjects may notice a tingling sensation or see flashes of light (phosphenes) during recording.



Analyzing an experiment

Experiments are analyzed by selecting the “create protocol from experiment” option in the main menu, then selecting the appropriate experiment file. You will then be prompted to select the type of experiment (paired test or correlation) and the variable that you want to analyze (or, in the case of a paired test experiment, the two sets of recordings you want to compare).

Once the data are selected, you will be prompted to enter a few more options:

Critical p—Threshold for how confident BrainKit must be that an association is not due to coincidence. before it reports it. The default option (0.05) is a standard cutoff and means that BrainKit will only report an association if coincidence could only generate an association with equivalent or higher strength 5% of the time. This value can be decrease to require higher confidence (at the expense of missing some associations) or increased to better capture weak associations (at the expense of an increased number of false positives)

Use Bonferroni correction for multiple comparisons—This option allows you to correct for false positives introduced by performing the same statistical test on 6 electrodes. It is recommended to leave it disabled unless you are performing a very rigorous experiment with large amounts of data, as enabling it can cause weak associations to be missed.

Permutations per variable—controls the size of the baseline population in the permutation analysis. Larger values will improve the accuracy of p-values but also increase the time to compute results.

Epoch size(seconds)–When processing the data, each block will be split into chunks of this size. 10 seconds appears to be a reasonable length but may need to be adjusted in some situation (i.e. if the total recording length is less than 10 seconds)

Epoch rejection threshold­–Epochs will be rejected if the head reactance value at any point differs from the epoch average value by more than this much. Empirically, 0.5 appears to be a good, if conservative, criterion but you may need to adjust this up if the analysis fails (this often occurs when all the epochs in one dataset are rejected).

Once the analysis options are set, the analysis will run. If any significant correlations are found, they will be displayed and BrainKit will offer to use them as the basis to design a stimulation montage. If none are found, a “no correlations were significant” box will appear.



Bugs, updates, and help



While BrainKit has been subject to extensive testing, it’s possible that there may still remain undisocvered issues with something. If you’re having issues setting up or using a BrainKit device, the best place t get help is the tDCS subreddit, where I (as well as many other knowledgeble people) tend to hang out.



Wow! Thanks for reading all that, and I hope you find BrainKit useful.

-Nathan

