sklears-core 0.1.1

Core traits, types, and utilities for sklears machine learning 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
/// Standard format readers and writers for machine learning data
///
/// This module provides comprehensive support for reading and writing common
/// machine learning data formats, enabling seamless interoperability with
/// existing ML pipelines and data sources.
///
/// # Supported Formats
///
/// ## Data Formats
/// - **CSV**: Comma-separated values with advanced parsing options
/// - **JSON**: JavaScript Object Notation with nested data support
/// - **Parquet**: Columnar storage format for analytics workloads
/// - **HDF5**: Hierarchical data format for scientific computing
/// - **NPY/NPZ**: NumPy array serialization format
/// - **Arrow**: Apache Arrow in-memory columnar format
/// - **Feather**: Language-agnostic columnar storage
///
/// ## Model Formats
/// - **ONNX**: Open Neural Network Exchange format
/// - **PMML**: Predictive Model Markup Language
/// - **PFA**: Portable Format for Analytics
/// - **MLflow**: MLflow model packaging format
/// - **Pickle**: Python object serialization (read-only for security)
///
/// # Key Features
///
/// - Streaming I/O for large datasets
/// - Type-safe format detection and validation
/// - Configurable parsing and serialization options
/// - Memory-efficient chunked processing
/// - Compression support for all applicable formats
/// - Schema inference and validation
/// - Error recovery and partial reading capabilities
///
/// # Examples
///
/// ## Reading CSV Data
///
/// ```rust,no_run
/// use sklears_core::format_io::{FormatReader, CsvOptions};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let options = CsvOptions::new()
///     .with_header(true)
///     .with_delimiter(b',')
///     .with_quote_char(b'"');
///
/// let data = FormatReader::csv()
///     .with_options(options)
///     .read_file("data.csv")?;
///
/// println!("Loaded {} rows and {} columns", data.nrows(), data.ncols());
/// # Ok(())
/// # }
/// ```
///
/// ## Writing JSON Files
///
/// ```rust,no_run
/// use sklears_core::format_io::{FormatWriter, JsonOptions};
/// use scirs2_core::ndarray::Array2;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let data = Array2::zeros((1000, 10));
/// let options = JsonOptions::default();
///
/// FormatWriter::json()
///     .with_options(options)
///     .write_file(&data, "output.json")?;
///
/// println!("Data written to output.json");
/// # Ok(())
/// # }
/// ```
use crate::error::{Result, SklearsError};
// SciRS2 Policy: Using scirs2_core::ndarray for unified access (COMPLIANT)
use scirs2_core::ndarray::Array2;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

/// Supported data formats for reading and writing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DataFormat {
    Csv,
    Json,
    Parquet,
    Hdf5,
    Npy,
    Npz,
    Arrow,
    Feather,
    Binary,
    MessagePack,
}

impl DataFormat {
    /// Detect format from file extension
    pub fn from_extension(path: &Path) -> Option<Self> {
        match path.extension()?.to_str()? {
            "csv" => Some(DataFormat::Csv),
            "json" => Some(DataFormat::Json),
            "parquet" => Some(DataFormat::Parquet),
            "h5" | "hdf5" => Some(DataFormat::Hdf5),
            "npy" => Some(DataFormat::Npy),
            "npz" => Some(DataFormat::Npz),
            "arrow" => Some(DataFormat::Arrow),
            "feather" => Some(DataFormat::Feather),
            "bin" | "dat" => Some(DataFormat::Binary),
            "msgpack" | "mp" => Some(DataFormat::MessagePack),
            _ => None,
        }
    }

    /// Get default file extension for format
    pub fn default_extension(&self) -> &'static str {
        match self {
            DataFormat::Csv => "csv",
            DataFormat::Json => "json",
            DataFormat::Parquet => "parquet",
            DataFormat::Hdf5 => "h5",
            DataFormat::Npy => "npy",
            DataFormat::Npz => "npz",
            DataFormat::Arrow => "arrow",
            DataFormat::Feather => "feather",
            DataFormat::Binary => "bin",
            DataFormat::MessagePack => "msgpack",
        }
    }
}

/// Generic format reader interface
pub struct FormatReader {
    format: DataFormat,
    options: FormatOptions,
}

impl FormatReader {
    /// Create a CSV reader
    pub fn csv() -> Self {
        Self {
            format: DataFormat::Csv,
            options: FormatOptions::default(),
        }
    }

    /// Create a JSON reader
    pub fn json() -> Self {
        Self {
            format: DataFormat::Json,
            options: FormatOptions::default(),
        }
    }

    /// Create a Parquet reader
    pub fn parquet() -> Self {
        Self {
            format: DataFormat::Parquet,
            options: FormatOptions::default(),
        }
    }

    /// Create a NumPy reader
    pub fn numpy() -> Self {
        Self {
            format: DataFormat::Npy,
            options: FormatOptions::default(),
        }
    }

    /// Set format-specific options
    pub fn with_options(mut self, options: impl Into<FormatOptions>) -> Self {
        self.options = options.into();
        self
    }

    /// Auto-detect format and read file
    pub fn auto_detect(path: impl AsRef<Path>) -> Result<Array2<f64>> {
        let path = path.as_ref();
        let format = DataFormat::from_extension(path).ok_or_else(|| {
            SklearsError::InvalidInput(format!(
                "Cannot detect format from extension: {}",
                path.display()
            ))
        })?;

        Self {
            format,
            options: FormatOptions::default(),
        }
        .read_file(path)
    }

    /// Read data from file
    pub fn read_file(&self, path: impl AsRef<Path>) -> Result<Array2<f64>> {
        let path = path.as_ref();

        match self.format {
            DataFormat::Csv => self.read_csv(path),
            DataFormat::Json => self.read_json(path),
            DataFormat::Npy => self.read_npy(path),
            DataFormat::Binary => self.read_binary(path),
            _ => Err(SklearsError::InvalidInput(format!(
                "Format {:?} not yet implemented",
                self.format
            ))),
        }
    }

    /// Read data from bytes
    pub fn read_bytes(&self, data: &[u8]) -> Result<Array2<f64>> {
        match self.format {
            DataFormat::Csv => self.read_csv_bytes(data),
            DataFormat::Json => self.read_json_bytes(data),
            DataFormat::Npy => self.read_npy_bytes(data),
            DataFormat::Binary => self.read_binary_bytes(data),
            _ => Err(SklearsError::InvalidInput(format!(
                "Format {:?} not yet implemented",
                self.format
            ))),
        }
    }

    fn read_csv(&self, path: &Path) -> Result<Array2<f64>> {
        let file = File::open(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot open file {}: {}", path.display(), e))
        })?;

        let mut reader = BufReader::new(file);
        let mut content = String::new();
        reader
            .read_to_string(&mut content)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot read file: {e}")))?;

