Skip to content

Instantly share code, notes, and snippets.

@MexsonFernandes
Created April 5, 2019 13:38
Show Gist options
  • Save MexsonFernandes/ae332b7575b147c206f74971a4089d09 to your computer and use it in GitHub Desktop.
Save MexsonFernandes/ae332b7575b147c206f74971a4089d09 to your computer and use it in GitHub Desktop.
Python code for data collection from Neurosky Mindwave Mobile headset device
import numpy as np
import pandas as pd
import sys
import json
import time
from telnetlib import Telnet
# Initializing the arrays required to store the data.
attention_values = np.array([])
meditation_values = np.array([])
delta_values = np.array([])
theta_values = np.array([])
lowAlpha_values = np.array([])
highAlpha_values = np.array([])
lowBeta_values = np.array([])
highBeta_values = np.array([])
lowGamma_values = np.array([])
highGamma_values = np.array([])
blinkStrength_values = np.array([])
time_array = np.array([])
tn=Telnet('localhost',13854);
start=time.clock();
i=0;
tn.write('{"enableRawOutput": true, "format": "Json"}');
outfile="null";
if len(sys.argv)>1:
outfile=sys.argv[len(sys.argv)-1];
outfptr=open(outfile,'w');
eSenseDict={'attention':0, 'meditation':0};
waveDict={'lowGamma':0, 'highGamma':0, 'highAlpha':0, 'delta':0, 'highBeta':0, 'lowAlpha':0, 'lowBeta':0, 'theta':0};
signalLevel=0;
while time.clock() - start < 30:
blinkStrength=0;
line=tn.read_until('\r');
if len(line) > 20:
timediff=time.clock()-start;
dict=json.loads(str(line));
if "poorSignalLevel" in dict:
signalLevel=dict['poorSignalLevel'];
if "blinkStrength" in dict:
blinkStrength=dict['blinkStrength'];
if "eegPower" in dict:
waveDict=dict['eegPower'];
eSenseDict=dict['eSense'];
outputstr=str(timediff)+ ", "+ str(signalLevel)+", "+str(blinkStrength)+", " + str(eSenseDict['attention']) + ", " + str(eSenseDict['meditation']) + ", "+str(waveDict['lowGamma'])+", " + str(waveDict['highGamma'])+", "+ str(waveDict['highAlpha'])+", "+str(waveDict['delta'])+", "+ str(waveDict['highBeta'])+", "+str(waveDict['lowAlpha'])+", "+str(waveDict['lowBeta'])+ ", "+str(waveDict['theta']);
time_array = np.append(time_array, [timediff]);
blinkStrength_values = np.append(blinkStrength_values, [blinkStrength]);
lowGamma_values = np.append(lowGamma_values, [waveDict['lowGamma']]);
highGamma_values = np.append(highGamma_values, [waveDict['highGamma']]);
highAlpha_values = np.append(highAlpha_values, [waveDict['highAlpha']]);
delta_values = np.append(delta_values, [waveDict['delta']]);
lowBeta_values = np.append(lowBeta_values, [waveDict['lowBeta']]);
highBeta_values = np.append(highBeta_values, [waveDict['highBeta']]);
theta_values = np.append(theta_values, [waveDict['theta']]);
lowAlpha_values = np.append(lowAlpha_values, [waveDict['lowAlpha']]);
attention_values = np.append(attention_values, [eSenseDict['attention']]);
meditation_values = np.append(meditation_values, [eSenseDict['meditation']]);
print (outputstr);
if outfile!="null":
outfptr.write(outputstr+"\n");
person_name = input('Enter the name of the person: ')
blink_label = input('Enter left or right eye blink(1 for left, 2 for right): ')
time_starting = input('When does TGC start: ')
lefty_righty = input('Is the person left-handed or right-handed: ')
time_blinking = input('The time of the blink: ')
# Data Recorded for a single person
data_row = pd.DataFrame({'Name': person_name, 'attention': [attention_values], 'meditation': [meditation_values], 'delta': [delta_values], 'theta': [theta_values], 'lowAlpha': [lowAlpha_values], 'highAlpha': [highAlpha_values], 'lowBeta': [lowBeta_values], 'highBeta': [highBeta_values],
'lowGamma':[lowGamma_values] , 'highGamma': [highGamma_values], 'blinkStrength': [blinkStrength_values], 'time': [time_array], 'LOR': blink_label})
# Reading the data stored till now
dataset = pd.read_csv('data_eeg.csv')
from numpy import nan as Nan
dataset = dataset.append(pd.Series([blink_label, person_name, [attention_values], [blinkStrength_values], [delta_values]
, [highAlpha_values], [highBeta_values], [highGamma_values], [lowAlpha_values], [lowBeta_values], [lowGamma_values], [meditation_values],
[theta_values], [time_array], time_starting, lefty_righty, time_blinking], index=['LOR', 'Name', 'attention', 'blinkStrength', 'delta', 'highAlpha', 'highBeta', 'highGamma', 'lowAlpha', 'lowBeta', 'lowGamma', 'meditation', 'theta', 'time', 'time_start', 'LTYRTY', 'time_blink']), ignore_index = True)
#Appending and storing the data in the same csv
#dataset.append(data_row)
dataset.to_csv('data_eeg.csv')
tn.close();
#outfptr.close();
@urmi2927
Copy link

urmi2927 commented Sep 4, 2022

Does anyone solve this error?
image

@gmierz
Copy link

gmierz commented Sep 6, 2022

@urmi2927 change '\r' to b'\r'

@MahdiAlnaimi
Copy link

can you explain this line please
tn=Telnet('localhost',13854);

@gmierz
Copy link

gmierz commented Nov 30, 2022

That's the telnet connection used to communicate data between the device and your computer. I suggest using this package so you don't get bogged down by these details: https://github.com/gmierz/mindwave-lsl

@MahdiAlnaimi
Copy link

I tried this link thank you for shearing it, but i still not understand how can it work and how I can connect this app with my headset? is there any information should I add it? i hope someone help me in this Issue

@gmierz
Copy link

gmierz commented Dec 15, 2022

You need to get the Thinkgear connector working first, then that'll provide you with a host and port to use in the package above (see the help section of the command).

@MahdiAlnaimi
Copy link

WHEN i trien the modul I get an error in related with pyxdf

@Deepucdactvm
Copy link

is it possible to get raw eeg data

@gmierz
Copy link

gmierz commented Jul 13, 2023

@mwahid2001
Copy link

is it possible to enable raw eeg data from mindwave neurosky 1, the headset with RF dongle connecting through usb port? and if how can i do that?

@Deepucdactvm
Copy link

Yes, but you need to make sure you do a write like this to enable it: https://github.com/gmierz/mindwave-lsl/blob/21827fe80f034e0a1d6b5da6b9165c6d0574ac23/test_mouse_movements.py#L19

but I tried it is not possible to get raw EEG data

@Deepucdactvm
Copy link

I'm using another single electrode FT$S mind link EEG Headband, is it any method to receive live raw EEG data from it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment