@@ -117,9 +117,6 @@ def _source_name(self):
117117 else :
118118 return self .dirname
119119
120- # from memory_profiler import profile
121- #
122- # @profile()
123120 def _parse_header (self ):
124121
125122 stream_channels = []
@@ -192,7 +189,7 @@ def _parse_header(self):
192189 for idx , chan_id in enumerate (chan_ids ):
193190 chan_name = chan_names [idx ]
194191
195- chan_uid = (chan_name , chan_id )
192+ chan_uid = (chan_name , str ( chan_id ) )
196193 if ext == 'ncs' :
197194 if info ['sampling_rate' ] not in ncs_sampling_rates :
198195 ncs_sampling_rates .append (info ['sampling_rate' ])
@@ -310,6 +307,14 @@ def _parse_header(self):
310307 self ._sigs_t_start = ncsSegTimestampLimits .t_start .copy ()
311308 self ._sigs_t_stop = ncsSegTimestampLimits .t_stop .copy ()
312309
310+ # precompute signal lengths within segments
311+ self ._sigs_length = []
312+ if self ._sigs_memmaps :
313+ for seg_idx , sig_container in enumerate (self ._sigs_memmaps ):
314+ self ._sigs_length .append ({})
315+ for chan_uid , sig_infos in sig_container .items ():
316+ self ._sigs_length [seg_idx ][chan_uid ] = int (sig_infos ['nb_valid' ].sum ())
317+
313318 # Determine timestamp limits in nev, nse file by scanning them.
314319 ts0 , ts1 = None , None
315320 for _data_memmap in (self ._spike_memmap , self ._nev_memmap ):
@@ -438,12 +443,12 @@ def _segment_t_stop(self, block_index, seg_index):
438443
439444 def _get_signal_size (self , block_index , seg_index , stream_index ):
440445 stream_id = self .header ['signal_streams' ][stream_index ]['id' ]
441- channel_indexes = np .where (self .header ['signal_channels' ]['stream_id' ] == stream_id )[0 ]
446+ stream_mask = self .header ['signal_channels' ]['stream_id' ] == stream_id
447+ signals = self .header ['signal_channels' ][stream_mask ]
442448
443- if len (channel_indexes ):
444- channel_index = channel_indexes [0 ]
445- chan = self .header ['signal_channels' ][channel_index ]
446- return self ._sigs_memmaps [seg_index ][(chan ['name' ], int (chan ['id' ]))]
449+ if len (signals ):
450+ sig = signals [0 ]
451+ return self ._sigs_length [seg_index ][(sig ['name' ], sig ['id' ])]
447452 else :
448453 return None
449454
@@ -475,7 +480,8 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop,
475480 if i_start is None :
476481 i_start = 0
477482 if i_stop is None :
478- i_stop = self ._sigs_length [seg_index ]
483+ i_stop = self .get_signal_size (block_index = block_index , seg_index = seg_index ,
484+ stream_index = stream_index )
479485
480486 block_start = i_start // NcsSection ._RECORD_SIZE
481487 block_stop = i_stop // NcsSection ._RECORD_SIZE + 1
@@ -489,7 +495,7 @@ def _get_analogsignal_chunk(self, block_index, seg_index, i_start, i_stop,
489495 stream_mask = self .header ['signal_channels' ]['stream_id' ] == stream_id
490496
491497 # channel_streams = self.
492- channel_ids = self .header ['signal_channels' ][stream_mask ][channel_indexes ]['id' ]. astype ( int )
498+ channel_ids = self .header ['signal_channels' ][stream_mask ][channel_indexes ]['id' ]
493499 channel_names = self .header ['signal_channels' ][stream_mask ][channel_indexes ]['name' ]
494500
495501 # create buffer for samples
@@ -632,10 +638,9 @@ def scan_ncs_files(self, ncs_filenames):
632638 nlxHeader = NlxHeader (ncs_filename )
633639
634640 if not chanSectMap or (chanSectMap and
635- not NcsSectionsFactory ._verifySectionsStructure (data ,
636- lastNcsSections )):
637- lastNcsSections = NcsSectionsFactory .build_for_ncs_file (data , nlxHeader )
638- chanSectMap [chan_uid ] = [lastNcsSections , nlxHeader , ncs_filename ]
641+ not NcsSectionsFactory ._verifySectionsStructure (data , chan_ncs_sections )):
642+ chan_ncs_sections = NcsSectionsFactory .build_for_ncs_file (data , nlxHeader )
643+ chanSectMap [chan_uid ] = [chan_ncs_sections , nlxHeader , ncs_filename ]
639644 del data
640645
641646 # Construct an inverse dictionary from NcsSections to list of associated chan_uids
@@ -646,7 +651,7 @@ def scan_ncs_files(self, ncs_filenames):
646651 latest_sections = v [0 ]
647652 # time tolerance of +- one data package (in microsec)
648653 tolerance = 512 / min (v [0 ].sampFreqUsed , latest_sections .sampFreqUsed ) * 1e6
649- if v [0 ].is_equivalent (latest_sections , abs_tol = tolerance ):
654+ if not v [0 ].is_equivalent (latest_sections , abs_tol = tolerance ):
650655 revSectMap .setdefault (latest_sections , []).append (k )
651656 else :
652657 revSectMap [v [0 ]] = [k ]
@@ -658,7 +663,7 @@ def scan_ncs_files(self, ncs_filenames):
658663 raise IOError (f'ncs files have { len (revSectMap )} different sections '
659664 f'structures. Unsupported configuration.' )
660665
661- seg_time_limits = SegmentTimeLimits (nb_segment = len (lastNcsSections .sects ),
666+ seg_time_limits = SegmentTimeLimits (nb_segment = len (chan_ncs_sections .sects ),
662667 t_start = [], t_stop = [], length = [],
663668 timestamp_limits = [])
664669 memmaps = [{} for seg_index in range (seg_time_limits .nb_segment )]
0 commit comments