Ejemplo n.º 1
0
def main():
    """Creates a sample client that reads data from a sample TCP server
    (see demo/server.py). Data is written to a buffer.db sqlite3 database
    and streamed through a GUI. These files are written in whichever directory
    the script was run.

    The client/server can be stopped with a Keyboard Interrupt (Ctl-C)."""

    from bcipy.acquisition.datastream.lsl_server import LslDataServer
    from bcipy.acquisition.devices import supported_device
    from bcipy.gui.viewer import data_viewer

    device_spec = supported_device('LSL')
    server = LslDataServer(device_spec=device_spec)

    try:
        server.start()
        data_viewer.main(data_file=None,
                         seconds=5,
                         downsample_factor=2,
                         refresh=500,
                         yscale=150,
                         display_screen=0)
        # Stop the server after the data_viewer GUI is closed
        server.stop()
    except KeyboardInterrupt:
        print("Keyboard Interrupt; stopping.")
        server.stop()
Ejemplo n.º 2
0
class TestLslDevice(unittest.TestCase):
    """Test the LslDevice"""
    def __init__(self, *args, **kwargs):
        super(TestLslDevice, self).__init__(*args, **kwargs)
        self.channels = ['C3', 'C4', 'Cz', 'FPz', 'POz', 'CPz', 'O1', 'O2']
        self.channel_count = len(self.channels)
        # pylint: disable=invalid-name
        self.hz = 100

    @property
    def include_meta(self):
        """Whether or not to include metadata"""
        raise Exception("Must be implemented in subclass")

    def setUp(self):
        """Run before each test."""
        self.server = LslDataServer(
            params={
                'name': 'LSL',
                'channels': self.channels,
                'hz': self.hz
            },
            generator=generator.random_data(channel_count=self.channel_count),
            include_meta=self.include_meta,
            add_markers=True)
        await_start(self.server)

    def tearDown(self):
        """Run after each test."""
        self.server.stop()
        self.server = None
Ejemplo n.º 3
0
def main(debug: bool = False):
    # pylint: disable=too-many-locals
    """Creates a sample lsl client that reads data from a sample TCP server
    (see demo/server.py). Data is written to a rawdata.csv file, as well as a
    buffer.db sqlite3 database. These files are written in whichever directory
    the script was run.

    The client/server can be stopped with a Keyboard Interrupt (Ctl-C)."""

    import time
    import sys

    # Allow the script to be run from the bci root, acquisition dir, or
    # demo dir.
    sys.path.append('.')
    sys.path.append('..')
    sys.path.append('../..')

    from bcipy.acquisition.devices import DeviceSpec
    from bcipy.acquisition.protocols.lsl.lsl_connector import LslConnector
    from bcipy.acquisition.client import DataAcquisitionClient
    from bcipy.acquisition.datastream.lsl_server import LslDataServer
    from bcipy.acquisition.datastream.tcp_server import await_start

    from bcipy.helpers.system_utils import log_to_stdout

    if debug:
        log_to_stdout()

    # Generic LSL device with 16 channels.
    device_spec = DeviceSpec(name="LSL_demo",
                             channels=[
                                 "Ch1", "Ch2", "Ch3", "Ch4", "Ch5", "Ch6",
                                 "Ch7", "Ch8", "Ch9", "Ch10", "Ch11", "Ch12",
                                 "Ch13", "Ch14", "Ch15", "Ch16"
                             ],
                             sample_rate=256.0)
    server = LslDataServer(device_spec=device_spec)
    await_start(server)

    # Device is for reading data.
    connector = LslConnector(connection_params={}, device_spec=device_spec)

    raw_data_name = 'demo_raw_data.csv'
    client = DataAcquisitionClient(connector=connector,
                                   raw_data_file_name=raw_data_name)

    try:
        client.start_acquisition()

        print("\nCollecting data for 3s... (Interrupt [Ctl-C] to stop)\n")

        while True:
            time.sleep(1)
            client.marker_writer.push_marker('calibration_trigger')
            time.sleep(2)
            # since calibration trigger was pushed after 1 second of sleep
            print(f"Offset: {client.offset}; this value should be close to 1.")
            client.stop_acquisition()
            client.cleanup()
            server.stop()
            print(f"\nThe collected data has been written to {raw_data_name}")
            break

    except KeyboardInterrupt:
        print("Keyboard Interrupt; stopping.")
        client.stop_acquisition()
        client.cleanup()
        server.stop()
        print(f"\nThe collected data has been written to {raw_data_name}")
Ejemplo n.º 4
0
def main():
    # pylint: disable=too-many-locals
    """Creates a sample lsl client that reads data from a sample TCP server
    (see demo/server.py). Data is written to a rawdata.csv file, as well as a
    buffer.db sqlite3 database. These files are written in whichever directory
    the script was run.

    The client/server can be stopped with a Keyboard Interrupt (Ctl-C)."""

    import time
    import sys

    # Allow the script to be run from the bci root, acquisition dir, or
    # demo dir.
    sys.path.append('.')
    sys.path.append('..')
    sys.path.append('../..')

    from bcipy.acquisition.datastream import generator
    from bcipy.acquisition.protocols import registry
    from bcipy.acquisition.client import DataAcquisitionClient
    from bcipy.acquisition.datastream.lsl_server import LslDataServer
    from bcipy.acquisition.datastream.server import await_start

    host = '127.0.0.1'
    port = 9000

    channel_count = 16
    sample_rate = 256
    channels = ['ch{}'.format(c + 1) for c in range(channel_count)]
    # The Protocol is for mocking data.
    server = LslDataServer(params={'name': 'LSL',
                                   'channels': channels,
                                   'hz': sample_rate},
                           generator=generator.random_data(
                               channel_count=channel_count))
    await_start(server)

    # Device is for reading data.
    # pylint: disable=invalid-name
    Device = registry.find_device('LSL')
    device = Device(connection_params={'host': host, 'port': port})
    client = DataAcquisitionClient(device=device)

    try:
        client.start_acquisition()

        print("\nCollecting data for 10s... (Interrupt [Ctl-C] to stop)\n")

        while True:
            time.sleep(10)
            client.stop_acquisition()
            client.cleanup()
            print("Number of samples: {0}".format(client.get_data_len()))
            server.stop()
            print("The collected data has been written to rawdata.csv")
            break

    except KeyboardInterrupt:
        print("Keyboard Interrupt; stopping.")
        client.stop_acquisition()
        client.cleanup()
        print("Number of samples: {0}".format(client.get_data_len()))
        server.stop()
        print("The collected data has been written to rawdata.csv")