예제 #1
0
def record(channel_data=[], time_stamps=[], KillSwitch=None):
    if VERBOSE:
        sio.savemat("recording_" + time_str() + ".mat", {
            "time_stamps": [1, 2, 3],
            "channel_data": [1, 2, 3]
        })
    else:
        streams = pylsl.resolve_stream('type', 'EEG')
        inlet = pylsl.stream_inlet(streams[0])

        while True:
            try:
                sample, time_stamp = inlet.pull_sample()
                time_stamp += inlet.time_correction()

                time_stamps.append(time_stamp)
                channel_data.append(sample)

                # first col of one row of the record_data matrix is time_stamp,
                # the following cols are the sampled channels
            except KeyboardInterrupt:
                complete_samples = min(len(time_stamps), len(channel_data))
                sio.savemat(
                    "recording_" + time_str() + ".mat", {
                        "time_stamps": time_stamps[:complete_samples],
                        "channel_data": channel_data[:complete_samples]
                    })
                break
    if KillSwitch.terminate:
        return False
def control(stop_event, X=[]):
    ### based on real_time.py
    stream = pylsl.resolve_stream(
        'type',
        'EEGPre')  # EEG for the raw signal; EEGPre for a preprocessed signal
    inlet = pylsl.stream_inlet(stream[0])
    #info = inlet.info()
    print_version_info()

    while not stop_event.is_set():
        x_streamed, time = inlet.pull_sample()
        X = np.array(x_streamed)
        X_test = np.zeros((1, 3, 1024))
        X_test[0, 0, :] = X[:1024]
        X_test[0, 1, :] = X[1024:2048]
        X_test[0, 2, :] = X[2048:3072]
        model2 = keras.models.load_model(
            'CNN_STFT__C2_run7monitoring.h5',
            custom_objects={'Spectrogram': kapre.time_frequency.Spectrogram})

        classes = model2.predict(X_test)
        a = classes[:, 0]
        print(a)
        b = classes[:, 1]
        print(b)
        c = a / b
        if (c > 4):
            print('left')
            #move robot arm left
        elif (0.5 < c < 4):
            print('middle')
            #move robot arm to the middle
        else:
            print('right')
예제 #3
0
def training_aquisition(streams, divisao_minima=1):
    # pegando os parâmetros de teste
    path_base, naq, duracao, file_dic, cabecalho = setup()
    #inlet, sample = connect_controller()

    end = False
    # preenche todos os arquivos de treinamento
    while not end:
        # pega a lista das aquisições que ainda não foram
        available = list(filter(lambda x: file_dic[x] is None,
                                file_dic.keys()))

        # se todas tiverem sido preenchidas, sai
        if not available:
            end = True
            break

        # sorteia uma frequencia
        current = random.choice(available)
        filename = path_base.format(current)

        # abre o arquivo de saida
        f = open(filename, 'w')
        csv_writer = csv.writer(f, lineterminator='\n')
        csv_writer.writerow(cabecalho)

        # avisa o usuario para olhar para uma determinada luz
        print('\n\n\nOlhe para o led de %s Hertz.' % (current.split('_')[0]))
        play_audio.play_file(current.split('_')[0])

        # definindo o inlet
        inlet = pylsl.stream_inlet(streams[0])

        # aquisitando

        start = tempo()
        last_it = tempo()
        index = 0
        while tempo() - start <= duracao:
            sample = []
            it_time = tempo()
            if it_time - last_it >= divisao_minima:
                last_it = tempo()
                index = 0
            timestamp = inlet.pull_sample(sample)
            if timestamp[0]:
                index += 1
                csv_writer.writerow([index] + timestamp[0] + [timestamp[1]])

        print('Pode parar de olhar.')

        # fechando o arquivo
        f.close()
        file_dic[current] = filename

    play_audio.play_file('Fim')
    return file_dic, path_base
예제 #4
0
    def __init__(self, sampling_frequency=256.0, classification_time=1.0):
        streams = pylsl.resolve_stream('type', 'EEG')
        inlet = pylsl.stream_inlet(streams[0])

        sample, _ = inlet.pull_sample()
        n_channels = len(sample)

        classification_length = int(sampling_frequency * classification_time)

        self.channel_data = np.zeros([n_channels, classification_length * 4])
        self.inlet = inlet
        self.classification_length = classification_length
        self.i_channel_data = 0
