Skip to content

Commit 590dbc2

Browse files
committed
bugfix: spikeglx streamer uses precise metadata sampling rate
1 parent d644ea8 commit 590dbc2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

brainbox/io/spikeglx.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,13 @@ def stream(pid, t0, nsecs=1, one=None, cache_folder=None, remove_cached=False, t
116116
:param one: An instance of ONE
117117
:param cache_folder:
118118
:param typ: 'ap' or 'lf'
119-
:return: sr, dsets, t0
119+
:return: sr, t0
120120
"""
121-
CHUNK_DURATION_SECS = 1 # the mtscomp chunk duration. Right now it's a constant
122121
if nsecs > 10:
123-
ValueError(f'Streamer works only with 10 or less seconds, set nsecs to lesss than {nsecs}')
122+
ValueError(f'Streamer works only with 10 or less seconds, set nsecs to less than {nsecs}')
124123
assert one
125124
assert typ in ['lf', 'ap']
126-
t0 = np.floor(t0 / CHUNK_DURATION_SECS) * CHUNK_DURATION_SECS
125+
127126
if cache_folder is None:
128127
samples_folder = Path(one.alyx._par.CACHE_DIR).joinpath('cache', typ)
129128

@@ -134,8 +133,13 @@ def stream(pid, t0, nsecs=1, one=None, cache_folder=None, remove_cached=False, t
134133
ch_file = one._download_datasets(ch_rec)[0]
135134
one._download_datasets(meta_rec)[0]
136135

137-
first_chunk = int(t0 / CHUNK_DURATION_SECS)
138-
last_chunk = int((t0 + nsecs) / CHUNK_DURATION_SECS) - 1
136+
import json
137+
with open(ch_file) as fid:
138+
chinfo = json.load(fid)
139+
tbounds = np.array(chinfo['chunk_bounds']) / chinfo['sample_rate']
140+
first_chunk = np.maximum(0, np.searchsorted(tbounds, t0) - 1)
141+
last_chunk = np.maximum(0, np.searchsorted(tbounds, t0 + nsecs) - 2)
142+
t0 = tbounds[first_chunk]
139143

140144
samples_folder.mkdir(exist_ok=True, parents=True)
141145
sr = spikeglx.download_raw_partial(

0 commit comments

Comments
 (0)