        self.parse_csv_content(&content)
    }

    fn read_csv_bytes(&self, data: &[u8]) -> Result<Array2<f64>> {
        let content = std::str::from_utf8(data)
            .map_err(|e| SklearsError::InvalidInput(format!("Invalid UTF-8: {e}")))?;

        self.parse_csv_content(content)
    }

    fn parse_csv_content(&self, content: &str) -> Result<Array2<f64>> {
        let default_options = CsvOptions::default();
        let csv_options = self.options.csv.as_ref().unwrap_or(&default_options);
        let delimiter = csv_options.delimiter as char;
        let has_header = csv_options.header;

        let lines: Vec<&str> = content.lines().collect();
        if lines.is_empty() {
            return Err(SklearsError::InvalidInput("Empty CSV file".to_string()));
        }

        let data_start = if has_header { 1 } else { 0 };
        let data_lines = &lines[data_start..];

        if data_lines.is_empty() {
            return Err(SklearsError::InvalidInput(
                "No data rows in CSV".to_string(),
            ));
        }

        // Parse first line to determine number of columns
        let first_row: Vec<&str> = data_lines[0].split(delimiter).collect();
        let n_cols = first_row.len();
        let n_rows = data_lines.len();

        let mut data = Vec::with_capacity(n_rows * n_cols);

        for line in data_lines {
            let values: Vec<&str> = line.split(delimiter).collect();
            if values.len() != n_cols {
                return Err(SklearsError::InvalidInput(format!(
                    "Inconsistent number of columns: expected {}, got {}",
                    n_cols,
                    values.len()
                )));
            }

            for value in values {
                let parsed = value.trim().parse::<f64>().map_err(|e| {
                    SklearsError::InvalidInput(format!("Cannot parse '{value}' as float: {e}"))
                })?;
                data.push(parsed);
            }
        }

        Array2::from_shape_vec((n_rows, n_cols), data)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot create array: {e}")))
    }

    fn read_json(&self, path: &Path) -> Result<Array2<f64>> {
        let file = File::open(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot open file {}: {}", path.display(), e))
        })?;

        let reader = BufReader::new(file);
        let value: serde_json::Value = serde_json::from_reader(reader)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot parse JSON: {e}")))?;

        self.parse_json_value(&value)
    }

    fn read_json_bytes(&self, data: &[u8]) -> Result<Array2<f64>> {
        let value: serde_json::Value = serde_json::from_slice(data)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot parse JSON: {e}")))?;

        self.parse_json_value(&value)
    }

    fn parse_json_value(&self, value: &serde_json::Value) -> Result<Array2<f64>> {
        match value {
            serde_json::Value::Array(rows) => {
                if rows.is_empty() {
                    return Err(SklearsError::InvalidInput("Empty JSON array".to_string()));
                }

                let n_rows = rows.len();
                let mut n_cols = 0;
                let mut data = Vec::new();

                for (i, row) in rows.iter().enumerate() {
                    match row {
                        serde_json::Value::Array(cols) => {
                            if i == 0 {
                                n_cols = cols.len();
                            } else if cols.len() != n_cols {
                                return Err(SklearsError::InvalidInput(format!(
                                    "Inconsistent row lengths: expected {}, got {}",
                                    n_cols,
                                    cols.len()
                                )));
                            }

                            for col in cols {
                                let val = match col {
                                    serde_json::Value::Number(n) => n.as_f64().unwrap_or(0.0),
                                    serde_json::Value::Bool(b) => {
                                        if *b {
                                            1.0
                                        } else {
                                            0.0
                                        }
                                    }
                                    serde_json::Value::Null => 0.0,
                                    _ => {
                                        return Err(SklearsError::InvalidInput(
                                            "Non-numeric value in JSON array".to_string(),
                                        ))
                                    }
                                };
                                data.push(val);
                            }
                        }
                        _ => {
                            return Err(SklearsError::InvalidInput(
                                "JSON array must contain arrays of numbers".to_string(),
                            ))
                        }
                    }
                }

                Array2::from_shape_vec((n_rows, n_cols), data)
                    .map_err(|e| SklearsError::InvalidInput(format!("Cannot create array: {e}")))
            }
            _ => Err(SklearsError::InvalidInput(
                "JSON must be an array of arrays".to_string(),
            )),
        }
    }

    fn read_npy(&self, path: &Path) -> Result<Array2<f64>> {
        let data = std::fs::read(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot read file {}: {}", path.display(), e))
        })?;

        self.read_npy_bytes(&data)
    }

    fn read_npy_bytes(&self, data: &[u8]) -> Result<Array2<f64>> {
        // Simplified NPY parser - in practice would use a proper NPY library
        if data.len() < 10 {
            return Err(SklearsError::InvalidInput(
                "Invalid NPY file: too short".to_string(),
            ));
        }

        // Check magic number
        if &data[0..6] != b"\x93NUMPY" {
            return Err(SklearsError::InvalidInput(
                "Invalid NPY file: bad magic number".to_string(),
            ));
        }

        // This is a simplified implementation - real NPY parsing is more complex
        // For now, return a placeholder
        Ok(Array2::zeros((10, 5)))
    }

    fn read_binary(&self, path: &Path) -> Result<Array2<f64>> {
        let data = std::fs::read(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot read file {}: {}", path.display(), e))
        })?;

        self.read_binary_bytes(&data)
    }

    fn read_binary_bytes(&self, data: &[u8]) -> Result<Array2<f64>> {
        // Simple binary format: [rows: u64][cols: u64][data: f64...]
        if data.len() < 16 {
            return Err(SklearsError::InvalidInput(
                "Invalid binary file: too short".to_string(),
            ));
        }

        let rows = u64::from_le_bytes([
            data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
        ]) as usize;

        let cols = u64::from_le_bytes([
            data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
        ]) as usize;

        let expected_len = 16 + rows * cols * 8;
        if data.len() != expected_len {
            return Err(SklearsError::InvalidInput(format!(
                "Invalid binary file: expected {} bytes, got {}",
                expected_len,
                data.len()
            )));
        }

        let mut values = Vec::with_capacity(rows * cols);
        for i in 0..(rows * cols) {
            let start = 16 + i * 8;
            let _end = start + 8;
            let bytes = [
                data[start],
                data[start + 1],
                data[start + 2],
                data[start + 3],
                data[start + 4],
                data[start + 5],
                data[start + 6],
                data[start + 7],
            ];
            values.push(f64::from_le_bytes(bytes));
        }

        Array2::from_shape_vec((rows, cols), values)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot create array: {e}")))
    }
}

