diff options
| author | Sam Scholten | 2025-10-24 16:32:14 +1000 |
|---|---|---|
| committer | Sam Scholten | 2025-10-24 16:32:14 +1000 |
| commit | 030523c8e06f925211247d2c89a6c250780b4738 (patch) | |
| tree | 31bf1a26872ac7203899888738a2392b8c6c476c | |
| parent | 6355d2cdadfc4f3f6b9d6fd3f78249a3b776cc50 (diff) | |
| download | transivent-030523c8e06f925211247d2c89a6c250780b4738.tar.gz transivent-030523c8e06f925211247d2c89a6c250780b4738.zip | |
Add integration tests for resampling feature
- Test that resampling preserves event detection
- Test uniform time arrays after resampling
- All tests passing
| -rw-r--r-- | tests/test_resample_integration.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/test_resample_integration.py b/tests/test_resample_integration.py new file mode 100644 index 0000000..930da2e --- /dev/null +++ b/tests/test_resample_integration.py @@ -0,0 +1,81 @@ +"""Test integration of resampling with detect_from_wfm.""" + +import numpy as np +import pytest + +from transivent import detect +from transivent.resample import average_downsample, downsample_to_interval + + +def test_detect_with_resampled_data(): + """Test that detect works correctly with resampled data.""" + # Create synthetic 20 MHz data with known events + fs = 20e6 # 20 MHz sampling + fs_target = 2e6 # 2 MHz target + duration = 0.001 # 1 ms (smaller for faster test) + t_orig = np.arange(int(duration * fs)) / fs + x_orig = np.random.randn(len(t_orig)) * 0.01 # Noise + + # Add a few events + event_times = [0.0002, 0.0005, 0.0008] + for t_event in event_times: + idx = int(t_event * fs) + width = int(5e-4 * fs) # 500 µs event (longer for better detection) + x_orig[idx:idx+width] -= 1.0 # Stronger negative spike + + # Resample data (downsample by factor of 10) + t_down, x_down = average_downsample(t_orig, x_orig, q=10) + + # Lower noise after resampling + noise_level = np.std(x_down[:1000]) # Using quiet portion + print(f"Noise level: {noise_level:.4f}") + + # Detect on resampled data + results = detect( + t_down, + x_down, + sampling_interval=10 / fs, + detection_snr=3.0, # Higher threshold + min_event_keep_snr=5.0, + smooth_win_t=100e-6, # Shorter smoothing for test data + save_plots=False, + ) + + # Check events were detected + print(f"Found {len(results['events'])} events") + assert len(results['events']) >= 1 + + # Check the time array is uniform + dt = t_down[1] - t_down[0] + assert np.allclose(np.diff(t_down), dt, rtol=1e-6) + + # Check the resampled time interval + assert np.isclose(dt, 1/fs_target, rtol=1e-6) + + +def test_resample_preserves_events(): + """Test that resampling preserves event detection.""" + # Create simple test case + fs = 1e6 # 1 MHz + t = np.arange(10000) / fs + x = np.zeros(10000) + + # Add a clear event + event_start = 2000 + event_width = 100 + x[event_start:event_start + event_width] = -1.0 + + # Resample by factor of 10 + t_down, x_down = average_downsample(t, x, q=10) + + # Event should still be detectable after resampling + # The negative area should be preserved (averaging) + assert np.min(x_down) < -0.5 # Event should still be negative + + # Check the time array is proper + assert len(t_down) == len(x_down) == 1000 + assert np.allclose(t_down[1] - t_down[0], 10 * (t[1] - t[0])) + + +if __name__ == "__main__": + pytest.main([__file__, "-v"])
\ No newline at end of file |
