def test_fieldtrip_rtepochs(free_tcp_port, tmpdir): """Test FieldTrip RtEpochs.""" raw_tmax = 7 raw = read_raw_fif(raw_fname, preload=True) raw.crop(tmin=0, tmax=raw_tmax) events_offline = find_events(raw, stim_channel='STI 014') event_id = list(np.unique(events_offline[:, 2])) tmin, tmax = -0.2, 0.5 epochs_offline = Epochs(raw, events_offline, event_id=event_id, tmin=tmin, tmax=tmax) epochs_offline.drop_bad() isi_max = (np.max(np.diff(epochs_offline.events[:, 0])) / raw.info['sfreq']) + 1.0 neuromag2ft_fname = op.realpath(op.join(os.environ['NEUROMAG2FT_ROOT'], 'neuromag2ft')) # Works with neuromag2ft-3.0.2 cmd = (neuromag2ft_fname, '--file', raw_fname, '--speed', '8.0', '--bufport', str(free_tcp_port)) with running_subprocess(cmd, after='terminate', verbose=False): data_rt = None events_ids_rt = None with pytest.warns(RuntimeWarning, match='Trying to guess it'): with FieldTripClient(host='localhost', port=free_tcp_port, tmax=raw_tmax, wait_max=2) as rt_client: # get measurement info guessed by MNE-Python raw_info = rt_client.get_measurement_info() assert ([ch['ch_name'] for ch in raw_info['chs']] == [ch['ch_name'] for ch in raw.info['chs']]) # create the real-time epochs object epochs_rt = RtEpochs(rt_client, event_id, tmin, tmax, stim_channel='STI 014', isi_max=isi_max) epochs_rt.start() time.sleep(0.5) for ev_num, ev in enumerate(epochs_rt.iter_evoked()): if ev_num == 0: data_rt = ev.data[None, :, :] events_ids_rt = int( ev.comment) # comment attribute contains event_id else: data_rt = np.concatenate( (data_rt, ev.data[None, :, :]), axis=0) events_ids_rt = np.append(events_ids_rt, int(ev.comment)) _call_base_epochs_public_api(epochs_rt, tmpdir) epochs_rt.stop(stop_receive_thread=True) assert_array_equal(events_ids_rt, epochs_rt.events[:, 2]) assert_array_equal(data_rt, epochs_rt.get_data()) assert len(epochs_rt) == len(epochs_offline) assert_array_equal(events_ids_rt, epochs_offline.events[:, 2]) assert_allclose(epochs_rt.get_data(), epochs_offline.get_data(), rtol=1.e-5, atol=1.e-8) # defaults of np.isclose
def test_fieldtrip_rtepochs(free_tcp_port, tmpdir): """Test FieldTrip RtEpochs.""" raw_tmax = 7 raw = read_raw_fif(raw_fname, preload=True) raw.crop(tmin=0, tmax=raw_tmax) events_offline = find_events(raw, stim_channel='STI 014') event_id = list(np.unique(events_offline[:, 2])) tmin, tmax = -0.2, 0.5 epochs_offline = Epochs(raw, events_offline, event_id=event_id, tmin=tmin, tmax=tmax) epochs_offline.drop_bad() isi_max = (np.max(np.diff(epochs_offline.events[:, 0])) / raw.info['sfreq']) + 1.0 kill_signal = _start_buffer_thread(free_tcp_port) try: data_rt = None events_ids_rt = None with pytest.warns(RuntimeWarning, match='Trying to guess it'): with FieldTripClient(host='localhost', port=free_tcp_port, tmax=raw_tmax, wait_max=2) as rt_client: # get measurement info guessed by MNE-Python raw_info = rt_client.get_measurement_info() assert ([ch['ch_name'] for ch in raw_info['chs']] == [ch['ch_name'] for ch in raw.info['chs']]) # create the real-time epochs object epochs_rt = RtEpochs(rt_client, event_id, tmin, tmax, stim_channel='STI 014', isi_max=isi_max) epochs_rt.start() time.sleep(0.5) for ev_num, ev in enumerate(epochs_rt.iter_evoked()): if ev_num == 0: data_rt = ev.data[None, :, :] events_ids_rt = int( ev.comment) # comment attribute contains event_id else: data_rt = np.concatenate( (data_rt, ev.data[None, :, :]), axis=0) events_ids_rt = np.append(events_ids_rt, int(ev.comment)) _call_base_epochs_public_api(epochs_rt, tmpdir) epochs_rt.stop(stop_receive_thread=True) assert_array_equal(events_ids_rt, epochs_rt.events[:, 2]) assert_array_equal(data_rt, epochs_rt.get_data()) assert len(epochs_rt) == len(epochs_offline) assert_array_equal(events_ids_rt, epochs_offline.events[:, 2]) assert_allclose(epochs_rt.get_data(), epochs_offline.get_data(), rtol=1.e-5, atol=1.e-8) # defaults of np.isclose finally: kill_signal.put(False) # stop the buffer
def test_mockclient(): """Test the RtMockClient.""" event_id, tmin, tmax = 1, -0.2, 0.5 epochs = Epochs(raw, events[:7], event_id=event_id, tmin=tmin, tmax=tmax, picks=picks, baseline=(None, 0), preload=True) data = epochs.get_data() rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_data = rt_epochs.get_data() assert_true(rt_data.shape == data.shape) assert_array_equal(rt_data, data)
def test_mockclient(): """Test the RtMockClient.""" raw = mne.io.read_raw_fif(raw_fname, preload=True, verbose=False, add_eeg_ref=False) picks = mne.pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=True, exclude=raw.info['bads']) event_id, tmin, tmax = 1, -0.2, 0.5 epochs = Epochs(raw, events[:7], event_id=event_id, tmin=tmin, tmax=tmax, picks=picks, baseline=(None, 0), preload=True, add_eeg_ref=False) data = epochs.get_data() rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, isi_max=0.5, add_eeg_ref=False) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) rt_data = rt_epochs.get_data() assert_true(rt_data.shape == data.shape) assert_array_equal(rt_data, data)
def test_mockclient(tmpdir): """Test the RtMockClient.""" raw = read_raw_fif(raw_fname, preload=True, verbose=False) picks = pick_types(raw.info, meg='grad', eeg=False, eog=True, stim=True, exclude=raw.info['bads']) event_id, tmin, tmax = 1, -0.2, 0.5 epochs = Epochs(raw, events[:7], event_id=event_id, tmin=tmin, tmax=tmax, picks=picks, baseline=(None, 0), preload=True) data = epochs.get_data() rt_client = MockRtClient(raw) # choose "large" value, should always be longer than execution time of # get_data() isi_max = 0.5 rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, isi_max=isi_max) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=0, tmax=10, buffer_size=1000) # get_data() should return immediately and not wait for the timeout start_time = time.time() rt_data = rt_epochs.get_data() retrieval_time = time.time() - start_time assert retrieval_time < isi_max assert rt_data.shape == data.shape assert_array_equal(rt_data, data) assert len(rt_epochs) == len(epochs) # iteration over epochs should block until timeout rt_iter_data = list() start_time = time.time() for cur_epoch in rt_epochs: rt_iter_data.append(cur_epoch) retrieval_time = time.time() - start_time assert retrieval_time >= isi_max rt_iter_data = np.array(rt_iter_data) assert rt_iter_data.shape == data.shape assert_array_equal(rt_iter_data, data) assert len(rt_epochs) == len(epochs) _call_base_epochs_public_api(rt_epochs, tmpdir)
def test_rejection(buffer_size): """Test rejection.""" event_id, tmin, tmax = 1, 0.0, 0.5 sfreq = 1000 ch_names = ['Fz', 'Cz', 'Pz', 'STI 014'] raw_tmax = 5 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=['eeg', 'eeg', 'eeg', 'stim']) raw_array = np.random.randn(len(ch_names), raw_tmax * sfreq) raw_array[-1, :] = 0 epoch_start_samples = np.arange(raw_tmax) * sfreq raw_array[-1, epoch_start_samples] = event_id reject_threshold = np.max(raw_array) - np.min(raw_array) + 1 reject = {'eeg': reject_threshold} epochs_to_reject = [1, 3] epochs_to_keep = np.setdiff1d(np.arange(len(epoch_start_samples)), epochs_to_reject) expected_drop_log = [list() for _ in range(len(epoch_start_samples))] for cur_epoch in epochs_to_reject: raw_array[1, epoch_start_samples[cur_epoch]] = reject_threshold + 1 expected_drop_log[cur_epoch] = [ch_names[1]] raw = RawArray(raw_array, info) events = find_events(raw, shortest_event=1, initial_event=True) picks = pick_types(raw.info, eeg=True) epochs = Epochs(raw, events, event_id=event_id, tmin=tmin, tmax=tmax, baseline=None, picks=picks, preload=True, reject=reject) epochs_data = epochs.get_data() assert len(epochs) == len(epoch_start_samples) - len(epochs_to_reject) assert_array_equal(epochs_data[:, 1, 0], raw_array[1, epoch_start_samples[epochs_to_keep]]) assert_array_equal(epochs.drop_log, expected_drop_log) assert_array_equal(epochs.selection, epochs_to_keep) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, baseline=None, isi_max=0.5, find_events=dict(initial_event=True), reject=reject) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=0, tmax=raw_tmax, buffer_size=buffer_size) assert len(rt_epochs) == len(epochs_to_keep) assert_array_equal(rt_epochs.drop_log, expected_drop_log) assert_array_equal(rt_epochs.selection, epochs_to_keep) rt_data = rt_epochs.get_data() assert rt_data.shape == epochs_data.shape assert_array_equal(rt_data, epochs_data)
def test_rejection(buffer_size): event_id, tmin, tmax = 1, 0.0, 0.5 sfreq = 1000 ch_names = ['Fz', 'Cz', 'Pz', 'STI 014'] raw_tmax = 5 info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=['eeg', 'eeg', 'eeg', 'stim']) raw_array = np.random.randn(len(ch_names), raw_tmax * sfreq) raw_array[-1, :] = 0 epoch_start_samples = np.arange(raw_tmax) * sfreq raw_array[-1, epoch_start_samples] = event_id reject_threshold = np.max(raw_array) - np.min(raw_array) + 1 reject = {'eeg': reject_threshold} epochs_to_reject = [1, 3] epochs_to_keep = np.setdiff1d(np.arange(len(epoch_start_samples)), epochs_to_reject) expected_drop_log = [list() for _ in range(len(epoch_start_samples))] for cur_epoch in epochs_to_reject: raw_array[1, epoch_start_samples[cur_epoch]] = reject_threshold + 1 expected_drop_log[cur_epoch] = [ch_names[1]] raw = RawArray(raw_array, info) events = find_events(raw, shortest_event=1, initial_event=True) picks = pick_types(raw.info, eeg=True) epochs = Epochs(raw, events, event_id=event_id, tmin=tmin, tmax=tmax, baseline=None, picks=picks, preload=True, reject=reject) epochs_data = epochs.get_data() assert len(epochs) == len(epoch_start_samples) - len(epochs_to_reject) assert_array_equal(epochs_data[:, 1, 0], raw_array[1, epoch_start_samples[epochs_to_keep]]) assert_array_equal(epochs.drop_log, expected_drop_log) assert_array_equal(epochs.selection, epochs_to_keep) rt_client = MockRtClient(raw) rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks, baseline=None, isi_max=0.5, find_events=dict(initial_event=True), reject=reject) rt_epochs.start() rt_client.send_data(rt_epochs, picks, tmin=0, tmax=raw_tmax, buffer_size=buffer_size) assert len(rt_epochs) == len(epochs_to_keep) assert_array_equal(rt_epochs.drop_log, expected_drop_log) assert_array_equal(rt_epochs.selection, epochs_to_keep) rt_data = rt_epochs.get_data() assert rt_data.shape == epochs_data.shape assert_array_equal(rt_data, epochs_data)
n_times = len(rt_epochs.times) scores_x, scores, std_scores = [], [], [] # define a simple linear svm classifier filt = FilterEstimator(rt_epochs.info, 1, 40) scaler = preprocessing.StandardScaler() vectorizer = Vectorizer() clf = SVC(C=1, kernel='linear') concat_classifier = Pipeline([('filter', filt), ('vector', vectorizer), ('scaler', scaler), ('svm', clf)]) data_picks = mne.pick_types(rt_epochs.info, meg='grad', eeg=False, eog=True, stim=False, exclude=raw.info['bads']) for ev_num, ev in enumerate(rt_epochs.iter_evoked()): print("Just got epoch %d" % (ev_num + 1)) print rt_epochs.get_data().shape print ev.data.shape if ev_num == 0: X = ev.data[None, data_picks, :] y = int(ev.comment) # the comment attribute contains the event_id else: X = np.concatenate((X, ev.data[None, data_picks, :]), axis=0) y = np.append(y, int(ev.comment)) if ev_num >= min_trials: cv = ShuffleSplit(len(y), 5, test_size=0.2, random_state=42) scores_t = cross_val_score(concat_classifier, X, y, cv=cv, n_jobs=1) * 100 std_scores.append(scores_t.std()) scores.append(scores_t.mean()) scores_x.append(ev_num)