예제 #5
0
def record(ser, channel_data=[], channel_force=[], time_stamps=[]):
    streams = pylsl.resolve_stream('type', 'EEG')
    inlet = pylsl.stream_inlet(streams[0])

    while True:
        try:
            sample, time_stamp = inlet.pull_sample()
            time_stamp += inlet.time_correction()

            time_stamps.append(time_stamp)
            channel_data.append(sample)

            #output_str = ser.readline()
            """
            if output_str.endswith('\n'):
                output_str = output_str.translate(None, ' \n\t\r')
                try:
                    force_sample = list(map(float, output_str.split(';')))

                    if len(force_sample) != 5:
                        force_sample = [0, 0, 0, 0, 0]
                except ValueError:
                    force_sample = [0, 0, 0, 0, 0]

            else:
                force_sample = [0, 0, 0, 0, 0]
            
            try:
                channel_force.append(float(output_str))
            except ValueError:
                channel_force.append(0)

            """
            try:
                channel_force.append(float(ser.readline()))
            except ValueError:
                channel_force.append(0)

            # first col of one row of the record_data matrix is time_stamp,
            # the following cols are the sampled channels
        except KeyboardInterrupt:
            complete_samples = min(len(time_stamps), len(channel_data),
                                   len(channel_force))
            sio.savemat(
                "recording_" + time_str() + ".mat", {
                    "time_stamps": time_stamps[:complete_samples],
                    "channel_data": channel_data[:complete_samples],
                    "channel_force": channel_force[:complete_samples]
                })
            break
예제 #6
0
def record(stop_event,
           channel_data=[],
           time_stamps=[],
           Fs=[],
           stream_name='openvibeSignal'):
    """
    function to be run in a thread to record data from lsl
    :param stop_event: threading.event that can be set to stop the server
    :param channel_data: buffer that samples of channel data will be appended to
    :param time_stamps: corresponding time stamps
    :param Fs: sampling frequency
    :param stream_name: name of the stream to be searched for
    :return:
    """
    print('The recording thread started.')
    # 1. get input stream
    streams = pylsl.resolve_stream('name', stream_name)
    print('Streams:', streams)
    inlet = pylsl.stream_inlet(streams[0])
    inletInfo = inlet.info()
    inlet_sampleRate = int(inletInfo.nominal_srate())
    inlet_numChannels = int(inletInfo.channel_count())
    Fs = inlet_sampleRate
    print("Reported sample rate: %i , number of channels: %i" %
          (inlet_sampleRate, inlet_numChannels))

    while not stop_event.is_set():
        try:
            sample, time_stamp = inlet.pull_sample()
            time_stamp += inlet.time_correction()

            time_stamps.append(time_stamp)
            channel_data.append(sample)
            if len(time_stamps) % 1000 == 0:
                print("Length of recorded data is ", len(time_stamps))

        except KeyboardInterrupt:
            # save data and exit on KeybordInterrupt
            complete_samples = min(len(time_stamps), len(channel_data))
            sio.savemat(
                "recording_" + time_str() + ".mat", {
                    "time_stamps": time_stamps[:complete_samples],
                    "channel_data": channel_data[:complete_samples],
                })
            break
def find_gUSBamp_stream(name):
    # required, because two different amplifiers, both sending a stream with type 'EEG' can be connected
    # the one with the label EMG will be used for EMG and the one with label EEG for EEG
    # returns the correct stream
    streams = pylsl.resolve_stream('type', 'EEG')
    if name == 'EEG':
        device_id = 'g.USBamp-UB-2016.08.03'
        n_channels = 9
    elif name == 'EMG':
        device_id = 'g.USBamp-UB-2016.08.04'
        n_channels = 8
    inlet = []
    for i in range(len(streams)):
        try:
            if streams[i].name() == device_id and streams[i].channel_count() == n_channels:
                inlet = pylsl.stream_inlet(streams[i])
        except:
           pass
    return inlet
예제 #8
0
def connect():
    # função que efetivamente conecta com o open BCI
    if inspect.stack()[1][3] != 'worker':
        print(
            'ATENÇÃO! Não chamar o connect diretamente! Usar o connect_controller'
        )
        return

    #  first resolve an EEG stream on the lab network
    print("Procurando pela stream EEG...")
    streams = pylsl.resolve_stream('type', 'EEG')

    # create a new inlet to read from the stream
    inlet = pylsl.stream_inlet(streams[0])

    #print(timestamp, list(sample))
    while True:
        tm = inlet.pull_sample([])
        if tm[0]:
            break
    return streams
예제 #9
0
#GLOBAL_VARS
stop_key = 'j'  #press j to kill the code
attention_threshold = 65
attention_confirm_threshold = 3  #tried 3,4,5... 3 is the best because too hard to turn on
#attentions = [] this is there to debug when the attention values are low

attention_confirmation = 0
# read the neurosky recordings via python through OpenVibe EEG Software
streams = pylsl.resolve_stream('type', 'signal')

