# How to make mne raw from LSL in realtime

**URL:** <https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534>\
**Category:** Support & Discussions\
**Tags:** mne-realtime\
**Created:** [August 11, 2021, 8:23pm UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534 "2021-08-11T20:23:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyuwanchoi](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kyuwanchoi](https://mne.discourse.group/u/kyuwanchoi)\
**Post date:** [August 11, 2021, 8:23pm UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534/1 "2021-08-11T20:23:55Z")

</div>

Dear MNE user,

Currently, I am using Zeto dry EEG headset and try to read the data in real-time through LSL. After reading the data in real-time, I would like process it with mne in real-time. If I enter the data of sample in LSL code into data variable as follows, can I make raw data of mne?

raw = mne.io.RawArray(data, info)

from pylsl import StreamInlet, resolve\_stream

def main():  
# first resolve an EEG stream on the lab network  
print(“looking for an EEG stream…”)  
streams = resolve\_stream(‘type’, ‘EEG’)

```
# create a new inlet to read from the stream
inlet = StreamInlet(streams[0])

while True:
    # get a new sample (you can also omit the timestamp part if you're not
    # interested in it)
    sample, timestamp = inlet.pull_chunk()
    print(timestamp, sample)

```

if **name** == ‘ **main** ’:  
main()

---

<div class="post-metadata">

**Author:** ![lokinou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/lokinou/32/756_2.png) [@lokinou](https://mne.discourse.group/u/lokinou)\
**Post date:** [August 12, 2021, 12:01am UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534/2 "2021-08-12T00:01:17Z")

</div>

It’s not easy but here is how I did it in one of my projects. @kyuwanchoi

1. retrieve the sampling rate from `info.nominal_srate()` looping on `for info in pylsl.resolve_streams()` and making sure you catch the right one by checking `info.name()` or other parameters
2. define the SteamInlet with the max\_buflen parameter matching the max number of seconds you want to retrieve, for me it was 2 seconds `pylsl.StreamInlet(info, max_buflen=2000, processing_flags=pylsl.proc_clocksync | pylsl.proc_dejitter)`
3. Create an empty numpy buffer or `np.ndarray(N electrodes, N seconds x sampling rate)`
4. This is still a mistery to me how to do it right, but periodically retrieve your data properly in a numpy array the size of the buffer.
5. create a mne.io.RawArray. I think you have to use `y = np.transpose(array)` because lsl and MNE have different structure. Then create a raw off your info and data `raw = mne.io.RawArray(data=y, info=data_inlet.mne_info, verbose=False)`
6. Try to find a way to rename the channels. Also, make sure to put a filename to prevent a bug in plotting your data: raw.\_filenames = [‘lsl\_is\_not\_a\_file\_duuh’]
7. Now you don’t want to reinitialize any raw. Copy your raw and perform stuff you want to do on it. Just make a copy before `reusable_raw = raw.copy()`
8. Erase the data from it `reusable_raw ._data = None`
9. Pickle the raw, it will keep information stored along with the raw: `pickled_raw= pickle.dumps(reusable_raw )`. Of course don’t do this after you performed any data transformation, but before.
10. on the next iteration, load the pickled raw `raw = pickle.loads(pickled_raw)`. and put the new data directly inside `raw._data = y`. Enjoy massive performance gains by using the pickle trick (“I’m Pickle riiick”)

Good luck 🙂

---

<div class="post-metadata">

**Author:** ![kyuwanchoi](https://avatars.discourse-cdn.com/v4/letter/k/a698b9/32.png) [@kyuwanchoi](https://mne.discourse.group/u/kyuwanchoi)\
**Post date:** [August 12, 2021, 12:38am UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534/3 "2021-08-12T00:38:54Z")

</div>

Hi lokinou, I really appreciate your support! Your reply is very helpful for me. By the way, do you have the code? I want to see the code.

Kyu

---

<div class="post-metadata">

**Author:** ![lokinou](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/lokinou/32/756_2.png) [@lokinou](https://mne.discourse.group/u/lokinou)\
**Post date:** [August 16, 2021, 9:59am UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534/4 "2021-08-16T09:59:50Z")

</div>

Glad you appreciate @kyuwanchoi . I can’t share the code, that’s why you got a nice description of how to do it :).

Please let me know if you managed to get through it.

---

<div class="post-metadata">

**Author:** ![MathieuSch-101](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mathieusch-101/32/367_2.png) [@MathieuSch-101](https://mne.discourse.group/u/MathieuSch-101)\
**Post date:** [August 16, 2021, 11:24am UTC](https://mne.discourse.group/t/how-to-make-mne-raw-from-lsl-in-realtime/3534/5 "2021-08-16T11:24:41Z")

</div>

If you want, you can have a look at NeuroDecode, especially to my [fork](https://github.com/bsl-tools/bsl) which improves the low-level LSL communication classes and methods. I will soon rename the library to bsl and publish it on pip.

Basically, it would look like this for EEG:

```
import mne
from neurodecode import StreamReceiver

sr = StreamReceiver(stream_name='Name')

sr.acquire()
data, timestamps = sr.get_window() # data is samples x channels
data *= 1e-6 # convert to Volts, to be adapted.
info = mne.create_info(
    ch_names=sr.streams['Name'].ch_list, 
    sfreq=sr.streams['Name'].sample_rate, 
    ch_types=['eeg']*len(sr.streams['Name'].ch_list))
raw = mne.io.RawArray(data.T, info) # expect channels x samples

```

The StreamReceiver allows you to connect to multiple LSL streams and to retrieve data from one or more of those streams. It basically is a wrapper to simplify the use of pylsl.

For recording, you have a StreamRecorder provided in the lib.
