# I would like to calculate buffersize to use in mockrtclient example

**URL:** <https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834>\
**Category:** Support & Discussions\
**Tags:** eeg, mne-realtime\
**Created:** [October 30, 2022, 7:00pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834 "2022-10-30T19:00:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AbeerRehanKhan](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/abeerrehankhan/32/2028_2.png) [@AbeerRehanKhan](https://mne.discourse.group/u/AbeerRehanKhan)\
**Post date:** [October 30, 2022, 7:00pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/1 "2022-10-30T19:00:50Z")

</div>

- 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:

```python
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

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [October 31, 2022, 10:02am UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/2 "2022-10-31T10:02:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AbeerRehanKhan](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/abeerrehankhan/32/2028_2.png) [@AbeerRehanKhan](https://mne.discourse.group/u/AbeerRehanKhan)\
**Post date:** [November 1, 2022, 11:38am UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/3 "2022-11-01T11:38:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [November 1, 2022, 12:19pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/4 "2022-11-01T12:19:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AbeerRehanKhan](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/abeerrehankhan/32/2028_2.png) [@AbeerRehanKhan](https://mne.discourse.group/u/AbeerRehanKhan)\
**Post date:** [November 1, 2022, 12:34pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/5 "2022-11-01T12:34:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)\
**Post date:** [November 1, 2022, 12:40pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/6 "2022-11-01T12:40:08Z")

</div>

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

```python
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/](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:

```python
$ 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:

```python
$ 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.

---

<div class="post-metadata">

**Author:** ![AbeerRehanKhan](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/abeerrehankhan/32/2028_2.png) [@AbeerRehanKhan](https://mne.discourse.group/u/AbeerRehanKhan)\
**Post date:** [November 1, 2022, 12:48pm UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/7 "2022-11-01T12:48:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mukil](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Mukil](https://mne.discourse.group/u/Mukil)\
**Post date:** [February 1, 2023, 7:48am UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/8 "2023-02-01T07:48:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [February 1, 2023, 9:37am UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/9 "2023-02-01T09:37:58Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![wmvanvliet](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/wmvanvliet/32/100_2.png) [@wmvanvliet](https://mne.discourse.group/u/wmvanvliet)\
**Post date:** [February 1, 2023, 9:41am UTC](https://mne.discourse.group/t/i-would-like-to-calculate-buffersize-to-use-in-mockrtclient-example/5834/10 "2023-02-01T09:41:33Z")

</div>

> [@AbeerRehanKhan](#):
>
> 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

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.
