scirs2-ndimage 0.4.2

N-dimensional image processing module for SciRS2 (scirs2-ndimage)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Comprehensive tests for filters
//!
//! This module contains integration tests for the filter operations,
//! including boundary condition handling, multi-dimensional arrays,
//! and various filter types.

#[cfg(test)]
#[allow(clippy::module_inception)]
mod tests {
    use super::super::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::{array, Array1, Array2, Array3, Ix2, IxDyn};

    #[test]
    fn test_filters_preserveshape() {
        // Test that all filters preserve the shape of the input array
        let input = array![[1.0, 2.0], [4.0, 5.0]]; // Smaller array for test

        // Apply various filters
        let uniform =
            uniform_filter(&input, &[2, 2], None, None).expect("uniform_filter should succeed");
        let min_filter =
            minimum_filter(&input, &[2, 2], None, None).expect("minimum_filter should succeed");
        let max_filter =
            maximum_filter(&input, &[2, 2], None, None).expect("maximum_filter should succeed");
        let gaussian =
            gaussian_filter(&input, 1.0, None, None).expect("gaussian_filter should succeed");
        let median = median_filter(&input, &[2, 2], None).expect("median_filter should succeed");

        // Check shapes
        assert_eq!(uniform.shape(), input.shape());
        assert_eq!(min_filter.shape(), input.shape());
        assert_eq!(max_filter.shape(), input.shape());
        assert_eq!(gaussian.shape(), input.shape());
        assert_eq!(median.shape(), input.shape());
    }

    #[test]
    fn test_uniform_filter_correctness() {
        // Test the correctness of uniform filter with a known example
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        // Apply 3x3 uniform filter
        let uniform = uniform_filter(&input, &[3, 3], None, None)
            .expect("uniform_filter should succeed for 3x3 input");

        // Expected result: For a 3x3 array with a 3x3 filter, all elements are the average
        let expected_avg = (1.0 + 2.0 + 3.0 + 4.0 + 5.0 + 6.0 + 7.0 + 8.0 + 9.0) / 9.0;

        // Center element should be exactly the average
        assert_abs_diff_eq!(uniform[[1, 1]], expected_avg, epsilon = 1e-10);

        // For a 3x3 filter on a 3x3 array, values may differ at edges due to padding
        // but should still be close to the average
        for i in 0..3 {
            for j in 0..3 {
                assert!(uniform[[i, j]] > 0.0);
                assert!(uniform[[i, j]] < 10.0);
            }
        }
    }

    #[test]
    fn test_extrema_filters_correctness() {
        // Test the correctness of min and max filters
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        // Apply filters
        let min_filter = minimum_filter(&input, &[3, 3], None, None)
            .expect("minimum_filter should succeed for extrema test");
        let max_filter = maximum_filter(&input, &[3, 3], None, None)
            .expect("maximum_filter should succeed for extrema test");

        // Check center values
        assert_eq!(min_filter[[1, 1]], 1.0); // Min value in the 3x3 array
        assert_eq!(max_filter[[1, 1]], 9.0); // Max value in the 3x3 array
    }

    #[test]
    fn test_gaussian_filter_correctness() {
        // Test that Gaussian filter properly smooths data
        let mut input = Array2::<f64>::zeros((5, 5));
        input[[2, 2]] = 1.0; // Single impulse in the center

        // Apply Gaussian filter with sigma=1.0
        let result = gaussian_filter(&input, 1.0, None, None)
            .expect("gaussian_filter should succeed for impulse test");

        // Check properties:
        // 1. Center value should be highest but less than 1.0 (due to smoothing)
        assert!(result[[2, 2]] > 0.0);
        assert!(result[[2, 2]] < 1.0);

        // 2. Adjacent values should be positive (smoothed outward)
        assert!(result[[1, 2]] > 0.0);
        assert!(result[[2, 1]] > 0.0);
        assert!(result[[3, 2]] > 0.0);
        assert!(result[[2, 3]] > 0.0);

        // 3. Sum should be approximately 1.0 (energy conservation)
        // Note: The actual sum may vary slightly due to discrete approximation
        // and edge effects, so we use a larger epsilon
        let sum: f64 = result.iter().sum();
        assert_abs_diff_eq!(sum, 1.0, epsilon = 0.3);
    }

