evlib 0.8.2

Event Camera Data Processing Library
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Configuration module for event augmentations
//!
//! This module provides configuration structures for all augmentation operations,
//! allowing flexible composition of different augmentation strategies.

use crate::ev_augmentation::{
    CenterCropAugmentation, DecimateAugmentation, DropAreaAugmentation, DropEventAugmentation,
    DropTimeAugmentation, GeometricTransformAugmentation, RandomCropAugmentation,
    SpatialJitterAugmentation, TimeJitterAugmentation, TimeReversalAugmentation,
    TimeSkewAugmentation, UniformNoiseAugmentation,
};
use std::fmt;
use thiserror::Error;

/// Result type for augmentation operations
pub type AugmentationResult<T> = Result<T, AugmentationError>;

/// Error type for augmentation operations
#[derive(Error, Debug)]
pub enum AugmentationError {
    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),

    #[error("Processing error: {0}")]
    ProcessingError(String),

    #[error("Invalid sensor size: width={0}, height={1}")]
    InvalidSensorSize(u16, u16),

    #[error("Invalid probability: {0} (must be between 0 and 1)")]
    InvalidProbability(f64),

    #[error("Invalid time range: start={0}, end={1}")]
    InvalidTimeRange(f64, f64),

    #[error("Invalid spatial bounds: x={0}-{1}, y={2}-{3}")]
    InvalidSpatialBounds(u16, u16, u16, u16),

    #[error("Insufficient events for operation: required={0}, available={1}")]
    InsufficientEvents(usize, usize),
}

/// Trait for validatable configurations
pub trait Validatable {
    /// Validate this configuration
    fn validate(&self) -> AugmentationResult<()>;
}

/// Main augmentation configuration
///
/// This struct allows composing multiple augmentation operations into a single pipeline.
/// Augmentations are applied in a specific order to ensure consistent results.
///
/// # Example
///
/// ```rust
/// use evlib::ev_augmentation::AugmentationConfig;
///
/// let config = AugmentationConfig::new()
///     .with_spatial_jitter(1.0, 1.0)
///     .with_time_jitter(1000.0)
///     .with_drop_event(0.1); // Drop 10% of events
/// ```
#[derive(Debug, Clone)]
pub struct AugmentationConfig {
    /// Spatial jitter augmentation
    pub spatial_jitter: Option<SpatialJitterAugmentation>,
    /// Geometric transformations (flips and polarity reversal)
    pub geometric_transforms: Option<GeometricTransformAugmentation>,
    /// Time jitter augmentation
    pub time_jitter: Option<TimeJitterAugmentation>,
    /// Time reversal augmentation
    pub time_reversal: Option<TimeReversalAugmentation>,
    /// Time skew augmentation
    pub time_skew: Option<TimeSkewAugmentation>,
    /// Drop event augmentation
    pub drop_event: Option<DropEventAugmentation>,
    /// Drop by time augmentation
    pub drop_time: Option<DropTimeAugmentation>,
    /// Drop by area augmentation
    pub drop_area: Option<DropAreaAugmentation>,
    /// Uniform noise augmentation
    pub uniform_noise: Option<UniformNoiseAugmentation>,
    /// Decimate augmentation
    pub decimate: Option<DecimateAugmentation>,
    /// Center crop augmentation
    pub center_crop: Option<CenterCropAugmentation>,
    /// Random crop augmentation
    pub random_crop: Option<RandomCropAugmentation>,
    /// Whether to ensure temporal order after augmentation
    pub ensure_temporal_order: bool,
    /// Whether to validate configuration before applying
    pub validate_config: bool,
    /// Random seed for reproducibility (None for random)
    pub random_seed: Option<u64>,
}

impl Default for AugmentationConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl AugmentationConfig {
    /// Create a new empty augmentation configuration
    pub fn new() -> Self {
        Self {
            spatial_jitter: None,
            time_jitter: None,
            time_reversal: None,
            time_skew: None,
            drop_event: None,
            drop_time: None,
            drop_area: None,
            uniform_noise: None,
            decimate: None,
            geometric_transforms: None,
            center_crop: None,
            random_crop: None,
            ensure_temporal_order: true,
            validate_config: true,
            random_seed: None,
        }
    }

    /// Add spatial jitter augmentation
    ///
    /// # Arguments
    ///
    /// * `var_x` - Variance for x-coordinate jitter
    /// * `var_y` - Variance for y-coordinate jitter
    pub fn with_spatial_jitter(mut self, var_x: f64, var_y: f64) -> Self {
        self.spatial_jitter = Some(SpatialJitterAugmentation::new(var_x, var_y));
        self
    }

    /// Add spatial jitter with correlation
    ///
    /// # Arguments
    ///
    /// * `var_x` - Variance for x-coordinate jitter
    /// * `var_y` - Variance for y-coordinate jitter
    /// * `sigma_xy` - Covariance between x and y
    pub fn with_spatial_jitter_correlated(mut self, var_x: f64, var_y: f64, sigma_xy: f64) -> Self {
        self.spatial_jitter =
            Some(SpatialJitterAugmentation::new(var_x, var_y).with_correlation(sigma_xy));
        self
    }

    /// Add geometric transformations
    ///
    /// # Arguments
    ///
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    pub fn with_geometric_transforms(mut self, sensor_width: u16, sensor_height: u16) -> Self {
        self.geometric_transforms = Some(GeometricTransformAugmentation::new(
            sensor_width,
            sensor_height,
        ));
        self
    }

    /// Add geometric transformations with horizontal flip probability
    ///
    /// # Arguments
    ///
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    /// * `flip_lr_probability` - Probability of horizontal flip (0.0 to 1.0)
    pub fn with_horizontal_flip(
        mut self,
        sensor_width: u16,
        sensor_height: u16,
        flip_lr_probability: f64,
    ) -> Self {
        self.geometric_transforms = Some(
            GeometricTransformAugmentation::new(sensor_width, sensor_height)
                .with_flip_lr_probability(flip_lr_probability),
        );
        self
    }

    /// Add geometric transformations with vertical flip probability
    ///
    /// # Arguments
    ///
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    /// * `flip_ud_probability` - Probability of vertical flip (0.0 to 1.0)
    pub fn with_vertical_flip(
        mut self,
        sensor_width: u16,
        sensor_height: u16,
        flip_ud_probability: f64,
    ) -> Self {
        self.geometric_transforms = Some(
            GeometricTransformAugmentation::new(sensor_width, sensor_height)
                .with_flip_ud_probability(flip_ud_probability),
        );
        self
    }

    /// Add geometric transformations with polarity flip probability
    ///
    /// # Arguments
    ///
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    /// * `flip_polarity_probability` - Probability of polarity reversal (0.0 to 1.0)
    pub fn with_polarity_flip(
        mut self,
        sensor_width: u16,
        sensor_height: u16,
        flip_polarity_probability: f64,
    ) -> Self {
        self.geometric_transforms = Some(
            GeometricTransformAugmentation::new(sensor_width, sensor_height)
                .with_flip_polarity_probability(flip_polarity_probability),
        );
        self
    }

    /// Add complete geometric transformations with all flip probabilities
    ///
    /// # Arguments
    ///
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    /// * `flip_lr_probability` - Probability of horizontal flip (0.0 to 1.0)
    /// * `flip_ud_probability` - Probability of vertical flip (0.0 to 1.0)
    /// * `flip_polarity_probability` - Probability of polarity reversal (0.0 to 1.0)
    pub fn with_all_geometric_flips(
        mut self,
        sensor_width: u16,
        sensor_height: u16,
        flip_lr_probability: f64,
        flip_ud_probability: f64,
        flip_polarity_probability: f64,
    ) -> Self {
        self.geometric_transforms = Some(
            GeometricTransformAugmentation::new(sensor_width, sensor_height)
                .with_flip_lr_probability(flip_lr_probability)
                .with_flip_ud_probability(flip_ud_probability)
                .with_flip_polarity_probability(flip_polarity_probability),
        );
        self
    }