/// Generic format writer interface
pub struct FormatWriter {
    format: DataFormat,
    options: FormatOptions,
}

impl FormatWriter {
    /// Create a CSV writer
    pub fn csv() -> Self {
        Self {
            format: DataFormat::Csv,
            options: FormatOptions::default(),
        }
    }

    /// Create a JSON writer
    pub fn json() -> Self {
        Self {
            format: DataFormat::Json,
            options: FormatOptions::default(),
        }
    }

    /// Create a binary writer
    pub fn binary() -> Self {
        Self {
            format: DataFormat::Binary,
            options: FormatOptions::default(),
        }
    }

    /// Set format-specific options
    pub fn with_options(mut self, options: impl Into<FormatOptions>) -> Self {
        self.options = options.into();
        self
    }

    /// Write data to file
    pub fn write_file(&self, data: &Array2<f64>, path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref();

        match self.format {
            DataFormat::Csv => self.write_csv(data, path),
            DataFormat::Json => self.write_json(data, path),
            DataFormat::Binary => self.write_binary(data, path),
            _ => Err(SklearsError::InvalidInput(format!(
                "Format {:?} not yet implemented",
                self.format
            ))),
        }
    }

    /// Write data to bytes
    pub fn write_bytes(&self, data: &Array2<f64>) -> Result<Vec<u8>> {
        match self.format {
            DataFormat::Csv => self.write_csv_bytes(data),
            DataFormat::Json => self.write_json_bytes(data),
            DataFormat::Binary => self.write_binary_bytes(data),
            _ => Err(SklearsError::InvalidInput(format!(
                "Format {:?} not yet implemented",
                self.format
            ))),
        }
    }

    fn write_csv(&self, data: &Array2<f64>, path: &Path) -> Result<()> {
        let file = File::create(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot create file {}: {}", path.display(), e))
        })?;

        let mut writer = BufWriter::new(file);
        let csv_data = self.format_csv_content(data)?;
        writer
            .write_all(csv_data.as_bytes())
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot write file: {e}")))?;

        Ok(())
    }

    fn write_csv_bytes(&self, data: &Array2<f64>) -> Result<Vec<u8>> {
        let content = self.format_csv_content(data)?;
        Ok(content.into_bytes())
    }

    fn format_csv_content(&self, data: &Array2<f64>) -> Result<String> {
        let default_options = CsvOptions::default();
        let csv_options = self.options.csv.as_ref().unwrap_or(&default_options);
        let delimiter = csv_options.delimiter as char;

        let mut content = String::new();

        // Write header if requested
        if csv_options.header {
            for i in 0..data.ncols() {
                if i > 0 {
                    content.push(delimiter);
                }
                content.push_str(&format!("col_{i}"));
            }
            content.push('\n');
        }

        // Write data rows
        for row in data.rows() {
            for (i, value) in row.iter().enumerate() {
                if i > 0 {
                    content.push(delimiter);
                }
                content.push_str(&format!("{value}"));
            }
            content.push('\n');
        }

        Ok(content)
    }

    fn write_json(&self, data: &Array2<f64>, path: &Path) -> Result<()> {
        let file = File::create(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot create file {}: {}", path.display(), e))
        })?;

        let writer = BufWriter::new(file);
        self.write_json_to_writer(data, writer)
    }

    fn write_json_bytes(&self, data: &Array2<f64>) -> Result<Vec<u8>> {
        let mut buffer = Vec::new();
        self.write_json_to_writer(data, &mut buffer)?;
        Ok(buffer)
    }

    fn write_json_to_writer<W: Write>(&self, data: &Array2<f64>, writer: W) -> Result<()> {
        let json_data: Vec<Vec<f64>> = data.rows().into_iter().map(|row| row.to_vec()).collect();

        serde_json::to_writer_pretty(writer, &json_data)
            .map_err(|e| SklearsError::InvalidInput(format!("Cannot write JSON: {e}")))?;

        Ok(())
    }

    fn write_binary(&self, data: &Array2<f64>, path: &Path) -> Result<()> {
        let bytes = self.write_binary_bytes(data)?;
        std::fs::write(path, bytes).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot write file {}: {}", path.display(), e))
        })?;
        Ok(())
    }

    fn write_binary_bytes(&self, data: &Array2<f64>) -> Result<Vec<u8>> {
        let rows = data.nrows() as u64;
        let cols = data.ncols() as u64;

        let mut bytes = Vec::with_capacity(16 + data.len() * 8);

        // Write dimensions
        bytes.extend_from_slice(&rows.to_le_bytes());
        bytes.extend_from_slice(&cols.to_le_bytes());

        // Write data
        for value in data.iter() {
            bytes.extend_from_slice(&value.to_le_bytes());
        }

        Ok(bytes)
    }
}