# continuously read from the EEG
while True:

    # this snippet gets the readings from the neurosky at the
    # current time
    inlet = pylsl.stream_inlet(streams[0])
    sample, timestamp = inlet.pull_sample()
    attention = sample[1]
    #print(attention)
    #attentions.append(attention)

    # high attention values lead to
    if (attention > attention_threshold):
        attention_confirmation += 1
    else:
        attention_confirmation = 0
        keyboard.release('u')

    # if the arrention has been high for long enough, then press the button
    if (attention_confirmation >= attention_confirm_threshold):
        #print('pressed')
예제 #10
0
파일: ReceiveData.py 프로젝트: sccn/SNAP
import sys; sys.path.append('..')  # make sure that pylsl is found (note: in a normal program you would bundle pylsl with the program)
import pylsl

# first resolve an EEG stream on the lab network
print("looking for an EEG stream...")
streams = pylsl.resolve_stream('type','EEG')

# create a new inlet to read from the stream
inlet = pylsl.stream_inlet(streams[0])

sample = pylsl.vectorf()
while True:
	# get a new sample (you can also omit the timestamp part if you're not interested in it)
	timestamp = inlet.pull_sample(sample)	
	print(timestamp, list(sample))
예제 #11
0
        return F.softmax(x, dim=1)


batch = 1
device = torch.device('cpu')
net = Net()
net = net.double()
net.load_state_dict(
    torch.load('models/60.79-2020-11-15 01-36-29.pt', map_location=device))

channels = 8  # Num of headset channels
trial = trials.left_right()  # Trial to view when collecting data.

stream = lsl.resolve_stream(
    'type', 'EEG')  # Start LSL Data stream with FFT graph in OpenBCI GUI
inlet = lsl.stream_inlet(stream[0])

print('Starting in: 3')
sleep(1)
print('Starting in: 2')
sleep(1)
print('Starting in: 1')
sleep(1)
print('Starting!')

while True:
    round_data = []
    for i in range(channels):
        sample, timestamp = inlet.pull_sample()
        round_data.append(sample)
    round_data = torch.from_numpy(np.array(round_data))
예제 #12
0
def get_markers(stop_event,
                channel_time=[],
                markers=[],
                markers_time=[],
                start_proc=[],
                stream_name='openvibeMarkers'):
    """
    function to be run in a thread to record markers from lsl
    :param stop_event: threading.event that can be set to stop the server
    :param channel_time: corresponding time stamps to data sample
    :param markers: buffer to append the markers to
    :param markers_time: buffer to append correponding time stamps to
    :param start_proc: buffer that True will be appended to once the experiment is over and processing can be started
    :param stream_name: name of the stream to be searched for
    :return:
    """
    codes = StimValues()
    print('The event thread started.')
    # 1. get input stream
    streams = pylsl.resolve_stream('name', stream_name)
    print('Streams:', streams)
    inlet = pylsl.stream_inlet(streams[0])
    inletInfo = inlet.info()
    inlet_sampleRate = int(inletInfo.nominal_srate())
    inlet_numChannels = int(inletInfo.channel_count())

    print("Reported sample rate: %i , number of channels: %i" %
          (inlet_sampleRate, inlet_numChannels))

    curr_time = 0.0
    exp_start = False
    while not stop_event.is_set():
        try:
            sample, time_stamp = inlet.pull_sample()
            # print(sample[0])
            if exp_start and sample[0] == codes.clock:
                # if a clock marker has been received update current clock
                # this is required, because marker stream comes without time stamps
                curr_time += codes.clock_time
            # elif not exp_start and not sample[0] == codes.exp_start:
            #     markers.append(sample)
            #     markers_time.append(-1)
            elif not exp_start and sample[0] == codes.exp_start:
                # if experiment start, synchronise to data stream and append
                print('Received Marker EXPERIMENT START')
                exp_start = True
                curr_time = channel_time[-1]
                markers_time.append(curr_time)
                markers.append(sample)
            elif exp_start and sample[0] == codes.exp_over:
                # if experiment end. append sample and append true, so that it can be detected in main loop
                print('Received Marker EXPERIMENT STOP')
                markers_time.append(curr_time)
                markers.append(sample)
                start_proc.append(True)
            elif exp_start:
                # if normal sample append with synchronised time
                markers_time.append(curr_time)
                markers.append(sample)
                if len(markers_time) % 1000 == 0:
                    print("Number of recorded events is ", len(markers_time))

        except KeyboardInterrupt:
            # save data and exit on KeybordInterrupt
            complete_samples = min(len(markers_time), len(markers))
            sio.savemat(
                "markers_" + time_str() + ".mat", {
                    "markers_time": markers_time[:complete_samples],
                    "markers": markers[:complete_samples],
                })
            break
