Home

Welcome to the Neurotech@Davis Wiki, your comprehensive resource for all things related to neurotechnology. Here you will find learning resources, documentation and guides.

What is Neurotech?

Neurotechnology, or Neurotech for short, is a dynamic and multidisciplinary field that harnesses the power of technology to study and interact with the brain and the nervous system. It encompasses a wide range of exciting areas, including:

  • Neuroimaging: Explore the world of brain imaging techniques, such as fMRI, EEG, and PET scans, that allow us to peer into the intricacies of the brain.
  • Brain-Computer Interfaces (BCIs): Learn about cutting-edge technologies that enable direct communication between the brain and external devices, opening up new possibilities for communication and control. Signal Processing: Dive into the world of signal processing, where we analyze and make sense of neural data to uncover hidden patterns and insights.
  • Machine Learning: Discover how machine learning and artificial intelligence are revolutionizing our understanding of the brain and its functions.
  • Deep Learning: Explore the role of deep learning in decoding brain signals and advancing neuroscientific research. Neuroethical Considerations: Delve into the ethical dimensions of neurotech, including issues related to privacy, consent, and responsible data use.

How to use this Wiki?

This wiki is designed to be a collaborative space where we can collect our resources and insights and share them with you. Here's how you can make the most of it:

  • Browse: Use the index on the left to navigate through the various pages. You will find sections within those pages making topics easier to find.
  • Contribute: Feel free to reach out to any Neurotech officer to contribute to this wiki, we are more than happy to let you contribute. There is always something to add in the Wiki as it is always a work in progress
  • Learn: Whether you're a beginner or an expert, you'll find articles that cater to your level of knowledge.

To clone this Wiki locally, enter the following command:

git clone https://github.com/Neurotech-Davis/Neurotech-Wiki.wiki.git

By Dhruv Sangamwar for Neurotech@Davis

Python is a versatile and popular programming language known for its simplicity and readability. In this tutorial, we will cover the basics of Python, including variables, data types, control structures, functions, and more.

Table of Contents

1. Getting Started

1.1. Installing Python

Before you can start programming in Python, you need to make sure you have a text-editor and the right environment.

MacOS

  • We personally recommend using Homebrew, a package manager that handles package download and installations
  • Follow the following commands to install , python and some libraries.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" : Installs Homebrew.
export PATH="/usr/local/opt/python/libexec/bin:$PATH" : This sets up the Path variable so that your command line can find python. The following commands will install python along with some libraries:

brew install python
python --version
pip install numpy
pip install seaborn
pip install matplotlib

We strongly reccomend that you use Visual Studio code as it has plenty of free plugins that make development in python easier.

If you want to use any additional packages, visit PyPi. This is the python package index where you can find installation instructions and documentation for packages. (Make sure to check which version you want before installation)

Windows

  • Environment setup has a couple more steps on Windows
  • Open command prompt as an administrator
  • Run wsl --install to install WSL2.
    • Restart your computer after the step.
  • At this point you should be able to run wsl in your command prompt which should start a linux vm.
    • If this is not the case please reach out to anyone on the projects division to debug platform related issues.
  • You can now use the same commands as above for MacOS to install python and all the related dependencies
  • Install Visual Studio code and run code . in your command prompt; code . should open up VScode where you can write all your python code.
    • WSL should detect that you are trying to open VScode, it will install the virtualized version of it automatically and open the desktop VScode client.

1.2. Running Python

You can run Python code in two ways: using the Python interactive interpreter or by creating Python scripts.

To start the Python interactive interpreter, open your terminal or command prompt and type:

python

You can also run Python scripts by creating a .py file and executing it with the python command.

# hello.py
print("Hello, Python!")

To run the script:

python hello.py

2.Variables and Data Types

In Python, you don't need to declare the data type of a variable explicitly. Python infers it based on the value assigned.

# Integer
x = 10

# Float
y = 3.14

# String
name = "Alice"

# Boolean
is_python_fun = True

# List
fruits = ["apple", "banana", "cherry"]

# Tuple
coordinates = (3, 4)

# Dictionary
person = {"name": "Bob", "age": 30}

3. Control Structures

Python provides various control structures like if, for, and while for managing program flow.

3.1. Conditional Statements

if condition:
    # Code to execute if the condition is True
elif another_condition:
    # Code to execute if the second condition is True
else:
    # Code to execute if none of the conditions are True

3.2. Loops

3.2.1. for Loop

for item in iterable:
    # Code to execute for each item in the iterable

3.2.2. while Loop

while condition:
    # Code to execute as long as the condition is True

4. Functions

Functions allow you to encapsulate code into reusable blocks.

def greet(name):
    """A simple greeting function."""
    print(f"Hello, {name}!")

# Calling the function
greet("Alice")\

5.Modules and Packages

Python has a rich ecosystem of modules and packages to extend its functionality. We installed some earlier if you followed the getting started section.

# Importing a module
import math
import numpy as np

# Using a module function
print(math.sqrt(16))

# using numpy
arr = np.array([[1, 2],
                [3, 4]])
print("Matrix: \n", arr)

6. File Handling

Python provides functions for reading from and writing to files.

# Opening a file for reading
with open("file.txt", "r") as file:
    content = file.read()

# Opening a file for writing
with open("new_file.txt", "w") as file:
    file.write("Hello, World!")

7. Error Handling

Use try and except blocks to handle errors gracefully.

try:
    # Code that might raise an exception
except ExceptionType:
    # Code to handle the exception
else:
    # Code to execute if no exception is raised
finally:
    # Code that always runs, regardless of exceptions

8. Object Oriented Programming

Python supports object-oriented programming (OOP) principles like classes and inheritance.

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        pass

class Dog(Animal):
    def speak(self):
        return f"{self.name} says Woof!"

# Creating objects
dog = Dog("Buddy")
print(dog.speak())

9. Conclusion

This Python tutorial covered the fundamental concepts of the Python programming language, including variables, data types, control structures, functions, modules, file handling, error handling, and object-oriented programming. Python is a versatile language with a vast ecosystem of libraries and frameworks, making it suitable for your Neurotech projects.

By Manik Sethi for Neurotech@Davis

Now that we've covered the basics of Python, we will dive into libraries which give us additional functionality. In this tutorial, we will cover NumPy and Pandas . To follow this tutorial, make sure you have Python installed on your computer and a text-editor ready to work with.

Table of Contents

GOAT STATUS

1. Getting Started

1.1. Making Sure You Have PIP

Since libraries are add-ons to the basic version of Python, we need to install them onto our machines. We will be using the package installer for Python, which is called pip. First, we will ensure that the machine you're working on has pip. To do so, open up a terminal window and type the following command:

MacOS python -m ensurepip --upgrade
Windows py -m ensurepip --upgrade

Typically, pip comes with the installation of Python in your system. But we will run these commands as a preventative measure for any errors down the line.

Now, we will install the packages onto our system. Enter the following terminal commands. 1

pip install numpy
pip install pandas

1.2. Importing Libraries

Now that we have our libraries installed, we can call upon them in. our .py files. To use the functionalities of our library in a given .py file, we type this at the very top.

import numpy as np
import pandas as pd

Let's break down these lines phrase by phrase

  • import followed by the library name tells our code to load in the library, allowing us to access its functions and variables.
  • Using the as keyword let's us shorten our library name and give it a nickname 2. That way, instead of having to call functions using numpy.function(), we can just do np.function()

2. NumPy

Now that we have imported NumPy, let's access and use it's functions and variables. For the sake of being concise we won't cover everything, but here is the documentation.

2.1. Array Fundamentals

Arrays will be incredibly useful for neurotech applications. All the brain data we will collect needs to be stored, and the np.array() datatype allows us to do so while also providing high functionality. Let's start by creating an array, which is just a list of objects.

Input:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
a
Output:
array([1, 2, 3, 4, 5, 6])

We used the np.array() function which takes in one argument: a list. However, this is just a one dimensional list, also known as a vector. In our use cases, it may be useful to have list of lists, also known as matrix. We can initialize one as shown below

matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

NumPy also comes with functions that return attributes of our array

  • matrix.ndim returns number of dimensions of the array. Since we made a 2D matrix, it'd be 2
  • matrix.shape returns the shape of our array, think of this as the length and width of our array, which is (3,3)
  • matrix.size tells you how many total elements exist in our array, which in our case is 9

If we used basic Python lists, we'd have to define these shape, size, and other functions on our own which becomes redundant quickly. NumPy takes care of this and lets us work on the important parts of the project.

2.2. Indexing and Slicing

The data we collect will also need to be manipulated. Thankfully, NumPy takes care of this. Sometimes we only want to work with specific parts of our data because of it's temporal importance (when it happened) or spatial importance (where it happened)

import numpy as np
...
data = np.array([[1, 7, 3, 9], 
				[2, 5, 0, 4], 
				[7, 3, 4, 1], 
				[8, 6, 0, 2]])
data[0]
data[2][1]
data[3]
data[5][0]

The integers inside the [] such as [0] represent the index of the element to retrieve. Since Python is 0-based, [0] is the first element in the array. Therefore, [2][1] is the first element of the second element! Here is a great resource for Indexing in Python. As practice, guess what the previous code snippet will return. The answer will be in the footnotes 3

Slicing lets us manipulate our matrix by cutting out specific parts. We will continue using the data variable from the previous example. If we want to access the first two objects inside data, we can write out the following line of code:

data[0:2]

the [0:2] lets our computer know we only want objects from the 0th index, up to the 2nd index (exclusive4). Here are the rules for slicing:

  • data[x:y] returns an object with elements from the xth index to yth index
  • data[x:] returns an object with all elements starting from the xth index
  • data[:y] returns an object with all elements up till the xth index

2.3. Reshaping and Flattening

Sometimes we need to reshape our data to execute certain operations on it. NumPy comes with a .reshape() function which takes in two arguments, the number of rows and columns

Input:
a = np.array([1, 2, 3, 4, 6])
b = a.reshape(3, 2)
Output:
array([1, 2, 3, 4, 5, 6])
array([0, 1], 
	  [2, 3], 
	  [4, 5] )

We reshaped our flat array a to have 3 rows and 2 columns. NumPy also has a function to go from an arbritary shape array back to a flat array.

Flattening our data gets rid of the dimensionality, and turns our higher dimensional data into a 1-D vector.

Input:
c = b.flatten()
Output:
array([1, 2, 3, 4, 5, 6])

No parameters are needed, since it will always turn the array back into a one dimensional list.

Reshaping and flattening are crucial operations for making sure our data is compatible with certain operations, such as matrix multiplication. If the dimensions don't match, we can reshape our data to fix it.

3. Pandas

Moving on, we will cover the basics of pandas. Once again, here is the link for the comprehensive documentation

3. 1. Dataframes

Pandas is used for two-dimensional tabular data, such as data stored in spreadsheets. The most common datatype we work with in pandas is called a DataFrame. Let's make one right now.

import pandas as pd
df = pd.DataFrame(
     {
         "Name": ["Avni", "Aryaman", "Sofia", "Manik", "Grace", "Priyal"],
         "Sex": ["female", "male", "female", "male", "female", "female"],
     }
 )

Here is a table of the current projects division board members. If we print it out, we would get the following |index|Name|Sex| |---|---|---| |0|Avni|female| |1|Aryaman|male| |2|Sofia|female| |3|Manik|male| |4|Grace|female| |5|Priyal|female|

Similar to NumPy, pandas has functions and indexing features allowing you to return rows and columns.

  • df["{column_name}"] returns the column corresponding to the column name
  • df.iloc[x:y] returns the rows ranging from index x to index y (exclusive)

3.2. Reading and Writing

Most likely, you will be creating dataframes with pre-existing csv files from data collection. Luckily, Pandas supports the ability to read from these files

data = pd.read_csv("directory/data.csv")

The variable data has a type DataFrame, which means all the previous functions we used can be applied here as well.

  • data.head(x) will display the first x rows of the dataframe
  • data.tail(x) does the opposite and displays the last x rows of the dataframe
  • data.shape returns the shape of our dataframe

To write out the tabular data we have manipulated, we can use the function df.to_csv("board_members.csv", sheet_name="board")

Here is a link for a more comprehensive guide towards dataframe indexing

4. Conclusion

This Python tutorial covered important libraries that will be relevant to your work in signal pre-processing and manipulation. Libraries such as NumPy and Pandas come with a vast amount of functionality, allowing users to focus on meaningful work, and generate results quickly.

1

sometimes python decides to be funny and requires you type pip3 instead of just pip. Please be mindful of what python version you have installed. If it's Python3, do the former. 2: When choosing a nickname, make sure it's intuitive as to what library it's referring. 3: [1 7 3 9], 3, [8 6 0 2], IndexError. An IndexError occurs because there is no value at the [5] index of our variable. 4: By exclusive, we mean that data[0:2] will not include the element at the 2nd index

EEG Data Processing

Write some intro stuff here??

What to do after collecting data?

After collecting EEG (electroencephalography) data in a neurotech project, there are several important steps you can take to process, analyze, and interpret the data effectively. These steps are:

  • Preprocessing the raw data
  • Feature Extraction
  • Classification

Why is signal processing necessary?

  • Noise Reduction and Artifact Removal: EEG signals are often contaminated with various types of noise, including muscle activity, eye blinks, electrical interference, and environmental noise. Signal processing techniques help to remove or reduce these artifacts, allowing researchers to focus on the brain's electrical activity of interest.
  • Enhancing Signal-to-Noise Ratio: EEG signals are relatively weak compared to the background noise. Signal processing methods can amplify the relevant brain signals while suppressing unwanted noise, thus improving the overall signal-to-noise ratio.
  • Feature Extraction: Signal processing allows researchers to extract meaningful features from EEG data that correspond to specific brain activities. These features can include frequency components, amplitude variations, and temporal patterns that provide insights into cognitive processes, mental states, and neurological conditions.
  • Frequency and Time-Frequency Analysis: Processing allows for the analysis of frequency components and changes over time. Brain activity is often associated with specific frequency bands, and time-frequency analysis helps identify when and where these frequency changes occur during different tasks or conditions.
  • Classification and Pattern Recognition: Processed EEG data can be used to train machine learning models for classification tasks. These models can differentiate between different cognitive states, emotions, or clinical conditions based on patterns present in the data.

What is Data Preprocessing?

Preprocessing refers to the procedure of removing noise and enhancing the signal of interest from the raw EEG data in order to get clean signals closer to the true neural activity. This helps transform raw data into a format that is suitable for analysis and extracting meaningful insights.

Why is Preprocessing Needed?

  • Signals picked up from the scalp are not an accurate representation of signals originating from the brain due to loss of spatial information.
  • EEG data is very noisy as artifacts such as eye movements, cardiac activity, or muscle movements can distort the data which can obscure weaker EEG signals.
  • It helps separate relevant neural activity from the random neural activity that occurs during EEG data recordings.

How does Preprocessing differ based on the analysis?

Before beginning to preprocess the data, it is important to choose an appropriate method of preprocessing. Some relevant questions to keep in mind while preprocessing data are:

  • What features of the EEG signal do you want to focus on? If you are planning on analyzing whether the brain is in a relaxed state, you would analyze the alpha waves between 8-12Hz.
  • What artifacts are present in the data? Which artifacts would you want to remove and which do you want to be aware of? Artifacts like jaw clenches, eye movements, and muscle movements might be considered noise in some circumstances but could be helpful in revealing important patterns.

Libraries used for Preprocessing:

  • MNE (Magnetoencephalography and Electroencephalography) is an open-source Python library focused on processing and analyzing EEG and MEG data, offering tools for data preprocessing, visualization, source localization, and statistical analysis.
  • Brainflow is an open-source library that provides a unified API for working with various EEG, EMG, and ECG devices, enabling developers to interact with neurophysiological data using a consistent interface.

Setting-Up the Environment

Before starting preprocessing import and install the dependencies.

import pandas as pd
import numpy as np
!pip install mne
import mne

Load the raw data and plot the data file

  • Import the raw EEG data from the recording equipment (OpenBCI data file) into your analysis software (Jupyter notebook) and plot the data.
  • Verify that the sampling rate is appropriate for your analysis. Common EEG sampling rates are 250 Hz or 500 Hz.
# Load CSV data using pandas
csv_file_path = '/content/drive/MyDrive/Sample/BrainFlow-RAW.csv'

# Load data from CSV into an array
trial_data = np.genfromtxt(csv_file_path)

# Declares channel names and types of each set of data
sfreq = 250  # sample rate in Hz
ch_names = ['Channel {}'.format(i) for i in range(trial_data.shape[1])]
ch_types = ['eeg' for i in range(trial_data.shape[1])]

# Create info structures and RawArray objects for each set of data
info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
raw = mne.io.RawArray(trial_data.T, info)

# Removing irrelevant channels
ch_names = [raw.ch_names]
ch_names_to_keep = [ch_names[0][1:9]]
raw = raw.pick_channels(ch_names_to_keep[0])

# Now you can work with the MNE Raw object
print(raw.info)
print(raw.get_data())

# Plot the specified interval
raw.plot(duration=200, scalings='auto')

Filtering

It is common to filter certain frequencies so that we can enhance the frequencies of interest.

High-Pass Filter:

  • A high-pass filter attenuates frequencies below a certain cutoff frequency and allows higher frequencies to pass through.
  • In EEG preprocessing, a high-pass filter is used to remove slow variations, DC offsets, and other low-frequency artifacts, such as electrode drift or baseline shifts.
  • Common cutoff frequencies for high-pass filters in EEG data preprocessing are around 0.5 Hz.
# Apply a high-pass filter with a cutoff frequency of 3 Hz
raw.filter(l_freq=3, h_freq=None)
raw.plot(duration=200, scalings='auto')

