def test_with_marker_stream(self): server = LslDataServer(device_spec=self.device_spec, add_markers=True) self.start_server(server) device = LslConnector(connection_params={}, device_spec=self.device_spec) device.connect() device.acquisition_init() self.assertEquals(self.channels, device.channels) self.assertEquals(len(self.channels), len(device.read_data()))
def test_connect(self): """Should require a connect call before initialization.""" device = LslConnector(connection_params={}, device_spec=DeviceSpec( name='LSL', channels=self.channels, sample_rate=self.sample_rate)) with pytest.raises(Exception): device.acquisition_init()
def test_device_info_channels(self): """Device should list the correct channels""" spec = DeviceSpec(name='LSL', channels=self.channels, sample_rate=self.sample_rate) device = LslConnector(connection_params={}, device_spec=spec) device.connect() device.acquisition_init() self.assertEqual(self.channels, device.channels)
def test_with_trigger_channel(self): """A device with a TRG channel should work as expected.""" server = LslDataServer(device_spec=self.device_spec) self.start_server(server) device = LslConnector(connection_params={}, device_spec=self.device_spec) device.connect() device.acquisition_init() self.assertEquals(self.channels, device.channels)
def test_incorrect_frequency(self): """Provided sample_rate should match sample rate read from device""" device = LslConnector(connection_params={}, device_spec=DeviceSpec(name='LSL', channels=self.channels, sample_rate=300)) device.connect() with pytest.raises(Exception): device.acquisition_init()
def test_with_marker_stream_included(self): server = LslDataServer(device_spec=self.device_spec, add_markers=True) self.start_server(server) device = LslConnector(connection_params={}, device_spec=self.device_spec, include_marker_streams=True) device.connect() device.acquisition_init() self.assertEquals(self.channels + [MARKER_STREAM_NAME], device.channels) self.assertEquals(len(self.channels) + 1, len(device.read_data()))
def test_incorrect_number_of_channels(self): """A list of channels with len that does not match channel_count should raise an exception.""" spec = DeviceSpec(name='LSL', channels=['ch1', 'ch2'], sample_rate=self.sample_rate) device = LslConnector(connection_params={}, device_spec=spec) self.assertEqual(spec.channel_count, 2) self.assertNotEqual(len(device.channels), self.channel_count) device.connect() with pytest.raises(Exception): device.acquisition_init()
def test_read_data(self): """Should produce a valid data record.""" print(self.device_spec.channels) device = LslConnector(connection_params={}, device_spec=DeviceSpec( name='LSL', channels=self.channels, sample_rate=self.sample_rate)) device.connect() device.acquisition_init() data = device.read_data() self.assertTrue(len(data) > 0) self.assertEqual(len(data), len(device.channels)) for channel in data[0:-1]: self.assertTrue(isinstance(channel, float))
def test_renaming_columns(self): server = LslDataServer(device_spec=self.device_spec, add_markers=True, marker_stream_name='TRG') self.start_server(server) device = LslConnector(connection_params={}, device_spec=self.device_spec, include_marker_streams=True, rename_rules={'TRG': 'TRG_device_stream'}) device.connect() device.acquisition_init() expected = [ 'C3', 'C4', 'Cz', 'FPz', 'POz', 'CPz', 'O1', 'O2', 'TRG_device_stream', 'TRG' ] self.assertEquals(expected, device.channels) self.assertEquals(expected, device.device_info.channels)
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}")