Skip to main content

A Python library for interfacing with Brains@Play

Project description

brainsatplay.py

A Python library for interfacing with Brains@Play

Installation

pip install brainsatplay

Example (with Brainflow)

import brainsatplay 
import time
import math
from brainflow.board_shim import BoardShim, BrainFlowInputParams, LogLevels, BoardIds
from brainflow.data_filter import DataFilter, FilterTypes
import sys, signal

def main():

    '''
    Connect to the Brainstorm
    '''   
    # Authentication
    username = 'guest'
    password = ''

    ## Set Connection Details
    brainstorm = brainsatplay.Brainstorm('http://localhost','80') # URL, Port

    ## Connect
    res = brainstorm.connect(username,password) # All optional (defaults to guest)

    '''
    Subscribe to a Particular Game
    '''   
    # # Connection Settings
    appname = 'brainstorm'
    devices = []
    props = ['raw','times','sps','deviceType','format','eegChannelTags']
    sessionid = None
    spectating = False # Spectate to view data without sending it

    res = brainstorm.getSessions(appname)

    if res['msg'] != 'appNotFound':
        sessionid = res['sessions'][0]['id']
    else:
        res = brainstorm.createSession(appname, devices, props)
        sessionid = res['sessionInfo']['id']

    # Handle Data from Subscribed Games
        def newData(json):
            for user in json['userData']:
                name = user['username']
                print('Data for {}'.format(name))
    res = brainstorm.subscribeToSession(sessionid,spectating, newData)


    '''
    Stream your Data
    '''

    # Setup Brainflow
    params = BrainFlowInputParams()
    board_id = BoardIds['SYNTHETIC_BOARD'].value
    board = BoardShim(board_id, params)
    board.rate = BoardShim.get_sampling_rate(board_id)
    board.channels = BoardShim.get_eeg_channels(board_id)
    board.time_channel = BoardShim.get_timestamp_channel(board_id)
    board.eeg_channels = BoardShim.get_eeg_channels(board_id)
    board.eeg_names = BoardShim.get_eeg_names(board_id)
    board.prepare_session()
    board.start_stream(num_samples=450000)

    # Handle CTRL-C Exit
    def onStop():
        board.stop_stream()
        board.release_session()

    loopCount = 0

    # Start Stream Loop
    def streamLoop():
        pass_data = []
        rate = DataFilter.get_nearest_power_of_two(board.rate)
        data = board.get_board_data()
        t = data[board.time_channel]

        data = data[board.eeg_channels] 

        for entry in data:
            pass_data.append((entry).tolist())

        data = {}
        data['raw'] = pass_data
        data['times'] = t.tolist()

        # Send Metadata on First Loop
        if loopCount == 0:
            data['sps'] = board.rate
            data['deviceType'] = 'eeg'
            data['format'] = 'brainflow'
            tags = []
            for i, channel in enumerate(board.eeg_channels):
                tags.append({'ch': channel-1, 'tag': board.eeg_names[i], 'analyze':True})

            data['eegChannelTags'] = tags

        return data

    res = brainstorm.startStream(streamLoop, onStop)

if __name__ == "__main__":
    main()

Support

If you are having issues, please email Garrett Flynn at gflynn@usc.edu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brainsatplay-0.1.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

brainsatplay-0.1.2-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

Details for the file brainsatplay-0.1.2.tar.gz.

File metadata

  • Download URL: brainsatplay-0.1.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.10

File hashes

Hashes for brainsatplay-0.1.2.tar.gz
Algorithm Hash digest
SHA256 b56a7ca0dfe93dd489a192ba5aaae121d7f3fbc6d8238c7e197c337d7bacef19
MD5 ac848a56bee5496a11293f8b17453c61
BLAKE2b-256 50877915cbe3e06af704b31d5ccae58336d1b69017efabb9ad310dd000e8c120

See more details on using hashes here.

File details

Details for the file brainsatplay-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: brainsatplay-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.10

File hashes

Hashes for brainsatplay-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ac4f8d1963b2d486d08084e70c87acc547481a0a37e7cae0b2e7f53a427f2916
MD5 af22317b28779e264b39021299d400d4
BLAKE2b-256 3dd78ae680a62447b789b4c6b7e40c3ea01ff40209c3402d8596ce7b3e33258e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page