def main(): """Initialize and start the server.""" import time import argparse from bcipy.acquisition.datastream.generator import file_data, random_data default_channels = ['ch' + str(i + 1) for i in range(16)] parser = argparse.ArgumentParser() parser.add_argument('-f', '--filename', default=None, help="file containing data to be streamed; " "if missing, random data will be served.") parser.add_argument('-c', '--channels', default=','.join(default_channels), help='comma-delimited list') parser.add_argument('-s', '--sample_rate', default='256', help='sample rate in hz') parser.add_argument('-m', '--markers', action="store_true", default=False) parser.add_argument('-n', '--name', default='LSL') args = parser.parse_args() params = { 'channels': args.channels.split(','), 'hz': int(args.sample_rate) } # Generate data from the file if provided, otherwise random data. generator = file_data(filename=args.filename) if args.filename \ else random_data(channel_count=len(params['channels'])) markers = True if args.markers else False try: server = LslDataServer(params=params, generator=generator, add_markers=markers, name=args.name) logging.debug("New server created") server.start() logging.debug("Server started") while True: time.sleep(1) except KeyboardInterrupt: print("Keyboard Interrupt") server.stop()
def test_file_generator(self): """Should stream data from a file.""" row_count = 100 header = ['col1,col2,col3'] data = list(mock_data(row_count, len(header))) rows = map(lambda x: ','.join(map(str, x)), data) test_data = '\n'.join(header + rows) with patch('bcipy.acquisition.datastream.generator.open', mock_open(read_data=test_data), create=True): gen = file_data(filename='foo', header_row=1) generated_data = [next(gen) for _ in range(row_count)] for i, row in enumerate(generated_data): self.assertEqual(row, data[i])
def test_file_generator_end(self): """Should throw an exception when all data has been consumed""" row_count = 10 header = ['col1,col2,col3'] data = list(mock_data(row_count, len(header))) rows = map(lambda x: ','.join(map(str, x)), data) test_data = '\n'.join(header + rows) with patch('bcipy.acquisition.datastream.generator.open', mock_open(read_data=test_data), create=True): gen = file_data(filename='foo', header_row=1) # exhaust the generator for _ in range(row_count): next(gen) with pytest.raises(StopIteration): data.append(next(gen))
def test_file_with_custom_encoder(self): """Should allow a custom encoder""" col_count = 3 row_count = 100 header = ['col1,col2,col3'] data = [[float(cnum + rnum) for cnum in range(col_count)] for rnum in range(row_count)] rows = map(lambda x: ','.join(map(str, x)), data) test_data = '\n'.join(header + rows) with patch('bcipy.acquisition.datastream.generator.open', mock_open(read_data=test_data), create=True): gen = file_data( filename='foo', header_row=1, encoder=CustomEncoder()) generated_data = [next(gen) for _ in range(row_count)] for _count, record in generated_data: self.assertEqual(len(record), col_count) self.assertEqual(generated_data[0][0], 1) self.assertEqual(generated_data[99][0], 100)