Пример #1
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)
Пример #2
0
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = mne.io.read_raw_fif(raw_fname, preload=True)

fig, ax = plt.subplots(1)
ax.set(xlabel='Trials',
       ylabel='Classification score (% correct)',
       title='Real-time feedback')
isi = 0.01  # this is unrealistic, but will make the example run quickly
n_trials = 40  # number of trials to simulate
n_start = 5  # number of trials to run before decoding
rng = np.random.RandomState(0)

# Instantiating stimulation server
# The with statement is necessary to ensure a clean exit
with StimServer(port=4218) as stim_server:

    # The channels to be used while decoding
    picks = mne.pick_types(raw.info, meg='grad')

    rt_client = MockRtClient(raw)

    # Constructing the pipeline for classification
    # don't highpass filter because of short signal length of epochs
    scaler = preprocessing.StandardScaler()
    vectorizer = Vectorizer()
    clf = SVC(C=1, kernel='linear')
    concat_classifier = Pipeline([('vector', vectorizer), ('scaler', scaler),
                                  ('svm', clf)])
    ev_list = list(rng.randint(3, 5, n_start))  # some random starting events
    score_lv, score_rv, score_x = [], [], []
from mne.datasets import sample
from mne.realtime import StimServer
from mne.realtime import MockRtClient
from mne.decoding import ConcatenateChannels, FilterEstimator

print(__doc__)

# Load fiff file to simulate data
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = mne.io.Raw(raw_fname, preload=True)

# Instantiating stimulation server

# The with statement is necessary to ensure a clean exit
with StimServer('localhost', port=4218) as stim_server:

    # The channels to be used while decoding
    picks = mne.pick_types(raw.info,
                           meg='grad',
                           eeg=False,
                           eog=True,
                           stim=True,
                           exclude=raw.info['bads'])

    rt_client = MockRtClient(raw)

    # Constructing the pipeline for classification
    filt = FilterEstimator(raw.info, 1, 40)
    scaler = preprocessing.StandardScaler()
    concatenator = ConcatenateChannels()