Low-Pass Filter:

  • A low-pass filter attenuates frequencies above a certain cutoff frequency and allows lower frequencies to pass through.
  • In EEG preprocessing, a low-pass filter helps remove high-frequency noise, muscle artifacts, and high-frequency components that are not relevant to the analysis.
  • Common cutoff frequencies for low-pass filters in EEG data preprocessing are typically around 40 Hz.
# Apply a low-pass filter with a cutoff frequency of 40 Hz
raw.filter(l_freq=None, h_freq=40)
raw.plot(duration=200, scalings='auto')

Band-Pass Filter:

  • A band-pass filter allows a specific range of frequencies, defined by a lower cutoff frequency and an upper cutoff frequency, to pass through while attenuating frequencies outside this range.
  • In EEG analysis, a band-pass filter is often used to isolate frequency bands of interest, such as alpha (8-13 Hz), beta (13-30 Hz), or gamma (30-100 Hz), which are associated with different cognitive processes.
  • Band-pass filters are useful for extracting features and patterns specific to certain frequency ranges.
# Apply a bandpass filter with cutoff frequencies of Alpha waves between 8 Hz (low) and 13 Hz (high)
raw.filter(l_freq=8, h_freq=13)
raw.plot(duration=200, scalings='auto')

Notch Filter (Band-Cut Filter):

  • A notch filter, also known as a band-cut filter that removes a single frequency.
  • Notch filters are used to remove specific sources of interference, such as power line noise (50 or 60 Hz) and their harmonics, which can contaminate EEG signals.
  • Notch filters help eliminate periodic noise sources that might be present due to electrical interference.
# Apply a notch filter to remove 60 Hz line noise
raw.notch_filter(60)
raw.plot_psd(fmin=2, fmax=80);

Artifact Removal and Correction

Artifacts can distort the EEG signal and interfere with accurate analysis and interpretation. There are various types of artifacts, including eye blinks, muscle movements, electrode noise, and external interference.

Artifact Detection:

  • Automatic Detection: Automated algorithms, such as independent component analysis (ICA), wavelet decomposition, or template matching, can identify components or segments that deviate significantly from the expected EEG patterns. These algorithms often require training data or templates to differentiate artifacts from brain activity.
  • Manual Detection: Visual inspection by experts involves reviewing EEG data to identify visually apparent artifacts, such as sharp spikes, slow drifts, or sudden jumps in signal amplitude.

Artifact Removal:

  • Independent Component Analysis (ICA): ICA is a widely used method that separates EEG data into independent components, some of which might represent artifacts. By manipulating these components, unwanted artifacts can be removed or minimized while preserving genuine brain-related components.
from mne.preprocessing import ICA
num_components = 6 #play around with this number to get components that seem to represent the actual brain activations well
ica = ICA(n_components=num_components, method='fastica')
ica.fit(raw)

raw.plot(scalings='auto')
  • Regression-Based Methods: Regression techniques can be used to model and remove artifacts by regressing out the artifact's contribution from the EEG signal. For instance, electrooculogram (EOG) channels can be used to regress out eye movement artifacts.
  • Interpolation: If an entire electrode or channel is affected by an artifact, interpolation techniques can be employed to estimate and replace the missing or distorted data based on neighboring electrodes.

Specialized Techniques:

  • Muscle Artifact Removal: Muscle artifacts can be removed by incorporating electromyogram (EMG) recordings or by applying high-pass filters to suppress low-frequency muscle activity.
  • Eye Blink Artifact Removal: Eye blink artifacts can be detected and corrected using EOG channels. These artifacts can be identified by the characteristic shape of EOG signals during eye movements.

Removing bad channels

EEG data might contain ‘bad’ channels that do not provide accurate information and it is important to remove them from the analysis. A channel might be excluded if:

  • The electrode was placed improperly or had poor contact with the scalp.
  • The channel malfunctioned.

By visualizing the raw data, we can identify ‘bad’ channels that have no signal or look noisier than other channels. After identifying the bad channels, we can exclude them from the analysis by creating a subset of channels marked as ‘bad’. An optional step would be to interpolate (fill in the missing data) by using the activity of surrounding channels to make an educated guess of activity at the bad channel.

# Mark channels as bad
bad_channels = ['Channel 2', 'Channel 6']  # List of channel names to mark as bad
raw.info['bads'] = bad_channels

# Remove bad channels from further analysis
raw.pick_types(eeg=True, exclude='bads')

# Plot the cleaned EEG data
raw.plot(scalings='auto')

Downsampling (optional)

Downsampling is the process of reducing the sampling rate of a signal by selecting and keeping only a subset of the original samples. This is typically done to reduce computational load, storage requirements, and to focus on specific frequency components of interest. Downsampling can also result in loss of high-frequency information and introduce aliasing effects if not performed carefully with appropriate anti-aliasing filters.

3.6 Re-referencing (optional)

Re-reference the data to a common point of reference if it was initially referenced to individual electrodes. References should be located as far away from the signal of interest (ie. Mastoid, earlobe)

What is epoching?

Epoching involves dividing continuous EEG data into smaller segments known as "epochs." Each epoch corresponds to a specific time interval of interest within the EEG recording. Epoching is a fundamental step that enables researchers to analyze brain activity in response to events, stimuli, or conditions.

Approaches to Epoching

  • Time-Based Epoching: In time-based epoching, EEG data is divided into fixed time intervals, regardless of specific events or stimuli. Epochs are created by splitting the continuous EEG signal into equal-duration segments, often referred to as "time windows." This approach is useful when you're interested in studying general patterns of brain activity over time, without focusing on specific events or stimuli. Time-based epoching can help capture long-term trends, such as changes in brain activity during different phases of a task or recording session.
  • Stimulation-Based Epoching: In simulation-based epoching, EEG data is segmented based on specific events or stimuli of interest. Epochs are defined around event markers that represent the occurrence of stimuli, tasks, or conditions. Each epoch captures the brain's response to a particular event, allowing researchers to analyze the neural processes associated with those events. Stimulation-based epoching is commonly used in event-related potential (ERP) studies, where researchers are interested in characterizing the brain's response to specific sensory, cognitive, or motor events.

What is feature extraction?

Feature extraction is a process in which meaningful and informative attributes or features are derived from raw data. In the context of EEG (electroencephalography) data analysis, feature extraction involves transforming the complex EEG signal into a set of representative features that capture essential characteristics of brain activity. These extracted features serve as inputs for further analysis, classification, and interpretation.

Why is feature extraction important?

  • Dimensionality Reduction: EEG data can consist of hundreds or thousands of data points (channels) collected over time. Extracting relevant features condenses this high-dimensional data into a smaller set of informative attributes, reducing computational complexity and memory requirements.
  • Information Compression: Feature extraction condenses raw EEG signals into meaningful representations that capture essential characteristics of brain activity. This compression retains important information while discarding noise and irrelevant details.
  • Machine Learning: Many machine learning algorithms work better with lower-dimensional feature sets. Well-chosen features can improve the performance of algorithms by providing them with more relevant information.
  • Statistical Significance: Extracted features can be used as input to statistical tests, making it easier to analyze and compare data between different conditions or groups. Depending on the research question, you can tailor feature extraction to focus on specific aspects of the EEG data, such as frequency content, temporal patterns, or connectivity.

Methods of feature extraction

  • Manual Feature Extraction: Manual feature extraction involves selecting and computing features from data based on domain knowledge and expertise. Researchers identify relevant attributes by understanding the underlying phenomena and selecting features that are informative for the analysis. This approach requires a deep understanding of the data and its context, as well as expertise in signal processing and neuroscience. Manual feature extraction can lead to highly interpretable and domain-specific features, but it can be time-consuming and might not capture all subtle patterns in complex data.
  • Automatic Feature Extraction: Automatic feature extraction employs algorithms to automatically identify relevant features from raw data. These algorithms learn from the data itself and capture patterns that might not be immediately apparent to humans. Techniques such as deep learning, wavelet transforms, and principal component analysis (PCA) are used to extract features without manual intervention. Automatic methods can efficiently handle high-dimensional data, uncover intricate patterns, and be more robust to noise. However, they might produce features that are less interpretable than those obtained through manual extraction.

Event Related Potential

Event-related potentials (ERPs) are small, rapid fluctuations in the electroencephalogram (EEG) signal that occur in response to specific sensory, cognitive, or motor events. ERPs are time-locked to the onset of these events and provide valuable insights into the brain's neural processing of different stimuli or conditions. ERPs are widely used in neuroscience, cognitive psychology, and clinical research to study the timing, amplitude, and topography of brain responses to various stimuli or tasks. Some examples of ERPs are:

  • P300 (Oddball Paradigm): In an oddball paradigm, participants are presented with a series of stimuli, most of which are common (frequent), and some are rare (infrequent or deviant). The P300 ERP is a positive deflection in the EEG waveform that occurs around 300 milliseconds after the presentation of a rare or target stimulus. P300 is associated with attention, cognitive processing, and the detection of significant or unexpected events.
  • N170 (Face Recognition): The N170 ERP is a negative deflection occurring around 170 milliseconds after the presentation of faces or other visually complex stimuli. N170 is particularly pronounced when participants are presented with faces compared to other object categories. It reflects early visual processing and is often associated with face perception and recognition.
  • Mismatch Negativity (MMN): MMN is a negative ERP component that occurs when participants are presented with an unexpected or deviant stimulus in a sequence of standard stimuli. It appears around 100-250 milliseconds after the presentation of the deviant stimulus. MMN is related to the brain's automatic detection and processing of auditory changes and can be used to study sensory memory and cognitive processes.
  • N400 (Language Processing): The N400 ERP is a negative deflection occurring around 400 milliseconds after the presentation of a semantically incongruent word or phrase within a sentence. It reflects semantic processing and is sensitive to violations in the meaning or context of language.
  • Contingent Negative Variation (CNV): CNV is a slow, negative-going potential that emerges during the anticipation of an upcoming event or stimulus. It is often used in studies involving temporal expectations and preparation for motor responses.
  • Error-Related Negativity (ERN): ERN is a negative deflection that occurs shortly after an individual makes an error during a cognitive task. It is thought to reflect the brain's detection of errors and is associated with conflict monitoring and response inhibition.

5.5 Feature Extraction Techniques

  • Principal component analysis, or PCA, is a dimensionality-reduction method that is often used to reduce the dimensionality of large data sets, by transforming a large set of variables into a smaller one that still contains most of the information in the large set.
  • ICA is a statistical procedure that splits a set of mixed signals to its sources without previous information on the nature of the signal. The only assumption involved in the ICA is that the unknown underlying sources are mutually independent in statistical terms. ICA assumes that the observed EEG signal is a mixture of several independent source signals coming from multiple cognitive activities or artifacts.
  • Locally linear embedding (LLE) seeks a lower-dimensional projection of the data which preserves distances within local neighborhoods. It can be thought of as a series of local Principal Component Analyses which are globally compared to find the best non-linear embedding.
  • t-SNE ( t-Distributed Stochastic Neighbor Embedding) is a technique that visualizes high dimensional data by giving each point a location in a two or three-dimensional map. The technique is the Stochastic Neighbor Embedding (SNE) variation that is much easier to optimize and produces significantly better visualization.

Fourier Transform

Fourier Transform is a mathematical technique used to analyze and transform a signal from its original time domain representation into its frequency domain representation. In the context of EEG (electroencephalography), the Fourier Transform is used to understand the frequency components present in the EEG signals, revealing information about the underlying neural activity.

  • Time Domain to Frequency Domain: EEG signals are originally recorded as a series of voltage measurements over time. The Fourier Transform converts these time-domain EEG signals into a representation that shows how much energy is present at different frequencies.
  • Components of Fourier Transform: The Fourier Transform decomposes the EEG signal into a sum of sinusoidal waves (sine and cosine functions) of different frequencies and amplitudes. Each component in the sum represents a specific frequency component within the original EEG signal.
  • Frequency Spectrum: The output of the Fourier Transform is a frequency spectrum, which is a plot of frequency on the x-axis and the corresponding magnitude or power of each frequency component on the y-axis. The spectrum shows which frequencies are present in the EEG signal and how strong they are.
  • Dominant Frequency Bands: EEG signals typically contain multiple frequency components, which can be categorized into different frequency bands: delta (0.5-4 Hz), theta (4-8 Hz), alpha (8-13 Hz), beta (13-30 Hz), and gamma (30-100 Hz). Analyzing the power distribution across these frequency bands can provide insights into the brain's activity during different cognitive states.
  • Power Spectral Density (PSD): The power spectral density is a measure of how the power of each frequency component in the EEG signal is distributed across the frequency spectrum. The PSD provides information about the relative strength of different frequency bands, allowing researchers to identify dominant frequencies and trends in brain activity.

What is a Classifier?

A classifier is a fundamental component in machine learning that's used to assign labels or categories to input data based on patterns and features present in the data. It's a model that learns from labeled examples in a training dataset and then predicts the labels of new, unseen data. The goal of a classifier is to generalize from the training data to accurately classify unknown instances.

How does a classifier work?

  • Training Phase: During the training phase, a classifier is presented with a labeled dataset where each data point is associated with a known category or label. The classifier analyzes the features of the training data and learns to recognize patterns that distinguish different classes.
  • Feature Extraction: Features extracted from the data are important inputs for the classifier. These features might be manually selected, automatically derived, or a combination of both.
  • Model Building: The classifier builds a model based on the relationships between the features and the corresponding labels in the training data. The model captures the decision boundaries or decision rules that separate different classes in the feature space.
  • Prediction Phase: Once the model is trained, it's used to predict the labels of new, unseen data that was not part of the training dataset. The classifier applies the learned decision rules to the feature vectors of the new data to make predictions.
  • Evaluation: The accuracy and performance of the classifier are evaluated using various metrics, such as accuracy, precision, recall, F1 score, and confusion matrices. The classifier's ability to generalize to new, unseen data is a key indicator of its effectiveness.
  • Classification Output: The output of the classifier is the predicted label or class for each input data point. The classifier assigns a label that it believes is most appropriate based on the patterns it learned during training.

Types of Classifiers

  • Decision Trees: Hierarchical tree-like structures that split data based on feature values.
  • Random Forests: Ensembles of decision trees that combine their predictions.
  • Support Vector Machines (SVM): Creates hyperplanes to separate data points of different classes.
  • K-Nearest Neighbors (KNN): Assigns a label to a data point based on the labels of its k-nearest neighbors.
  • Naive Bayes: Uses probabilistic models based on Bayes' theorem to predict labels.
  • Neural Networks: Complex models that consist of layers of interconnected nodes, known as neurons.

BCI Pipeline

Making cool connections...

By Priyal Patel for Neurotech@Davis

Introduction

What connects all the topics we have covered so far together?

the BCI Pipeline <3

What is a BCI?

A brain computer interface, which means some kind of project that incorporates brain signals in performing a task or predicting a result.

What is a Pipeline?

A way to represent how different components of a project are "connected" together.

How would you link components of a project together?

Using connectors, the special glue. Some modern day examples include bluetooth, usb, wires, electrode gel, python libraries, and so many more.

When researching connectors, two key questions to Google:

  1. How can [component x] communicate or link with [component y]?
    • see how other people have tried to link these components
  2. What are the limitations of using [connector z]?
    • see what problems people have had using this type of connector & their suggestions for alternative connectors

Usually there won't be a direct mention of a "BCI pipeline" in a research paper or open-source project; by the identifying the pipeline steps outline on the next page in these online sources, you can gain ideas for how people went about forming their pipelines.

By Priyal Patel for Neurotech@Davis

Keys Steps in our BCI Pipeline

Big Picture

bci_pipeline

Steps

  1. EEG (electroencephalogram) electrodes are placed on the scalp with electrode gel

Signal Processing Hardware (1) 6 41 06 PM

  1. The brain produces continuous electrical signals (analog signals) that are picked up from the scalp by the electrodes

  2. These analog signals collected from the electrodes are converted to digital signals (non-continuous & discrete values) by the signal processing hardware

Signal Processing Hardware (1) 6 41 06 PM Signal Processing Hardware (2) 6 41 06 PM

  1. Using bluetooth communication between the signal processing hardware and a bluetooth usb, the digital signals are passed to the hardware's UI program running

hardware

hardware
  1. These digital signals are displayed in by the hardware's UI program revealing wave like patterns

Signal Processing Hardware (7)

  1. The amplitude (height of the wave) of each wave measured in microvolts is collected per second and passed as input to the preprocessing program running

Signal Processing Hardware (8)

  1. The preprocessing program applies filters to remove uncharacteristic microvolt values and any additional key changes to the data occur in this step

  2. The cleaned data is passed as input to the algorithm or machine learning model to perform a task or predict an outcome

Signal Processing Hardware (9)

  1. Finally, the output from the algorithm or machine learning model is printed to display on the laptop: "Person is focused"

Signal Processing Hardware (10)

  1. This happens in real-time: each step can be occurring simulataneously as electrical signals are constantly being collected from the scalp

By Priyal Patel for Neurotech@Davis

Adding Hardware

Let's say you want to map the output from the algorithm or machine learning model to make a light flash or make an object move in a certain direction.

So how do you actually do that?

Using hardware ^_^

There are many different ways to accomplish these tasks, but one way we have used in the past is connecting an Arduino to motors, lights, or other pieces of hardware.

How does this work?

1- Create desired input and output mappings

Using a special software language, you can write an instruction set for the arduino to follow that details how to process the input it will receive and what to output. Using a wired usb connection, these instructions are loaded into the Arduino board.

2- Connect output of Arduino board to other hardware

Let's say we are using servo motors. There will be a wire connected to the arduino board to a bigger board and a wire from this board to the servo motor. Servo motors are special because they will rotate a certain amount based on the input received through a wire.

3- Send the output from the computer program to the Arduino board

Using a wired usb connection, the computer can send the output from an algorithm or ML model to the Arduino board. The input will be read and based on the Arduino's instructions a certain position will be sent through a wire to the servo motor.

4- Move an object by rotating the motor

Based on the input received, the servo motor will rotate to this position. There is another wire connected to the motor and another object; this will cause the object to move simultaneously while the motor is rotating.

By Priyal Patel for Neurotech@Davis

Example: Robotic Arm Pipeline

Project Repo

https://github.com/Neurotech-Davis/RoboticArm

Materials

  • Emotiv Insight Pro Headset
  • Laptop
  • Wires
  • Breadboard
  • Elegoo UNO R3 microcontroller
  • 3D Printed Robotic Arm Casing
  • Servo Motors
  • Batteries

Context

Emotiv has a pre-built mental command detection algorithm. To increase the accuracy of this algorithm for a particular participant, trainings were conducted and using these trainings the algorithm is able to form patterns in brain activity that match each mental command.

Trainings were conducted prior to assist the Emotiv's algorithm in detecting mental commands for our participant Grace Lim (Projects Member 2022-2024, Project's Board Co-Lead 2024-2025).

Node-Red Toolbox Pipeline

node-red-flow

How did we create our Pipeline?

1- Key Research Questions

What are the distinct components that we need to connect?

Data collected by the Emotiv headset, Emotiv's mental command detection algorithm, program to map mental commands to arm movement directions, microcontroller, and the robotic arm.

How to feed data collected by the Emotiv headset to Emotiv's mental command detection algorithm?

By examining Emotiv's website, we found two options: using Emotiv's API functions or using the Node-Red Toolbox. We first tried using the API, but needed special authorization information from paid features to access. Then, we pivoted to using the Node-Red Toolbox. The Emotiv website was missing some setup steps, so we leveraged video tutorials on youtube to assist us. After, we tested the software's functionality by mimicing examples we found online and were able to succesfully connect these two components.

How to map mental commands to arm movement directions?

We brainstormed and collectively agreed that the easiest way was to map the name of the command detected to a number; we choose to do this by creating python scripts that use serial connection communication to send the appropriate number to the microcontroller. Node-Red Toolbox enabled us to have a separate node for each command that actived when detected; directly after the related python script node would activate causing this script to run, sending a number to the microcontroller through a wired usb connection.

How to use the input the microcontroller will receive to make the robotic arm move?

A team member with hardware and circuits experience suggested using servo motors to achieve the rotating motion we desired for our robotic arm. We ran into the problem of the arm not being able to rotate enough and added extra batteries to power the motors. Using wired connections and a breadboard, we were able to connect a number for a mental command with a position for the servo motors.

2- Cost Analysis

How much do we want to spend on the project?

As little as possible...found a free online 3D cad design for the arm, used a team member's friend 3D printing to print for cheap, paid for servo motors, paid for batteries, used a team member's microcontroller, used a team member's wiring/breadboard kit, used free software programs, used our Emotiv headset we won at OpenBCI Spring 2023 competition, and picked a project that could hopefully be completed in time for the April 2024 Neurotech Conference competition.

Hardware Setup

hardwaresetup

Results

When everything connects, exciting things can happen...

Demo of the drop command with participant Grace

Extras

We are so excited to see all the cool ideas and amazing projects built by our fellow Neurotechies!

Neuotech@Davis has made a lot of neruotech projects over the past year. This article will go over the work we have done in the past as well as briefly touch on what exactly those projects did.

Dino Game

Developed by Prakhar Sinha, Maitri Khanna, Raymond Doan, Avni Bafna, Brian Nguyen, Nhu Bui, Ankita Chatterjee

The Dino Game was one of the first fully functional BCI projects that we developed. It was quite rudimentary but it was a major stepping stone for our club to build more interesting and complex BCI’s.

What it did

The Dino Game BCI was spawned out of the need to have a project to show off at the 2023 Davis Neurofest event. This was an event dedicated to showcasing UC Davis’s advancements in neurotechnology and Neuroscience. We needed to completely develop this project in less than 3 months.

After brainstorming, we came to the conclusion that making a BCI based on the famous Chrome “no-internet game” would be a crowd-pleaser and it would also be a project that wouldn’t be too difficult to develop. We chose the Dino Game, in particular, because it was a game that required very minimal inputs and therefore would be a good first project for us to develop in such a short amount of time.

The hardware we used was the Muse 2. Compared to the OpenBCI Cyton, the Muse 2 is a lot easier to set up and develop. Indeed, a big reason for making a lot of the decisions that we made on this project was due to time constraints. Following this logic, we opted to use the Muse 2 instead of the Muse for ease of development.

The Dino Game functioned as follows. On the “front-end”, the user was able to interact with a recreation of the Chrome “no-internet” game that we cloned off of GitHub. The way the user would interact with the game was by blinking their eyes in order to make the dino jump over obstacles. It was meant to be a simple, but powerful, demonstration of what BCI hardware, like the Muse 2, could accomplish.

Expanding the P300 Oddball Paradigm for a Versatile BCI System with Macro Command Input

Developed by Prakhar Sinha, Maitri Khanna, Jordan Ogbu Felix, Devia Dham, Aurielle Jocom, Mathangi Venkatakrishnan, Raymond Kang

The P300 Macro Speller was a really novel idea that was in development for the 2023 Neurotech California Conference. It utilized the P300 paradigm in order to simulate Vim commands.

What it did

The P300 macro speller was a project that was in development to present at the 2023 Neurotech California Conference. It posed this simple question: “what if, instead of having to manually switch modes in Vim, we could think about them to reduce development time?”. The development of this project was sadly met with a few problems but it was a very interesting idea we hope to explore further in later years.

This project is based on a well-established neurological phenomenon known as the P300 signal [could hyperlink to p300 article here]. This BCI would take advantage of these signals in order to detect when the user is thinking of a Vim command.

In their system, the stimuli are flashing lights presented on a screen, with the user focusing their attention on the desired stimulus to generate a P300 response. They expanded on this paradigm to allow for the selection of macros and commands, rather than just spelling out letters. This means that the user can input a range of commands, such as opening an application or initiating a specific action.

