Difference between revisions of "DataHub"

From TSG Doc
Jump to navigation Jump to search
Line 49: Line 49:
 
A short example for sending lsl streaming data:
 
A short example for sending lsl streaming data:
 
<syntaxhighlight lang="python" line>
 
<syntaxhighlight lang="python" line>
#!/usr/bin/env python
+
#!/usr/bin/env python3.10
 +
# -*- coding: utf-8 -*-
  
import threading
+
import time
from pylsl import StreamInfo, StreamOutlet
+
import numpy as np
 +
import keyboard
 +
import random
 +
from pylsl import StreamInfo, StreamOutlet, local_clock
 +
from LSLsettings import MarkerStreamSettings, DynamicMultiChannelStreamSettings
  
def getData():
+
print(f"Setting up streams...")
    while running:
+
marker_settings = MarkerStreamSettings()
        buffer_in = getSensorData()
 
        send_data = True
 
        time.sleep(0.001)
 
  
info = StreamInfo(
+
# info marker StreamInfo, outlet
     name='MyStream',  
+
marker_info = StreamInfo(
     type='COP',  
+
     name=marker_settings.stream_name,
     channel_count=4,  
+
     type=marker_settings.stream_type,
     nominal_srate = 200,  
+
     channel_count=marker_settings.channel_count,
     source_id='BalanceBoard_stream'
+
     nominal_srate=marker_settings.sample_rate,
    )
+
    channel_format=marker_settings.channel_format,
outlet = StreamOutlet(info)  
+
     source_id=marker_settings.source_id
 +
)
 +
marker_outlet = StreamOutlet(marker_info, chunk_size=marker_settings.push_chunk_size)
  
threading.Thread(target=getData).start()
+
data_settings = DynamicMultiChannelStreamSettings(
while running == True:
+
    n_channels=4,
if send_data:
+
    stream_name="TestStream",
outlet.push_chunk(buffer_in)
+
    stream_type="TestData",
send_data = False
+
    sample_rate=100,
</syntaxhighlight>
+
    channel_names=["A", "B", "C", "D"]
 +
)
  
A short example for receiving lsl data:
+
# info data StreamInfo, outlet
<syntaxhighlight lang="python" line>
+
data_info = StreamInfo(
#!/usr/bin/env python
+
    name=data_settings.stream_name,
 +
    type=data_settings.stream_type,
 +
    channel_count=data_settings.channel_count,
 +
    nominal_srate=data_settings.sample_rate,
 +
    channel_format=data_settings.channel_format,
 +
    source_id=data_settings.source_id
 +
)
 +
data_outlet = StreamOutlet(data_info, chunk_size=data_settings.push_chunk_size)
  
from pylsl import StreamInlet, resolve_stream
+
running = True
 +
marker_outlet.push_sample(["Start"])
 +
buffer_dtype = object if data_settings.channel_format == "string" else float
 +
buffer_in = np.array(data_settings.default_buffer, dtype=buffer_dtype)
  
streams = resolve_stream('name', 'MyStream')
+
print("Start streaming... Press 'q' to stop.")
#streams = resolve_streams()
+
 
 +
while not(keyboard.is_pressed('q')):
 +
 
 +
    random_numbers = [random.randint(0, 1) for _ in range(4)]
 +
    random_numbers = [num + 1 for num in random_numbers]
 +
    data_outlet.push_sample(random_numbers)
 +
 
 +
    time.sleep(data_settings.push_interval)
 +
 
 +
marker_outlet.push_sample(["Stop"])
  
inlet = StreamInlet(streams[0])
 
while True:
 
    sample, timestamp = inlet.pull_sample()
 
    print(timestamp, sample)
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 11:13, 28 April 2025

DataHub
Developer(s)Christian Kothe; Chadwick Boulay.
Development status-in development-
Written inC, C++, Python, Java, C#, MATLAB
Operating systemWindows, Linux, MacOS, Android, iOS
TypeData collection
LicenseOpen source
WebsiteLSL webpage

Resources

DataHub
Downloads
Manuals
Templates

The DataHub Makes use of the lab streaming layer. The lab streaming layer (LSL) is a system for the unified collection of measurement time series in research experiments that handles both the networking, time-synchronization, (near-) real-time access as well as optionally the centralized collection, viewing and disk recording of the data.


Installation

Our support for LSL is mainly done in python. Download python here: Please choose a 64 bit version.. Run the installer and make sure to add Python to the file path (it's an option in the installer). Open a command prompt, start with upgrading the pip installer by typing:
c:>python -m pip install --upgrade pip
Then:
c:>pip install pylsl

more info: cross platform pylsl

Versions

The TSG uses the version 1.15.0. Open a command and type the following to find the version used:

c:>python
>>> import pylsl
>>> print(pylsl.__version__)

Usage

Python

A short example for sending lsl streaming data:

 1#!/usr/bin/env python3.10
 2# -*- coding: utf-8 -*-
 3
 4import time
 5import numpy as np
 6import keyboard
 7import random
 8from pylsl import StreamInfo, StreamOutlet, local_clock
 9from LSLsettings import MarkerStreamSettings, DynamicMultiChannelStreamSettings
10
11print(f"Setting up streams...")
12marker_settings = MarkerStreamSettings()
13
14# info marker StreamInfo, outlet
15marker_info = StreamInfo(
16    name=marker_settings.stream_name,
17    type=marker_settings.stream_type,
18    channel_count=marker_settings.channel_count,
19    nominal_srate=marker_settings.sample_rate,
20    channel_format=marker_settings.channel_format,
21    source_id=marker_settings.source_id
22)
23marker_outlet = StreamOutlet(marker_info, chunk_size=marker_settings.push_chunk_size)
24
25data_settings = DynamicMultiChannelStreamSettings(
26    n_channels=4,
27    stream_name="TestStream",
28    stream_type="TestData",
29    sample_rate=100,
30    channel_names=["A", "B", "C", "D"]
31)
32
33# info data StreamInfo, outlet
34data_info = StreamInfo(
35    name=data_settings.stream_name,
36    type=data_settings.stream_type,
37    channel_count=data_settings.channel_count,
38    nominal_srate=data_settings.sample_rate,
39    channel_format=data_settings.channel_format,
40    source_id=data_settings.source_id
41)
42data_outlet = StreamOutlet(data_info, chunk_size=data_settings.push_chunk_size)
43
44running = True
45marker_outlet.push_sample(["Start"])
46buffer_dtype = object if data_settings.channel_format == "string" else float
47buffer_in = np.array(data_settings.default_buffer, dtype=buffer_dtype)
48
49print("Start streaming... Press 'q' to stop.")
50
51while not(keyboard.is_pressed('q')):
52
53    random_numbers = [random.randint(0, 1) for _ in range(4)]
54    random_numbers = [num + 1 for num in random_numbers]
55    data_outlet.push_sample(random_numbers)
56
57    time.sleep(data_settings.push_interval)
58
59marker_outlet.push_sample(["Stop"])

Matlab

Please, read the instructions on the GitHub labstreaminglayer website (https://github.com/labstreaminglayer/liblsl-Matlab) on how to prepare Matlab to work with LSL. You can either use the latest release for your Matlab version, or if that doesn't workout well, build it from the source files. Make sure to add the liblsl-Matlab folder to your path recursively to make it available to your own scripts.

A short example for sending lsl streaming data:

 1%% instantiate the library
 2disp('Loading library...');
 3lib = lsl_loadlib();
 4
 5% make a new stream outlet
 6disp('Creating a new streaminfo...');
 7info = lsl_streaminfo(lib,'BioSemi','EEG',8,100,'cf_float32','sdfwerr32432');
 8
 9disp('Opening an outlet...');
10outlet = lsl_outlet(info);
11
12% send data into the outlet, sample by sample
13disp('Now transmitting data...');
14while true
15    outlet.push_sample(randn(8,1));
16    pause(0.01);
17end

A short example for receiving lsl streaming data:

 1%% instantiate the library
 2disp('Loading the library...');
 3lib = lsl_loadlib();
 4
 5% resolve a stream...
 6disp('Resolving an EEG stream...');
 7result = {};
 8while isempty(result)
 9    result = lsl_resolve_byprop(lib,'type','EEG'); end
10
11% create a new inlet
12disp('Opening an inlet...');
13inlet = lsl_inlet(result{1});
14
15disp('Now receiving data...');
16while true
17    % get data from the inlet
18    [vec,ts] = inlet.pull_sample();
19    % and display it
20    fprintf('%.2f\t',vec);
21    fprintf('%.5f\n',ts);
22end