|
| 1 | +"""Performance tests to verify optimizations.""" |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | +import time |
| 5 | +from openpiv import pyprocess, validation, filters |
| 6 | + |
| 7 | + |
| 8 | +def test_find_all_first_peaks_performance(): |
| 9 | + """Test that find_all_first_peaks uses vectorized operations.""" |
| 10 | + # Create test correlation maps |
| 11 | + n_windows = 100 |
| 12 | + window_size = 32 |
| 13 | + corr = np.random.rand(n_windows, window_size, window_size) |
| 14 | + |
| 15 | + # Add clear peaks |
| 16 | + for i in range(n_windows): |
| 17 | + peak_i = np.random.randint(5, window_size-5) |
| 18 | + peak_j = np.random.randint(5, window_size-5) |
| 19 | + corr[i, peak_i, peak_j] = 100.0 |
| 20 | + |
| 21 | + start = time.time() |
| 22 | + indexes, peaks = pyprocess.find_all_first_peaks(corr) |
| 23 | + elapsed = time.time() - start |
| 24 | + |
| 25 | + # Verify results |
| 26 | + assert indexes.shape == (n_windows, 3) |
| 27 | + assert peaks.shape == (n_windows,) |
| 28 | + assert np.all(peaks >= 0) |
| 29 | + |
| 30 | + # Should be fast (< 10ms for 100 windows) |
| 31 | + assert elapsed < 0.01, f"find_all_first_peaks took {elapsed:.4f}s, expected < 0.01s" |
| 32 | + |
| 33 | + |
| 34 | +def test_normalize_intensity_performance(): |
| 35 | + """Test that normalize_intensity avoids unnecessary conversions.""" |
| 36 | + # Test with float32 input (should not convert) |
| 37 | + window_float = np.random.rand(50, 64, 64).astype(np.float32) |
| 38 | + |
| 39 | + start = time.time() |
| 40 | + result = pyprocess.normalize_intensity(window_float) |
| 41 | + elapsed_float = time.time() - start |
| 42 | + |
| 43 | + assert result.dtype == np.float32 |
| 44 | + |
| 45 | + # Test with uint8 input (needs conversion) |
| 46 | + window_uint = (np.random.rand(50, 64, 64) * 255).astype(np.uint8) |
| 47 | + |
| 48 | + start = time.time() |
| 49 | + result = pyprocess.normalize_intensity(window_uint) |
| 50 | + elapsed_uint = time.time() - start |
| 51 | + |
| 52 | + assert result.dtype == np.float32 |
| 53 | + |
| 54 | + # Should be reasonably fast (< 50ms for 50 windows) |
| 55 | + assert elapsed_float < 0.05, f"normalize_intensity (float32) took {elapsed_float:.4f}s" |
| 56 | + assert elapsed_uint < 0.05, f"normalize_intensity (uint8) took {elapsed_uint:.4f}s" |
| 57 | + |
| 58 | + |
| 59 | +def test_global_std_performance(): |
| 60 | + """Test that global_std avoids unnecessary array copies.""" |
| 61 | + # Create test data |
| 62 | + u = np.random.randn(100, 100) * 10 |
| 63 | + v = np.random.randn(100, 100) * 10 |
| 64 | + |
| 65 | + # Test with regular arrays |
| 66 | + start = time.time() |
| 67 | + flag = validation.global_std(u, v, std_threshold=3) |
| 68 | + elapsed_regular = time.time() - start |
| 69 | + |
| 70 | + assert flag.shape == u.shape |
| 71 | + |
| 72 | + # Test with masked arrays |
| 73 | + u_masked = np.ma.masked_array(u, mask=np.random.rand(100, 100) > 0.9) |
| 74 | + v_masked = np.ma.masked_array(v, mask=np.random.rand(100, 100) > 0.9) |
| 75 | + |
| 76 | + start = time.time() |
| 77 | + flag = validation.global_std(u_masked, v_masked, std_threshold=3) |
| 78 | + elapsed_masked = time.time() - start |
| 79 | + |
| 80 | + assert flag.shape == u.shape |
| 81 | + |
| 82 | + # Should be fast (< 10ms for 100x100 arrays) |
| 83 | + assert elapsed_regular < 0.01, f"global_std (regular) took {elapsed_regular:.4f}s" |
| 84 | + assert elapsed_masked < 0.01, f"global_std (masked) took {elapsed_masked:.4f}s" |
| 85 | + |
| 86 | + |
| 87 | +def test_replace_outliers_performance(): |
| 88 | + """Test that replace_outliers only creates masked arrays when needed.""" |
| 89 | + # Create test data |
| 90 | + u = np.random.randn(50, 50) * 10 |
| 91 | + v = np.random.randn(50, 50) * 10 |
| 92 | + flags = np.random.rand(50, 50) > 0.95 # 5% outliers |
| 93 | + |
| 94 | + # Test with regular arrays |
| 95 | + start = time.time() |
| 96 | + uf, vf = filters.replace_outliers(u, v, flags, method='localmean', max_iter=3) |
| 97 | + elapsed = time.time() - start |
| 98 | + |
| 99 | + assert uf.shape == u.shape |
| 100 | + assert vf.shape == v.shape |
| 101 | + |
| 102 | + # Should be reasonably fast (< 100ms for 50x50 with 3 iterations) |
| 103 | + assert elapsed < 0.1, f"replace_outliers took {elapsed:.4f}s, expected < 0.1s" |
| 104 | + |
| 105 | + |
| 106 | +def test_vectorized_sig2noise_ratio_performance(): |
| 107 | + """Test that vectorized sig2noise ratio is faster than loop version.""" |
| 108 | + # Create test correlation maps |
| 109 | + n_windows = 200 |
| 110 | + window_size = 32 |
| 111 | + corr = np.random.rand(n_windows, window_size, window_size) * 0.5 |
| 112 | + |
| 113 | + # Add clear peaks |
| 114 | + for i in range(n_windows): |
| 115 | + peak_i = np.random.randint(5, window_size-5) |
| 116 | + peak_j = np.random.randint(5, window_size-5) |
| 117 | + corr[i, peak_i, peak_j] = 10.0 |
| 118 | + |
| 119 | + # Test vectorized version |
| 120 | + start = time.time() |
| 121 | + s2n_vectorized = pyprocess.vectorized_sig2noise_ratio( |
| 122 | + corr, sig2noise_method='peak2peak', width=2 |
| 123 | + ) |
| 124 | + elapsed_vectorized = time.time() - start |
| 125 | + |
| 126 | + assert s2n_vectorized.shape == (n_windows,) |
| 127 | + assert np.all(s2n_vectorized >= 0) |
| 128 | + |
| 129 | + # Should be fast (< 50ms for 200 windows) |
| 130 | + assert elapsed_vectorized < 0.05, \ |
| 131 | + f"vectorized_sig2noise_ratio took {elapsed_vectorized:.4f}s, expected < 0.05s" |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + # Run tests manually with timing output |
| 136 | + print("Running performance tests...") |
| 137 | + |
| 138 | + print("\n1. Testing find_all_first_peaks_performance...") |
| 139 | + test_find_all_first_peaks_performance() |
| 140 | + print(" ✓ Passed") |
| 141 | + |
| 142 | + print("\n2. Testing normalize_intensity_performance...") |
| 143 | + test_normalize_intensity_performance() |
| 144 | + print(" ✓ Passed") |
| 145 | + |
| 146 | + print("\n3. Testing global_std_performance...") |
| 147 | + test_global_std_performance() |
| 148 | + print(" ✓ Passed") |
| 149 | + |
| 150 | + print("\n4. Testing replace_outliers_performance...") |
| 151 | + test_replace_outliers_performance() |
| 152 | + print(" ✓ Passed") |
| 153 | + |
| 154 | + print("\n5. Testing vectorized_sig2noise_ratio_performance...") |
| 155 | + test_vectorized_sig2noise_ratio_performance() |
| 156 | + print(" ✓ Passed") |
| 157 | + |
| 158 | + print("\n✅ All performance tests passed!") |
0 commit comments