.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "generated/tutorials/30_stream_manual.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_generated_tutorials_30_stream_manual.py: Automatic vs Manual acquisition =============================== .. include:: ./../../links.inc The :class:`~mne_lsl.stream.StreamLSL` object offers 2 mode of acquisition: automatic or manual. In automatic mode, the stream object acquires new chunks of samples at a regular interval. In manual mode, the user has to call the :meth:`~mne_lsl.stream.StreamLSL.acquire` to acquire new chunks of samples from the network. The automatic or manual acquisition is selected via the ``acquisition_delay`` argument of :meth:`~mne_lsl.stream.StreamLSL.connect`: - a non-zero positive integer value will set the acquisition to automatic mode with the specified delay in seconds. - ``0`` will set the acquisition to manual mode. Automatic acquisition --------------------- When the stream is set to automatically acquire new samples at a regular interval, a background thread is created with :class:`concurrent.futures.ThreadPoolExecutor`. The background thread is periodically receives a job to acquire new samples from the network. .. important:: If the main thread is hogging all of the CPU resources, the delay between two acquisition job might be longer than the specified delay. The background thread will always do its best to acquire new samples at the specified delay, but it is not able to do so if the CPU is busy. .. GENERATED FROM PYTHON SOURCE LINES 33-49 .. code-block:: Python import uuid from time import sleep from matplotlib import pyplot as plt from mne_lsl.datasets import sample from mne_lsl.player import PlayerLSL from mne_lsl.stream import StreamLSL # create a mock LSL stream for this tutorial fname = sample.data_path() / "sample-ant-raw.fif" source_id = uuid.uuid4().hex player = PlayerLSL(fname, chunk_size=200, source_id=source_id).start() player.info .. raw:: html
General
MNE object type Info
Measurement date Unknown
Participant Unknown
Experimenter mne_anonymize
Acquisition
Sampling frequency 1024.00 Hz
Channels
EEG
EOG
ECG
Stimulus
Galvanic skin response
Head & sensor digitization Not available
Filters
Highpass 0.00 Hz
Lowpass 512.00 Hz


.. GENERATED FROM PYTHON SOURCE LINES 50-55 .. note:: A ``chunk_size`` of 200 samples is used here to ensure stability and reliability while building the documentation on the CI. In practice, a ``chunk_size`` of 200 samples is too large to represent a real-time application. .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. code-block:: Python stream = StreamLSL(bufsize=2, source_id=source_id).connect(acquisition_delay=0.1) sleep(2) # wait for new samples print(f"New samples acquired: {stream.n_new_samples}") stream.disconnect() .. rst-class:: sphx-glr-script-out .. code-block:: none New samples acquired: 3200 .. GENERATED FROM PYTHON SOURCE LINES 62-69 Manual acquisition ------------------- In manual acquisition mode, the user has to call the :meth:`~mne_lsl.stream.StreamLSL.acquire` to get new samples from the network. In this mode, all operation happens in the main thread and the user has full control over when to acquire new samples. .. GENERATED FROM PYTHON SOURCE LINES 69-76 .. code-block:: Python stream = StreamLSL(bufsize=2, source_id=source_id).connect(acquisition_delay=0) sleep(2) # wait for new samples print(f"New samples acquired (before stream.acquire()): {stream.n_new_samples}") stream.acquire() print(f"New samples acquired (after stream.acquire()): {stream.n_new_samples}") .. rst-class:: sphx-glr-script-out .. code-block:: none New samples acquired (before stream.acquire()): 0 New samples acquired (after stream.acquire()): 2048 .. GENERATED FROM PYTHON SOURCE LINES 77-80 However, it is also now up to the user to make sure he acquires new samples regularly and does not miss part of the stream. The created :class:`~mne_lsl.lsl.StreamInlet` has its buffer set to the same value as the :class:`~mne_lsl.stream.StreamLSL` object. .. GENERATED FROM PYTHON SOURCE LINES 80-95 .. code-block:: Python stream.acquire() data1, ts1 = stream.get_data(picks="Cz") sleep(4) # wait for 2 buffers stream.acquire() data2, ts2 = stream.get_data(picks="Cz") f, ax = plt.subplots(1, 1, layout="constrained") ax.plot(ts1 - ts1[0], data1.squeeze(), color="blue", label="acq 1") ax.plot(ts2 - ts1[0], data2.squeeze(), color="red", label="acq 2") ax.legend() ax.set_xlabel("Time (s)") ax.set_ylabel("EEG amplitude") plt.show() .. image-sg:: /generated/tutorials/images/sphx_glr_30_stream_manual_001.png :alt: 30 stream manual :srcset: /generated/tutorials/images/sphx_glr_30_stream_manual_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 96-102 Free resources -------------- When you are done with a :class:`~mne_lsl.player.PlayerLSL` or :class:`~mne_lsl.stream.StreamLSL`, don't forget to free the resources they both use to continuously mock an LSL stream or receive new data from an LSL stream. .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python stream.disconnect() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python player.stop() .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 12.609 seconds) **Estimated memory usage:** 277 MB .. _sphx_glr_download_generated_tutorials_30_stream_manual.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 30_stream_manual.ipynb <30_stream_manual.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 30_stream_manual.py <30_stream_manual.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 30_stream_manual.zip <30_stream_manual.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_