This project utilized the OpenBCI Cyton and Openvibe. Openvibe is meant to be an easy and accessible EEG recording and processing platform but it is also riddled with its own problems. The most prominent of these include many bugs while setting up and recording the EEG data. The most notable challenges faced by the group included having to wrestle with the Openvibe software to do what they wanted it to do.

Maximizing Learning Potential: An EEG-based Haptic Feedback BCI Solution for Improving Student Focus

Developed by Prakhar Sinha, Maitri Khanna, Jordan Ogbu Felix, Ayraman Bhatia, Grace Lim, Nhu Bui, Ramneek Chahil, Vraj Thakkar

The haptic feedback BCI is arguably Neurotech@Davis’s most successful and robust BCI to date. This BCI is especially notable for winning 3rd place in the 2023 NeurotechX competition and winning against the likes of UCLA, UC Berkeley, and Rice University.

What it does

This BCI was also a project that was in development to present at the 2023 Neurotech California Conference. Their BCI project utilized EEG technology to detect loss of focus in students during study and learning sessions. By monitoring the presence of common indicators of focus that consist of ratios of alpha, beta, and gamma waves, They were able to alert the user when their focus began to wane.

This BCI has utilized several different platforms/technologies: OpenBCI Cyton, Brainflow and Arduino. They were able to develop a simple yet effective solution to use haptic feedback so the user is alerted that their focus is wavering whether it may be towards the end of the session or during the session with a small vibration.

OpenBCI EEG hardware was used to collect samples of the brain waves which were used to detect focus. The placements of the electrodes consisted of the ground and reference electrodes, prefrontal cortex, frontal cortex, parietal cortex, and occipital cortex which helped aid in the detection of focus.

Controllerless Basketball Game using EEG-Based BCI: A Novel Approach for Virtual Reality Gaming

Developed by Prakhar Sinha, Maitri Khanna, Jordan Ogbu Felix, Adit Jain, Priyal Patel, Rytham D, Sasinathaya Aphichatphokhin, Tarun Devesetti

The haptic feedback was a big step for Neurotech@Davis as it was the first fully functional BCI using the OpenBCI Cyton board that we developed. This BCI is especially notable for winning 3rd place (out of 3 entrees) in the 2023 Neurotech California Conference.

What it does

This BCI started off incredibly ambitious but generally started to scale back as they realized what was and what was not possible given the skillset and development time that they did. Originally, the group had aimed to develop a Unity game in VR and using different neurological signals in order to perform different actions in the game. However, as deadlines inched closer and closer, we realized that we would need to cut some corners on our ambition in order for the project to be completed on time.

Their BCI project was centered around constructing a controller-less basketball game. The basic idea was for the user to shoot the ball by clenching their jaw and aligning the pointer with the basketball hoop. Utilizing EEG signals, the program was able to detect when a user clenches their jaw to trigger the shot in the game environment, while the duration of jaw-clenching controls the strength or distance of the shot. Later this was changed to just detecting jaw clenches in order to shoot the ball.

OpenBCI EEG hardware was used to collect samples of jaw clench data to identify the range of frequencies needed to train the model to detect the user's jaw clenching. The electrodes are placed above the motor cortex area of the brain, based on previous research that identified this area as being involved in jaw clenching.

Brainflow was used to parse the EEG signals and detect the desired stimuli in real-time. This BCI project offered an immersive and engaging gaming experience and was a really cool proof of concept to show what could be done with BCI’s. By using EEG signals to detect the user's jaw clenching, the game was able to respond in real time and it was a new and interesting look at the intersection between BCI’s and gaming.

Approach to Stress Detection and Alleviation using EEG-Based BCI with Targeted Music Therapy

Developed by Prakhar Sinha, Maitri Khanna, Jordan Ogbu Felix, Anisha Patel, Avni Bafna, Brian Nguyen, Dat Mai, Heidi Sailor, Nabeha Barkatullah

The development of this BCI was slow and steady but it ultimately yielded an innovative and robust result. It was developed for the 2023 Neurotech California Conference.

What it does

The main idea of this BCI was to improve the lives of individuals experiencing stress by providing appropriate responses to alleviate its negative effects. Through the utilization of cutting-edge technology, the project was able to detect stress by analyzing the size of alpha and beta activities in the frontal hemisphere and monitoring brain activity in the right frontal hemisphere. Once stress is detected, the BCI is designed to provide calming responses to the user. The team has identified the optimal frequency for focus and concentration to be 18.5 Hz, and we have utilized this frequency to create a calming music response to reduce stress levels.

The project's innovative approach to detecting and alleviating stress provided a unique solution to a common problem. By utilizing EEG signals to detect stress levels and providing real-time appropriate responses, the BCI project aimed to improve the quality of life for individuals who experience stress on a daily basis. The combination of technology and research used in this project provided an effective and efficient method to alleviate stress, which had the potential to be widely applicable in various settings such as educational institutions, workplaces, and healthcare facilities.

Some lectures may not have recording, those that don't will not have a link.

Lectures

Lecture 1 - Intro to Neurotechnology & BCIs | Recording

Lecture 2 - Intro to Neuroscience & EEGs | Recording

Lecture 3 - The BCI Pipeline | No Recording

Lecture 4 - Intro to Machine Learning | No Recording

Lecture 5 - EEG Signal Preprocessing | Recording

Lecture 6 - EEG Signal Processing | Recording

Workshops

Workshop 1 - OpenBCI & MUSE Demo

Workshop 2 - EEG Signal Analysis Workshop

This page will contain links to more learning resources on EEG, openBCI, etc.

Websites

The BCI Guys - Great resource!

NeuroTechX Edu - Great resource!

Neurotech@Davis Youtube Channel!

Intro to Neurotechnology - Berkeley course

Neuromatch Academy

Beginner’s Guide to Brain Computer Interfaces

Textbooks

Applied Event-Related Potential Data Analysis

Computational Cognitive Neuroscience, 4th Edition

More about EEG