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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
// Data formats module
// Handles reading and writing events from various file formats

// HDF5 is only available on Unix platforms (Linux/macOS)
#[cfg(unix)]
use hdf5_metno::File as H5File;
use polars::prelude::*;
use pyo3::prelude::*;
use pyo3_arrow::PyRecordBatch;
// memmap2 removed - no longer using unsafe binary format
use std::fs::File;
use std::io::{BufRead, BufReader, Result as IoResult};

// Format detection module
pub mod format_detector;
pub use format_detector::{
    detect_event_format, EventFormat, FormatDetectionError, FormatDetectionResult, FormatDetector,
    FormatMetadata,
};

// AEDAT format reader module
pub mod aedat_reader;
pub use aedat_reader::{AedatConfig, AedatError, AedatMetadata, AedatReader, AedatVersion};

// AER format reader module
pub mod aer_reader;
pub use aer_reader::{AerConfig, AerError, AerMetadata, AerReader, TimestampMode};

// EVT2 format reader module
pub mod evt2_reader;
pub use evt2_reader::{Evt2Config, Evt2Error, Evt2Metadata, Evt2Reader};

// EVT2.1 format reader module
pub mod evt21_reader;
pub use evt21_reader::{Evt21Config, Evt21Error, Evt21Metadata, Evt21Reader};

// EVT3 format reader module
pub mod evt3_reader;
pub use evt3_reader::{Evt3Config, Evt3Error, Evt3Metadata, Evt3Reader};

// Polarity encoding handler module
pub mod polarity_handler;
pub use polarity_handler::{
    PolarityConfig, PolarityEncoding, PolarityError, PolarityHandler, PolarityStats,
};

// Streaming module for large file processing
pub mod streaming;
pub use streaming::{
    estimate_memory_usage, should_use_streaming, Event, PolarsEventStreamer, StreamingConfig,
};

// Apache Arrow integration for zero-copy data transfer
pub mod arrow_builder;
pub use arrow_builder::{
    arrow_to_events, create_event_arrow_schema, ArrowBuilderError, ArrowEventBuilder,
    ArrowEventStreamer,
};

// ECF (Event Compression Format) codec for Prophesee HDF5 files
pub mod ecf_codec;
pub use ecf_codec::{ECFDecoder, ECFEncoder, EventCD};

// Prophesee ECF codec implementation
pub mod prophesee_ecf_codec;
pub use prophesee_ecf_codec::{PropheseeECFDecoder, PropheseeECFEncoder, PropheseeEvent};

// Native HDF5 reader with ECF support (Unix only)
// HDF5 is only available on Unix platforms (Linux/macOS)
#[cfg(unix)]
pub mod hdf5_reader;
#[cfg(unix)]
pub use hdf5_reader::load_events_from_hdf5;

// DataFrame construction utilities for direct event processing
pub mod dataframe_builder;
pub use dataframe_builder::{
    calculate_optimal_chunk_size, convert_timestamp, create_empty_events_dataframe,
    EventDataFrameBuilder, EventDataFrameStreamer,
};

// Python bindings for DataFrame-based operations (defined inline below)

// Polars support integrated directly into file readers

/// Configuration for loading events with filtering options
#[derive(Debug, Clone, Default)]
pub struct LoadConfig {
    /// Start time filter (inclusive)
    pub t_start: Option<f64>,
    /// End time filter (inclusive)
    pub t_end: Option<f64>,
    /// Minimum x coordinate (inclusive)
    pub min_x: Option<u16>,
    /// Maximum x coordinate (inclusive)
    pub max_x: Option<u16>,
    /// Minimum y coordinate (inclusive)
    pub min_y: Option<u16>,
    /// Maximum y coordinate (inclusive)
    pub max_y: Option<u16>,
    /// Polarity filter (true for positive, false for negative, None for both)
    pub polarity: Option<bool>,
    /// Sort events by timestamp after loading
    pub sort: bool,
    /// Chunk size for memory management (not used for filtering, but affects performance)
    pub chunk_size: Option<usize>,
    /// Custom column index for x coordinate (0-based, for text files)
    pub x_col: Option<usize>,
    /// Custom column index for y coordinate (0-based, for text files)
    pub y_col: Option<usize>,
    /// Custom column index for timestamp (0-based, for text files)
    pub t_col: Option<usize>,
    /// Custom column index for polarity (0-based, for text files)
    pub p_col: Option<usize>,
    /// Number of header lines to skip (for text files)
    pub header_lines: usize,
    /// Polarity encoding configuration
    pub polarity_encoding: Option<PolarityEncoding>,
}

impl LoadConfig {
    /// Create a new LoadConfig with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Set time window filter
    pub fn with_time_window(mut self, t_start: Option<f64>, t_end: Option<f64>) -> Self {
        self.t_start = t_start;
        self.t_end = t_end;
        self
    }

    /// Set spatial bounds filter
    pub fn with_spatial_bounds(
        mut self,
        min_x: Option<u16>,
        max_x: Option<u16>,
        min_y: Option<u16>,
        max_y: Option<u16>,
    ) -> Self {
        self.min_x = min_x;
        self.max_x = max_x;
        self.min_y = min_y;
        self.max_y = max_y;
        self
    }

    /// Set polarity filter
    pub fn with_polarity(mut self, polarity: Option<bool>) -> Self {
        self.polarity = polarity;
        self
    }

    /// Enable sorting by timestamp
    pub fn with_sorting(mut self, sort: bool) -> Self {
        self.sort = sort;
        self
    }

    /// Set polarity encoding configuration
    pub fn with_polarity_encoding(mut self, encoding: PolarityEncoding) -> Self {
        self.polarity_encoding = Some(encoding);
        self
    }

    /// Set custom column mapping for text files
    pub fn with_custom_columns(
        mut self,
        t_col: Option<usize>,
        x_col: Option<usize>,
        y_col: Option<usize>,
        p_col: Option<usize>,
    ) -> Self {
        self.t_col = t_col;
        self.x_col = x_col;
        self.y_col = y_col;
        self.p_col = p_col;
        self
    }

    /// Set number of header lines to skip
    pub fn with_header_lines(mut self, header_lines: usize) -> Self {
        self.header_lines = header_lines;
        self
    }

    /// Check if an event passes all filters
    pub fn passes_filters(&self, event: &Event) -> bool {
        // Time window filter
        if let Some(t_start) = self.t_start {
            if event.t < t_start {
                return false;
            }
        }
        if let Some(t_end) = self.t_end {
            if event.t > t_end {
                return false;
            }
        }

        // Spatial bounds filter
        if let Some(min_x) = self.min_x {
            if event.x < min_x {
                return false;
            }
        }
        if let Some(max_x) = self.max_x {
            if event.x > max_x {
                return false;
            }
        }
        if let Some(min_y) = self.min_y {
            if event.y < min_y {
                return false;
            }
        }
        if let Some(max_y) = self.max_y {
            if event.y > max_y {
                return false;
            }
        }

        // Polarity filter
        if let Some(polarity) = self.polarity {
            if (event.polarity > 0) != polarity {
                return false;
            }
        }

        true
    }
}

/// Returns the conversion factor to convert to seconds
#[allow(dead_code)]
fn detect_timestamp_units(timestamps: &[i64]) -> f64 {
    if timestamps.is_empty() {
        return 1_000_000.0; // Default to microseconds
    }

    // Sample multiple timestamps to be more robust
    let sample_size = std::cmp::min(10, timestamps.len());
    let mut max_timestamp = 0i64;

    for &ts in timestamps.iter().take(sample_size) {
        max_timestamp = std::cmp::max(max_timestamp, ts.abs());
    }

    // Determine units based on timestamp magnitude
    // Nanoseconds: typically > 10^15 for recent data (e.g., 1.6e18 for year 2021+)
    // Microseconds: typically 10^9 to 10^15
    // Seconds: typically < 10^9
    if max_timestamp > 1_000_000_000_000_000 {
        // > 10^15
        1_000_000_000.0 // nanoseconds to seconds
    } else if max_timestamp > 1_000_000_000 {
        // > 10^9
        1_000_000.0 // microseconds to seconds
    } else {
        1.0 // already in seconds
    }
}

/// Detect timestamp units for f64 arrays
/// Returns the conversion factor to convert to seconds
#[allow(dead_code)]
fn detect_timestamp_units_f64(timestamps: &[f64]) -> f64 {
    if timestamps.is_empty() {
        return 1_000_000.0; // Default to microseconds
    }

    // Sample multiple timestamps to be more robust
    let sample_size = std::cmp::min(10, timestamps.len());
    let mut max_timestamp = 0.0f64;

    for &ts in timestamps.iter().take(sample_size) {
        max_timestamp = max_timestamp.max(ts.abs());
    }

    // Determine units based on timestamp magnitude
    if max_timestamp > 1_000_000_000_000_000.0 {
        // > 10^15
        1_000_000_000.0 // nanoseconds to seconds
    } else if max_timestamp > 1_000_000_000.0 {
        // > 10^9
        1_000_000.0 // microseconds to seconds
    } else {
        1.0 // already in seconds
    }
}

/// Validate that coordinates are within reasonable bounds for event cameras
#[allow(dead_code)]
fn validate_coordinates(x: u16, y: u16) -> bool {
    // Most event cameras have resolutions <= 1280x720 (Gen4) or 640x480 (DAVIS)
    // Allow some margin for unusual sensors, but reject clearly invalid values
    x <= 2048 && y <= 2048
}
/// # Arguments
/// * `path` - Path to the text file
/// * `config` - Configuration with filtering options
pub fn load_events_from_text(path: &str, config: &LoadConfig) -> IoResult<DataFrame> {
    let file = File::open(path)?;
    let reader = BufReader::new(file);
    let mut events = Vec::<Event>::new();

    // Estimate capacity if possible (reduce if filtering is likely to remove many events)
    if let Ok(metadata) = std::fs::metadata(path) {
        let file_size = metadata.len() as usize;
        // Assuming average of 20 bytes per line, but reduce estimate if filters are active
        let estimated_capacity = if config.t_start.is_some()
            || config.t_end.is_some()
            || config.min_x.is_some()
            || config.max_x.is_some()
            || config.min_y.is_some()
            || config.max_y.is_some()
            || config.polarity.is_some()
        {
            (file_size / 20) / 2 // Assume filters will remove ~50% of events
        } else {
            file_size / 20
        };
        events.reserve(estimated_capacity);
    } else {
        events.reserve(1000000); // Default pre-allocation
    }

    // Determine column indices
    let t_col = config.t_col.unwrap_or(0);
    let x_col = config.x_col.unwrap_or(1);
    let y_col = config.y_col.unwrap_or(2);
    let p_col = config.p_col.unwrap_or(3);

    let max_col = [t_col, x_col, y_col, p_col].iter().max().unwrap() + 1;

    let mut lines_processed = 0;

    for (line_num, line_res) in reader.lines().enumerate() {
        let line = line_res?;

        // Skip header lines
        if lines_processed < config.header_lines {
            lines_processed += 1;
            continue;
        }

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() < max_col {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Line {}: Expected at least {max_col} values for column mapping, got {}",
                    line_num + 1,
                    parts.len()
                ),
            ));
        }

        // Parse values using custom column mapping
        let t = parts[t_col].parse::<f64>().map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Line {}: Invalid timestamp '{}': {e}",
                    line_num + 1,
                    parts[t_col]
                ),
            )
        })?;
        let x = parts[x_col].parse::<u16>().map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Line {}: Invalid x coordinate '{}': {e}",
                    line_num + 1,
                    parts[x_col]
                ),
            )
        })?;
        let y = parts[y_col].parse::<u16>().map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Line {}: Invalid y coordinate '{}': {e}",
                    line_num + 1,
                    parts[y_col]
                ),
            )
        })?;
        let polarity_raw = parts[p_col].parse::<i8>().map_err(|e| {
            std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!(
                    "Line {}: Invalid polarity '{}': {e}",
                    line_num + 1,
                    parts[p_col]
                ),
            )
        })?;

        // For text files, preserve original polarity encoding (0/1)
        // Don't convert to -1/1 like other formats
        let polarity = polarity_raw;

        let event = Event { t, x, y, polarity };

        // Apply filters
        if config.passes_filters(&event) {
            events.push(event);
        }

        // Early termination for time-sorted files
        if let Some(t_end) = config.t_end {
            if t > t_end {
                break; // Assume file is sorted by time
            }
        }
    }

    // Sort events if requested
    if config.sort {
        events.sort_by(|a, b| a.t.partial_cmp(&b.t).unwrap_or(std::cmp::Ordering::Equal));
    }

    python::build_polars_dataframe(&events, EventFormat::Text)
        .map_err(|e| std::io::Error::other(format!("DataFrame conversion failed: {}", e)))
}

// Binary format (mmap_events) removed due to safety and reliability issues:
// 1. Unsafe memory operations with no validation
// 2. Assumes files contain raw Event structs (almost never true)
// 3. Produces misleading results with text files
// 4. No error handling for malformed binary data
//
// All file types now use the safe, reliable text parser

// Backward compatibility functions (maintain old API)

/// Load events from a text file (backward compatibility)
pub fn load_events_from_text_simple(path: &str) -> IoResult<DataFrame> {
    load_events_from_text(path, &LoadConfig::new())
}

/// Generic load function with automatic format detection and filtering
pub fn load_events_with_config(
    path: &str,
    config: &LoadConfig,
) -> Result<DataFrame, Box<dyn std::error::Error>> {
    // Use format detector to determine the file format
    let detection_result = format_detector::detect_event_format(path)?;

    match detection_result.format {
        #[cfg(unix)]
        EventFormat::HDF5 => {
            let events = load_events_from_hdf5(path, None)?;
            // Apply filters to the loaded events
            // TODO: Apply filters using Polars DataFrame operations
            // Sort if requested
            if config.sort {
                // TODO: Apply sorting using Polars DataFrame.sort() operation\n                // events = events.sort([\"t\"], SortMultipleOptions::new())?;
            }
            Ok(events)
        }
        #[cfg(not(unix))]
        EventFormat::HDF5 => Err(Box::new(std::io::Error::new(
            std::io::ErrorKind::Unsupported,
            "HDF5 support is only available on Unix platforms (Linux/macOS), not on Windows.",
        ))),
        EventFormat::Text => Ok(load_events_from_text(path, config)?),
        EventFormat::AEDAT1 | EventFormat::AEDAT2 | EventFormat::AEDAT3 | EventFormat::AEDAT4 => {
            // Use comprehensive AEDAT reader
            let aedat_config = AedatConfig {
                validate_timestamps: true,
                validate_coordinates: true,
                validate_polarity: true,
                skip_invalid_events: false,
                max_events: None,
                max_resolution: Some((1024, 1024)),
            };
            let reader = AedatReader::with_config(aedat_config);
            let (events, _metadata) = reader.read_file(path)?;
            Ok(events)
        }
        EventFormat::AER => {
            // Use comprehensive AER reader
            let aer_config = AerConfig::default()
                .with_validation(true, true) // Validate coordinates and skip invalid events
                .with_timestamp_generation(true, TimestampMode::Sequential, 0.0, 1e-6);

            let reader = AerReader::with_config(aer_config);
            let events = reader
                .read_with_config(path, config)
                .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })?;
            Ok(events)
        }
        EventFormat::EVT2 => {
            // Use EVT2 reader
            let evt2_config = Evt2Config {
                validate_coordinates: false,
                skip_invalid_events: false,
                max_events: None,
                sensor_resolution: detection_result.metadata.sensor_resolution,
                chunk_size: 1_000_000,
                polarity_encoding: config.polarity_encoding,
            };
            let reader = Evt2Reader::with_config(evt2_config);
            let events = reader
                .read_with_config(path, config)
                .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })?;
            Ok(events)
        }
        EventFormat::EVT21 => {
            // Use EVT2.1 reader
            let evt21_config = Evt21Config {
                validate_coordinates: false,
                skip_invalid_events: false,
                max_events: None,
                sensor_resolution: detection_result.metadata.sensor_resolution,
                chunk_size: 500_000,
                polarity_encoding: config.polarity_encoding,
                decode_vectorized: true,
            };
            let reader = Evt21Reader::with_config(evt21_config);
            let events = reader
                .read_with_config(path, config)
                .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })?;
            Ok(events)
        }
        EventFormat::EVT3 => {
            // Use EVT3 reader
            let evt3_config = Evt3Config {
                validate_coordinates: false, // Disable validation for better compatibility
                skip_invalid_events: false,
                max_events: None,
                sensor_resolution: detection_result.metadata.sensor_resolution,
                chunk_size: 1_000_000,
                polarity_encoding: config.polarity_encoding,
            };
            let reader = Evt3Reader::with_config(evt3_config);
            let events = reader
                .read_with_config(path, config)
                .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })?;
            Ok(events)
        }
        EventFormat::Binary => {
            // Binary format is not supported for safety reasons
            let format = detection_result.format;
            let confidence = detection_result.confidence;
            Err(format!(
                "Binary format is not supported for safety reasons. Detected format: {format} (confidence: {confidence:.2})"
            ).into())
        }
        EventFormat::Unknown => {
            // Fall back to text format for unknown files
            Ok(load_events_from_text(path, config)?)
        }
    }
}

/// Load events to Apache Arrow RecordBatch with automatic format detection
///
/// This function provides zero-copy data transfer to Arrow format, enabling
/// efficient interoperability with PyArrow, DuckDB, and other Arrow ecosystem tools.
///
/// # Arguments
/// * `path` - Path to the event file
/// * `config` - Configuration with filtering options
///
/// # Returns
/// Result containing an Arrow RecordBatch with event data
pub fn load_events_to_arrow(
    path: &str,
    config: &LoadConfig,
) -> Result<arrow::record_batch::RecordBatch, Box<dyn std::error::Error>> {
    use crate::ev_formats::arrow_builder::{ArrowEventBuilder, ArrowEventStreamer};
    use crate::ev_formats::streaming::should_use_streaming;

    // Use format detector to determine the file format
    let detection_result = format_detector::detect_event_format(path)?;

    // Load events using existing pipeline
    let events = load_events_with_config(path, config)?;

    // Convert DataFrame to Event iterator for Arrow construction
    // Note: This conversion is for compatibility with Arrow integration
    // For large datasets, consider direct DataFrame->Arrow conversion
    let event_iter = dataframe_to_event_iterator(&events)?;
    let events_vec: Vec<Event> = event_iter.collect();

    // Check if we should use streaming based on event count
    let event_count = events_vec.len();
    let default_threshold = 5_000_000; // 5M events
    let streaming_threshold = config.chunk_size.unwrap_or(default_threshold);

    if should_use_streaming(event_count, Some(streaming_threshold)) {
        // Use streaming for large datasets
        let chunk_size =
            crate::ev_formats::streaming::PolarsEventStreamer::calculate_optimal_chunk_size(
                event_count,
                512,
            );
        let streamer = ArrowEventStreamer::new(chunk_size, detection_result.format);
        streamer
            .stream_to_arrow(events_vec.into_iter())
            .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })
    } else {
        // Direct construction for smaller datasets
        ArrowEventBuilder::from_events_zero_copy(&events_vec, detection_result.format)
            .map_err(|e| -> Box<dyn std::error::Error> { Box::new(e) })
    }
}

/// Convert DataFrame to Event iterator for Arrow integration
/// This is a helper function for compatibility with Arrow builder
fn dataframe_to_event_iterator(
    df: &DataFrame,
) -> Result<impl Iterator<Item = Event>, Box<dyn std::error::Error>> {
    use crate::ev_formats::streaming::Event;

    let t_series = df
        .column("t")
        .map_err(|e| format!("Missing timestamp column: {}", e))?;
    let x_series = df
        .column("x")
        .map_err(|e| format!("Missing x column: {}", e))?;
    let y_series = df
        .column("y")
        .map_err(|e| format!("Missing y column: {}", e))?;
    let polarity_series = df
        .column("polarity")
        .map_err(|e| format!("Missing polarity column: {}", e))?;

    // Convert to vectors for iterator creation
    let timestamps: Vec<f64> = t_series.f64()?.into_no_null_iter().collect();
    let x_coords: Vec<u16> = x_series
        .i16()?
        .into_no_null_iter()
        .map(|v| v as u16)
        .collect();
    let y_coords: Vec<u16> = y_series
        .i16()?
        .into_no_null_iter()
        .map(|v| v as u16)
        .collect();
    let polarities: Vec<i8> = polarity_series.i8()?.into_no_null_iter().collect();

    // Create iterator of Event structs
    let events: Vec<Event> = timestamps
        .into_iter()
        .zip(x_coords)
        .zip(y_coords)
        .zip(polarities)
        .map(|(((t, x), y), polarity)| Event { t, x, y, polarity })
        .collect();

    Ok(events.into_iter())
}

/// Load events to Apache Arrow RecordBatch (simple version)
///
/// # Arguments
/// * `path` - Path to the event file
///
/// # Returns
/// Result containing an Arrow RecordBatch with event data
pub fn load_events_to_arrow_simple(
    path: &str,
) -> Result<arrow::record_batch::RecordBatch, Box<dyn std::error::Error>> {
    load_events_to_arrow(path, &LoadConfig::new())
}

/// Struct for iterating through a text file of events line by line
/// without loading everything into memory at once
pub struct EventFileIterator {
    reader: BufReader<File>,
}

impl EventFileIterator {
    /// Create a new iterator from a text file path
    pub fn new(path: &str) -> IoResult<Self> {
        let file = File::open(path)?;
        Ok(EventFileIterator {
            reader: BufReader::new(file),
        })
    }
}

impl Iterator for EventFileIterator {
    type Item = IoResult<Event>;

    fn next(&mut self) -> Option<Self::Item> {
        let mut line = String::new();

        // Read the next line
        match self.reader.read_line(&mut line) {
            Ok(0) => None, // EOF
            Ok(_) => {
                // Skip empty lines and comments
                if line.trim().is_empty() || line.starts_with('#') {
                    return self.next();
                }

                // Parse the line
                let parts: Vec<&str> = line.split_whitespace().collect();
                if parts.len() < 4 {
                    return self.next(); // Not enough fields
                }

                // Parse values
                let t = match parts[0].parse::<f64>() {
                    Ok(v) => v,
                    Err(e) => {
                        return Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))
                    }
                };

                let x = match parts[1].parse::<u16>() {
                    Ok(v) => v,
                    Err(e) => {
                        return Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))
                    }
                };

                let y = match parts[2].parse::<u16>() {
                    Ok(v) => v,
                    Err(e) => {
                        return Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))
                    }
                };

                let p = match parts[3].parse::<i8>() {
                    Ok(v) => v,
                    Err(e) => {
                        return Some(Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)))
                    }
                };

                // Create and return event
                Some(Ok(Event {
                    t,
                    x,
                    y,
                    polarity: if p > 0 { 1 } else { -1 },
                }))
            }
            Err(e) => Some(Err(e)),
        }
    }
}

// Window-based event iterator that returns chunks of events based on time windows
pub struct TimeWindowIter<'a> {
    events: &'a Vec<Event>,
    window_duration: f64,
    current_idx: usize,
    start_time: f64,
    end_time: f64,
}

impl<'a> TimeWindowIter<'a> {
    /// Create a new iterator that returns time-windowed chunks of events
    ///
    /// # Arguments
    /// * `events` - Event array to iterate over
    /// * `window_duration` - Duration of each time window in seconds
    pub fn new(events: &'a Vec<Event>, window_duration: f64) -> Self {
        let start_time = if !events.is_empty() { events[0].t } else { 0.0 };

        let end_time = start_time + window_duration;

        TimeWindowIter {
            events,
            window_duration,
            current_idx: 0,
            start_time,
            end_time,
        }
    }
}

impl Iterator for TimeWindowIter<'_> {
    type Item = Vec<Event>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.current_idx >= self.events.len() {
            return None;
        }

        let mut window = Vec::new();
        let mut idx = self.current_idx;

        // Collect events within current time window
        while idx < self.events.len() && self.events[idx].t < self.end_time {
            window.push(self.events[idx]);
            idx += 1;
        }

        // Update state for next iteration
        self.current_idx = idx;
        self.start_time = self.end_time;
        self.end_time += self.window_duration;

        // Only return Some if we found events in this window
        if window.is_empty() {
            self.next()
        } else {
            Some(window)
        }
    }
}

/// Python bindings for the formats module
pub mod python {
    use super::*;
    use numpy::PyReadonlyArray1;
    use polars::prelude::{col, lit, DataFrame, IntoLazy, SortMultipleOptions};
    use std::io::Write;

    // NOTE: convert_polarity function removed - functionality moved to vectorized Polars operations
    // in build_polars_dataframe() for better performance

    /// Convert timestamp to microseconds for Polars Duration type
    fn convert_timestamp(timestamp: f64) -> i64 {
        if timestamp >= 1_000_000_000.0 {
            // Likely nanoseconds, convert to microseconds
            (timestamp / 1_000.0) as i64
        } else if timestamp >= 1_000.0 {
            // Likely already in microseconds
            timestamp as i64
        } else {
            // Likely in seconds, convert to microseconds
            (timestamp * 1_000_000.0) as i64
        }
    }

    /// Build Polars DataFrame directly from events using Series builders for optimal memory efficiency
    pub fn build_polars_dataframe(
        events: &[Event],
        format: EventFormat,
    ) -> Result<polars::prelude::DataFrame, polars::prelude::PolarsError> {
        use polars::prelude::*;

        let len = events.len();

        if len == 0 {
            // Create empty DataFrame with proper schema
            let empty_x = Series::new("x".into(), Vec::<i16>::new());
            let empty_y = Series::new("y".into(), Vec::<i16>::new());
            let empty_timestamp = Series::new("t".into(), Vec::<i64>::new())
                .cast(&DataType::Duration(TimeUnit::Microseconds))?;
            let empty_polarity = Series::new("polarity".into(), Vec::<i8>::new());

            return DataFrame::new(vec![
                empty_x.into(),
                empty_y.into(),
                empty_timestamp.into(),
                empty_polarity.into(),
            ]);
        }

        // Use optimal data types for memory efficiency
        // x, y: Int16 (sufficient for coordinates, saves 50% memory vs Int32)
        // timestamp: Int64 (required for microsecond precision)
        // polarity: Int8 (sufficient for -1/0/1 values, saves 87.5% memory vs Int64)
        let mut x_builder = PrimitiveChunkedBuilder::<Int16Type>::new("x".into(), len);
        let mut y_builder = PrimitiveChunkedBuilder::<Int16Type>::new("y".into(), len);
        let mut timestamp_builder = PrimitiveChunkedBuilder::<Int64Type>::new("t".into(), len);
        let mut polarity_builder = PrimitiveChunkedBuilder::<Int8Type>::new("polarity".into(), len);

        // Single iteration with direct population - zero intermediate copies
        // Store polarity as raw bool first, convert vectorized later
        for event in events {
            x_builder.append_value(event.x as i16);
            y_builder.append_value(event.y as i16);
            timestamp_builder.append_value(convert_timestamp(event.t));
            // Store raw bool polarity (0/1) - will convert vectorized later
            polarity_builder.append_value(event.polarity);
        }

        // Build Series from builders
        let x_series = x_builder.finish().into_series();
        let y_series = y_builder.finish().into_series();
        let polarity_series_raw = polarity_builder.finish().into_series();

        // Convert timestamp to Duration type
        let timestamp_series = timestamp_builder
            .finish()
            .into_series()
            .cast(&DataType::Duration(TimeUnit::Microseconds))?;

        // Create initial DataFrame with raw polarity
        let df = DataFrame::new(vec![
            x_series.into(),
            y_series.into(),
            timestamp_series.into(),
            polarity_series_raw.into(),
        ])?;

        // VECTORIZED polarity conversion (much faster than per-event)
        let df = match format {
            EventFormat::EVT2 | EventFormat::EVT21 | EventFormat::EVT3 => {
                // EVT2 family: Convert 0/1 to -1/1 using vectorized operations
                df.lazy()
                    .with_column(
                        when(col("polarity").eq(lit(0)))
                            .then(lit(-1i8))
                            .otherwise(lit(1i8))
                            .alias("polarity")
                            .cast(DataType::Int8),
                    )
                    .collect()?
            }
            #[cfg(not(windows))]
            EventFormat::HDF5 => {
                // HDF5: Convert 0/1 to -1/1 for proper polarity encoding
                df.lazy()
                    .with_column(
                        when(col("polarity").eq(lit(0)))
                            .then(lit(-1i8))
                            .otherwise(lit(1i8))
                            .alias("polarity")
                            .cast(DataType::Int8),
                    )
                    .collect()?
            }
            #[cfg(windows)]
            EventFormat::HDF5 => {
                return Err(PolarsError::ComputeError(
                    "HDF5 support is disabled on Windows due to build complexity.".into(),
                ));
            }
            _ => {
                // Text and other formats: Keep 0/1 encoding as-is, but ensure Int8 type
                df.lazy()
                    .with_column(col("polarity").cast(DataType::Int8))
                    .collect()?
            }
        };

        Ok(df)
    }

    /// Convert Polars DataFrame to Python dictionary for LazyFrame creation
    fn return_polars_lazyframe_to_python(
        py: Python<'_>,
        lf: polars::prelude::LazyFrame,
    ) -> PyResult<PyObject> {
        // Convert Polars LazyFrame to Python object directly
        // This leverages polars-python bindings for maximum efficiency
        use pyo3::types::PyModule;

        // Import polars module in Python
        let polars_module = PyModule::import(py, "polars")?;

        // Convert the LazyFrame to Python
        // For now, we'll collect to DataFrame and convert to Python, then make lazy
        let df = lf.collect().map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                "Failed to collect LazyFrame: {e}"
            ))
        })?;

        // Convert DataFrame to Python dict with schema preservation
        let (data_dict, schema_dict) = polars_dataframe_to_python_dict_with_schema(py, df)?;

        // Create Polars DataFrame from dict in Python with explicit schema and return as LazyFrame
        let py_df = polars_module.call_method1("DataFrame", (data_dict, schema_dict))?;
        let py_lazyframe = py_df.call_method0("lazy")?;

        Ok(py_lazyframe.into())
    }

    fn polars_dataframe_to_python_dict_with_schema(
        py: Python<'_>,
        df: polars::prelude::DataFrame,
    ) -> PyResult<(PyObject, PyObject)> {
        use polars::prelude::*;
        use pyo3::types::{PyDict, PyModule};

        let mut data_dict: std::collections::HashMap<String, PyObject> =
            std::collections::HashMap::new();
        let schema_dict = PyDict::new(py);

        // Import polars for Python type creation
        let polars_module = PyModule::import(py, "polars")?;

        for column in df.get_columns() {
            let column_name = column.name();
            let (column_data, py_dtype) = match column.dtype() {
                DataType::Int16 => {
                    let values: Vec<i16> = column
                        .i16()
                        .map_err(|e| {
                            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                "Failed to extract i16 column: {e}"
                            ))
                        })?
                        .into_no_null_iter()
                        .collect();
                    let py_type = polars_module.getattr("Int16")?;
                    (values.into_pyobject(py)?.into(), py_type)
                }
                DataType::Int32 => {
                    let values: Vec<i32> = column
                        .i32()
                        .map_err(|e| {
                            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                "Failed to extract i32 column: {e}"
                            ))
                        })?
                        .into_no_null_iter()
                        .collect();
                    let py_type = polars_module.getattr("Int32")?;
                    (values.into_pyobject(py)?.into(), py_type)
                }
                DataType::Int8 => {
                    let values: Vec<i8> = column
                        .i8()
                        .map_err(|e| {
                            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                "Failed to extract i8 column: {e}"
                            ))
                        })?
                        .into_no_null_iter()
                        .collect();
                    let py_type = polars_module.getattr("Int8")?;
                    (values.into_pyobject(py)?.into(), py_type)
                }
                DataType::Duration(TimeUnit::Microseconds) => {
                    let values: Vec<i64> = column
                        .duration()
                        .map_err(|e| {
                            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                                "Failed to extract duration column: {e}"
                            ))
                        })?
                        .into_no_null_iter()
                        .collect();
                    let duration_type = polars_module.call_method1("Duration", ("us",))?;
                    (values.into_pyobject(py)?.into(), duration_type)
                }
                _ => {
                    return Err(PyErr::new::<pyo3::exceptions::PyTypeError, _>(format!(
                        "Unsupported column type: {:?}",
                        column.dtype()
                    )))
                }
            };

            data_dict.insert(column_name.to_string(), column_data);
            schema_dict.set_item(column_name.as_str(), py_dtype)?;
        }

        Ok((data_dict.into_pyobject(py)?.into(), schema_dict.into()))
    }

    /// Load events from a file with filtering support (using Polars backend)
    ///
    /// Automatically detects the format based on file extension
    ///
    /// Args:
    ///     path: Path to the event file
    ///     t_start: Start time filter (inclusive)
    ///     t_end: End time filter (inclusive)
    ///     min_x, max_x, min_y, max_y: Spatial bounds filters
    ///     polarity: Polarity filter (1 for positive, -1 for negative, None for both)
    ///     sort: Sort events by timestamp after loading
    ///     x_col, y_col, t_col, p_col: Custom column indices for text files
    ///     header_lines: Number of header lines to skip in text files
    ///
    /// Returns:
    ///     Python dictionary with event data for Polars LazyFrame creation
    ///
    /// Load events directly into a Polars DataFrame (optimized path)
    /// This bypasses the intermediate Event struct and builds DataFrames directly from format readers
    pub fn load_events_to_dataframe_py(
        py: Python<'_>,
        path: &str,
        config: &LoadConfig,
    ) -> PyResult<PyObject> {
        // Detect format for proper reader selection
        let format_result = detect_event_format(path).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to detect format: {e}"))
        })?;

        let df = match format_result.format {
            EventFormat::EVT2 | EventFormat::EVT21 => {
                // Use EVT2 reader with direct DataFrame output
                let mut evt2_config = Evt2Config {
                    validate_coordinates: true,
                    skip_invalid_events: true,
                    ..Default::default()
                };

                // Use chunk_size as max_events limit if specified
                if let Some(chunk_size) = config.chunk_size {
                    evt2_config.max_events = Some(chunk_size);
                }

                let reader = Evt2Reader::with_config(evt2_config);
                let (df, _metadata) = reader.read_file_to_dataframe(path).map_err(|e| {
                    PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                        "Failed to read EVT2 file: {e}"
                    ))
                })?;

                // Apply additional filtering if needed using Polars operations
                apply_config_filters_to_dataframe(df, config)?
            }
            _ => {
                // For other formats, fall back to the existing approach for now
                // TODO: Add direct DataFrame readers for other formats
                let events = load_events_with_config(path, config).map_err(|e| {
                    PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                        "Failed to load events: {e}"
                    ))
                })?;

                // load_events_with_config already returns a DataFrame, so use it directly
                events
            }
        };

        // Return Polars DataFrame directly to Python
        use pyo3::IntoPyObject;
        let py_df = pyo3_polars::PyDataFrame(df);
        Ok(py_df.into_pyobject(py)?.into())
    }

    /// Apply LoadConfig filters to a DataFrame using Polars operations
    fn apply_config_filters_to_dataframe(
        df: DataFrame,
        config: &LoadConfig,
    ) -> PyResult<DataFrame> {
        let mut lazy_df = df.lazy();

        // Apply time window filter
        if let (Some(t_start), Some(t_end)) = (config.t_start, config.t_end) {
            let t_start_micros = (t_start * 1_000_000.0) as i64;
            let t_end_micros = (t_end * 1_000_000.0) as i64;

            lazy_df = lazy_df.filter(
                col("t")
                    .gt_eq(lit(t_start_micros))
                    .and(col("t").lt_eq(lit(t_end_micros))),
            );
        } else if let Some(t_start) = config.t_start {
            let t_start_micros = (t_start * 1_000_000.0) as i64;
            lazy_df = lazy_df.filter(col("t").gt_eq(lit(t_start_micros)));
        } else if let Some(t_end) = config.t_end {
            let t_end_micros = (t_end * 1_000_000.0) as i64;
            lazy_df = lazy_df.filter(col("t").lt_eq(lit(t_end_micros)));
        }

        // Apply spatial bounds
        if let Some(min_x) = config.min_x {
            lazy_df = lazy_df.filter(col("x").gt_eq(lit(min_x as i16)));
        }
        if let Some(max_x) = config.max_x {
            lazy_df = lazy_df.filter(col("x").lt_eq(lit(max_x as i16)));
        }
        if let Some(min_y) = config.min_y {
            lazy_df = lazy_df.filter(col("y").gt_eq(lit(min_y as i16)));
        }
        if let Some(max_y) = config.max_y {
            lazy_df = lazy_df.filter(col("y").lt_eq(lit(max_y as i16)));
        }

        // Apply polarity filter
        if let Some(polarity) = config.polarity {
            let polarity_value = if polarity { 1i8 } else { -1i8 };
            lazy_df = lazy_df.filter(col("polarity").eq(lit(polarity_value)));
        }

        // Apply sorting if requested
        if config.sort {
            lazy_df = lazy_df.sort(["t"], SortMultipleOptions::default());
        }

        // Collect the result
        lazy_df.collect().map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                "Failed to apply filters: {e}"
            ))
        })
    }

    #[pyfunction]
    #[pyo3(
        signature = (
            path,
            t_start=None,
            t_end=None,
            min_x=None,
            max_x=None,
            min_y=None,
            max_y=None,
            polarity=None,
            sort=false,
            x_col=None,
            y_col=None,
            t_col=None,
            p_col=None,
            header_lines=0
        ),
        name = "load_events"
    )]
    #[allow(clippy::too_many_arguments)]
    pub fn load_events_py(
        py: Python<'_>,
        path: &str,
        t_start: Option<f64>,
        t_end: Option<f64>,
        min_x: Option<u16>,
        max_x: Option<u16>,
        min_y: Option<u16>,
        max_y: Option<u16>,
        polarity: Option<i8>,
        sort: bool,
        x_col: Option<usize>,
        y_col: Option<usize>,
        t_col: Option<usize>,
        p_col: Option<usize>,
        header_lines: usize,
    ) -> PyResult<PyObject> {
        // Convert i8 polarity filter to bool
        let polarity_bool = polarity.map(|p| p > 0);

        let config = LoadConfig::new()
            .with_time_window(t_start, t_end)
            .with_spatial_bounds(min_x, max_x, min_y, max_y)
            .with_polarity(polarity_bool)
            .with_sorting(sort)
            .with_custom_columns(t_col, x_col, y_col, p_col)
            .with_header_lines(header_lines);

        // Detect format for proper polarity encoding
        let _format_result = detect_event_format(path).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to detect format: {e}"))
        })?;

        // Load events using existing Rust logic
        let events = load_events_with_config(path, &config).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to load events: {e}"))
        })?;

        // Return Polars DataFrame/LazyFrame directly to Python
        // load_events_with_config already returns a DataFrame, so use it directly
        {
            // Return Polars LazyFrame directly to Python
            // This is much more efficient than converting to dict and back
            return_polars_lazyframe_to_python(py, events.lazy())
        }
    }

    /// Save events to an HDF5 file
    #[pyfunction]
    #[pyo3(name = "save_events_to_hdf5")]
    #[cfg(unix)]
    pub fn save_events_to_hdf5_py(
        xs: PyReadonlyArray1<i64>,
        ys: PyReadonlyArray1<i64>,
        ts: PyReadonlyArray1<f64>,
        ps: PyReadonlyArray1<i64>,
        path: &str,
    ) -> PyResult<()> {
        // Validate array lengths
        let n = ts.len()?;
        if xs.len()? != n || ys.len()? != n || ps.len()? != n {
            return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "Arrays must have the same length",
            ));
        }

        // Create HDF5 file
        let file = H5File::create(path).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to create HDF5 file: {e}"))
        })?;

        // Create a group to store the data
        let group = file.create_group("events").map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to create group: {e}"))
        })?;

        // Convert arrays to Rust vectors
        let xs_vec: Vec<u16> = xs.as_array().iter().map(|&x| x as u16).collect();
        let ys_vec: Vec<u16> = ys.as_array().iter().map(|&y| y as u16).collect();
        let ts_vec: Vec<f64> = ts.as_slice().unwrap().to_vec();
        let ps_vec: Vec<i8> = ps
            .as_array()
            .iter()
            .map(|&p| {
                if p == -1 {
                    -1i8
                } else if p == 1 {
                    1i8
                } else {
                    0i8
                }
            })
            .collect();

        // Create datasets for each component
        let xs_shape = [n];
        let xs_dataset = group
            .new_dataset::<u16>()
            .shape(xs_shape)
            .create("xs")
            .map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                    "Failed to create xs dataset: {e}"
                ))
            })?;
        xs_dataset.write(&xs_vec).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write xs data: {e}"))
        })?;

        let ys_dataset = group
            .new_dataset::<u16>()
            .shape(xs_shape)
            .create("ys")
            .map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                    "Failed to create ys dataset: {e}"
                ))
            })?;
        ys_dataset.write(&ys_vec).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write ys data: {e}"))
        })?;

        let ts_dataset = group
            .new_dataset::<f64>()
            .shape(xs_shape)
            .create("ts")
            .map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                    "Failed to create ts dataset: {e}"
                ))
            })?;
        ts_dataset.write(&ts_vec).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write ts data: {e}"))
        })?;

        let ps_dataset = group
            .new_dataset::<i8>()
            .shape(xs_shape)
            .create("ps")
            .map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                    "Failed to create ps dataset: {e}"
                ))
            })?;
        ps_dataset.write(&ps_vec).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write ps data: {e}"))
        })?;

        Ok(())
    }

    /// Save events to a text file, one event per line: "t x y p"
    #[pyfunction]
    #[pyo3(name = "save_events_to_text")]
    pub fn save_events_to_text_py(
        xs: PyReadonlyArray1<i64>,
        ys: PyReadonlyArray1<i64>,
        ts: PyReadonlyArray1<f64>,
        ps: PyReadonlyArray1<i64>,
        path: &str,
    ) -> PyResult<()> {
        // Validate array lengths
        let n = ts.len()?;
        if xs.len()? != n || ys.len()? != n || ps.len()? != n {
            return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                "Arrays must have the same length",
            ));
        }

        // Create output file
        let mut file = std::fs::File::create(path).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to create file: {e}"))
        })?;

        // Write header
        file.write_all(b"# timestamp x y polarity\n").map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write header: {e}"))
        })?;

        // Write events
        for i in 0..n {
            let line = format!(
                "{:.12} {} {} {}\n",
                ts.get(i).unwrap(),
                xs.get(i).unwrap(),
                ys.get(i).unwrap(),
                ps.get(i).unwrap()
            );
            file.write_all(line.as_bytes()).map_err(|e| {
                PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Failed to write line: {e}"))
            })?;
        }

        Ok(())
    }

    /// Detect the format of an event data file
    #[pyfunction]
    #[pyo3(name = "detect_format")]
    pub fn detect_format_py(
        path: &str,
    ) -> PyResult<(String, f64, std::collections::HashMap<String, String>)> {
        let result = format_detector::detect_event_format(path).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!("Format detection failed: {e}"))
        })?;

        Ok((
            result.format.to_string(),
            result.confidence,
            result.metadata.properties,
        ))
    }

    /// Get format description
    #[pyfunction]
    #[pyo3(name = "get_format_description")]
    pub fn get_format_description_py(format: &str) -> PyResult<String> {
        let event_format = match format {
            "Text" => EventFormat::Text,
            "HDF5" => EventFormat::HDF5,
            "AER" => EventFormat::AER,
            "AEDAT 1.0" => EventFormat::AEDAT1,
            "AEDAT 2.0" => EventFormat::AEDAT2,
            "AEDAT 3.1" => EventFormat::AEDAT3,
            "AEDAT 4.0" => EventFormat::AEDAT4,
            "EVT2" => EventFormat::EVT2,
            "EVT2.1" => EventFormat::EVT21,
            "EVT3" => EventFormat::EVT3,
            "Binary" => EventFormat::Binary,
            _ => EventFormat::Unknown,
        };

        Ok(FormatDetector::get_format_description(&event_format).to_string())
    }

    /// Test Prophesee ECF decoder with raw compressed data
    #[pyfunction]
    #[pyo3(name = "test_prophesee_ecf_decode")]
    pub fn test_prophesee_ecf_decode_py(
        compressed_data: &[u8],
        debug: Option<bool>,
    ) -> PyResult<Vec<(u16, u16, i16, i64)>> {
        use crate::ev_formats::prophesee_ecf_codec::PropheseeECFDecoder;

        let decoder = PropheseeECFDecoder::new().with_debug(debug.unwrap_or(false));

        match decoder.decode(compressed_data) {
            Ok(events) => {
                // Convert PropheseeEvent to Python-friendly tuple
                let result: Vec<(u16, u16, i16, i64)> =
                    events.into_iter().map(|e| (e.x, e.y, e.p, e.t)).collect();
                Ok(result)
            }
            Err(e) => Err(pyo3::exceptions::PyRuntimeError::new_err(format!(
                "ECF decoding failed: {}",
                e
            ))),
        }
    }

    /// Load events as PyArrow Table for zero-copy data transfer
    ///
    /// This function provides direct PyArrow Table creation, enabling efficient
    /// zero-copy data transfer to Python and integration with PyArrow ecosystem.
    ///
    /// Args:
    ///     path: Path to the event file
    ///     t_start: Start time filter (inclusive)
    ///     t_end: End time filter (inclusive)
    ///     min_x, max_x, min_y, max_y: Spatial bounds filters
    ///     polarity: Polarity filter (1 for positive, -1 for negative, None for both)
    ///     sort: Sort events by timestamp after loading
    ///     x_col, y_col, t_col, p_col: Custom column indices for text files
    ///     header_lines: Number of header lines to skip in text files
    ///
    /// Returns:
    ///     PyArrow Table with event data
    #[pyfunction]
    #[pyo3(
        signature = (
            path,
            t_start=None,
            t_end=None,
            min_x=None,
            max_x=None,
            min_y=None,
            max_y=None,
            polarity=None,
            sort=false,
            x_col=None,
            y_col=None,
            t_col=None,
            p_col=None,
            header_lines=0
        ),
        name = "load_events_to_arrow"
    )]
    #[allow(clippy::too_many_arguments)]
    pub fn load_events_to_pyarrow(
        py: Python<'_>,
        path: &str,
        t_start: Option<f64>,
        t_end: Option<f64>,
        min_x: Option<u16>,
        max_x: Option<u16>,
        min_y: Option<u16>,
        max_y: Option<u16>,
        polarity: Option<i8>,
        sort: bool,
        x_col: Option<usize>,
        y_col: Option<usize>,
        t_col: Option<usize>,
        p_col: Option<usize>,
        header_lines: usize,
    ) -> PyResult<PyObject> {
        // Convert i8 polarity filter to bool
        let polarity_bool = polarity.map(|p| p > 0);

        let config = LoadConfig::new()
            .with_time_window(t_start, t_end)
            .with_spatial_bounds(min_x, max_x, min_y, max_y)
            .with_polarity(polarity_bool)
            .with_sorting(sort)
            .with_custom_columns(t_col, x_col, y_col, p_col)
            .with_header_lines(header_lines);

        // Load events to Arrow RecordBatch using existing Rust implementation
        let record_batch = load_events_to_arrow(path, &config).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyIOError, _>(format!(
                "Failed to load events to Arrow: {}",
                e
            ))
        })?;

        // Convert Rust Arrow RecordBatch to Python using pyo3-arrow
        let py_record_batch = PyRecordBatch::new(record_batch);

        // Convert to PyArrow object using pyo3-arrow's to_pyarrow method
        py_record_batch.to_pyarrow(py).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                "Failed to convert to PyArrow: {}",
                e
            ))
        })
    }

    /// Convert PyArrow RecordBatch to events
    ///
    /// Args:
    ///     record_batch: PyArrow RecordBatch to convert
    ///
    /// Returns:
    ///     Dictionary with event arrays for Polars LazyFrame creation
    #[pyfunction]
    #[pyo3(name = "pyarrow_to_events")]
    pub fn pyarrow_to_events_py(py: Python<'_>, record_batch: PyRecordBatch) -> PyResult<PyObject> {
        use crate::ev_formats::arrow_builder::arrow_to_events;

        // Extract the underlying Arrow RecordBatch from PyRecordBatch
        // PyRecordBatch automatically converts from Python Arrow object
        let arrow_batch = record_batch.as_ref();

        // Convert Arrow RecordBatch to Events vector using our existing function
        let events = arrow_to_events(arrow_batch).map_err(|e| {
            PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(format!(
                "Failed to convert Arrow to events: {}",
                e
            ))
        })?;

        // Convert events to Python dict format for compatibility
        let mut data_dict: std::collections::HashMap<String, PyObject> =
            std::collections::HashMap::new();

        let mut x_vec = Vec::with_capacity(events.len());
        let mut y_vec = Vec::with_capacity(events.len());
        let mut t_vec = Vec::with_capacity(events.len());
        let mut p_vec = Vec::with_capacity(events.len());

        for event in events {
            x_vec.push(event.x as i64);
            y_vec.push(event.y as i64);
            t_vec.push(event.t);
            p_vec.push(event.polarity as i64);
        }

        data_dict.insert("x".to_string(), x_vec.into_pyobject(py)?.into());
        data_dict.insert("y".to_string(), y_vec.into_pyobject(py)?.into());
        data_dict.insert("t".to_string(), t_vec.into_pyobject(py)?.into());
        data_dict.insert("polarity".to_string(), p_vec.into_pyobject(py)?.into());

        Ok(data_dict.into_pyobject(py)?.into())
    }

    // ============================================================================
    // MIGRATED FROM ev_core::python - Legacy compatibility functions
    // These functions maintain backward compatibility for existing Python code
    // ============================================================================

    /// Convert events to a block representation (direct numpy array processing)
    #[pyfunction]
    #[pyo3(name = "events_to_block")]
    pub fn events_to_block_py(
        py: Python<'_>,
        xs: PyReadonlyArray1<i64>,
        ys: PyReadonlyArray1<i64>,
        ts: PyReadonlyArray1<f64>,
        ps: PyReadonlyArray1<i64>,
    ) -> PyResult<PyObject> {
        use ndarray::Array2;
        use numpy::IntoPyArray;

        let xs_array = xs.as_array();
        let ys_array = ys.as_array();
        let ts_array = ts.as_array();
        let ps_array = ps.as_array();

        let len = xs_array
            .len()
            .min(ys_array.len())
            .min(ts_array.len())
            .min(ps_array.len());

        // Create a 2D array with shape (n, 4) directly from numpy arrays
        let mut block = Array2::<f64>::zeros((len, 4));

        // Fill in the values: [x, y, t, p]
        for i in 0..len {
            block[[i, 0]] = xs_array[i] as f64;
            block[[i, 1]] = ys_array[i] as f64;
            block[[i, 2]] = ts_array[i];
            block[[i, 3]] = if ps_array[i] > 0 { 1.0 } else { 0.0 };
        }

        Ok(block.into_pyarray(py).into())
    }

    /// Merge multiple sets of events into a single chronologically sorted list (direct array processing)
    #[pyfunction]
    #[pyo3(name = "merge_events")]
    pub fn merge_events_py(
        py: Python<'_>,
        event_sets: &Bound<'_, pyo3::types::PyTuple>,
    ) -> PyResult<PyObject> {
        use ndarray::Array1;
        use numpy::IntoPyArray;
        use pyo3::types::PyTuple;

        // Collect all data from input arrays
        let mut all_xs = Vec::new();
        let mut all_ys = Vec::new();
        let mut all_ts = Vec::new();
        let mut all_ps = Vec::new();

        for event_set in event_sets.iter() {
            let tuple = event_set.downcast::<PyTuple>()?;
            if tuple.len() != 4 {
                return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(
                    "Each event set must be a tuple of (xs, ys, ts, ps)",
                ));
            }
            let xs = tuple.get_item(0)?.extract::<PyReadonlyArray1<i64>>()?;
            let ys = tuple.get_item(1)?.extract::<PyReadonlyArray1<i64>>()?;
            let ts = tuple.get_item(2)?.extract::<PyReadonlyArray1<f64>>()?;
            let ps = tuple.get_item(3)?.extract::<PyReadonlyArray1<i64>>()?;

            let xs_array = xs.as_array();
            let ys_array = ys.as_array();
            let ts_array = ts.as_array();
            let ps_array = ps.as_array();

            let len = xs_array
                .len()
                .min(ys_array.len())
                .min(ts_array.len())
                .min(ps_array.len());

            for i in 0..len {
                all_xs.push(xs_array[i]);
                all_ys.push(ys_array[i]);
                all_ts.push(ts_array[i]);
                all_ps.push(ps_array[i]);
            }
        }

        // Create indices for sorting by timestamp
        let mut indices: Vec<usize> = (0..all_ts.len()).collect();
        indices.sort_by(|&a, &b| all_ts[a].partial_cmp(&all_ts[b]).unwrap());

        // Rearrange arrays according to sorted indices
        let xs_sorted: Vec<i64> = indices.iter().map(|&i| all_xs[i]).collect();
        let ys_sorted: Vec<i64> = indices.iter().map(|&i| all_ys[i]).collect();
        let ts_sorted: Vec<f64> = indices.iter().map(|&i| all_ts[i]).collect();
        // Convert polarities from -1/1 to 0/1 for consistency with events_to_block_py
        let ps_sorted: Vec<i64> = indices
            .iter()
            .map(|&i| if all_ps[i] > 0 { 1 } else { 0 })
            .collect();

        // Convert arrays to Python objects
        let xs_py: PyObject = Array1::from(xs_sorted).into_pyarray(py).into();
        let ys_py: PyObject = Array1::from(ys_sorted).into_pyarray(py).into();
        let ts_py: PyObject = Array1::from(ts_sorted).into_pyarray(py).into();
        let ps_py: PyObject = Array1::from(ps_sorted).into_pyarray(py).into();

        // Create result tuple
        let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
        Ok(tuple.into())
    }

    /// Add random events drawn from a uniform distribution (migrated from ev_core)
    #[pyfunction]
    #[pyo3(name = "add_random_events")]
    #[pyo3(signature = (xs, ys, ts, ps, to_add, sensor_resolution=None, sort=true, return_merged=true))]
    #[allow(clippy::too_many_arguments)]
    pub fn add_random_events_py(
        py: Python<'_>,
        xs: PyReadonlyArray1<i64>,
        ys: PyReadonlyArray1<i64>,
        ts: PyReadonlyArray1<f64>,
        ps: PyReadonlyArray1<i64>,
        to_add: usize,
        sensor_resolution: Option<(i64, i64)>,
        sort: bool,
        return_merged: bool,
    ) -> PyResult<PyObject> {
        use ndarray::Array1;
        use numpy::IntoPyArray;
        use pyo3::types::PyTuple;
        use rand::prelude::*;

        let xs_array = xs.as_array();
        let ys_array = ys.as_array();
        let ts_array = ts.as_array();
        let ps_array = ps.as_array();

        // Generate random events
        let max_x = match sensor_resolution {
            Some((w, _)) => w - 1,
            None => xs_array.fold(0, |acc, &x| acc.max(x)),
        };

        let max_y = match sensor_resolution {
            Some((_, h)) => h - 1,
            None => ys_array.fold(0, |acc, &y| acc.max(y)),
        };

        let mut rng = thread_rng();

        let mut xs_new = Vec::with_capacity(to_add);
        let mut ys_new = Vec::with_capacity(to_add);
        let mut ts_new = Vec::with_capacity(to_add);
        let mut ps_new = Vec::with_capacity(to_add);

        let min_ts = ts_array.fold(f64::INFINITY, |acc, &t| acc.min(t));
        let max_ts = ts_array.fold(f64::NEG_INFINITY, |acc, &t| acc.max(t));

        for _ in 0..to_add {
            xs_new.push(rng.gen_range(0..=max_x));
            ys_new.push(rng.gen_range(0..=max_y));
            ts_new.push(rng.gen_range(min_ts..=max_ts));
            ps_new.push(if rng.gen_bool(0.5) { 1 } else { -1 });
        }

        if return_merged {
            // Merge both arrays
            let mut all_xs = Vec::with_capacity(xs_array.len() + xs_new.len());
            let mut all_ys = Vec::with_capacity(ys_array.len() + ys_new.len());
            let mut all_ts: Vec<f64> = Vec::with_capacity(ts_array.len() + ts_new.len());
            let mut all_ps = Vec::with_capacity(ps_array.len() + ps_new.len());

            all_xs.extend(xs_array.iter());
            all_xs.extend(xs_new.iter());

            all_ys.extend(ys_array.iter());
            all_ys.extend(ys_new.iter());

            all_ts.extend(ts_array.iter());
            all_ts.extend(ts_new.iter());

            all_ps.extend(ps_array.iter());
            all_ps.extend(ps_new.iter());

            let merged_xs = Array1::from(all_xs);
            let merged_ys = Array1::from(all_ys);
            let merged_ts = Array1::from(all_ts);
            let merged_ps = Array1::from(all_ps);

            if sort {
                // Sort by timestamp
                let mut indices: Vec<usize> = (0..merged_ts.len()).collect();
                indices.sort_by(|&i, &j| merged_ts[i].partial_cmp(&merged_ts[j]).unwrap());

                let sorted_xs = indices
                    .iter()
                    .map(|&i| merged_xs[i])
                    .collect::<Array1<i64>>();
                let sorted_ys = indices
                    .iter()
                    .map(|&i| merged_ys[i])
                    .collect::<Array1<i64>>();
                let sorted_ts = indices
                    .iter()
                    .map(|&i| merged_ts[i])
                    .collect::<Array1<f64>>();
                let sorted_ps = indices
                    .iter()
                    .map(|&i| merged_ps[i])
                    .collect::<Array1<i64>>();

                // Convert arrays to Python objects
                let xs_py: PyObject = sorted_xs.into_pyarray(py).into();
                let ys_py: PyObject = sorted_ys.into_pyarray(py).into();
                let ts_py: PyObject = sorted_ts.into_pyarray(py).into();
                let ps_py: PyObject = sorted_ps.into_pyarray(py).into();

                // Create result tuple
                let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
                Ok(tuple.into())
            } else {
                // Convert arrays to Python objects without sorting
                let xs_py: PyObject = merged_xs.into_pyarray(py).into();
                let ys_py: PyObject = merged_ys.into_pyarray(py).into();
                let ts_py: PyObject = merged_ts.into_pyarray(py).into();
                let ps_py: PyObject = merged_ps.into_pyarray(py).into();

                // Create result tuple
                let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
                Ok(tuple.into())
            }
        } else {
            // Return only the new events
            let xs_new_array = Array1::from(xs_new);
            let ys_new_array = Array1::from(ys_new);
            let ts_new_array = Array1::from(ts_new);
            let ps_new_array = Array1::from(ps_new);

            if sort {
                // Sort by timestamp
                let mut indices: Vec<usize> = (0..ts_new_array.len()).collect();
                indices.sort_by(|&i, &j| ts_new_array[i].partial_cmp(&ts_new_array[j]).unwrap());

                let sorted_xs = indices
                    .iter()
                    .map(|&i| xs_new_array[i])
                    .collect::<Array1<i64>>();
                let sorted_ys = indices
                    .iter()
                    .map(|&i| ys_new_array[i])
                    .collect::<Array1<i64>>();
                let sorted_ts = indices
                    .iter()
                    .map(|&i| ts_new_array[i])
                    .collect::<Array1<f64>>();
                let sorted_ps = indices
                    .iter()
                    .map(|&i| ps_new_array[i])
                    .collect::<Array1<i64>>();

                // Convert arrays to Python objects
                let xs_py: PyObject = sorted_xs.into_pyarray(py).into();
                let ys_py: PyObject = sorted_ys.into_pyarray(py).into();
                let ts_py: PyObject = sorted_ts.into_pyarray(py).into();
                let ps_py: PyObject = sorted_ps.into_pyarray(py).into();

                // Create result tuple
                let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
                Ok(tuple.into())
            } else {
                // Convert arrays to Python objects
                let xs_py: PyObject = xs_new_array.into_pyarray(py).into();
                let ys_py: PyObject = ys_new_array.into_pyarray(py).into();
                let ts_py: PyObject = ts_new_array.into_pyarray(py).into();
                let ps_py: PyObject = ps_new_array.into_pyarray(py).into();

                // Create result tuple
                let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
                Ok(tuple.into())
            }
        }
    }

    /// Remove events by random selection (migrated from ev_core)
    #[pyfunction]
    #[pyo3(name = "remove_events")]
    #[pyo3(signature = (xs, ys, ts, ps, to_remove, add_noise=0))]
    pub fn remove_events_py(
        py: Python<'_>,
        xs: PyReadonlyArray1<i64>,
        ys: PyReadonlyArray1<i64>,
        ts: PyReadonlyArray1<f64>,
        ps: PyReadonlyArray1<i64>,
        to_remove: usize,
        add_noise: usize,
    ) -> PyResult<PyObject> {
        use ndarray::Array1;
        use numpy::IntoPyArray;
        use pyo3::types::PyTuple;
        use rand::prelude::*;

        let xs_array = xs.as_array();
        let ys_array = ys.as_array();
        let ts_array = ts.as_array();
        let ps_array = ps.as_array();

        let n = xs_array.len();

        if to_remove >= n {
            // Return empty arrays
            let empty_xs = Array1::<i64>::zeros(0);
            let empty_ys = Array1::<i64>::zeros(0);
            let empty_ts = Array1::<f64>::zeros(0);
            let empty_ps = Array1::<i64>::zeros(0);

            // Convert arrays to Python objects
            let xs_py: PyObject = empty_xs.into_pyarray(py).into();
            let ys_py: PyObject = empty_ys.into_pyarray(py).into();
            let ts_py: PyObject = empty_ts.into_pyarray(py).into();
            let ps_py: PyObject = empty_ps.into_pyarray(py).into();

            // Create result tuple
            let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
            return Ok(tuple.into());
        }

        let to_select = n - to_remove;

        // Generate random indices without replacement
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n).collect();
        indices.shuffle(&mut rng);
        indices.truncate(to_select);
        indices.sort();

        // Extract selected events
        let selected_xs = indices
            .iter()
            .map(|&i| xs_array[i])
            .collect::<Array1<i64>>();
        let selected_ys = indices
            .iter()
            .map(|&i| ys_array[i])
            .collect::<Array1<i64>>();
        let selected_ts = indices
            .iter()
            .map(|&i| ts_array[i])
            .collect::<Array1<f64>>();
        let selected_ps = indices
            .iter()
            .map(|&i| ps_array[i])
            .collect::<Array1<i64>>();

        if add_noise == 0 {
            // Convert arrays to Python objects
            let xs_py: PyObject = selected_xs.into_pyarray(py).into();
            let ys_py: PyObject = selected_ys.into_pyarray(py).into();
            let ts_py: PyObject = selected_ts.into_pyarray(py).into();
            let ps_py: PyObject = selected_ps.into_pyarray(py).into();

            // Create result tuple
            let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
            Ok(tuple.into())
        } else {
            // Generate random events for noise
            let max_x = xs_array.fold(0, |acc, &x| acc.max(x));
            let max_y = ys_array.fold(0, |acc, &y| acc.max(y));

            let mut xs_noise = Vec::with_capacity(add_noise);
            let mut ys_noise = Vec::with_capacity(add_noise);
            let mut ts_noise = Vec::with_capacity(add_noise);
            let mut ps_noise = Vec::with_capacity(add_noise);

            let min_ts = ts_array.fold(f64::INFINITY, |acc, &t| acc.min(t));
            let max_ts = ts_array.fold(f64::NEG_INFINITY, |acc, &t| acc.max(t));

            for _ in 0..add_noise {
                xs_noise.push(rng.gen_range(0..=max_x));
                ys_noise.push(rng.gen_range(0..=max_y));
                ts_noise.push(rng.gen_range(min_ts..=max_ts));
                ps_noise.push(if rng.gen_bool(0.5) { 1 } else { -1 });
            }

            // Merge selected events and noise
            let mut all_xs = Vec::with_capacity(selected_xs.len() + add_noise);
            let mut all_ys = Vec::with_capacity(selected_ys.len() + add_noise);
            let mut all_ts: Vec<f64> = Vec::with_capacity(selected_ts.len() + add_noise);
            let mut all_ps = Vec::with_capacity(selected_ps.len() + add_noise);

            all_xs.extend(selected_xs.iter());
            all_xs.extend(xs_noise.iter());

            all_ys.extend(selected_ys.iter());
            all_ys.extend(ys_noise.iter());

            all_ts.extend(selected_ts.iter());
            all_ts.extend(ts_noise.iter());

            all_ps.extend(selected_ps.iter());
            all_ps.extend(ps_noise.iter());

            let merged_xs = Array1::from(all_xs);
            let merged_ys = Array1::from(all_ys);
            let merged_ts = Array1::from(all_ts);
            let merged_ps = Array1::from(all_ps);

            // Sort by timestamp
            let mut indices: Vec<usize> = (0..merged_ts.len()).collect();
            indices.sort_by(|&i, &j| merged_ts[i].partial_cmp(&merged_ts[j]).unwrap());

            let sorted_xs = indices
                .iter()
                .map(|&i| merged_xs[i])
                .collect::<Array1<i64>>();
            let sorted_ys = indices
                .iter()
                .map(|&i| merged_ys[i])
                .collect::<Array1<i64>>();
            let sorted_ts = indices
                .iter()
                .map(|&i| merged_ts[i])
                .collect::<Array1<f64>>();
            let sorted_ps = indices
                .iter()
                .map(|&i| merged_ps[i])
                .collect::<Array1<i64>>();

            // Convert arrays to Python objects
            let xs_py: PyObject = sorted_xs.into_pyarray(py).into();
            let ys_py: PyObject = sorted_ys.into_pyarray(py).into();
            let ts_py: PyObject = sorted_ts.into_pyarray(py).into();
            let ps_py: PyObject = sorted_ps.into_pyarray(py).into();

            // Create result tuple
            let tuple = PyTuple::new(py, [xs_py, ys_py, ts_py, ps_py])?;
            Ok(tuple.into())
        }
    }
}