Daily tips and tricks from the experts at Adafruit!
View this email in your browser

Researching brainwave patterns requires working with large data sets. This post explains how we can quickly review a huge CSV file generated from the MUSE EEG headband using simple command line tools. Muse has recently released a new app for iOS and Windows 10 called Muse Direct which allows for easy recording, graphing and savings of EEG data to the their cloud. Once we have a recording we can download a CSV file and summarize the data.

After launching Muse Direct and doing a brief recording we can see a new file is available on the Muse Direct Cloud website. We can download five different formats, but today we will focus on just downloaded the CSV file as it requires the least amount of tools to work with.  The other formats have many dependencies on python libraries and Matlab (which starts at $150).

We will be using the csvkit to rapidly shape arrays into smaller datasets. This utility can be installed from the command line using:

$ pip install csvkit

Let’s get familiar with the data by checking out the first line of the file which names all the columns.

$ head -1 eeg.meditation.csv

timestamps,eeg_1,eeg_2,eeg_3,eeg_4,eeg_5,eeg_6,acc_1,acc_2,acc_3,gyro_1,gyro_2,gyro_3,batt_1,batt_2,batt_3,batt_4,drlref_1,drlref_2,mag_1,mag_2,mag_3,uv_1,uv_2,uv_3,press,temp,notch_filtered_eeg_1,notch_filtered_eeg_2,notch_filtered_eeg_3,notch_filtered_eeg_4,variance_eeg_1,variance_eeg_2,variance_eeg_3,variance_eeg_4,variance_notch_filtered_eeg_1,variance_notch_filtered_eeg_2,variance_notch_filtered_eeg_3,variance_notch_filtered_eeg_4,delta_absolute_1,delta_absolute_2,delta_absolute_3,delta_absolute_4,theta_absolute_1,theta_absolute_2,theta_absolute_3,theta_absolute_4,alpha_absolute_1,alpha_absolute_2,alpha_absolute_3,alpha_absolute_4,beta_absolute_1,beta_absolute_2,beta_absolute_3,beta_absolute_4,gamma_absolute_1,gamma_absolute_2,gamma_absolute_3,gamma_absolute_4,delta_relative_1,delta_relative_2,delta_relative_3,delta_relative_4,theta_relative_1,theta_relative_2,theta_relative_3,theta_relative_4,alpha_relative_1,alpha_relative_2,alpha_relative_3,alpha_relative_4,beta_relative_1,beta_relative_2,beta_relative_3,beta_relative_4,gamma_relative_1,gamma_relative_2,gamma_relative_3,gamma_relative_4,delta_session_score_1,delta_session_score_2,delta_session_score_3,delta_session_score_4,theta_session_score_1,theta_session_score_2,theta_session_score_3,theta_session_score_4,alpha_session_score_1,alpha_session_score_2,alpha_session_score_3,alpha_session_score_4,beta_session_score_1,beta_session_score_2,beta_session_score_3,beta_session_score_4,gamma_session_score_1,gamma_session_score_2,gamma_session_score_3,gamma_session_score_4,blink,jaw_clench,headband_on,is_good_1,is_good_2,is_good_3,is_good_4,hsi_precision,note,recorder_info,config,recording_notes

The csvstat  included with the csvkit utilities provides a powerful summary for each column complete with# ranges for the values. Since this five minute recording resulted in a 85MB file let’s limit our query to looking at the first 500 lines of the file.

$ head -500 eeg.meditation.csv | csvstat

1. “timestamps”

Type of data:Number

Contains null values:False

Unique values: 106

Smallest value:1,528,057,484.807

Largest value: 1,528,057,486.13

Sum: 762,500,685,266.869

Mean:1,528,057,485.505

Median:1,528,057,485.49

StDev: 0.36

Most common values:1,528,057,485.17 (24x)

1,528,057,485.27 (23x)

1,528,057,485.78 (23x)

1,528,057,485.88 (23x)

1,528,057,484.96 (21x)

2. “eeg_1”

Type of data:Boolean

Contains null values:True (excluded from calculations)

Unique values: 1

Most common values:None (499x)

3. “eeg_2”

Type of data:Boolean

Contains null values:True (excluded from calculations)

Unique values: 1

Most common values:None (499x)

One fun stat that EEGs are good at picking up is eye blinking. We can get a summary of how many times I blinked my eyes during this session by using the following commands. It looks as though I blinked 27x!

$ head -1 eeg.meditation.csv | csvstat | grep -i blink

100. “blink”

$ csvcut -c 100 eeg.meditation.csv | csvstat

1. “blink”

Type of data:Boolean

Contains null values:True (excluded from calculations)

Unique values: 3

Most common values:None (499928x)

False (3446x)

True (27x)

Row count: 503401

Was I clenching my jaw? That would show up too.

$ head -1 eeg.meditation.csv | csvstat | grep -i jaw_clench

101. “jaw_clench”

$ csvcut -c 101 eeg.meditation.csv | csvstat

1. “jaw_clench”

Type of data:Boolean

Contains null values:True (excluded from calculations)

Unique values: 3

Most common values:None (499928x)

False (3384x)

True (89x)

Row count: 503401

The absolute readings for each brainwave {delta, theta, alpha, beta and gamma} on all four electrodes is interesting. We can get a quick summary for every brainwave on each electrode to narrow down what part of our head different brainwaves map to and how strong on average they are.

$ head -1 eeg.meditation.csv | csvstat | grep -i absolute

40. “delta_absolute_1”

41. “delta_absolute_2”

42. “delta_absolute_3”

43. “delta_absolute_4”

44. “theta_absolute_1”

45. “theta_absolute_2”

46. “theta_absolute_3”

47. “theta_absolute_4”

48. “alpha_absolute_1”

49. “alpha_absolute_2”

50. “alpha_absolute_3”

51. “alpha_absolute_4”

52. “beta_absolute_1”

53. “beta_absolute_2”

54. “beta_absolute_3”

55. “beta_absolute_4”

56. “gamma_absolute_1”

57. “gamma_absolute_2”

58. “gamma_absolute_3”

59. “gamma_absolute_4”

It might be easier to work with a seperate file so we can create eeg.absolute.csv which only contains the columns we are interested in examining.

$ csvcut -c 40-59 eeg.meditation.csv > eeg.absolute.csv

$ csvstat –mean eeg.absolute.csv

  1. delta_absolute_1: 0.231

  2. delta_absolute_2: 0.384

  3. delta_absolute_3: 0.175

  4. delta_absolute_4: 0.213

  5. theta_absolute_1: 0.201

  6. theta_absolute_2: 0.028

  7. theta_absolute_3: -0.069

  8. theta_absolute_4: 0.087

  9. alpha_absolute_1: 0.796

10. alpha_absolute_2: 0.075

11. alpha_absolute_3: 0.174

12. alpha_absolute_4: 0.704

13. beta_absolute_1: 0.421

14. beta_absolute_2: -0.235

15. beta_absolute_3: 0.042

16. beta_absolute_4: 0.494

17. gamma_absolute_1: 0.219

18. gamma_absolute_2: -0.568

19. gamma_absolute_3: -0.046

20. gamma_absolute_4: 0.208