Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make .get_data_as_epoch timeout a configurable parameter #49

Merged
merged 6 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def reset_warnings(gallery_conf, fname):
'DigMontage': 'mne.channels.DigMontage',
'ConductorModel': 'mne.bem.ConductorModel',
'EpochsSpectrum': 'mne.time_frequency.EpochsSpectrum',
'EpochsArray': 'mne.EpochsArray',
# mne_realtime
'RtEpochs': 'mne_realtime.RtEpochs',
}
Expand Down
20 changes: 14 additions & 6 deletions mne_realtime/lsl_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,42 @@ class LSLClient(_BaseClient):
"""

@fill_doc
def get_data_as_epoch(self, n_samples=1024, picks=None):
"""Return last n_samples from current time.
def get_data_as_epoch(self, n_samples=1024, picks=None, timeout=None):
"""Return n_samples from LSL in FIFO order.

Parameters
----------
n_samples : int
Number of samples to fetch.
%(picks_all)s
timeout : float | None
Maximum amount of time to wait for data from LSL
if None: waits for 5x n_samples / sfreq

Returns
-------
epoch : instance of Epochs
epoch : instance of EpochsArray | None
The samples fetched as an Epochs object.
None if no data was returned from pylsl.

See Also
--------
mne.Epochs.iter_evoked
"""
# set up timeout in case LSL process hang. wait arb 5x expected time
wait_time = n_samples * 5. / self.info['sfreq']
if timeout is None:
# set up timeout in case LSL process hang. wait arb 5x expected time
timeout = n_samples * 5. / self.info['sfreq']

# create an event at the start of the data collection
events = np.expand_dims(np.array([0, 1, 1]), axis=0)
_, timestamps = self.client.pull_chunk(
max_samples=min(n_samples, self.buffer.shape[0]),
timeout=wait_time,
timeout=timeout,
dest_obj=self.buffer,
)
if not timestamps:
return None

data = self.buffer[:len(timestamps)].transpose() # n_channels x n_samples

picks = _picks_to_idx(self.info, picks, 'all', exclude=())
Expand Down
12 changes: 12 additions & 0 deletions mne_realtime/tests/test_lsl_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ def test_lsl_client():

assert raw_info['nchan'], sfreq == epoch.get_data().shape[1:]


@requires_pylsl
@testing.requires_testing_data
def test_lsl_client_nodata():
"""Test that LSLClient gracefully handles no-data from LSL."""
raw = read_raw_fif(raw_fname)
raw_info = raw.info
with MockLSLStream(host, raw, ch_type='eeg', status=True):
with LSLClient(info=raw_info, host=host, wait_max=5) as client:
epoch = client.get_data_as_epoch(n_samples=0, timeout=0)
assert epoch is None

@requires_pylsl
def test_connect(mocker):
"""Mock connect to LSL stream."""
Expand Down