@@ -205,28 +205,37 @@ def _parse_header(self):
205205 keep = info_channel_groups ["TankEvType" ] & EVTYPE_MASK == EVTYPE_STREAM
206206 missing_sev_channels = []
207207 for stream_index , info in enumerate (info_channel_groups [keep ]):
208+ stream_index = int (stream_index ) # This transforms numpy scalar to python native int
208209 self ._sig_sample_per_chunk [stream_index ] = info ["NumPoints" ]
209210
210- stream_name = str (info ["StoreName" ])
211+ stream_name_bytes = info ["StoreName" ]
212+ stream_name = info ["StoreName" ].decode ("utf-8" )
211213 stream_id = f"{ stream_index } "
212214 buffer_id = ""
213215 signal_streams .append ((stream_name , stream_id , buffer_id ))
214216
215- for c in range (info ["NumChan" ]):
217+ for channel_index in range (info ["NumChan" ]):
216218 global_chan_index = len (signal_channels )
217- chan_id = c + 1 # several StoreName can have same chan_id: this is ok
219+ chan_id = channel_index + 1
218220
219221 # loop over segment to get sampling_rate/data_index/data_buffer
220222 sampling_rate = None
221223 dtype = None
222224 for seg_index , segment_name in enumerate (segment_names ):
223225 # get data index
224226 tsq = self ._tsq [seg_index ]
225- mask = (
226- (tsq ["evtype" ] & EVTYPE_MASK == EVTYPE_STREAM )
227- & (tsq ["evname" ] == info ["StoreName" ])
228- & (tsq ["channel" ] == chan_id )
229- )
227+ # Filter TSQ events to find all data chunks belonging to the current stream and channel
228+ # This identifies which parts of the TEV/SEV files contain our signal data
229+ is_stream_event = (
230+ tsq ["evtype" ] & EVTYPE_MASK
231+ ) == EVTYPE_STREAM # Get only stream events (continuous data)
232+ matches_store_name = (
233+ tsq ["evname" ] == stream_name_bytes
234+ ) # Match the 4-char store name (e.g., 'RSn1')
235+ matches_channel = tsq ["channel" ] == chan_id # Match the specific channel number
236+
237+ # Combine all conditions - we want events that satisfy all three criteria
238+ mask = is_stream_event & matches_store_name & matches_channel
230239 data_index = tsq [mask ].copy ()
231240 self ._sigs_index [seg_index ][global_chan_index ] = data_index
232241
@@ -252,11 +261,11 @@ def _parse_header(self):
252261 # sampling_rate and dtype
253262 if len (data_index ):
254263 _sampling_rate = float (data_index ["frequency" ][0 ])
255- _dtype = data_formats [data_index ["dataformat" ][0 ]]
264+ _dtype = data_formats_map [data_index ["dataformat" ][0 ]]
256265 else :
257266 # if no signal present use dummy values
258267 _sampling_rate = 1.0
259- _dtype = int
268+ _dtype = " int"
260269 if sampling_rate is None :
261270 sampling_rate = _sampling_rate
262271 dtype = _dtype
@@ -277,17 +286,15 @@ def _parse_header(self):
277286 # path = self.dirname / segment_name
278287 if self .tdt_block_mode == "multi" :
279288 # for multi block datasets the names of sev files are fixed
280- store = info ["StoreName" ].decode ("ascii" )
281- sev_stem = f"{ tankname } _{ segment_name } _{ store } _ch{ chan_id } "
289+ sev_stem = f"{ tankname } _{ segment_name } _{ stream_name } _ch{ chan_id } "
282290 sev_filename = (path / sev_stem ).with_suffix (".sev" )
283291 else :
284- # for single block datasets the exact name of sev files in not known
292+ # for single block datasets the exact name of sev files is not known
285293 sev_regex = f"*_[cC]h{ chan_id } .sev"
286294 sev_filename = list (self .dirname .parent .glob (str (sev_regex )))
287295 # in case multiple sev files are found, try to find the one for current stream
288296 if len (sev_filename ) > 1 :
289- store = info ["StoreName" ].decode ("ascii" )
290- sev_regex = f"*_{ store } _Ch{ chan_id } .sev"
297+ sev_regex = f"*_{ stream_name } _Ch{ chan_id } .sev"
291298 sev_filename = list (self .dirname .parent .glob (str (sev_regex )))
292299
293300 # in case non or multiple sev files are found for current stream + channel
@@ -305,14 +312,14 @@ def _parse_header(self):
305312 raise NeoReadWriteError ("no TEV nor SEV data to read" )
306313 self ._sigs_data_buf [seg_index ][global_chan_index ] = data
307314
308- chan_name = f"{ info [ 'StoreName' ] } { c + 1 } "
315+ channel_name = f"{ stream_name } { channel_index + 1 } "
309316 sampling_rate = sampling_rate
310317 units = "uV" # see https://github.com/NeuralEnsemble/python-neo/issues/1369
311318 gain = 1.0
312319 offset = 0.0
313320 buffer_id = ""
314321 signal_channels .append (
315- (chan_name , str (chan_id ), sampling_rate , dtype , units , gain , offset , stream_id , buffer_id )
322+ (channel_name , str (chan_id ), sampling_rate , dtype , units , gain , offset , stream_id , buffer_id )
316323 )
317324
318325 if missing_sev_channels :
@@ -354,7 +361,7 @@ def _parse_header(self):
354361 )
355362
356363 self ._waveforms_size .append (info ["NumPoints" ])
357- self ._waveforms_dtype .append (np .dtype (data_formats [info ["DataFormat" ]]))
364+ self ._waveforms_dtype .append (np .dtype (data_formats_map [info ["DataFormat" ]]))
358365
359366 spike_channels = np .array (spike_channels , dtype = _spike_channel_dtype )
360367
@@ -417,13 +424,13 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
417424 global_chan_indexes = global_chan_indexes [channel_indexes ]
418425 signal_channels = signal_channels [channel_indexes ]
419426
420- dt = self ._sig_dtype_by_group [stream_index ]
421- raw_signals = np .zeros ((i_stop - i_start , signal_channels .size ), dtype = dt )
427+ dtype = self ._sig_dtype_by_group [stream_index ]
428+ raw_signals = np .zeros ((i_stop - i_start , signal_channels .size ), dtype = dtype )
422429
423430 sample_per_chunk = self ._sig_sample_per_chunk [stream_index ]
424431 bl0 = i_start // sample_per_chunk
425432 bl1 = int (np .ceil (i_stop / sample_per_chunk ))
426- chunk_nb_bytes = sample_per_chunk * dt .itemsize
433+ chunk_nb_bytes = sample_per_chunk * dtype .itemsize
427434
428435 for c , global_index in enumerate (global_chan_indexes ):
429436 data_index = self ._sigs_index [seg_index ][global_index ]
@@ -434,7 +441,7 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop, strea
434441 for bl in range (bl0 , bl1 ):
435442 ind0 = data_index [bl ]["offset" ]
436443 ind1 = ind0 + chunk_nb_bytes
437- data = data_buf [ind0 :ind1 ].view (dt )
444+ data = data_buf [ind0 :ind1 ].view (dtype )
438445
439446 if bl == bl1 - 1 :
440447 # right border
@@ -620,7 +627,7 @@ def read_tbk(tbk_filename):
620627EVMARK_STARTBLOCK = int ("0001" , 16 ) # 1
621628EVMARK_STOPBLOCK = int ("0002" , 16 ) # 2
622629
623- data_formats = {
630+ data_formats_map = {
624631 0 : "float32" ,
625632 1 : "int32" ,
626633 2 : "int16" ,
0 commit comments