예제 #13
0
def run_forever(rede,
                scaler,
                module,
                streams,
                path,
                divisao_minima=1,
                tamanho_janela_segundos=4,
                size=10,
                high_Wn=0.1,
                high_N=8,
                low_Wn=0.1,
                low_N=8,
                hann=False,
                hamm=False,
                fltop=False,
                wvlt=False):
    # vetor para guardar o sinal do open bci
    signal = []

    # index da posição de cada sinal na janela
    index = 0

    # lista que guardará os indexes maximos das janelas anteriores
    index_lista = []

    # quantos outputs são guardados
    output_size = 100
    # lista para guardar esses outputs
    output_lista = []

    # cabecalho do dataframe
    cabecalho = 'index_num,canal_1,canal_2,canal_3,canal_4,canal_5,canal_6,canal_7,canal_8,timestamp'
    cols = cabecalho.split(',')

    # dataframe para guardar as os de varias divisoes
    df = pd.DataFrame(columns=cols)
    tam_divisao = int(tamanho_janela_segundos / divisao_minima)

    # file e objetos para salvar o sinal
    filename = os.path.join(path, 'tempo_real.csv')
    file_obj = open(filename, 'w')
    writer = csv.writer(file_obj, lineterminator='\n')
    writer.writerow(cols)

    # tempo no inicio da aquisição

    last_time = tempo()
    current_time = tempo()

    # definindo o inlet
    inlet = pylsl.stream_inlet(streams[0])

    while True:
        sample = []
        # caso passe a divisão minima, pega os resultados, processa e preve
        if current_time - last_time >= divisao_minima:
            # passa_lista, para salvar o index dessa iteração
            index_lista = passa_lista(index_lista, index, size)

            # reseta o valor index (controle)
            index = 0

            # o tempo será tirado a partir de agora
            last_time = current_time

            # do dataframe que guarda uma janela, tira a mais antiga
            if len(index_lista) > tam_divisao:
                df = df.iloc[index_lista[-tam_divisao -
                                         1]:].copy().reset_index(drop=True)

            # e poe a nova
            df = pd.concat((df, pd.DataFrame(signal, columns=cols)))

            # reseta o vetor de entrada
            signal = []
            # processa o sinal
            if len(index_lista) > tam_divisao:
                sinal_processado = processa.processa_real_time(
                    df.copy(),
                    tamanho_janela_segundos=tamanho_janela_segundos,
                    high_Wn=high_Wn,
                    high_N=high_N,
                    low_Wn=low_Wn,
                    low_N=low_N,
                    hann=hann,
                    hamm=hamm,
                    fltop=fltop,
                    wvlt=wvlt)

                # transforma o output do processamento em um np array
                dataset = np.array(sinal_processado)

                # tenta prever o estado
                saida = module.prever(rede, scaler, dataset)

                print(saida)

                # salva o output
                output_lista = passa_lista(output_lista, saida, output_size)

        timestamp = inlet.pull_sample(sample)
        if timestamp[0]:
            current_time = tempo()
            index = index + 1

            # append na lista
            signal.append([index] + timestamp[0] + [timestamp[1]])

            # salva o sinal
            writer.writerow([index] + timestamp[0] + [timestamp[1]])
