Example #1
0
def test_fieldtrip_client():
    """Test fieldtrip_client"""

    neuromag2ft_fname = op.realpath(
        op.join(os.environ['NEUROMAG2FT_ROOT'], 'neuromag2ft'))

    kill_signal = queue.Queue()
    thread = threading.Thread(target=_run_buffer,
                              args=(kill_signal, neuromag2ft_fname))
    thread.daemon = True
    thread.start()

    # Start the FieldTrip buffer
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        with FieldTripClient(host='localhost', port=1972, tmax=5,
                             wait_max=1) as rt_client:
            tmin_samp1 = rt_client.tmin_samp

    time.sleep(1)  # Pause measurement
    assert_true(len(w) == 1)

    # Start the FieldTrip buffer again
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always')
        with FieldTripClient(host='localhost', port=1972, tmax=5,
                             wait_max=1) as rt_client:
            print(rt_client.tmin_samp)
            tmin_samp2 = rt_client.tmin_samp

    kill_signal.put(False)  # stop the buffer
    assert_true(tmin_samp2 > tmin_samp1)
    assert_true(len(w) == 1)
Example #2
0
def test_fieldtrip_client():
    """Test fieldtrip_client"""

    neuromag2ft_fname = op.realpath(
        op.join(os.environ['NEUROMAG2FT_ROOT'], 'neuromag2ft'))

    kill_signal = queue.Queue()
    thread = threading.Thread(target=_run_buffer,
                              args=(kill_signal, neuromag2ft_fname))
    thread.daemon = True
    thread.start()
    time.sleep(0.25)

    try:
        # Start the FieldTrip buffer
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            with FieldTripClient(host='localhost',
                                 port=1972,
                                 tmax=5,
                                 wait_max=1) as rt_client:
                tmin_samp1 = rt_client.tmin_samp

        time.sleep(1)  # Pause measurement
        assert_true(len(w) >= 1)

        # Start the FieldTrip buffer again
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            with FieldTripClient(host='localhost',
                                 port=1972,
                                 tmax=5,
                                 wait_max=1) as rt_client:
                raw_info = rt_client.get_measurement_info()

                tmin_samp2 = rt_client.tmin_samp
                picks = mne.pick_types(raw_info,
                                       meg='grad',
                                       eeg=False,
                                       stim=False,
                                       eog=False)
                epoch = rt_client.get_data_as_epoch(n_samples=5, picks=picks)
                n_channels, n_samples = epoch.get_data().shape[1:]

                epoch2 = rt_client.get_data_as_epoch(n_samples=5, picks=picks)
                n_channels2, n_samples2 = epoch2.get_data().shape[1:]

                # case of picks=None
                epoch = rt_client.get_data_as_epoch(n_samples=5)

        assert_true(tmin_samp2 > tmin_samp1)
        assert_true(len(w) >= 1)
        assert_equal(n_samples, 5)
        assert_equal(n_samples2, 5)
        assert_equal(n_channels, len(picks))
        assert_equal(n_channels2, len(picks))
        kill_signal.put(False)  # stop the buffer
    except Exception:
        kill_signal.put(False)  # stop the buffer even if tests fail
        raise
Example #3
0
def _start_buffer_thread(buffer_port):
    """Start a FieldTrip Buffer in a background thread."""
    signal_queue = queue.Queue()
    thread = threading.Thread(target=_run_buffer,
                              args=(signal_queue, buffer_port))
    thread.daemon = True
    thread.start()
    return signal_queue
Example #4
0
def test_connection():
    """Test TCP/IP connection for StimServer <-> StimClient.
    """
    global _server, _have_put_in_trigger

    # have to start a thread to simulate the effect of two
    # different computers since stim_server.start() is designed to
    # be a blocking method

    # use separate queues because timing matters
    trig_queue1 = queue.Queue()
    trig_queue2 = queue.Queue()

    # start a thread to emulate 1st client
    thread1 = threading.Thread(target=_connect_client, args=(trig_queue1, ))
    thread1.daemon = True

    # start another thread to emulate 2nd client
    thread2 = threading.Thread(target=_connect_client, args=(trig_queue2, ))
    thread2.daemon = True

    thread1.start()
    thread2.start()
    with StimServer(port=4218, n_clients=2) as stim_server:
        _server = stim_server
        stim_server.start(timeout=10.0)  # don't allow test to hang

        # Add the trigger to the queue for both clients
        stim_server.add_trigger(20)
        _have_put_in_trigger = True  # monkey patch

        # the assert_equal must be in the test_connection() method
        # Hence communication between threads is necessary
        trig1 = trig_queue1.get(timeout=_max_wait)
        trig2 = trig_queue2.get(timeout=_max_wait)
        assert_equal(trig1, 20)

        # test if both clients receive the same trigger
        assert_equal(trig1, trig2)

    # test timeout for stim_server
    with StimServer(port=4218) as stim_server:
        assert_raises(StopIteration, stim_server.start, 0.1)