class LslStream(object): """ This class creates the basic connection between the computer and a Lab Streaming Layer data stream. With it connecting is made simpler and pulling and processing information directly is made trivial. METHODS: __init__(**stream_info): Initiates a connection when the class is called connect(**stream_info): Connects to a data stream in the network given defined by the keyword args pull(**kwargs): Pulls a sample from the connected data stream chunk(**kwargs): Pulls a chunk of samples from the data stream ATTRIBUTES: streams: List of found LSL streams in the network inlet: Stream inlet used to pull data from the stream metainfo: Metadata from the stream """ def __init__(self, **stream_info): self.connect(**stream_info) def connect(self, **stream_info): """ This method connects to a LSL data stream. It accepts keyword arguments that define the data stream we are searching. Normally this would be (use keywords given between quotes as key for the argument) 'name' (e.g. 'Cognionics Quick-20'), 'type' (e.g. 'EEG'), 'channels' (e.g. 8), 'freq' (from frequency, e.g. 500), 'dtype' (type of data, e.g. 'float32'), 'serialn' (e.g. 'quick_20'). After receiving the information of the stream, the script searches for it in the network and resolves it, and then connects to it (or the first one in case there are many, that's the reason why one has to be as specific as possible if many instances of LSL are being used in the lab). It prints some of the metadata of the data stream to the screen so the user can check if it is right, and returns the inlet to be used in other routines. INPUT: **kwargs: Keyword arguments defining the data stream RELATED ATTRIBUTES: streams, inlet, metainfo """ # Put the known information of the stream in a tuple. It is better to know as much # as possible if more than one kit is running LSL at the same time. stream_info_list = [] for key, val in stream_info.items(): stream_info_list.append(key) stream_info_list.append(val) # Resolve the stream from the lab network self.streams = resolve_stream(*stream_info_list) # Create a new inlet to read from the stream self.inlet = StreamInlet(self.streams[0], max_buflen=938) # Get stream information (including custom meta-data) and break it down self.metainfo = self.inlet.info() def pull(self, **kwargs): """ This method pulls data from the connected stream (using more information for the pull as given by kwargs). INPUT: kwargs: Extra specifications for the data pull from the stream OUTPUT: the data from the stream """ # Retrieve data from the data stream return self.inlet.pull_sample(**kwargs) def init_pull(self, **kwargs): """ This serves as a sacrificial initialization pull to get the streaming going. This method pulls data from the connected stream (using more information for the pull as given by kwargs). INPUT: kwargs: Extra specifications for the data pull from the stream OUTPUT: the data from the stream """ print('Marker Stream Initializarion Pull: ', self.inlet.pull_sample(**kwargs)) def chunk(self, **kwargs): """ This method pulls chunks. Uses sames formating as .pull """ # chunk, timestamp = self.inlet.pull_chunk(**kwargs) return self.inlet.pull_chunk(**kwargs) def mark_check(self, **kwargs): # Available Markers. ava_markers = self.inlet.samples_available() print('Available Markers: ', ava_markers) def inlet_del(self, **kwargs): self.inlet.__del__()
### Test Area t = time.time() ## Teste: Tempo entre cada timestamp e cada coleta de dado # sample1, timestamp1 = inlet.pull_sample() # sample2, timestamp2 = inlet.pull_sample() # print(time.time_ns()) # print(timestamp1) # print(timestamp2) ## Resultado: 0,00390625s (1/256) ## Teste: Dados introduzidos no vetor após 1s de execução arr=[] while time.time() - t < 1: sample, timestamp = inlet.pull_sample() print(inlet.samples_available()) arr.append(sample) print("depois de 1s:") print(len(arr)) print(arr) ## Resultado: 257 ### sample, timestamp = inlet.pull_sample() time_window = np.array(sample) sample, timestamp = inlet.pull_sample() sample = np.array(sample) time_window = np.column_stack((sample, time_window))
print("found streams") brain_inlet = StreamInlet(brain_stream[0]) video_inlet = StreamInlet(video_stream[0]) brain_inlet.open_stream() video_inlet.open_stream() saver = Saver() video_info = None print("While entered") try: timestamp = None while True: brain_info, timestamp = brain_inlet.pull_sample() if video_inlet.samples_available(): video_info, _ = video_inlet.pull_sample() video_info = video_info[0] saver.check_reading(video_info) saver.save_chunk(video_info) if saver.status: saver.add_data(timestamp, brain_info, video_info) saver.save_data(video_info) saver.check_reading_final(video_info) video_info = None except KeyboardInterrupt: brain_inlet.close_stream() video_inlet.close_stream() except Exception as e: print(e) brain_inlet.close_stream()