    /// Add time jitter augmentation
    ///
    /// # Arguments
    ///
    /// * `std_us` - Standard deviation in microseconds
    pub fn with_time_jitter(mut self, std_us: f64) -> Self {
        self.time_jitter = Some(TimeJitterAugmentation::new(std_us));
        self
    }

    /// Add time reversal augmentation
    ///
    /// # Arguments
    ///
    /// * `probability` - Probability of applying time reversal (0.0 to 1.0)
    pub fn with_time_reversal(mut self, probability: f64) -> Self {
        self.time_reversal = Some(TimeReversalAugmentation::new(probability));
        self
    }

    /// Add time skew augmentation
    ///
    /// # Arguments
    ///
    /// * `coefficient` - Multiplicative coefficient for timestamps
    pub fn with_time_skew(mut self, coefficient: f64) -> Self {
        self.time_skew = Some(TimeSkewAugmentation::new(coefficient));
        self
    }

    /// Add time skew with offset
    ///
    /// # Arguments
    ///
    /// * `coefficient` - Multiplicative coefficient for timestamps
    /// * `offset` - Additive offset in seconds
    pub fn with_time_skew_offset(mut self, coefficient: f64, offset: f64) -> Self {
        self.time_skew = Some(TimeSkewAugmentation::new(coefficient).with_offset(offset));
        self
    }

    /// Add drop event augmentation
    ///
    /// # Arguments
    ///
    /// * `probability` - Probability of dropping each event (0-1)
    pub fn with_drop_event(mut self, probability: f64) -> Self {
        self.drop_event = Some(DropEventAugmentation::new(probability));
        self
    }

    /// Add drop by time augmentation
    ///
    /// # Arguments
    ///
    /// * `duration_ratio` - Ratio of sequence duration to drop (0-1)
    pub fn with_drop_time(mut self, duration_ratio: f64) -> Self {
        self.drop_time = Some(DropTimeAugmentation::new(duration_ratio));
        self
    }

    /// Add drop by area augmentation
    ///
    /// # Arguments
    ///
    /// * `area_ratio` - Ratio of sensor area to drop (0-1)
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    pub fn with_drop_area(
        mut self,
        area_ratio: f64,
        sensor_width: u16,
        sensor_height: u16,
    ) -> Self {
        self.drop_area = Some(DropAreaAugmentation::new(
            area_ratio,
            sensor_width,
            sensor_height,
        ));
        self
    }

    /// Add uniform noise augmentation
    ///
    /// # Arguments
    ///
    /// * `n_events` - Number of noise events to add
    /// * `sensor_width` - Sensor width in pixels
    /// * `sensor_height` - Sensor height in pixels
    pub fn with_uniform_noise(
        mut self,
        n_events: usize,
        sensor_width: u16,
        sensor_height: u16,
    ) -> Self {
        self.uniform_noise = Some(UniformNoiseAugmentation::new(
            n_events,
            sensor_width,
            sensor_height,
        ));
        self
    }

    /// Add decimate augmentation
    ///
    /// # Arguments
    ///
    /// * `n` - Keep every nth event per pixel
    pub fn with_decimate(mut self, n: usize) -> Self {
        self.decimate = Some(DecimateAugmentation::new(n));
        self
    }

    /// Add center crop augmentation
    ///
    /// # Arguments
    ///
    /// * `crop_width` - Width of cropped region
    /// * `crop_height` - Height of cropped region
    /// * `sensor_width` - Original sensor width
    /// * `sensor_height` - Original sensor height
    pub fn with_center_crop(
        mut self,
        crop_width: u16,
        crop_height: u16,
        sensor_width: u16,
        sensor_height: u16,
    ) -> Self {
        self.center_crop = Some(CenterCropAugmentation::new(
            crop_width,
            crop_height,
            sensor_width,
            sensor_height,
        ));
        self
    }

    /// Add random crop augmentation
    ///
    /// # Arguments
    ///
    /// * `crop_width` - Width of cropped region
    /// * `crop_height` - Height of cropped region
    /// * `sensor_width` - Original sensor width
    /// * `sensor_height` - Original sensor height
    pub fn with_random_crop(
        mut self,
        crop_width: u16,
        crop_height: u16,
        sensor_width: u16,
        sensor_height: u16,
    ) -> Self {
        self.random_crop = Some(RandomCropAugmentation::new(
            crop_width,
            crop_height,
            sensor_width,
            sensor_height,
        ));
        self
    }

    /// Set whether to ensure temporal order after augmentation
    pub fn with_temporal_order(mut self, ensure: bool) -> Self {
        self.ensure_temporal_order = ensure;
        self
    }

    /// Set whether to validate configuration before applying
    pub fn with_validation(mut self, validate: bool) -> Self {
        self.validate_config = validate;
        self
    }

    /// Set random seed for reproducibility
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.random_seed = Some(seed);
        self
    }

    /// Check if any augmentation is configured
    pub fn is_empty(&self) -> bool {
        self.spatial_jitter.is_none()
            && self.time_jitter.is_none()
            && self.time_reversal.is_none()
            && self.time_skew.is_none()
            && self.drop_event.is_none()
            && self.drop_time.is_none()
            && self.drop_area.is_none()
            && self.uniform_noise.is_none()
            && self.decimate.is_none()
            && self.geometric_transforms.is_none()
            && self.center_crop.is_none()
            && self.random_crop.is_none()
    }

    /// Get description of configured augmentations
    pub fn description(&self) -> String {
        let mut parts = Vec::new();

        if let Some(ref aug) = self.spatial_jitter {
            parts.push(format!("spatial_jitter({})", aug.description()));
        }
        if let Some(ref aug) = self.geometric_transforms {
            parts.push(format!("geometric_transforms({})", aug.description()));
        }
        if let Some(ref aug) = self.time_jitter {
            parts.push(format!("time_jitter({})", aug.description()));
        }
        if let Some(ref aug) = self.time_reversal {
            parts.push(format!("time_reversal({})", aug.description()));
        }
        if let Some(ref aug) = self.time_skew {
            parts.push(format!("time_skew({})", aug.description()));
        }
        if let Some(ref aug) = self.drop_event {
            parts.push(format!("drop_event({})", aug.description()));
        }
        if let Some(ref aug) = self.drop_time {
            parts.push(format!("drop_time({})", aug.description()));
        }
        if let Some(ref aug) = self.drop_area {
            parts.push(format!("drop_area({})", aug.description()));
        }
        if let Some(ref aug) = self.uniform_noise {
            parts.push(format!("uniform_noise({})", aug.description()));
        }
        if let Some(ref aug) = self.decimate {
            parts.push(format!("decimate({})", aug.description()));
        }
        if let Some(ref aug) = self.center_crop {
            parts.push(format!("center_crop({})", aug.description()));
        }
        if let Some(ref aug) = self.random_crop {
            parts.push(format!("random_crop({})", aug.description()));
        }

        if parts.is_empty() {
            "No augmentations configured".to_string()
        } else {
            parts.join(", ")
        }
    }
}

