evlib 0.8.6

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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Event cropping augmentations
//!
//! This module implements spatial cropping operations for event camera data:
//! - CenterCrop: Crop events to center region of specified size
//! - RandomCrop: Crop events to random region of specified size
//!
//! Key features:
//! - Coordinate remapping: events are transformed to crop coordinate system
//! - Event filtering: events outside crop region are removed
//! - Polars-first implementations for high performance
//! - Reproducible seeding support for RandomCrop

use crate::ev_augmentation::{AugmentationError, AugmentationResult, Validatable};
// Removed: use crate::{Event, Events}; - legacy types no longer exist
use rand::Rng;
use rand_distr::{Distribution, Uniform};
// Tracing imports removed due to unused warnings

#[cfg(not(unix))]
macro_rules! debug {
    ($($args:tt)*) => {};
}

#[cfg(not(unix))]
macro_rules! info {
    ($($args:tt)*) => {};
}

#[cfg(not(unix))]
macro_rules! warn {
    ($($args:tt)*) => {
        eprintln!("[WARN] {}", format!($($args)*))
    };
}

#[cfg(not(unix))]
macro_rules! trace {
    ($($args:tt)*) => {};
}

#[cfg(not(unix))]
macro_rules! error {
    ($($args:tt)*) => {
        eprintln!("[ERROR] {}", format!($($args)*))
    };
}

#[cfg(not(unix))]
macro_rules! instrument {
    ($($args:tt)*) => {};
}

use crate::ev_augmentation::{COL_X, COL_Y};

use polars::prelude::*;

/// Center crop augmentation
///
/// Crops events to the center region of the specified size and remaps coordinates
/// to the crop coordinate system. Events outside the crop region are removed.
///
/// Mathematical transformation:
/// - offset_x = (sensor_width - crop_width) / 2
/// - offset_y = (sensor_height - crop_height) / 2
/// - x_new = x_old - offset_x
/// - y_new = y_old - offset_y
/// - Events with x_old ∉ [offset_x, offset_x + crop_width) are removed
/// - Events with y_old ∉ [offset_y, offset_y + crop_height) are removed
#[derive(Debug, Clone)]
pub struct CenterCropAugmentation {
    /// Width of the crop region
    pub crop_width: u16,
    /// Height of the crop region
    pub crop_height: u16,
    /// Original sensor width
    pub sensor_width: u16,
    /// Original sensor height
    pub sensor_height: u16,
}

impl CenterCropAugmentation {
    /// Create a new center crop augmentation
    ///
    /// # Arguments
    ///
    /// * `crop_width` - Width of the crop region
    /// * `crop_height` - Height of the crop region
    /// * `sensor_width` - Original sensor width
    /// * `sensor_height` - Original sensor height
    pub fn new(crop_width: u16, crop_height: u16, sensor_width: u16, sensor_height: u16) -> Self {
        Self {
            crop_width,
            crop_height,
            sensor_width,
            sensor_height,
        }
    }

    /// Get the crop offset coordinates
    pub fn get_offsets(&self) -> (u16, u16) {
        let offset_x = (self.sensor_width - self.crop_width) / 2;
        let offset_y = (self.sensor_height - self.crop_height) / 2;
        (offset_x, offset_y)
    }

    /// Get the crop bounds (inclusive start, exclusive end)
    pub fn get_bounds(&self) -> (u16, u16, u16, u16) {
        let (offset_x, offset_y) = self.get_offsets();
        (
            offset_x,
            offset_x + self.crop_width,
            offset_y,
            offset_y + self.crop_height,
        )
    }

    /// Get description of this augmentation
    pub fn description(&self) -> String {
        format!(
            "{}x{} from {}x{}",
            self.crop_width, self.crop_height, self.sensor_width, self.sensor_height
        )
    }
}

impl Validatable for CenterCropAugmentation {
    fn validate(&self) -> AugmentationResult<()> {
        if self.crop_width == 0 || self.crop_height == 0 {
            return Err(AugmentationError::InvalidConfig(
                "Crop dimensions must be positive".to_string(),
            ));
        }
        if self.sensor_width == 0 || self.sensor_height == 0 {
            return Err(AugmentationError::InvalidSensorSize(
                self.sensor_width,
                self.sensor_height,
            ));
        }
        if self.crop_width > self.sensor_width || self.crop_height > self.sensor_height {
            return Err(AugmentationError::InvalidConfig(
                "Crop size cannot be larger than sensor size".to_string(),
            ));
        }
        Ok(())
    }
}

/* Commented out - legacy SingleAugmentation trait no longer exists
impl SingleAugmentation for CenterCropAugmentation {
    fn apply(&self, events: &Events) -> AugmentationResult<Events> {
        center_crop(events, self)
    }

    fn description(&self) -> String {
        format!("Center crop: {}", self.description())
    }
}
*/

/// Random crop augmentation
///
/// Crops events to a randomly positioned region of the specified size and remaps coordinates
/// to the crop coordinate system. Events outside the crop region are removed.
///
/// Mathematical transformation:
/// - offset_x = random uniform from [0, sensor_width - crop_width]
/// - offset_y = random uniform from [0, sensor_height - crop_height]
/// - x_new = x_old - offset_x
/// - y_new = y_old - offset_y
/// - Events with x_old ∉ [offset_x, offset_x + crop_width) are removed
/// - Events with y_old ∉ [offset_y, offset_y + crop_height) are removed
#[derive(Debug, Clone)]
pub struct RandomCropAugmentation {
    /// Width of the crop region
    pub crop_width: u16,
    /// Height of the crop region
    pub crop_height: u16,
    /// Original sensor width
    pub sensor_width: u16,
    /// Original sensor height
    pub sensor_height: u16,
    /// Random seed for reproducibility
    pub seed: Option<u64>,
}

impl RandomCropAugmentation {
    /// Create a new random crop augmentation
    ///
    /// # Arguments
    ///
    /// * `crop_width` - Width of the crop region
    /// * `crop_height` - Height of the crop region
    /// * `sensor_width` - Original sensor width
    /// * `sensor_height` - Original sensor height
    pub fn new(crop_width: u16, crop_height: u16, sensor_width: u16, sensor_height: u16) -> Self {
        Self {
            crop_width,
            crop_height,
            sensor_width,
            sensor_height,
            seed: None,
        }
    }

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

    /// Get random crop offset coordinates
    pub fn get_random_offsets(&self, rng: &mut impl Rng) -> (u16, u16) {
        let max_offset_x = self.sensor_width - self.crop_width;
        let max_offset_y = self.sensor_height - self.crop_height;

        let offset_x = if max_offset_x > 0 {
            Uniform::new(0, max_offset_x).sample(rng)
        } else {
            0
        };

        let offset_y = if max_offset_y > 0 {
            Uniform::new(0, max_offset_y).sample(rng)
        } else {
            0
        };

        (offset_x, offset_y)
    }

    /// Get description of this augmentation
    pub fn description(&self) -> String {
        format!(
            "{}x{} from {}x{}",
            self.crop_width, self.crop_height, self.sensor_width, self.sensor_height
        )
    }
}

impl Validatable for RandomCropAugmentation {
    fn validate(&self) -> AugmentationResult<()> {
        if self.crop_width == 0 || self.crop_height == 0 {
            return Err(AugmentationError::InvalidConfig(
                "Crop dimensions must be positive".to_string(),
            ));
        }
        if self.sensor_width == 0 || self.sensor_height == 0 {
            return Err(AugmentationError::InvalidSensorSize(
                self.sensor_width,
                self.sensor_height,
            ));
        }
        if self.crop_width > self.sensor_width || self.crop_height > self.sensor_height {
            return Err(AugmentationError::InvalidConfig(
                "Crop size cannot be larger than sensor size".to_string(),
            ));
        }
        Ok(())
    }
}

/* Commented out - legacy SingleAugmentation trait no longer exists
impl SingleAugmentation for RandomCropAugmentation {
    fn apply(&self, events: &Events) -> AugmentationResult<Events> {
        random_crop(events, self)
    }

    fn description(&self) -> String {
        format!("Random crop: {}", self.description())
    }
}
*/

/* Commented out - legacy Events type no longer exists
/// Apply center crop to events
#[cfg_attr(unix, instrument(skip(events), fields(n_events = events.len())))]
pub fn center_crop(events: &Events, config: &CenterCropAugmentation) -> AugmentationResult<Events> {
    let start_time = std::time::Instant::now();

    if events.is_empty() {
        debug!("No events to crop");
        return Ok(Vec::new());
    }

    // Validate configuration
    config.validate()?;

    let (offset_x, offset_y) = config.get_offsets();
    let (x_start, x_end, y_start, y_end) = config.get_bounds();

    // Filter and remap events
    let mut cropped_events = Vec::new();
    let mut kept_count = 0;

    for event in events {
        // Check if event is within crop bounds
        if event.x >= x_start && event.x < x_end && event.y >= y_start && event.y < y_end {
            // Remap coordinates to crop coordinate system
            let new_event = Event {
                t: event.t,
                x: event.x - offset_x,
                y: event.y - offset_y,
                polarity: event.polarity,
            };
            cropped_events.push(new_event);
            kept_count += 1;
        }
    }

    let processing_time = start_time.elapsed().as_secs_f64();

    info!(
        "Center crop applied ({}x{} at offset {},{}) : {} -> {} events in {:.3}s",
        config.crop_width,
        config.crop_height,
        offset_x,
        offset_y,
        events.len(),
        kept_count,
        processing_time
    );

    Ok(cropped_events)
}
*/

/* Commented out - legacy Events type no longer exists
/// Apply random crop to events
#[cfg_attr(unix, instrument(skip(events), fields(n_events = events.len())))]
pub fn random_crop(events: &Events, config: &RandomCropAugmentation) -> AugmentationResult<Events> {
    let start_time = std::time::Instant::now();

    if events.is_empty() {
        debug!("No events to crop");
        return Ok(Vec::new());
    }

    // Validate configuration
    config.validate()?;

    // Initialize RNG
    let mut rng = if let Some(seed) = config.seed {
        rand::rngs::StdRng::seed_from_u64(seed)
    } else {
        rand::rngs::StdRng::from_entropy()
    };

    let (offset_x, offset_y) = config.get_random_offsets(&mut rng);
    let x_start = offset_x;
    let x_end = offset_x + config.crop_width;
    let y_start = offset_y;
    let y_end = offset_y + config.crop_height;

    // Filter and remap events
    let mut cropped_events = Vec::new();
    let mut kept_count = 0;

    for event in events {
        // Check if event is within crop bounds
        if event.x >= x_start && event.x < x_end && event.y >= y_start && event.y < y_end {
            // Remap coordinates to crop coordinate system
            let new_event = Event {
                t: event.t,
                x: event.x - offset_x,
                y: event.y - offset_y,
                polarity: event.polarity,
            };
            cropped_events.push(new_event);
            kept_count += 1;
        }
    }

    let processing_time = start_time.elapsed().as_secs_f64();

    info!(
        "Random crop applied ({}x{} at offset {},{}) : {} -> {} events in {:.3}s",
        config.crop_width,
        config.crop_height,
        offset_x,
        offset_y,
        events.len(),
        kept_count,
        processing_time
    );

    Ok(cropped_events)
}
*/

/// Apply center crop using Polars operations
pub fn apply_center_crop_polars(
    df: LazyFrame,
    config: &CenterCropAugmentation,
) -> PolarsResult<LazyFrame> {
    let (offset_x, offset_y) = config.get_offsets();
    let (x_start, x_end, y_start, y_end) = config.get_bounds();

    // Apply vectorized bounds filtering and coordinate remapping
    let filtered_df = df
        .filter(
            col(COL_X)
                .gt_eq(lit(x_start as i64))
                .and(col(COL_X).lt(lit(x_end as i64)))
                .and(col(COL_Y).gt_eq(lit(y_start as i64)))
                .and(col(COL_Y).lt(lit(y_end as i64))),
        )
        .with_columns([
            (col(COL_X) - lit(offset_x as i64)).alias(COL_X),
            (col(COL_Y) - lit(offset_y as i64)).alias(COL_Y),
        ]);

    Ok(filtered_df)
}

/* Commented out - depends on legacy random_crop function
/// Apply random crop using Polars operations
pub fn apply_random_crop_polars(
    df: LazyFrame,
    config: &RandomCropAugmentation,
) -> PolarsResult<LazyFrame> {
    // For random operations, we need to generate the offset once
    // We'll collect the DataFrame, apply the crop, and return as LazyFrame
    let collected_df = df.collect()?;

    // Convert to events, apply random crop, and convert back
    let events = crate::ev_augmentation::dataframe_to_events(&collected_df)
        .map_err(|e| PolarsError::ComputeError(format!("Conversion error: {}", e).into()))?;

    let cropped_events = random_crop(&events, config)
        .map_err(|e| PolarsError::ComputeError(format!("Random crop error: {}", e).into()))?;

    let result_df = crate::events_to_dataframe(&cropped_events)?;
    Ok(result_df.lazy())
}
*/

/* Commented out - legacy Events type no longer exists
#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_events() -> Events {
        let mut events = Vec::new();

        // Create a 5x5 grid of events at coordinates (10*i, 10*j) for i,j in 0..5
        // This gives us events at (0,0), (0,10), ..., (40,40)
        // Total: 25 events in a 50x50 region
        for i in 0..5 {
            for j in 0..5 {
                events.push(Event {
                    t: (i * 5 + j) as f64,
                    x: (i * 10) as u16,
                    y: (j * 10) as u16,
                    polarity: (i + j) % 2 == 0,
                });
            }
        }
        events
    }

    #[test]
    fn test_center_crop_augmentation() {
        let events = create_test_events();
        let config = CenterCropAugmentation::new(30, 30, 50, 50);

        let cropped = config.apply(&events).unwrap();

        // With a 30x30 crop from 50x50 sensor, offset is (10, 10)
        // Events at (10,10), (10,20), (10,30), (20,10), (20,20), (20,30), (30,10), (30,20), (30,30) should be kept
        // After remapping: (0,0), (0,10), (0,20), (10,0), (10,10), (10,20), (20,0), (20,10), (20,20)
        assert!(cropped.len() > 0);
        assert!(cropped.len() <= events.len());

        // Check that all coordinates are within crop bounds
        for event in &cropped {
            assert!(event.x < 30);
            assert!(event.y < 30);
        }
    }

    #[test]
    fn test_random_crop_augmentation() {
        let events = create_test_events();
        let config = RandomCropAugmentation::new(30, 30, 50, 50).with_seed(42);

        let cropped = config.apply(&events).unwrap();

        // Should have some events
        assert!(cropped.len() <= events.len());

        // Check that all coordinates are within crop bounds
        for event in &cropped {
            assert!(event.x < 30);
            assert!(event.y < 30);
        }

        // Test reproducibility
        let cropped2 = config.apply(&events).unwrap();
        assert_eq!(cropped.len(), cropped2.len());

        // Should have same events (due to seed)
        for (e1, e2) in cropped.iter().zip(cropped2.iter()) {
            assert_eq!(e1.x, e2.x);
            assert_eq!(e1.y, e2.y);
            assert_eq!(e1.t, e2.t);
            assert_eq!(e1.polarity, e2.polarity);
        }
    }

    #[test]
    fn test_center_crop_coordinate_remapping() {
        let events = vec![
            Event {
                t: 1.0,
                x: 25,
                y: 25,
                polarity: true,
            }, // Should be at center after crop
        ];

        let config = CenterCropAugmentation::new(30, 30, 50, 50);
        let cropped = center_crop(&events, &config).unwrap();

        assert_eq!(cropped.len(), 1);
        // Original (25, 25) with offset (10, 10) should become (15, 15)
        assert_eq!(cropped[0].x, 15);
        assert_eq!(cropped[0].y, 15);
        assert_eq!(cropped[0].t, 1.0);
        assert_eq!(cropped[0].polarity, true);
    }

    #[test]
    fn test_random_crop_coordinate_remapping() {
        let events = vec![Event {
            t: 1.0,
            x: 25,
            y: 25,
            polarity: true,
        }];

        let config = RandomCropAugmentation::new(20, 20, 40, 40).with_seed(42);
        let cropped = random_crop(&events, &config).unwrap();

        if !cropped.is_empty() {
            // Check coordinate bounds
            assert!(cropped[0].x < 20);
            assert!(cropped[0].y < 20);
            assert_eq!(cropped[0].t, 1.0);
            assert_eq!(cropped[0].polarity, true);
        }
    }

    #[test]
    fn test_crop_edge_cases() {
        let events = create_test_events();

        // Crop size equals sensor size
        let config = CenterCropAugmentation::new(50, 50, 50, 50);
        let cropped = config.apply(&events).unwrap();
        assert_eq!(cropped.len(), events.len());

        // All coordinates should remain the same (offset is 0)
        for (original, cropped) in events.iter().zip(cropped.iter()) {
            assert_eq!(original.x, cropped.x);
            assert_eq!(original.y, cropped.y);
        }

        // Very small crop
        let config = CenterCropAugmentation::new(1, 1, 50, 50);
        let cropped = config.apply(&events).unwrap();
        // Only events exactly at the center pixel should remain
        for event in &cropped {
            assert_eq!(event.x, 0); // After remapping to (0,0)
            assert_eq!(event.y, 0);
        }
    }

    #[test]
    fn test_crop_validation() {
        // Valid configurations
        assert!(CenterCropAugmentation::new(30, 30, 50, 50)
            .validate()
            .is_ok());
        assert!(RandomCropAugmentation::new(30, 30, 50, 50)
            .validate()
            .is_ok());

        // Invalid configurations
        assert!(CenterCropAugmentation::new(0, 30, 50, 50)
            .validate()
            .is_err()); // Zero width
        assert!(CenterCropAugmentation::new(30, 0, 50, 50)
            .validate()
            .is_err()); // Zero height
        assert!(CenterCropAugmentation::new(30, 30, 0, 50)
            .validate()
            .is_err()); // Zero sensor width
        assert!(CenterCropAugmentation::new(30, 30, 50, 0)
            .validate()
            .is_err()); // Zero sensor height
        assert!(CenterCropAugmentation::new(60, 30, 50, 50)
            .validate()
            .is_err()); // Crop larger than sensor
        assert!(CenterCropAugmentation::new(30, 60, 50, 50)
            .validate()
            .is_err()); // Crop larger than sensor

        // Same for random crop
        assert!(RandomCropAugmentation::new(60, 30, 50, 50)
            .validate()
            .is_err());
    }

    #[test]
    fn test_empty_events() {
        let events = Vec::new();

        let center_config = CenterCropAugmentation::new(30, 30, 50, 50);
        let cropped = center_config.apply(&events).unwrap();
        assert!(cropped.is_empty());

        let random_config = RandomCropAugmentation::new(30, 30, 50, 50);
        let cropped = random_config.apply(&events).unwrap();
        assert!(cropped.is_empty());
    }

    #[test]
    fn test_crop_offset_calculations() {
        let config = CenterCropAugmentation::new(30, 40, 100, 80);
        let (offset_x, offset_y) = config.get_offsets();
        assert_eq!(offset_x, 35); // (100 - 30) / 2
        assert_eq!(offset_y, 20); // (80 - 40) / 2

        let (x_start, x_end, y_start, y_end) = config.get_bounds();
        assert_eq!(x_start, 35);
        assert_eq!(x_end, 65); // 35 + 30
        assert_eq!(y_start, 20);
        assert_eq!(y_end, 60); // 20 + 40
    }

    #[test]
    fn test_center_crop_polars() {
        use crate::events_to_dataframe;

        let events = create_test_events();
        let df = events_to_dataframe(&events).unwrap().lazy();

        let config = CenterCropAugmentation::new(30, 30, 50, 50);
        let cropped_df = apply_center_crop_polars(df, &config).unwrap();
        let result = cropped_df.collect().unwrap();

        // Should have some events
        assert!(result.height() > 0);
        assert!(result.height() <= events.len());

        // Check coordinate bounds
        let x_series = result.column(COL_X).unwrap().i64().unwrap();
        let y_series = result.column(COL_Y).unwrap().i64().unwrap();

        for i in 0..result.height() {
            let x = x_series.get(i).unwrap();
            let y = y_series.get(i).unwrap();
            assert!(x >= 0 && x < 30);
            assert!(y >= 0 && y < 30);
        }
    }

    #[test]
    fn test_random_crop_polars() {
        use crate::events_to_dataframe;

        let events = create_test_events();
        let df = events_to_dataframe(&events).unwrap().lazy();

        let config = RandomCropAugmentation::new(30, 30, 50, 50).with_seed(42);
        let cropped_df = apply_random_crop_polars(df, &config).unwrap();
        let result = cropped_df.collect().unwrap();

        // Check coordinate bounds
        if result.height() > 0 {
            let x_series = result.column(COL_X).unwrap().i64().unwrap();
            let y_series = result.column(COL_Y).unwrap().i64().unwrap();

            for i in 0..result.height() {
                let x = x_series.get(i).unwrap();
                let y = y_series.get(i).unwrap();
                assert!(x >= 0 && x < 30);
                assert!(y >= 0 && y < 30);
            }
        }
    }
}
*/