#!/usr/bin/env python # coding: utf-8 # # Analyze Electrooculography EOG data (eye blinks, saccades, etc.) # In[26]: # This is temporary to load the dev version of NK, needs to be removed once it's in master import os os.chdir('D:/Dropbox/RECHERCHE/N/NeuroKit/') # ----------------------- import numpy as np import pandas as pd import matplotlib.pyplot as plt import neurokit2 as nk plt.rcParams['figure.figsize'] = [14, 8] # Increase plot size # ## Explore the EOG signal # Let's load the example dataset corresponding to a vertical EOG signal. # In[47]: eog_signal = pd.read_csv("data/eog_100hz.csv") eog_signal = nk.as_vector(eog_signal) # Extract the only column as a vector nk.signal_plot(eog_signal) # Let's zoom in to some areas where clear blinks are present. # In[48]: nk.signal_plot(eog_signal[100:1700]) # ## Clean the signal # We can now filter the signal to remove some noise and trends. # In[49]: eog_cleaned = nk.eog_clean(eog_signal, sampling_rate=100, method='neurokit') # Let's visualize the same chunk and compare the clean version with the original signal. # In[50]: nk.signal_plot([eog_signal[100:1700], eog_cleaned[100:1700]]) # ## Detect and visualize eye blinks # We will nor run a peak detection algorithm to detect peaks location. # In[54]: blinks = nk.eog_findpeaks(eog_cleaned, sampling_rate=100, method="mne") blinks # In[77]: events = nk.epochs_create(eog_cleaned, blinks, sampling_rate=100, epochs_start=-0.3, epochs_end=0.7) # In[79]: data = nk.epochs_to_array(events) # Convert to 2D array data = nk.standardize(data) # Rescale so that all the blinks are on the same scale # Plot with their median (used here as a robust average) plt.plot(data, linewidth=0.4) plt.plot(np.median(data, axis=1), linewidth=3, linestyle='--', color="black") # In[ ]: