Last active
April 26, 2021 21:17
-
-
Save cboulay/006bab4dc971cbd9c7b49e7d32c9cd30 to your computer and use it in GitHub Desktop.
Test pylsl flush vs pull_chunks gets correct timestamps with postproc enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pylsl | |
import numpy as np | |
import time | |
import sys, os | |
T_TOTAL = 70 | |
T_SWITCH = 11.1 | |
def main(): | |
srate = None | |
inlet = None | |
proc_on = True | |
streams = pylsl.resolve_streams(2.0) | |
for stream in streams: | |
if stream.nominal_srate() != 0.0: | |
inlet = pylsl.StreamInlet(stream, processing_flags=pylsl.proc_ALL if proc_on else pylsl.proc_none) | |
srate = stream.nominal_srate() | |
break | |
do_flush = False | |
timestamps = np.array([]) | |
t_start = pylsl.local_clock() | |
t_switch = t_start | |
splits = [t_switch] | |
inlet.pull_chunk() # First chunk often empty | |
while inlet is not None: | |
if do_flush: | |
n_skipped = inlet.flush() | |
timestamps = np.append(timestamps, np.nan * np.ones(n_skipped)) | |
else: | |
samples, ts = inlet.pull_chunk() | |
if len(ts) > 0: | |
timestamps = np.append(timestamps, ts) | |
t_now = pylsl.local_clock() | |
if (t_now - t_switch) > T_SWITCH: | |
splits.append(t_now) | |
do_flush = not do_flush | |
t_switch = t_now | |
print("Switching modes") | |
time.sleep(0.05) | |
if (t_now - t_start) > T_TOTAL: | |
inlet = None | |
print(srate, len(timestamps) / (t_now - t_start)) | |
import matplotlib.pyplot as plt | |
expected_ts = np.arange(len(timestamps)) / srate + timestamps[0] | |
plt.plot(expected_ts, 1000 * (timestamps - expected_ts)) | |
plt.xlabel('Expected Timestamps (s)') | |
plt.ylabel('Deviation From Expected (ms)') | |
plt.title('LSL `flush` ts dev. with postproc ' + ('on' if proc_on else 'off')) | |
plt.show() | |
print("Break here") | |
if __name__ == '__main__': | |
try: | |
main() | |
except KeyboardInterrupt: | |
print('Interrupted') | |
try: | |
sys.exit(0) | |
except SystemExit: | |
os._exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment