@@ -210,6 +210,98 @@ def test_resample_by_chunks():
210210 plt .show ()
211211
212212
213+ def test_resample_preserves_t_start ():
214+ """Resampling should preserve t_start when the parent has one."""
215+ sampling_frequency = 30000
216+ t_start = 100.5
217+ traces = np .random .randn (sampling_frequency * 2 , 2 ).astype (np .float32 )
218+ parent_rec = NumpyRecording (traces , sampling_frequency )
219+ parent_rec ._recording_segments [0 ].t_start = t_start
220+
221+ resampled = resample (parent_rec , 500 )
222+ assert resampled ._recording_segments [0 ].t_start == t_start
223+ assert not resampled .has_time_vector ()
224+ assert np .isclose (resampled .get_times ()[0 ], t_start )
225+
226+
227+ def test_resample_does_not_mutate_parent ():
228+ """Resampling should not modify the parent recording's time_vector."""
229+ sampling_frequency = 30000
230+ n_samples = sampling_frequency * 2
231+ traces = np .random .randn (n_samples , 2 ).astype (np .float32 )
232+ parent_rec = NumpyRecording (traces , sampling_frequency )
233+ time_vector = np .arange (n_samples , dtype = "float64" ) / sampling_frequency + 50.0
234+ parent_rec .set_times (time_vector )
235+
236+ assert parent_rec .has_time_vector ()
237+ resample (parent_rec , 500 )
238+ assert parent_rec .has_time_vector (), "Parent time_vector was mutated by resample!"
239+ np .testing .assert_array_equal (parent_rec .get_times (), time_vector )
240+
241+
242+ def test_resample_preserves_time_vector_integer_ratio ():
243+ """Resampling with integer ratio should slice the parent time_vector."""
244+ sampling_frequency = 30000
245+ resample_rate = 500
246+ n_samples = sampling_frequency * 2
247+ traces = np .random .randn (n_samples , 2 ).astype (np .float32 )
248+ parent_rec = NumpyRecording (traces , sampling_frequency )
249+
250+ # Create a time_vector with a gap (simulating artifact removal)
251+ time_vector = np .arange (n_samples , dtype = "float64" ) / sampling_frequency
252+ # Insert a 5-second gap at the midpoint
253+ midpoint = n_samples // 2
254+ time_vector [midpoint :] += 5.0
255+ parent_rec .set_times (time_vector )
256+
257+ resampled = resample (parent_rec , resample_rate )
258+
259+ assert resampled .has_time_vector ()
260+ resampled_times = resampled .get_times ()
261+ n_out = resampled .get_num_samples ()
262+
263+ # Output length should be consistent
264+ assert len (resampled_times ) == n_out
265+
266+ # The gap should be preserved: check that the jump exists in the resampled times
267+ diffs = np .diff (resampled_times )
268+ normal_dt = 1.0 / resample_rate
269+ gap_indices = np .where (diffs > normal_dt * 2 )[0 ]
270+ assert len (gap_indices ) == 1 , "The gap should appear exactly once in resampled times"
271+ assert np .isclose (diffs [gap_indices [0 ]], normal_dt + 5.0 , atol = normal_dt )
272+
273+ # Start time should match
274+ assert np .isclose (resampled_times [0 ], time_vector [0 ])
275+
276+
277+ def test_resample_preserves_time_vector_non_integer_ratio ():
278+ """Resampling with non-integer ratio should interpolate the time_vector."""
279+ sampling_frequency = 30000
280+ resample_rate = 700 # 30000 / 700 is not integer
281+ n_samples = sampling_frequency * 2
282+ traces = np .random .randn (n_samples , 2 ).astype (np .float32 )
283+ parent_rec = NumpyRecording (traces , sampling_frequency )
284+
285+ time_vector = np .arange (n_samples , dtype = "float64" ) / sampling_frequency + 10.0
286+ parent_rec .set_times (time_vector )
287+
288+ import warnings as _warnings
289+
290+ with _warnings .catch_warnings (record = True ) as w :
291+ _warnings .simplefilter ("always" )
292+ resampled = resample (parent_rec , resample_rate )
293+ assert any ("non-integer ratio" in str (warning .message ).lower () for warning in w )
294+
295+ assert resampled .has_time_vector ()
296+ resampled_times = resampled .get_times ()
297+ assert len (resampled_times ) == resampled .get_num_samples ()
298+ assert np .isclose (resampled_times [0 ], 10.0 , atol = 1.0 / sampling_frequency )
299+
300+
213301if __name__ == "__main__" :
214302 test_resample_freq_domain ()
215303 test_resample_by_chunks ()
304+ test_resample_preserves_t_start ()
305+ test_resample_does_not_mutate_parent ()
306+ test_resample_preserves_time_vector_integer_ratio ()
307+ test_resample_preserves_time_vector_non_integer_ratio ()
0 commit comments