impl Validatable for AugmentationConfig {
    fn validate(&self) -> AugmentationResult<()> {
        // Validate individual augmentations
        if let Some(ref aug) = self.spatial_jitter {
            aug.validate()?;
        }
        if let Some(ref aug) = self.geometric_transforms {
            aug.validate()?;
        }
        if let Some(ref aug) = self.time_jitter {
            aug.validate()?;
        }
        if let Some(ref aug) = self.time_reversal {
            aug.validate()?;
        }
        if let Some(ref aug) = self.time_skew {
            aug.validate()?;
        }
        if let Some(ref aug) = self.drop_event {
            aug.validate()?;
        }
        if let Some(ref aug) = self.drop_time {
            aug.validate()?;
        }
        if let Some(ref aug) = self.drop_area {
            aug.validate()?;
        }
        if let Some(ref aug) = self.uniform_noise {
            aug.validate()?;
        }
        if let Some(ref aug) = self.decimate {
            aug.validate()?;
        }
        if let Some(ref aug) = self.center_crop {
            aug.validate()?;
        }
        if let Some(ref aug) = self.random_crop {
            aug.validate()?;
        }

        // Check for conflicting configurations
        if self.drop_event.is_some() && self.drop_time.is_some() && self.drop_area.is_some() {
            // Warning: multiple drop operations may remove more events than expected
            // This is allowed but worth noting
        }

        Ok(())
    }
}

impl fmt::Display for AugmentationConfig {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "AugmentationConfig: {}", self.description())
    }
}

/// Common augmentation presets
pub mod presets {
    use super::*;

    /// Light augmentation for validation sets
    pub fn light_augmentation() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_spatial_jitter(0.5, 0.5)
            .with_time_jitter(500.0) // 0.5ms
    }

    /// Standard augmentation for training
    pub fn standard_augmentation() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_spatial_jitter(1.0, 1.0)
            .with_time_jitter(1000.0) // 1ms
            .with_drop_event(0.1) // Drop 10%
    }

    /// Heavy augmentation for robust training
    pub fn heavy_augmentation() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_spatial_jitter(2.0, 2.0)
            .with_time_jitter(2000.0) // 2ms
            .with_drop_event(0.2) // Drop 20%
            .with_uniform_noise(1000, 640, 480) // Add 1000 noise events
    }

    /// Temporal-only augmentation
    pub fn temporal_only() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_time_jitter(1000.0)
            .with_time_reversal(0.5) // 50% chance of reversal
            .with_time_skew(1.1) // 10% time stretch
    }

    /// Spatial-only augmentation
    pub fn spatial_only() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_spatial_jitter(1.0, 1.0)
            .with_drop_area(0.1, 640, 480) // Drop 10% area
    }

    /// Noise injection augmentation
    pub fn noise_injection() -> AugmentationConfig {
        AugmentationConfig::new()
            .with_uniform_noise(2000, 640, 480)
            .with_drop_event(0.05) // Small dropout to balance
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_creation() {
        let config = AugmentationConfig::new();
        assert!(config.is_empty());
        assert_eq!(config.description(), "No augmentations configured");
    }

    #[test]
    fn test_config_builder() {
        let config = AugmentationConfig::new()
            .with_spatial_jitter(1.0, 1.0)
            .with_time_jitter(1000.0)
            .with_drop_event(0.1);

        assert!(!config.is_empty());
        assert!(config.spatial_jitter.is_some());
        assert!(config.time_jitter.is_some());
        assert!(config.drop_event.is_some());
    }

    #[test]
    fn test_config_validation() {
        // Valid config
        let valid_config = AugmentationConfig::new()
            .with_spatial_jitter(1.0, 1.0)
            .with_drop_event(0.5);
        assert!(valid_config.validate().is_ok());

        // Invalid config (negative variance)
        let invalid_config = AugmentationConfig::new().with_spatial_jitter(-1.0, 1.0);
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_presets() {
        let light = presets::light_augmentation();
        assert!(light.spatial_jitter.is_some());
        assert!(light.time_jitter.is_some());
        assert!(light.drop_event.is_none());

        let heavy = presets::heavy_augmentation();
        assert!(heavy.spatial_jitter.is_some());
        assert!(heavy.time_jitter.is_some());
        assert!(heavy.drop_event.is_some());
        assert!(heavy.uniform_noise.is_some());
    }
}