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
reject=dict(grad=4000e-13, eog=150e-6), decim=1, isi_max=2.0, proj=None) rt_epochs.start() for ii, ev in enumerate(rt_epochs.iter_evoked()): print("Just got epoch %d" % (ii + 1)) ev.pick_types(meg=True, eog=False) if ii == 0: evoked = ev else: evoked = mne.combine_evoked([evoked, ev], weights='nave') ax[0].cla() ax[1].cla() # clear axis plot_events(rt_epochs.events[-5:], sfreq=ev.info['sfreq'], first_samp=-rt_client.tmin_samp, axes=ax[0]) # plot on second subplot evoked.plot(axes=ax[1], selectable=False, time_unit='s') ax[1].set_title('Evoked response for gradiometer channels' '(event_id = %d)' % event_id) plt.pause(0.05 / speedup) plt.draw() rt_epochs.stop()
# create the real-time epochs object and start acquisition rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, stim_channel='STI 014', picks=picks, reject=dict(grad=4000e-13, eog=150e-6), decim=1, isi_max=2.0, proj=None) rt_epochs.start() for ii, ev in enumerate(rt_epochs.iter_evoked()): print("Just got epoch %d" % (ii + 1)) ev.pick_types(meg=True, eog=False) if ii == 0: evoked = ev else: evoked = mne.combine_evoked([evoked, ev], weights='nave') ax[0].cla() ax[1].cla() # clear axis plot_events(rt_epochs.events[-5:], sfreq=ev.info['sfreq'], first_samp=-rt_client.tmin_samp, axes=ax[0]) # plot on second subplot evoked.plot(axes=ax[1], selectable=False, time_unit='s') ax[1].set_title('Evoked response for gradiometer channels' '(event_id = %d)' % event_id) plt.pause(0.05 / speedup) plt.draw() rt_epochs.stop()