    #[test]
    fn test_median_filter_correctness() {
        // Test that median filter removes outliers
        let mut input = Array2::<f64>::zeros((5, 5));
        input[[2, 2]] = 100.0; // Outlier

        // Apply median filter
        let result = median_filter(&input, &[3, 3], None)
            .expect("median_filter should succeed for outlier test");

        // Check that outlier is removed (for a 5x5 array of zeros with one 100, median is 0)
        assert_eq!(result[[2, 2]], 0.0);
    }

    #[test]
    fn test_border_modes() {
        // Test different border modes with a small array
        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // 3x3 uniform filter with different border modes
        let constant = uniform_filter(&input, &[3, 3], Some(BorderMode::Constant), None)
            .expect("uniform_filter with Constant mode should succeed");
        let reflect = uniform_filter(&input, &[3, 3], Some(BorderMode::Reflect), None)
            .expect("uniform_filter with Reflect mode should succeed");
        let nearest = uniform_filter(&input, &[3, 3], Some(BorderMode::Nearest), None)
            .expect("uniform_filter with Nearest mode should succeed");
        let wrap = uniform_filter(&input, &[3, 3], Some(BorderMode::Wrap), None)
            .expect("uniform_filter with Wrap mode should succeed");
        let mirror = uniform_filter(&input, &[3, 3], Some(BorderMode::Mirror), None)
            .expect("uniform_filter with Mirror mode should succeed");

        // Check that the results are different for each mode
        assert!(constant[[0, 0]] != reflect[[0, 0]]);
        assert!(reflect[[0, 0]] != nearest[[0, 0]]);
        assert!(nearest[[0, 0]] != wrap[[0, 0]]);
        assert!(wrap[[0, 0]] != mirror[[0, 0]]);

        // Some specific checks for the border modes
        // Constant mode should have the lowest values (since padding with zeros)
        assert!(constant[[0, 0]] < reflect[[0, 0]]);
        assert!(constant[[0, 0]] < nearest[[0, 0]]);
    }

    #[test]
    fn test_3d_filtering() {
        // Test filtering on 3D arrays
        let mut input = Array3::<f64>::zeros((3, 3, 3));
        input[[1, 1, 1]] = 1.0; // Center value is 1, rest are 0

        // Test that 3D uniform filter now works (implementation has been added)
        let uniform_result = uniform_filter(&input, &[3, 3, 3], None, None);
        assert!(uniform_result.is_ok());
        let uniform_output = uniform_result.expect("Test: operation failed");
        assert!(uniform_output[[1, 1, 1]] > 0.0); // Should smooth the center value
        assert_eq!(uniform_output.shape(), &[3, 3, 3]);

        // Test that minimum and maximum filters work for 3D
        let min_result = minimum_filter(&input, &[3, 3, 3], None, None);
        if let Ok(min_output) = min_result {
            assert_eq!(min_output[[1, 1, 1]], 0.0); // Should be 0 due to surrounding zeros
        } else {
            // If not implemented yet, that's expected
            assert!(min_result.is_err());
        }

        let max_result = maximum_filter(&input, &[3, 3, 3], None, None);
        if let Ok(max_output) = max_result {
            assert_eq!(max_output[[1, 1, 1]], 1.0); // Should be 1 from the center value
        } else {
            // If not implemented yet, that's expected
            assert!(max_result.is_err());
        }

        // Separable and Gaussian filters should work
        let gaussian3d = gaussian_filter(&input, 1.0, None, None)
            .expect("gaussian_filter should succeed for 3D input");
        assert!(gaussian3d[[1, 1, 1]] > 0.0); // Should be positive
        assert_eq!(gaussian3d.shape(), &[3, 3, 3]);

        let sep_result = uniform_filter_separable(&input, &[3, 3, 3], None, None);
        assert!(sep_result.is_ok());
        let sep_output = sep_result.expect("Test: operation failed");
        assert_eq!(sep_output.shape(), &[3, 3, 3]);
    }