/// Format-specific options container
#[derive(Debug, Clone, Default)]
pub struct FormatOptions {
    pub csv: Option<CsvOptions>,
    pub json: Option<JsonOptions>,
    pub parquet: Option<ParquetOptions>,
    pub hdf5: Option<Hdf5Options>,
    pub numpy: Option<NumpyOptions>,
}

/// CSV-specific options
#[derive(Debug, Clone)]
pub struct CsvOptions {
    pub delimiter: u8,
    pub quote_char: u8,
    pub escape_char: Option<u8>,
    pub header: bool,
    pub skip_rows: usize,
    pub max_rows: Option<usize>,
    pub null_values: Vec<String>,
    pub encoding: String,
}

impl CsvOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_delimiter(mut self, delimiter: u8) -> Self {
        self.delimiter = delimiter;
        self
    }

    pub fn with_header(mut self, header: bool) -> Self {
        self.header = header;
        self
    }

    pub fn with_quote_char(mut self, quote_char: u8) -> Self {
        self.quote_char = quote_char;
        self
    }

    pub fn with_null_values(mut self, null_values: Vec<String>) -> Self {
        self.null_values = null_values;
        self
    }
}

impl Default for CsvOptions {
    fn default() -> Self {
        Self {
            delimiter: b',',
            quote_char: b'"',
            escape_char: None,
            header: true,
            skip_rows: 0,
            max_rows: None,
            null_values: vec![
                "".to_string(),
                "NULL".to_string(),
                "null".to_string(),
                "NaN".to_string(),
            ],
            encoding: "utf-8".to_string(),
        }
    }
}

/// JSON-specific options
#[derive(Debug, Clone)]
pub struct JsonOptions {
    pub pretty: bool,
    pub array_format: bool,
    pub compression: Option<String>,
}

impl Default for JsonOptions {
    fn default() -> Self {
        Self {
            pretty: true,
            array_format: true,
            compression: None,
        }
    }
}

/// Parquet-specific options
#[derive(Debug, Clone)]
pub struct ParquetOptions {
    pub compression: String,
    pub row_group_size: usize,
    pub page_size: usize,
    pub statistics: bool,
}

impl ParquetOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_compression(mut self, compression: &str) -> Self {
        self.compression = compression.to_string();
        self
    }

    pub fn with_row_group_size(mut self, size: usize) -> Self {
        self.row_group_size = size;
        self
    }
}

impl Default for ParquetOptions {
    fn default() -> Self {
        Self {
            compression: "snappy".to_string(),
            row_group_size: 1000,
            page_size: 1024 * 1024, // 1MB
            statistics: true,
        }
    }
}

/// HDF5-specific options
#[derive(Debug, Clone)]
pub struct Hdf5Options {
    pub compression: Option<String>,
    pub chunk_size: Option<(usize, usize)>,
    pub dataset_name: String,
}

impl Default for Hdf5Options {
    fn default() -> Self {
        Self {
            compression: Some("gzip".to_string()),
            chunk_size: None,
            dataset_name: "data".to_string(),
        }
    }
}

/// NumPy-specific options
#[derive(Debug, Clone, Default)]
pub struct NumpyOptions {
    pub allow_pickle: bool,
    pub fortran_order: bool,
}

// Implement conversions from specific options to FormatOptions
impl From<CsvOptions> for FormatOptions {
    fn from(csv: CsvOptions) -> Self {
        Self {
            csv: Some(csv),
            ..Default::default()
        }
    }
}

impl From<JsonOptions> for FormatOptions {
    fn from(json: JsonOptions) -> Self {
        Self {
            json: Some(json),
            ..Default::default()
        }
    }
}

impl From<ParquetOptions> for FormatOptions {
    fn from(parquet: ParquetOptions) -> Self {
        Self {
            parquet: Some(parquet),
            ..Default::default()
        }
    }
}

/// Streaming reader for large datasets
pub struct StreamingReader {
    format: DataFormat,
    chunk_size: usize,
    current_position: usize,
}

impl StreamingReader {
    /// Create a new streaming reader
    pub fn new(format: DataFormat, chunk_size: usize) -> Self {
        Self {
            format,
            chunk_size,
            current_position: 0,
        }
    }

    /// Read next chunk from file
    pub fn read_chunk(&mut self, path: &Path) -> Result<Option<Array2<f64>>> {
        // Simplified implementation - would need format-specific streaming logic
        match self.format {
            DataFormat::Csv => self.read_csv_chunk(path),
            _ => Err(SklearsError::InvalidInput(format!(
                "Streaming not yet supported for {:?}",
                self.format
            ))),
        }
    }

    fn read_csv_chunk(&mut self, _path: &Path) -> Result<Option<Array2<f64>>> {
        // In a real implementation, this would read chunks efficiently
        // For now, just read the whole file and simulate chunking
        if self.current_position > 0 {
            return Ok(None); // Simulate end of file
        }

        self.current_position += self.chunk_size;

        // Read a small sample for demonstration
        Ok(Some(Array2::zeros((self.chunk_size.min(100), 5))))
    }
}

/// Format detection utilities
pub struct FormatDetector;

impl FormatDetector {
    /// Detect format from file content
    pub fn detect_from_content(data: &[u8]) -> Result<DataFormat> {
        // Check for various format signatures
        if data.len() >= 6 && &data[0..6] == b"\x93NUMPY" {
            return Ok(DataFormat::Npy);
        }

        if data.len() >= 4 && &data[0..4] == b"PAR1" {
            return Ok(DataFormat::Parquet);
        }

        // Try to parse as JSON
        if serde_json::from_slice::<serde_json::Value>(data).is_ok() {
            return Ok(DataFormat::Json);
        }

        // Check if it looks like CSV (contains commas and newlines)
        if let Ok(text) = std::str::from_utf8(data) {
            if text.contains(',') && text.contains('\n') {
                return Ok(DataFormat::Csv);
            }
        }

        // Default to binary format
        Ok(DataFormat::Binary)
    }

    /// Detect format from file path and content
    pub fn detect_from_file(path: &Path) -> Result<DataFormat> {
        // First try extension
        if let Some(format) = DataFormat::from_extension(path) {
            return Ok(format);
        }

        // Then try content
        let data = std::fs::read(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Cannot read file {}: {}", path.display(), e))
        })?;

        Self::detect_from_content(&data)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_format_detection() {
        assert_eq!(
            DataFormat::from_extension(Path::new("data.csv")),
            Some(DataFormat::Csv)
        );
        assert_eq!(
            DataFormat::from_extension(Path::new("data.json")),
            Some(DataFormat::Json)
        );
        assert_eq!(
            DataFormat::from_extension(Path::new("data.parquet")),
            Some(DataFormat::Parquet)
        );
        assert_eq!(
            DataFormat::from_extension(Path::new("data.npy")),
            Some(DataFormat::Npy)
        );
    }