예제 #14
0
def run_debug_mode(rede,
                   scaler,
                   module,
                   streams,
                   path,
                   divisao_minima=1,
                   tamanho_janela_segundos=4,
                   size=10,
                   full=False,
                   high_Wn=0.1,
                   high_N=8,
                   low_Wn=0.1,
                   low_N=8,
                   hann=False,
                   hamm=False,
                   fltop=False,
                   wvlt=False):
    possible_freqs = {'7': 0, '9': 1, '11': 2, '13': 3, '100': 5}
    i = 0

    duration = 30
    n = 1
    aq = 0

    print(path)

    # vetor para guardar o sinal do open bci
    signal = []

    # index da posição de cada sinal na janela
    index = 0

    # lista que guardará os indexes maximos das janelas anteriores
    index_lista = []

    # quantos outputs são guardados
    output_size = 100
    # lista para guardar esses outputs
    output_lista = []

    # cabecalho do dataframe
    cabecalho = 'index_num,canal_1,canal_2,canal_3,canal_4,canal_5,canal_6,canal_7,canal_8,timestamp,freq,aq'
    cols = cabecalho.split(',')

    # tempo no inicio da aquisição
    inicio = tempo()
    last_time = tempo()
    current_time = tempo()

    if full:
        # definindo o inlet
        inlet = pylsl.stream_inlet(streams[0])

        # dataframe para guardar as os de varias divisoes
        df = pd.DataFrame(columns=cols)
        tam_divisao = int(tamanho_janela_segundos / divisao_minima)

        # file e objetos para salvar o sinal
        filename = os.path.join(path, 'tempo_real.csv')
        file_obj = open(filename, 'w')
        writer = csv.writer(file_obj, lineterminator='\n')
        writer.writerow(cols)

        # escolhendo uma label
        freq, i = choose_freq_play_file(possible_freqs, i)
        # definindo o tempo para duracao total
        tempo_0 = tempo()
        duracao_total = duration * (n + 1) * (len(possible_freqs.keys()))

        while True:
            sample = []
            # caso passe a divisão minima, pega os resultados, processa e preve
            if current_time - last_time >= divisao_minima:

                # passa_lista, para salvar o index dessa iteração
                index_lista = passa_lista(index_lista, index, size)

                # reseta o valor index (controle)
                index = 0

                # o tempo será tirado a partir de agora
                last_time = current_time

                # do dataframe que guarda uma janela, tira a mais antiga
                if len(index_lista) > tam_divisao:
                    df = df.iloc[index_lista[-tam_divisao -
                                             1]:].copy().reset_index(drop=True)

                # e poe a nova
                df = pd.concat((df, pd.DataFrame(signal, columns=cols)))

                # reseta o vetor de entrada
                signal = []
                # processa o sinal
                if len(index_lista) > tam_divisao:
                    sinal_processado = processa.processa_real_time(
                        df.copy(),
                        tamanho_janela_segundos=tamanho_janela_segundos,
                        high_Wn=high_Wn,
                        high_N=high_N,
                        low_Wn=low_Wn,
                        low_N=low_N,
                        hann=hann,
                        hamm=hamm,
                        fltop=fltop,
                        wvlt=wvlt)

                    # transforma o output do processamento em um np array
                    dataset = np.array(sinal_processado)

                    # tenta prever o estado
                    saida = module.prever(rede, scaler, dataset)

                    print(saida)

                    # salva o output
                    output_lista = passa_lista(output_lista, saida,
                                               output_size)

            timestamp = inlet.pull_sample(sample)
            if timestamp[0]:
                current_time = tempo()
                index = index + 1

                # append na lista
                signal.append([index] + timestamp[0] +
                              [timestamp[1], freq, aq])

                # salva o sinal
                writer.writerow([index] + timestamp[0] +
                                [timestamp[1], freq, aq])

            if current_time - inicio >= duration:
                inicio = tempo()
                freq, i = choose_freq_play_file(possible_freqs, i)
                aq += 1
                # redefinindo o inlet
                inlet = pylsl.stream_inlet(streams[0])

            if current_time - tempo_0 >= duracao_total:
                play_audio.play_file('Fim')
                break
        file_obj.close()
    file_dic = transform_real_time_files(path, possible_freqs)
    #file_dic = get_file_dic('')
    dataset, output = processa.roda_processamento(
        filenames_dic=file_dic,
        source='tempo_real',
        tamanho_janela_segundos=tamanho_janela_segundos,
        high_Wn=high_Wn,
        high_N=high_N,
        low_Wn=low_Wn,
        low_N=low_N,
        hann=hann,
        hamm=hamm,
        fltop=fltop,
        wvlt=wvlt)

    module.prever_debug(rede, scaler, dataset, output)
    return file_dic
예제 #15
0
from Tkinter import *
import random, pylsl
from pylsl import stream_inlet, stream_outlet, stream_info
info = stream_info('RandomData', 'EEG', 4, 50, pylsl.cf_int32, 'RandomDev123')
outlet = stream_outlet(info)
inlet = stream_inlet(info)
inlet.open_stream()

WIDTH = 800
HEIGHT = 600
SEG_SIZE = 20
IN_GAME = True


def create_block():
    global BLOCK
    posx = SEG_SIZE * random.randint(1, (WIDTH-SEG_SIZE) / SEG_SIZE)
    posy = SEG_SIZE * random.randint(1, (HEIGHT-SEG_SIZE) / SEG_SIZE)
    BLOCK = c.create_oval(posx, posy, posx+SEG_SIZE, posy+SEG_SIZE, fill="red")

    
def main():
    global IN_GAME
    if IN_GAME:
        s.move()
        head_coords = c.coords(s.segments[-1].instance)
        x1, y1, x2, y2 = head_coords
        insample = pylsl.vectorf()
        inlet_list = []
        while inlet.samples_available():
            inlet.pull_sample(insample)