@@ -211,6 +211,57 @@ def test_blackrockrawio_ptp_timestamps(self):
211211 # Spikes enabled on channels 1-129 but channel 129 had 0 events.
212212 self .assertEqual (128 , reader .spike_channels_count ())
213213
214+ def test_gap_tolerance_ms_parameter (self ):
215+ """
216+ Test gap_tolerance_ms parameter for gap handling with files that have actual gaps.
217+
218+ Tests the error-by-default behavior where files with timestamp gaps raise ValueError
219+ unless the user explicitly opts in with gap_tolerance_ms parameter.
220+ """
221+ # Use stubbed files with missing samples (timestamp gaps) from SimulatedSpikes data
222+ dirname = self .get_local_path ("blackrock/blackrock_ptp_with_missing_samples/Hub1-NWBtestfile_neural_wspikes" )
223+
224+ # Test 1: Default behavior (None) raises ValueError for files with gaps
225+ # This is the error-by-default behavior to ensure users are aware of data issues
226+ with self .assertRaises (ValueError ) as context :
227+ reader = BlackrockRawIO (filename = dirname , nsx_to_load = 6 )
228+ reader .parse_header ()
229+
230+ # Verify the error message contains gap information
231+ error_msg = str (context .exception )
232+ self .assertIn ("gap" , error_msg .lower ())
233+ self .assertIn ("gap_tolerance_ms" , error_msg )
234+ self .assertIn ("Gap Report" , error_msg )
235+
236+ # Test 2: Explicit tolerance allows loading files with gaps
237+ # User opts in by providing gap_tolerance_ms, so no warning is issued
238+ reader_with_tolerance = BlackrockRawIO (filename = dirname , nsx_to_load = 6 , gap_tolerance_ms = 10.0 )
239+ reader_with_tolerance .parse_header ()
240+ segments_with_tolerance = reader_with_tolerance .segment_count (0 )
241+ self .assertEqual (1 , segments_with_tolerance ) # Gaps < 10ms are ignored
242+
243+ # Test 3: Very strict tolerance creates multiple segments
244+ # With strict tolerance (0.5ms), gaps > 0.5ms will create new segments
245+ reader_strict = BlackrockRawIO (filename = dirname , nsx_to_load = 6 , gap_tolerance_ms = 0.5 )
246+ reader_strict .parse_header ()
247+ segments_strict = reader_strict .segment_count (0 )
248+ self .assertGreater (segments_strict , 1 ) # Should have multiple segments due to gaps
249+
250+ # Test 4: Different NSX files can have different gap patterns
251+ # Test ns4 file which has different gaps than ns6
252+ with self .assertRaises (ValueError ) as context_ns4 :
253+ reader_ns4 = BlackrockRawIO (filename = dirname , nsx_to_load = 4 )
254+ reader_ns4 .parse_header ()
255+
256+ error_msg_ns4 = str (context_ns4 .exception )
257+ self .assertIn ("ns4" , error_msg_ns4 )
258+ self .assertIn ("gap" , error_msg_ns4 .lower ())
259+
260+ # ns4 should also load with tolerance
261+ reader_ns4_with_tolerance = BlackrockRawIO (filename = dirname , nsx_to_load = 4 , gap_tolerance_ms = 10.0 )
262+ reader_ns4_with_tolerance .parse_header ()
263+ self .assertEqual (1 , reader_ns4_with_tolerance .segment_count (0 ))
264+
214265
215266if __name__ == "__main__" :
216267 unittest .main ()
0 commit comments