    #[test]
    fn test_dynamic_dimensions() {
        // Test that filters work with dynamically-sized arrays
        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // Convert to dynamic dimensions
        let input_dyn = input
            .clone()
            .into_dimensionality::<IxDyn>()
            .expect("dimensionality conversion should succeed");

        // Apply filter - using smaller kernels to avoid overflow
        let result_dyn = uniform_filter(&input_dyn, &[2, 2], None, None)
            .expect("uniform_filter should succeed for dynamic dimensions");

        // Convert back
        let result = result_dyn
            .clone()
            .into_dimensionality::<Ix2>()
            .expect("dimensionality conversion back to Ix2 should succeed");

        // Should get the same result as with static dimensions
        let direct_result = uniform_filter(&input, &[2, 2], None, None)
            .expect("uniform_filter for comparison should succeed");

        assert_eq!(result.shape(), direct_result.shape());
        for (r1, r2) in result.iter().zip(direct_result.iter()) {
            assert_abs_diff_eq!(*r1, *r2, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_separable_uniform_filter() {
        // Test that separable uniform filter gives acceptable results
        // Use a small array to avoid stack overflow
        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // Apply both implementations using small kernel
        let direct = uniform_filter(&input, &[2, 2], None, None)
            .expect("direct uniform_filter should succeed");
        let separable = uniform_filter_separable(&input, &[2, 2], None, None)
            .expect("separable uniform_filter should succeed");

        // Results should have the same shape
        assert_eq!(direct.shape(), separable.shape());

        // Note: The implementations are different (one is direct 2D, the other is sequential 1D),
        // so exact values will differ. Just check that results are reasonable.
        for i in 0..2 {
            for j in 0..2 {
                // Check values are positive and not too large
                assert!(separable[[i, j]] > 0.0);
                assert!(separable[[i, j]] < 5.0);
            }
        }
    }

    #[test]
    fn test_custom_origin() {
        // Test filters with custom origin
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        // Default origin (centered, [1,1] for 3x3)
        let centered = uniform_filter(&input, &[3, 3], None, None)
            .expect("centered uniform_filter should succeed");

        // Custom origin (top-left, [0,0] for 3x3)
        let top_left = uniform_filter(&input, &[3, 3], None, Some(&[0, 0]))
            .expect("top_left uniform_filter should succeed");

        // Should be different
        assert!(centered[[0, 0]] != top_left[[0, 0]]);
    }

    #[test]
    fn test_single_size_expansion() {
        // Test that providing a single size expands to all dimensions
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        // Apply filter with single size
        let result1 = uniform_filter(&input, &[3], None, None)
            .expect("uniform_filter with single size should succeed");

        // Apply filter with explicitly specified size for each dimension
        let result2 = uniform_filter(&input, &[3, 3], None, None)
            .expect("uniform_filter with explicit sizes should succeed");

        // Results should be the same
        assert_eq!(result1.shape(), result2.shape());
        for (a, b) in result1.iter().zip(result2.iter()) {
            assert_abs_diff_eq!(*a, *b, epsilon = 1e-10);
        }
    }

    #[test]
    fn test_filter_pipeline() {
        // Test a pipeline of multiple filters
        // Use a smaller 2x2 array to avoid stack overflow
        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // Apply a series of filters with smaller kernel sizes
        let smoothed = gaussian_filter(&input, 1.0, None, None)
            .expect("gaussian_filter in pipeline should succeed");
        let enhanced = minimum_filter(&smoothed, &[2, 2], None, None)
            .expect("minimum_filter in pipeline should succeed");
        let final_result = maximum_filter(&enhanced, &[2, 2], None, None)
            .expect("maximum_filter in pipeline should succeed");

        // Should have proper shape
        assert_eq!(final_result.shape(), input.shape());
    }

    #[test]
    fn test_generic_filter_mean() {
        use super::super::filter_functions;
        use super::super::generic_filter;

        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        let result = generic_filter(&input, filter_functions::mean, &[3, 3], None, None)
            .expect("generic_filter with mean function should succeed");

        // Center element should be the mean of all elements
        let expected = (1.0 + 2.0 + 3.0 + 4.0 + 5.0 + 6.0 + 7.0 + 8.0 + 9.0) / 9.0;
        assert_abs_diff_eq!(result[[1, 1]], expected, epsilon = 1e-6);

        // Check that shape is preserved
        assert_eq!(result.shape(), input.shape());
    }

    #[test]
    fn test_generic_filter_range() {
        use super::super::filter_functions;
        use super::super::generic_filter;

        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        let result = generic_filter(&input, filter_functions::range, &[3, 3], None, None)
            .expect("generic_filter with range function should succeed");

        // Center element should be the range of all elements (9 - 1 = 8)
        assert_abs_diff_eq!(result[[1, 1]], 8.0, epsilon = 1e-6);
    }

    #[test]
    fn test_generic_filter_custom_function() {
        use super::super::generic_filter;
        use super::super::BorderMode;

        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // Custom function that returns the maximum value
        let max_func =
            |values: &[f64]| -> f64 { values.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b)) };

        // Test with BorderMode::Nearest
        let result = generic_filter(&input, max_func, &[2, 2], Some(BorderMode::Nearest), None)
            .expect("generic_filter with custom function should succeed");

        // Check shape preservation and that center position sees the global maximum
        assert_eq!(result.shape(), input.shape());

        // The bottom-right position should see the global maximum (4.0)
        // because it includes the original (1,1) position
        assert_abs_diff_eq!(result[[1, 1]], 4.0, epsilon = 1e-6);

        // Other positions will see different maxima based on their neighborhoods
        assert!(result[[0, 0]] >= 1.0); // Should see at least the minimum value
        assert!(result[[0, 1]] >= 1.0);
        assert!(result[[1, 0]] >= 1.0);
    }

    #[test]
    fn test_generic_filter_1d() {
        use super::super::filter_functions;
        use super::super::generic_filter;
        use scirs2_core::ndarray::Array1;

        let input = Array1::from(vec![1.0, 2.0, 3.0, 4.0, 5.0]);

        let result = generic_filter(&input, filter_functions::mean, &[3], None, None)
            .expect("generic_filter 1D should succeed");

        // Should preserve shape
        assert_eq!(result.len(), input.len());

        // Center element should be mean of [2, 3, 4] = 3.0
        assert_abs_diff_eq!(result[2], 3.0, epsilon = 1e-6);
    }

    #[test]
    fn test_generic_filter_std_dev() {
        use super::super::filter_functions;
        use super::super::generic_filter;

        // Create a uniform array (std dev should be 0)
        let input = array![[5.0, 5.0, 5.0], [5.0, 5.0, 5.0], [5.0, 5.0, 5.0]];

        let result = generic_filter(&input, filter_functions::std_dev, &[3, 3], None, None)
            .expect("generic_filter with std_dev should succeed");

        // Standard deviation of uniform values should be 0
        assert_abs_diff_eq!(result[[1, 1]], 0.0, epsilon = 1e-6);
    }

    #[test]
    fn test_generic_filter_border_modes() {
        use super::super::filter_functions;
        use super::super::generic_filter;
        use super::super::BorderMode;

        let input = array![[1.0, 2.0], [3.0, 4.0]];

        // Test different border modes
        let constant = generic_filter(
            &input,
            filter_functions::mean,
            &[3, 3],
            Some(BorderMode::Constant),
            Some(0.0),
        )
        .expect("generic_filter with Constant border mode should succeed");
        let reflect = generic_filter(
            &input,
            filter_functions::mean,
            &[3, 3],
            Some(BorderMode::Reflect),
            None,
        )
        .expect("generic_filter with Reflect border mode should succeed");
        let nearest = generic_filter(
            &input,
            filter_functions::mean,
            &[3, 3],
            Some(BorderMode::Nearest),
            None,
        )
        .expect("generic_filter with Nearest border mode should succeed");

        // Results should be different for different border modes
        assert!(constant[[0, 0]] != reflect[[0, 0]]);
        assert!(reflect[[0, 0]] != nearest[[0, 0]]);
    }

    #[test]
    fn test_extreme_kernel_sizes() {
        // Test very small (1x1) kernels
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        let result_1x1 = uniform_filter(&input, &[1, 1], None, None)
            .expect("uniform_filter with 1x1 kernel should succeed");
        // 1x1 kernel should leave the array unchanged
        assert_eq!(result_1x1, input);

        let median_1x1 = median_filter(&input, &[1, 1], None)
            .expect("median_filter with 1x1 kernel should succeed");
        assert_eq!(median_1x1, input);

        // Test kernels larger than input
        let small_input = array![[1.0, 2.0]];
        let result_large = uniform_filter(&small_input, &[5, 5], None, None)
            .expect("uniform_filter with large kernel should succeed");
        assert_eq!(result_large.shape(), small_input.shape());
    }

    #[test]
    fn test_numerical_edge_cases() {
        // Test with arrays containing extreme values
        let input_extreme = array![
            [f64::MIN, f64::MAX, 0.0],
            [f64::NEG_INFINITY, f64::INFINITY, f64::NAN]
        ];

        // These should not panic, even with extreme values
        let result = uniform_filter(&input_extreme, &[2, 2], None, None);
        assert!(result.is_ok() || result.is_err()); // Either outcome is acceptable

        // Test with very small sigma for Gaussian
        let normal_input = array![[1.0, 2.0], [3.0, 4.0]];
        let tiny_sigma = gaussian_filter(&normal_input, 1e-10, None, None)
            .expect("gaussian_filter with tiny sigma should succeed");
        // Very small sigma should approximate the original
        assert_abs_diff_eq!(tiny_sigma[[0, 0]], normal_input[[0, 0]], epsilon = 0.1);
    }

    #[test]
    fn test_degenerate_arrays() {
        // Test 1D arrays
        let input_1d = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let result_1d = gaussian_filter(&input_1d, 1.0, None, None)
            .expect("gaussian_filter on 1D array should succeed");
        assert_eq!(result_1d.shape(), input_1d.shape());

        // Test minimum size 2D arrays
        let input_2x1 = array![[1.0], [2.0]];
        let result_2x1 = median_filter(&input_2x1, &[1, 1], None)
            .expect("median_filter on 2x1 array should succeed");
        assert_eq!(result_2x1, input_2x1);

        // Test single-pixel array
        let single_pixel = array![[42.0]];
        let result_single = uniform_filter(&single_pixel, &[1, 1], None, None)
            .expect("uniform_filter on single pixel should succeed");
        assert_eq!(result_single, single_pixel);
    }

    #[test]
    fn test_consistency_across_dimensions() {
        // Test that the same operation on different dimensional views gives consistent results
        let input_3d = Array3::from_shape_fn((4, 4, 4), |(i, j, k)| (i + j + k) as f64);

        // Apply filter to each 2D slice separately
        let mut manual_result = input_3d.clone();
        for mut slice in manual_result.axis_iter_mut(scirs2_core::ndarray::Axis(2)) {
            let temp = gaussian_filter(&slice.to_owned(), 0.5, None, None)
                .expect("gaussian_filter on slice should succeed");
            slice.assign(&temp);
        }

        // The operation should not fail and should preserve shape
        assert_eq!(manual_result.shape(), input_3d.shape());
    }

    #[test]
    fn test_filter_with_all_border_modes() {
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        use crate::BorderMode;

        // Test each border mode doesn't crash and produces reasonable results
        let modes = [
            BorderMode::Constant,
            BorderMode::Reflect,
            BorderMode::Mirror,
            BorderMode::Wrap,
            BorderMode::Nearest,
        ];

        for mode in &modes {
            let result = gaussian_filter(&input, 1.0, Some(*mode), None)
                .expect("gaussian_filter with border mode should succeed");
            assert_eq!(result.shape(), input.shape());

            // Results should be finite for all modes except potentially with extreme inputs
            assert!(result.iter().all(|&x| x.is_finite()));

            let median_result = median_filter(&input, &[3, 3], Some(*mode))
                .expect("median_filter with border mode should succeed");
            assert_eq!(median_result.shape(), input.shape());
        }
    }

    #[test]
    fn test_filter_precision_preservation() {
        // Test that filters preserve reasonable precision
        let input = array![
            [1.0, 1.0000001, 1.0],
            [1.0000001, 1.0, 1.0000001],
            [1.0, 1.0000001, 1.0]
        ];

        let result = gaussian_filter(&input, 0.1, None, None)
            .expect("gaussian_filter for precision test should succeed");

        // With very small sigma, should preserve fine differences
        let variance: f64 = result.iter().map(|&x| (x - 1.0).powi(2)).sum();
        assert!(
            variance > 0.0,
            "Filter should preserve some of the input variation"
        );
    }

    #[test]
    fn test_asymmetric_kernels() {
        let input = Array2::from_shape_fn((10, 20), |(i, j)| (i * j) as f64);

        // Test asymmetric kernel sizes
        let result = uniform_filter(&input, &[3, 7], None, None)
            .expect("uniform_filter with asymmetric kernel should succeed");
        assert_eq!(result.shape(), input.shape());

        let median_result = median_filter(&input, &[5, 3], None)
            .expect("median_filter with asymmetric kernel should succeed");
        assert_eq!(median_result.shape(), input.shape());
    }

    #[test]
    fn test_filter_memory_efficiency() {
        // Test that filters work with moderately large arrays without excessive memory usage
        let large_input = Array2::from_shape_fn((100, 100), |(i, j)| ((i + j) as f64).sin());

        // These should complete without running out of memory
        let gaussian_result = gaussian_filter(&large_input, 2.0, None, None)
            .expect("gaussian_filter on large array should succeed");
        assert_eq!(gaussian_result.shape(), large_input.shape());

        let uniform_result = uniform_filter(&large_input, &[5, 5], None, None)
            .expect("uniform_filter on large array should succeed");
        assert_eq!(uniform_result.shape(), large_input.shape());
    }

    #[test]
    fn test_filter_commutativity() {
        // Test that certain filter combinations are commutative where expected
        let input = array![
            [1.0, 2.0, 3.0, 4.0],
            [5.0, 6.0, 7.0, 8.0],
            [9.0, 10.0, 11.0, 12.0]
        ];

        // Two small Gaussian filters should approximate one larger one
        let small_twice = {
            let temp = gaussian_filter(&input, 0.5, None, None)
                .expect("first gaussian_filter in commutativity test should succeed");
            gaussian_filter(&temp, 0.5, None, None)
                .expect("second gaussian_filter in commutativity test should succeed")
        };

        let larger_once = gaussian_filter(&input, 0.707, None, None)
            .expect("gaussian_filter for commutativity comparison should succeed"); // sqrt(0.5^2 + 0.5^2)

        // Results should be similar (not exact due to discrete kernels)
        let max_diff = small_twice
            .iter()
            .zip(larger_once.iter())
            .map(|(&a, &b)| (a - b).abs())
            .fold(0.0, f64::max);

        assert!(
            max_diff < 0.5,
            "Sequential small filters should approximate single larger filter"
        );
    }
}