Jump to content

Programming Reference:SignalSharing Python Demo

From BCI2000 Wiki
Revision as of 23:46, 30 January 2024 by Wengelhardt (talk | contribs) (Creation of SignalSharing Python Demo)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Demo example of the flexibility of visualizations with Python

Location

src/core/SignalProcessing/SignalSharingDemo/PythonApp

Synopsis

The SignalSharing Python Demo demonstrates how to make a complex real-time visualization in Python using data parallelly collected in BCI2000.

Function

The SignalSharing Python Demo creates a basic visualization in Python using the data collected in BCI2000. Since this is done outside of the BCI2000 processing loop, rendering visualizations can take as long as needed. Using Python also allows for the use of the numerous packages that are available to create complex figures.

This Demo has two parts, one which shares the data as a BCI2000 SignalProcessing Filter, the other which visualizes the data:

  1. SignalSharing Signal Processing Filter: This is the same filter as what is used for the SignalSharingDemo with the C++ application. Please refer to that page for details on the BCI2000 Filter aspect.
  2. Python visualization app: A simple Python script that connects with the port that is being used to share the data, and updates the visualization as data is being streamed.


The BCI2000 data is decoded according to the format specified in BCI2000 Messages Wiki page, as Descriptor=4: Visualization and Brain Signal Data Format, then Descriptor Supplement=1: Signal Data, then data type 2. The Python script assumes the data is in this format, and that the data stream is only sending the name of the shared memory. This happens if the Python script is running on the same machine as BCI2000, otherwise the whole signal will be sent in the stream. The Python script will most likely not work if this happens, but one can easily edit it to handle the data accordingly.

How to run

  1. Build BCI2000 as you would, make sure to check BUILD_DEMOS
  2. Make sure the SignalSharingDemo works first
  3. Navigate to src\core\SignalProcessing\SignalSharingDemo\PythonApp, where there is a batch file and a Python file. Copy the batch file to your BCI2000 batch folder
  4. Run the Python file. For example from the command line, navigate to the folder and run python SignalSharingPythonDemo.py
  5. Run the batch file, and press "Start Run" (it already sets the configuration, it won't work it you press it multiple times)
    • If you change the Parameter SignalSharingDemoClientAddress, make sure to also change it in the Python script

Python Script

Here is the same code that is on the SVN

#import libraries
import socket
from multiprocessing import shared_memory
import matplotlib.pyplot as plt
import numpy as np

#user input
HOST, PORT = "localhost", 1879
print("Waiting for BCI2000 on %s at port %i" %(HOST, PORT))

#initialize axes
setProps = False
chNames = []
figure, ax = plt.subplots(figsize=(10, 8))
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
figure.set_facecolor((0,0,0,1))
ax.set_axis_off()
ax.set_frame_on(0)
figure.canvas.draw()

#attempt connection to specified port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        try:
            while True: #go until we manually stop program
                while setProps is False: #get properties
                    b = conn.recv(1)
                    if b==b'{': #start of channel names
                        name = ""
                        while b != b'}': #end of channel names
                            b = conn.recv(1)
                            if b != b' ':
                                name += str(b, encoding='utf-8') #gather whole ch name
                            elif name != "":
                                chNames = np.append(chNames, name) #move to next name
                                name = ""
                        CHANNELS = len(chNames) #we have all the channels
                        
                        #element size is next
                        elementString = ""
                        while len(elementString) == 0 or b!= b' ':
                            b = conn.recv(1)
                            if b != b' ':
                                elementString += str(b, encoding='utf-8')

                        #initialize variables once we have channels and elements
                        ELEMENTS = int(elementString)
                        phi = np.zeros((CHANNELS, ELEMENTS))
                        bla = np.zeros((ELEMENTS,1))
                        lineArr = list(range(CHANNELS))
                        for i in range(0,CHANNELS):
                            lineArr[i], = ax.plot(bla, bla)

                        print("Properties: Channels: %i, Elements: %i" %(CHANNELS, ELEMENTS))
                        print("Visualizing data...")
                        setProps= True

                #continuously update stream and get data!
                stream = conn.recv(128)
                for b in stream:
                    if b==47: #47=b'\', right before memory name
                        #get memory name
                        streamName = stream.split(b'/')[1] #mem name right after
                        byteName = streamName.split(b'\x00')[0]
                        mName = str(byteName, encoding='utf-8')
                        mem = shared_memory.SharedMemory(mName)

                        #we got data
                        data = np.ndarray((CHANNELS,ELEMENTS),dtype=np.double, buffer=mem.buf)
                        for ch in range(0,CHANNELS):
                            for el in range(0, ELEMENTS):
                                phi[ch, el] = el*2*np.pi/(ELEMENTS-1)

                            xdata = np.multiply(1+0.003*data[ch,:],np.cos(phi[ch,:]))
                            ydata = np.multiply(1+0.003*data[ch,:],np.sin(phi[ch,:]))
                            #update plots
                            lineArr[ch].set_xdata(xdata)
                            lineArr[ch].set_ydata(ydata)

                        figure.canvas.draw()
                        plt.pause(0.01) #render update                   
        except:
            print('exception')
            conn.close() #close connection to client
        finally:
            print('disconnected')
            conn.close() #close connection to client

Conclusion

This demo shows how to grab data from BCI2000 and plot it in Python! The main advantage of this demo shows how to access the data in Python. Once this is done, Python's extensive library can be used to create complex, real-time visualizations and calculations!


See also

Programming Reference:GenericSignal ClassTechnical Reference:BCI2000 MessagesProgramming Reference:SignalSharingDemo Signal Processing