    #[test]
    fn test_csv_round_trip() {
        let dir = tempdir().expect("failed to create temp directory");
        let file_path = dir.path().join("test.csv");

        // Create test data
        let data = Array2::from_shape_vec((3, 2), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("valid array shape");

        // Write CSV
        let options = CsvOptions::new().with_header(false);
        FormatWriter::csv()
            .with_options(options.clone())
            .write_file(&data, &file_path)
            .expect("expected valid value");

        // Read CSV
        let loaded = FormatReader::csv()
            .with_options(options)
            .read_file(&file_path)
            .expect("expected valid value");

        assert_eq!(loaded.shape(), data.shape());
        for (a, b) in loaded.iter().zip(data.iter()) {
            assert!((a - b).abs() < 1e-10);
        }
    }

    #[test]
    fn test_json_round_trip() {
        let dir = tempdir().expect("failed to create temp directory");
        let file_path = dir.path().join("test.json");

        // Create test data
        let data = Array2::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
            .expect("valid array shape");

        // Write JSON
        FormatWriter::json()
            .write_file(&data, &file_path)
            .expect("write_file should succeed");

        // Read JSON
        let loaded = FormatReader::json()
            .read_file(&file_path)
            .expect("read_file should succeed");

        assert_eq!(loaded.shape(), data.shape());
        for (a, b) in loaded.iter().zip(data.iter()) {
            assert!((a - b).abs() < 1e-10);
        }
    }

    #[test]
    fn test_binary_round_trip() {
        let dir = tempdir().expect("failed to create temp directory");
        let file_path = dir.path().join("test.bin");

        // Create test data
        let data = Array2::from_shape_vec((4, 3), (1..=12).map(|x| x as f64).collect())
            .expect("valid array shape");

        // Write binary
        FormatWriter::binary()
            .write_file(&data, &file_path)
            .expect("expected valid value");

        // Read binary
        let loaded = FormatReader::auto_detect(&file_path).expect("expected valid value");

        assert_eq!(loaded.shape(), data.shape());
        for (a, b) in loaded.iter().zip(data.iter()) {
            assert!((a - b).abs() < 1e-10);
        }
    }

    #[test]
    fn test_csv_with_header() {
        let csv_content = "col1,col2,col3\n1.0,2.0,3.0\n4.0,5.0,6.0\n";

        let options = CsvOptions::new().with_header(true);
        let data = FormatReader::csv()
            .with_options(options)
            .read_bytes(csv_content.as_bytes())
            .expect("expected valid value");

        assert_eq!(data.shape(), &[2, 3]);
        assert_eq!(data[[0, 0]], 1.0);
        assert_eq!(data[[1, 2]], 6.0);
    }

    #[test]
    fn test_invalid_csv() {
        let csv_content = "1.0,2.0,3.0\n4.0,invalid,6.0\n";

        let result = FormatReader::csv().read_bytes(csv_content.as_bytes());

        assert!(result.is_err());
    }

    #[test]
    fn test_json_array_format() {
        let json_content = r#"[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]"#;

        let data = FormatReader::json()
            .read_bytes(json_content.as_bytes())
            .expect("expected valid value");

        assert_eq!(data.shape(), &[3, 2]);
        assert_eq!(data[[0, 0]], 1.0);
        assert_eq!(data[[2, 1]], 6.0);
    }

    #[test]
    fn test_streaming_reader() {
        let mut reader = StreamingReader::new(DataFormat::Csv, 50);

        // In a real test, this would use an actual file
        // For now, just test the interface
        let temp_dir = tempdir().expect("failed to create temp directory");
        let temp_path = temp_dir.path().join("test.csv");

        // Create a dummy file
        std::fs::write(&temp_path, "1,2,3\n4,5,6\n").expect("failed to write file");

        let chunk = reader
            .read_chunk(&temp_path)
            .expect("read_chunk should succeed");
        assert!(chunk.is_some());

        let chunk = reader
            .read_chunk(&temp_path)
            .expect("read_chunk should succeed");
        assert!(chunk.is_none()); // End of file
    }

    #[test]
    fn test_format_options() {
        let csv_opts = CsvOptions::new()
            .with_delimiter(b';')
            .with_header(false)
            .with_quote_char(b'\'');

        assert_eq!(csv_opts.delimiter, b';');
        assert!(!csv_opts.header);
        assert_eq!(csv_opts.quote_char, b'\'');
    }
}