How to get available samples using pylsl library? (pull returning no samples)

102 Views Asked by At

I was hesitant to write this as the simple answer would be to do a self.inlet.pull_sample or a self.inlet.pull_chunk however several times I've done this and it has returned no samples. Upon investigating I noticed that when I do self.inlet.available samples it returns that there are in fact available samples.

With the code below I'll often get warning messages like Sample not found, trying again Available samples:10. Why is this and how do I retrieve those available samples?

from time import time
from numpy import array, asarray, floor, mean, ndarray, zeros
from pylsl import StreamInlet, resolve_byprop

from warnings import warn

class MyClass:
    def __init__(self, n_samples):
        streams = resolve_byprop('type', self.type, timeout=10)

        self.fs = int(streams[0].nominal_srate())    #usually 256 for an EEG stream

        self.n_samples = n_samples    #usually set to about 15
        self.timeout = 10*self.n_samples/self.fs

        self.inlet = StreamInlet(self.info, max_buflen=self.n_samples)

    def get_raw_chunk(self):
        self.inlet.flush()
        sample = array(self.inlet.pull_chunk(timeout=self.timeout)[0])
        start_time = time()

        while sample.size == 0:
            msg = "\nSample not found, trying again \nAvailable samples:" + str(
                self.inlet.samples_available())
            warn(msg)

            sample = array(self.inlet.pull_chunk(timeout=self.timeout)[0])

            if (time() - start_time > 10*self.timeout) and (sample.size == 0):
                msg = "\nCan't find samples, runtime:", time(
                )-self.started_at, "Available samples:", self.inlet.samples_available()
                raise TimeoutError(msg)

        return sample

When debugging in Visual Studio Code sometimes I'll find over 100 available samples and it still won't pull any: When debugging in Visual Studio Code sometimes I'll find over 100 available samples and it still won't pull any.

pylsl library

I've tried self.inlet.pull_sample and self.inlet.pull_chunk and it returned None even when self.inlet.samples_available() is not 0. I've also changed the order of code to check for samples before and after pulling them; it still finds available samples and won't pull them. I have also used self.inlet.buffers and found the data is in the buffer and is being updated there but not showing when pulling.

0

There are 0 best solutions below