I would like to calculate buffersize to use in mockrtclient example

  • MNE version: e.g. 0.24.0
  • operating system: Windows 10

The data which I am using is summarized as follows:

Measurement date Unknown
Experimenter Unknown
Participant Unknown
Digitized points 0 points
Good channels 63 EEG, 1 ECG, 1 Stimulus
Bad channels None
EOG channels Not available
ECG channels ECG
Sampling frequency 8000.00 Hz
Highpass 0.00 Hz
Lowpass 4000.00 Hz
Duration 00:10:07 (HH:MM:SS)

Now, I need to plot mock real time data using this code:

rt_client = MockRtClient(new_raw)
rt_epochs = RtEpochs(rt_client, event_id, tmin, tmax, picks=picks,decim=1,stim_channel='STI')
rt_epochs.start()
rt_client.send_data(rt_epochs, picks, tmin=0, tmax=100, buffer_size=28)
for ii, ev in enumerate(rt_epochs.iter_evoked()):
     print("Just got epoch %d" % (ii + 1))
     ev.pick_types(eeg= True, meg=False, eog=False)  # leave out the eog channel
     if ii == 0:
         evoked = ev
     else:
         evoked = mne.combine_evoked([evoked, ev], weights='nave')
     plt.clf()  # clear canvas
     evoked.plot(axes=plt.gca(), time_unit='s')  # plot on current figure
     plt.pause(1)

Now I need to understand what the buffer size is and how it can be calculated? I know the buffer size is equivalent to number of samples but could someone who worked on mne realtime expand upon this

I don’t have an answer to your question, but I have a comment on your sampling frequency. 8 kHz is gigantic for EEG acquisition, and there are only downsides in recording at such a high sampling rate, especially in real-time applications.

Make sure you need such a high sampling-rate, and if you are unsure, you can expand on the rationale behind your choice if you want some feedback.

I reduced the sampling rate down to 100 Hz but the data visualization stops after parsing data for 2 or 3 graphs depending on the buffer size and stops with the comment
“Time of 2.0 seconds exceeded.”
I really have no idea how MNE real-time is working

Let’s go back one step: You are trying to acquire EEG data in real-time, what do you want to do with it in real time? What is your goal and what is your EEG system?

I already have the data in a file.
Basically, I was provided sample data in a mat file which I have already converted to Mne raw data structure. And already filtered it to the frequency range I need. I also downsampled it to 100 Hz.
Now, what I want is to use mockRtclient to simulate real-time and use it to plot the data in simulated real-time as well as plot PSD from it.

Ok, but why do you want to complicate your life by replaying the data instead of doing some offline analysis? e.g.

from mne.io import read_raw_fif

raw = read_raw_fif(file, preload=True)
raw.plot()
spectrum = raw.compute_psd()

Anyway, I can’t speak for mne-realtime and for the FieldTrip client on which I don’t have any experience, but if you have the data in .fif file, I can suggest using LSL via https://bsl-tools.github.io/

In a command prompt/terminal, create a server that will make the data from your file available on the network:

$ bsl_stream_player FakeStream path/to/dataset-raw.fif

And in a second command prompt/terminal, start a viewer to plot the data in real-time:

$ bsl_stream_viewer

Edit: Also 100 Hz is quite low… Usually EEG data is sampled at 500 Hz, 512 Hz, 1000 Hz or 1024 Hz.

Thank you for the advice. I will try it out.
My task requires that I test out MNE-realtime to see if other similar continuous data can be used to do realtime analysis. That is why I am using realtime.

Are there any workarounds for online analysis of continuous EEG real-time data with MNE python? If not, what would be the way to go about it?

@AbeerRehanKhan If it’s still relevant: the reason why you get buffer errors is because of the plt.pause(1). The epochs are coming in faster than 1 second apart. Hence data is being produced faster than it is being read. Hence the buffer will slowly fill up and then run over. Try plt.pause(0.1) instead.

When streaming realtime data, data is typically coming in chunks. Reading 1 sample, then reading another 1 sample, is just too slow even for modern hardware. Rather, with a buffer size of 28, we wait until 28 samples are produced (how long this takes depends on the sampling rate) and then all 28 samples are read at once. In other words, the MockRtClient is producing a stream of little Raw objects of length 28.

Note that this is not related to your buffer problem in the code you posted at the top. That was about not reading data as fast as it was produced.

1 Like