Exemple #1
0
    def test_zero_offset(self):
        """Test offset value override"""

        channels = ['ch1', 'ch2', 'TRG']
        sample_hz = 100
        trigger_at = 10
        num_records = 500
        n_channels = len(channels) - 1

        data = [mock_record(n_channels) + [0 if (i + 1) < trigger_at else 1]
                for i in range(num_records)]

        device = _MockDevice(data=data, channels=channels, fs=sample_hz)
        daq = DataAcquisitionClient(device=device,
                                    processor=NullProcessor(),
                                    buffer_name='buffer_client_test_offset.db',
                                    clock=CountClock())

        daq.is_calibrated = True  # force the override.
        daq.start_acquisition()
        time.sleep(0.1)
        daq.stop_acquisition()

        self.assertTrue(daq.is_calibrated)
        self.assertEqual(daq.offset, 0.0, "Setting the is_calibrated to True\
            should override the offset calcution.")

        daq.cleanup()
Exemple #2
0
    def test_zero_offset(self):
        """Test offset value override"""

        channels = ['ch1', 'ch2', 'TRG']
        sample_hz = 100
        trigger_at = 10
        num_records = 500
        n_channels = len(channels) - 1

        data = [
            mock_record(n_channels) + [0 if (i + 1) < trigger_at else 1]
            for i in range(num_records)
        ]

        device = _MockConnector(data=data,
                                device_spec=DeviceSpec(name="Mock_device",
                                                       channels=channels,
                                                       sample_rate=sample_hz))
        daq = DataAcquisitionClient(connector=device,
                                    buffer_name='buffer_client_test_offset.db',
                                    raw_data_file_name=None,
                                    delete_archive=True,
                                    clock=CountClock())

        daq.is_calibrated = True  # force the override.
        daq.start_acquisition()
        time.sleep(0.1)
        daq.stop_acquisition()

        self.assertTrue(daq.is_calibrated)
        self.assertEqual(
            daq.offset, 0.0, "Setting the is_calibrated to True\
            should override the offset calcution.")

        daq.cleanup()
Exemple #3
0
def init_eeg_acquisition(parameters: dict,
                         save_folder: str,
                         clock=CountClock(),
                         server: bool = False):
    """Initialize EEG Acquisition.

    Initializes a client that connects with the EEG data source and begins
    data collection.

    Parameters
    ----------
        parameters : dict
            configuration details regarding the device type and other relevant
            connection information.
             {
               "acq_device": str,
               "acq_host": str,
               "acq_port": int,
               "buffer_name": str,
               "raw_data_name": str
             }
        clock : Clock, optional
            optional clock used in the client; see client for details.
        server : bool, optional
            optionally start a server that streams random DSI data; defaults
            to true; if this is True, the client will also be a DSI client.
    Returns
    -------
        (client, server) tuple
    """

    # Initialize the needed DAQ Parameters
    host = parameters['acq_host']
    port = parameters['acq_port']

    parameters = {
        'acq_show_viewer': parameters['acq_show_viewer'],
        'viewer_screen': 1 if int(parameters['stim_screen']) == 0 else 0,
        'buffer_name': save_folder + '/' + parameters['buffer_name'],
        'device': parameters['acq_device'],
        'filename': save_folder + '/' + parameters['raw_data_name'],
        'connection_params': {
            'host': host,
            'port': port
        }
    }

    # Set configuration parameters (with default values if not provided).
    buffer_name = parameters.get('buffer_name', 'buffer.db')
    connection_params = parameters.get('connection_params', {})
    device_name = parameters.get('device', 'DSI')
    filename = parameters.get('filename', 'rawdata.csv')

    dataserver = False
    if server:
        if device_name == 'DSI':
            protocol = registry.default_protocol(device_name)
            dataserver, port = start_socket_server(protocol, host, port)
            connection_params['port'] = port
        elif device_name == 'LSL':
            channel_count = 16
            sample_rate = 256
            channels = ['ch{}'.format(c + 1) for c in range(channel_count)]
            dataserver = LslDataServer(
                params={
                    'name': 'LSL',
                    'channels': channels,
                    'hz': sample_rate
                },
                generator=generator.random_data(channel_count=channel_count))
            await_start(dataserver)
        else:
            raise ValueError(
                'Server (fake data mode) for this device type not supported')

    Device = registry.find_device(device_name)

    filewriter = FileWriter(filename=filename)
    proc = filewriter
    if parameters['acq_show_viewer']:
        proc = DispatchProcessor(
            filewriter,
            ViewerProcessor(display_screen=parameters['viewer_screen']))

    # Start a client. We assume that the channels and fs will be set on the
    # device; add a channel parameter to Device to override!
    client = DataAcquisitionClient(
        device=Device(connection_params=connection_params),
        processor=proc,
        buffer_name=buffer_name,
        clock=clock)

    client.start_acquisition()

    # If we're using a server or data generator, there is no reason to
    # calibrate data.
    if server and device_name != 'LSL':
        client.is_calibrated = True

    return (client, dataserver)
Exemple #4
0
def init_eeg_acquisition(parameters: dict,
                         save_folder: str,
                         clock=CountClock(),
                         server: bool = False):
    """Initialize EEG Acquisition.

    Initializes a client that connects with the EEG data source and begins
    data collection.

    Parameters
    ----------
        parameters : dict
            configuration details regarding the device type and other relevant
            connection information.
             {
               "acq_device": str,
               "acq_connection_method": str,
               "acq_host": str,
               "acq_port": int,
               "buffer_name": str,
               "raw_data_name": str
             }
        clock : Clock, optional
            optional clock used in the client; see client for details.
        server : bool, optional
            optionally start a mock data server that streams random data.
    Returns
    -------
        (client, server) tuple
    """

    # Set configuration parameters with default values if not provided.
    host = parameters['acq_host']
    port = parameters['acq_port']
    buffer_name = str(
        Path(save_folder, parameters.get('buffer_name', 'raw_data.db')))
    raw_data_file = Path(save_folder,
                         parameters.get('raw_data_name', 'raw_data.csv'))

    connection_method = ConnectionMethod.by_name(
        parameters['acq_connection_method'])
    # TODO: parameter for loading devices; path to devices.json?
    # devices.load(devices_path)
    device_spec = supported_device(parameters['acq_device'])

    dataserver = False
    if server:
        dataserver = start_server(connection_method, device_spec, host, port)

    # TODO: only use these connection_params if this is ConnectionMethod.TCP
    # Refactor to extract only the needed connection params from parameters.
    connection_params = {'host': host, 'port': port}
    connector = registry.make_connector(device_spec, connection_method,
                                        connection_params)

    client = DataAcquisitionClient(connector=connector,
                                   buffer_name=buffer_name,
                                   delete_archive=False,
                                   raw_data_file_name=raw_data_file,
                                   clock=clock)

    client.start_acquisition()

    if parameters['acq_show_viewer']:
        viewer_screen = 1 if int(parameters['stim_screen']) == 0 else 0
        start_viewer(display_screen=viewer_screen)

    # If we're using a server or data generator, there is no reason to
    # calibrate data.
    if server and connection_method != ConnectionMethod.LSL:
        client.is_calibrated = True

    return (client, dataserver)