tenflowers-dataset 0.1.1

Data pipeline and dataset utilities for TenfloweRS
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
//! Zarr format support for large-scale multidimensional array datasets
//!
//! This module provides support for the Zarr format, which is particularly useful for:
//! - Large multidimensional scientific datasets  
//! - Cloud-native storage with efficient chunking
//! - Parallel I/O operations
//! - Medical imaging, satellite imagery, and other scientific ML applications

use crate::Dataset;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tenflowers_core::{Result, Tensor, TensorError};

#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

// Cloud-native and compression support
#[cfg(feature = "compression")]
use oxiarc_archive::GzipReader;
#[cfg(feature = "cloud")]
/// Cloud storage backend types
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum CloudBackend {
    /// Local filesystem (default)
    Local,
    /// Amazon S3 compatible storage
    #[cfg(feature = "cloud")]
    S3 {
        bucket: String,
        region: String,
        endpoint: Option<String>,
    },
    /// Google Cloud Storage
    #[cfg(feature = "cloud")]
    Gcs { bucket: String },
    /// Azure Blob Storage
    #[cfg(feature = "cloud")]
    Azure { account: String, container: String },
}

#[cfg(feature = "cloud")]
impl Default for CloudBackend {
    fn default() -> Self {
        Self::Local
    }
}

/// Compression algorithms supported for Zarr format
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub enum ZarrCompressionType {
    None,
    #[cfg(feature = "compression")]
    Gzip,
    #[cfg(feature = "compression")]
    Blosc,
    #[cfg(feature = "compression")]
    Lz4,
    #[cfg(feature = "compression")]
    Zstd,
}

impl Default for ZarrCompressionType {
    fn default() -> Self {
        Self::None
    }
}

/// Configuration for Zarr dataset loading
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ZarrConfig {
    /// Path to the Zarr array directory
    pub array_path: PathBuf,
    /// Optional path to labels array
    pub labels_path: Option<PathBuf>,
    /// Chunk cache size in bytes
    pub chunk_cache_size: usize,
    /// Whether to load data lazily
    pub lazy_loading: bool,
    /// Maximum number of chunks to load in parallel
    pub max_parallel_chunks: usize,
    /// Whether to use memory mapping for chunks
    pub use_memory_mapping: bool,
    /// Dimension order for tensor conversion (e.g., "NCHW" for images)
    pub dimension_order: Option<String>,
    /// Cloud storage backend configuration
    #[cfg(feature = "cloud")]
    pub cloud_backend: CloudBackend,
    /// Compression type for chunk storage
    pub compression: ZarrCompressionType,
    /// Enable async I/O operations
    pub async_io: bool,
    /// Connection timeout for cloud backends (in seconds)
    pub connection_timeout: u64,
    /// Retry attempts for failed operations
    pub retry_attempts: usize,
}

impl Default for ZarrConfig {
    fn default() -> Self {
        Self {
            array_path: PathBuf::new(),
            labels_path: None,
            chunk_cache_size: 100_000_000, // 100MB
            lazy_loading: true,
            max_parallel_chunks: 4,
            use_memory_mapping: true,
            dimension_order: None,
            #[cfg(feature = "cloud")]
            cloud_backend: CloudBackend::default(),
            compression: ZarrCompressionType::default(),
            async_io: false,
            connection_timeout: 30,
            retry_attempts: 3,
        }
    }
}

/// Zarr array metadata
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct ZarrArrayInfo {
    pub shape: Vec<usize>,
    pub dtype: String,
    pub chunks: Vec<usize>,
    pub compressor: Option<String>,
    pub fill_value: Option<f64>,
    pub order: String, // 'C' or 'F'
    pub zarr_format: u32,
}

/// Builder for Zarr datasets
#[derive(Debug, Clone)]
pub struct ZarrDatasetBuilder<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static,
{
    config: ZarrConfig,
    _phantom: std::marker::PhantomData<T>,
}

impl<T> ZarrDatasetBuilder<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static
        + scirs2_core::num_traits::cast::NumCast,
{
    /// Create a new Zarr dataset builder
    pub fn new() -> Self {
        Self {
            config: ZarrConfig::default(),
            _phantom: std::marker::PhantomData,
        }
    }

    /// Set the path to the Zarr array
    pub fn array_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.config.array_path = path.as_ref().to_path_buf();
        self
    }

    /// Set the path to the labels array
    pub fn labels_path<P: AsRef<Path>>(mut self, path: P) -> Self {
        self.config.labels_path = Some(path.as_ref().to_path_buf());
        self
    }

    /// Set chunk cache size in bytes
    pub fn chunk_cache_size(mut self, size: usize) -> Self {
        self.config.chunk_cache_size = size;
        self
    }

    /// Enable or disable lazy loading
    pub fn lazy_loading(mut self, enabled: bool) -> Self {
        self.config.lazy_loading = enabled;
        self
    }

    /// Set maximum parallel chunks
    pub fn max_parallel_chunks(mut self, count: usize) -> Self {
        self.config.max_parallel_chunks = count;
        self
    }

    /// Enable or disable memory mapping
    pub fn use_memory_mapping(mut self, enabled: bool) -> Self {
        self.config.use_memory_mapping = enabled;
        self
    }

    /// Set dimension order for tensor conversion
    pub fn dimension_order<S: AsRef<str>>(mut self, order: S) -> Self {
        self.config.dimension_order = Some(order.as_ref().to_string());
        self
    }

    /// Set cloud backend configuration
    #[cfg(feature = "cloud")]
    pub fn cloud_backend(mut self, backend: CloudBackend) -> Self {
        self.config.cloud_backend = backend;
        self
    }

    /// Set compression type
    pub fn compression(mut self, compression: ZarrCompressionType) -> Self {
        self.config.compression = compression;
        self
    }

    /// Enable async I/O operations
    pub fn async_io(mut self, enabled: bool) -> Self {
        self.config.async_io = enabled;
        self
    }

    /// Set connection timeout for cloud backends
    pub fn connection_timeout(mut self, timeout: u64) -> Self {
        self.config.connection_timeout = timeout;
        self
    }

    /// Set retry attempts for failed operations
    pub fn retry_attempts(mut self, attempts: usize) -> Self {
        self.config.retry_attempts = attempts;
        self
    }

    /// Build the Zarr dataset
    pub fn build(self) -> Result<ZarrDataset<T>> {
        ZarrDataset::from_config(self.config)
    }
}

impl<T> Default for ZarrDatasetBuilder<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static
        + scirs2_core::num_traits::cast::NumCast,
{
    fn default() -> Self {
        Self::new()
    }
}

/// Zarr dataset implementation
#[derive(Debug, Clone)]
pub struct ZarrDataset<T>
where
    T: Clone + Default + scirs2_core::numeric::Zero + Send + Sync + 'static,
{
    config: ZarrConfig,
    array_info: ZarrArrayInfo,
    labels_info: Option<ZarrArrayInfo>,
    chunk_cache: Arc<std::sync::Mutex<HashMap<Vec<usize>, Vec<T>>>>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T> ZarrDataset<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static
        + scirs2_core::num_traits::cast::NumCast,
{
    /// Create a new Zarr dataset from configuration
    pub fn from_config(config: ZarrConfig) -> Result<Self> {
        // Read array metadata
        let array_info = Self::read_array_metadata(&config.array_path)?;

        // Read labels metadata if provided
        let labels_info = if let Some(ref labels_path) = config.labels_path {
            Some(Self::read_array_metadata(labels_path)?)
        } else {
            None
        };

        let chunk_cache = Arc::new(std::sync::Mutex::new(HashMap::new()));

        Ok(Self {
            config,
            array_info,
            labels_info,
            chunk_cache,
            _phantom: std::marker::PhantomData,
        })
    }

    /// Read Zarr array metadata from .zarray file
    fn read_array_metadata(array_path: &Path) -> Result<ZarrArrayInfo> {
        let zarray_path = array_path.join(".zarray");

        if !zarray_path.exists() {
            return Err(TensorError::invalid_argument(format!(
                "Zarr metadata file not found: {zarray_path:?}"
            )));
        }

        let metadata_content = fs::read_to_string(&zarray_path).map_err(|e| {
            TensorError::invalid_argument(format!("Failed to read Zarr metadata: {e}"))
        })?;

        // Parse JSON metadata (simplified - in a real implementation, use serde_json)
        Self::parse_zarr_metadata(&metadata_content)
    }

    /// Parse Zarr metadata JSON with enhanced parsing logic
    fn parse_zarr_metadata(content: &str) -> Result<ZarrArrayInfo> {
        // Enhanced JSON parsing logic - still simplified but more robust
        // In a production environment, you would use serde_json for proper JSON parsing

        // Extract key fields using improved string parsing
        let shape = if content.contains("\"shape\"") {
            // Try to extract shape array with basic parsing
            if let Some(start) = content.find("\"shape\"") {
                if let Some(array_start) = content[start..].find('[') {
                    if let Some(array_end) = content[start + array_start..].find(']') {
                        let array_content =
                            &content[start + array_start + 1..start + array_start + array_end];
                        let numbers: Vec<usize> = array_content
                            .split(',')
                            .filter_map(|s| s.trim().parse().ok())
                            .collect();
                        if !numbers.is_empty() {
                            numbers
                        } else {
                            vec![1000, 224, 224, 3] // Default
                        }
                    } else {
                        vec![1000, 224, 224, 3]
                    }
                } else {
                    vec![1000, 224, 224, 3]
                }
            } else {
                vec![1000, 224, 224, 3]
            }
        } else {
            vec![1000, 224, 224, 3]
        };

        let dtype = if content.contains("\"dtype\"") {
            if let Some(start) = content.find("\"dtype\"") {
                if let Some(colon) = content[start..].find(':') {
                    if let Some(quote_start) = content[start + colon..].find('\"') {
                        if let Some(quote_end) =
                            content[start + colon + quote_start + 1..].find('\"')
                        {
                            content[start + colon + quote_start + 1
                                ..start + colon + quote_start + 1 + quote_end]
                                .to_string()
                        } else {
                            "<f4".to_string()
                        }
                    } else {
                        "<f4".to_string()
                    }
                } else {
                    "<f4".to_string()
                }
            } else {
                "<f4".to_string()
            }
        } else {
            "<f4".to_string()
        };

        let chunks = if content.contains("\"chunks\"") {
            // Similar extraction logic for chunks array
            if let Some(start) = content.find("\"chunks\"") {
                if let Some(array_start) = content[start..].find('[') {
                    if let Some(array_end) = content[start + array_start..].find(']') {
                        let array_content =
                            &content[start + array_start + 1..start + array_start + array_end];
                        let numbers: Vec<usize> = array_content
                            .split(',')
                            .filter_map(|s| s.trim().parse().ok())
                            .collect();
                        if !numbers.is_empty() {
                            numbers
                        } else {
                            vec![100, 224, 224, 3] // Default
                        }
                    } else {
                        vec![100, 224, 224, 3]
                    }
                } else {
                    vec![100, 224, 224, 3]
                }
            } else {
                vec![100, 224, 224, 3]
            }
        } else {
            vec![100, 224, 224, 3]
        };

        let compressor = if content.contains("\"compressor\"") {
            if content.contains("\"blosc\"") {
                Some("blosc".to_string())
            } else if content.contains("\"gzip\"") {
                Some("gzip".to_string())
            } else if content.contains("\"lz4\"") {
                Some("lz4".to_string())
            } else if content.contains("\"zstd\"") {
                Some("zstd".to_string())
            } else {
                None
            }
        } else {
            None
        };

        let fill_value = if content.contains("\"fill_value\"") {
            // Try to extract numeric fill value
            if let Some(start) = content.find("\"fill_value\"") {
                if let Some(colon) = content[start..].find(':') {
                    let after_colon = &content[start + colon + 1..];
                    // Find the next comma or closing brace
                    let end_pos = after_colon
                        .find(',')
                        .or_else(|| after_colon.find('}'))
                        .unwrap_or(after_colon.len());
                    let value_str = after_colon[..end_pos].trim();
                    if value_str == "null" {
                        None
                    } else {
                        value_str.parse::<f64>().ok()
                    }
                } else {
                    Some(0.0)
                }
            } else {
                Some(0.0)
            }
        } else {
            None
        };

        let order = if content.contains("\"order\": \"F\"") {
            "F".to_string()
        } else {
            "C".to_string()
        };

        let zarr_format = if content.contains("\"zarr_format\": 3") {
            3
        } else {
            2
        };

        Ok(ZarrArrayInfo {
            shape,
            dtype,
            chunks,
            compressor,
            fill_value,
            order,
            zarr_format,
        })
    }

    /// Get array information
    pub fn array_info(&self) -> &ZarrArrayInfo {
        &self.array_info
    }

    /// Get labels array information
    pub fn labels_info(&self) -> Option<&ZarrArrayInfo> {
        self.labels_info.as_ref()
    }

    /// Load a specific chunk by coordinates
    pub fn load_chunk(&self, chunk_coords: &[usize]) -> Result<Vec<T>> {
        // Check cache first
        {
            let cache = self
                .chunk_cache
                .lock()
                .expect("lock should not be poisoned");
            if let Some(cached_data) = cache.get(chunk_coords) {
                return Ok(cached_data.clone());
            }
        }

        // Load chunk from disk
        let chunk_data = self.load_chunk_from_disk(chunk_coords)?;

        // Cache the loaded chunk
        {
            let mut cache = self
                .chunk_cache
                .lock()
                .expect("lock should not be poisoned");
            cache.insert(chunk_coords.to_vec(), chunk_data.clone());
        }

        Ok(chunk_data)
    }

    /// Load chunk from disk storage
    fn load_chunk_from_disk(&self, chunk_coords: &[usize]) -> Result<Vec<T>> {
        // Construct chunk file path
        let chunk_name = chunk_coords
            .iter()
            .map(|&coord| coord.to_string())
            .collect::<Vec<_>>()
            .join(".");

        let chunk_path = self.config.array_path.join(chunk_name);

        if !chunk_path.exists() {
            return Err(TensorError::invalid_argument(format!(
                "Chunk file not found: {chunk_path:?}"
            )));
        }

        // Read and decompress chunk data
        let chunk_bytes = fs::read(&chunk_path)
            .map_err(|e| TensorError::invalid_argument(format!("Failed to read chunk: {e}")))?;

        // Decompress if needed
        let decompressed_data = self.decompress_chunk_data(&chunk_bytes)?;

        // Convert bytes to typed data
        self.bytes_to_typed_data(&decompressed_data)
    }

    /// Convert raw bytes to typed data with proper type handling
    fn bytes_to_typed_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        // Handle different data types based on the dtype string
        match self.array_info.dtype.as_str() {
            "<f4" | ">f4" => self.parse_float32_data(bytes),
            "<f8" | ">f8" => self.parse_float64_data(bytes),
            "<i4" | ">i4" => self.parse_int32_data(bytes),
            "<i8" | ">i8" => self.parse_int64_data(bytes),
            "<u1" | ">u1" => self.parse_uint8_data(bytes),
            _ => {
                // Default to float32 for unknown types
                self.parse_float32_data(bytes)
            }
        }
    }

    /// Parse float32 data from bytes
    fn parse_float32_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        if bytes.len() % 4 != 0 {
            return Err(TensorError::invalid_argument(
                "Byte array length not divisible by 4 for float32 data".to_string(),
            ));
        }

        let num_elements = bytes.len() / 4;
        let mut data = Vec::with_capacity(num_elements);
        let is_little_endian = self.array_info.dtype.starts_with('<');

        for i in 0..num_elements {
            let byte_slice = &bytes[i * 4..(i + 1) * 4];
            let _value = if is_little_endian {
                f32::from_le_bytes([byte_slice[0], byte_slice[1], byte_slice[2], byte_slice[3]])
            } else {
                f32::from_be_bytes([byte_slice[0], byte_slice[1], byte_slice[2], byte_slice[3]])
            };

            // Convert to target type T using num_traits for safe conversion
            let converted =
                scirs2_core::num_traits::cast::NumCast::from(_value).unwrap_or_else(|| {
                    eprintln!(
                        "Warning: Failed to convert f32 {_value} to target type, using default"
                    );
                    T::default()
                });
            data.push(converted);
        }

        Ok(data)
    }

    /// Parse float64 data from bytes
    fn parse_float64_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        if bytes.len() % 8 != 0 {
            return Err(TensorError::invalid_argument(
                "Byte array length not divisible by 8 for float64 data".to_string(),
            ));
        }

        let num_elements = bytes.len() / 8;
        let mut data = Vec::with_capacity(num_elements);
        let is_little_endian = self.array_info.dtype.starts_with('<');

        for i in 0..num_elements {
            let byte_slice = &bytes[i * 8..(i + 1) * 8];
            let _value = if is_little_endian {
                f64::from_le_bytes([
                    byte_slice[0],
                    byte_slice[1],
                    byte_slice[2],
                    byte_slice[3],
                    byte_slice[4],
                    byte_slice[5],
                    byte_slice[6],
                    byte_slice[7],
                ])
            } else {
                f64::from_be_bytes([
                    byte_slice[0],
                    byte_slice[1],
                    byte_slice[2],
                    byte_slice[3],
                    byte_slice[4],
                    byte_slice[5],
                    byte_slice[6],
                    byte_slice[7],
                ])
            };

            // Convert to target type T using num_traits for safe conversion
            let converted =
                scirs2_core::num_traits::cast::NumCast::from(_value).unwrap_or_else(|| {
                    eprintln!(
                        "Warning: Failed to convert f64 {_value} to target type, using default"
                    );
                    T::default()
                });
            data.push(converted);
        }

        Ok(data)
    }

    /// Parse int32 data from bytes
    fn parse_int32_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        if bytes.len() % 4 != 0 {
            return Err(TensorError::invalid_argument(
                "Byte array length not divisible by 4 for int32 data".to_string(),
            ));
        }

        let num_elements = bytes.len() / 4;
        let mut data = Vec::with_capacity(num_elements);
        let is_little_endian = self.array_info.dtype.starts_with('<');

        for i in 0..num_elements {
            let byte_slice = &bytes[i * 4..(i + 1) * 4];
            let _value = if is_little_endian {
                i32::from_le_bytes([byte_slice[0], byte_slice[1], byte_slice[2], byte_slice[3]])
            } else {
                i32::from_be_bytes([byte_slice[0], byte_slice[1], byte_slice[2], byte_slice[3]])
            };

            // Convert to target type T using num_traits for safe conversion
            let converted =
                scirs2_core::num_traits::cast::NumCast::from(_value).unwrap_or_else(|| {
                    eprintln!(
                        "Warning: Failed to convert i32 {_value} to target type, using default"
                    );
                    T::default()
                });
            data.push(converted);
        }

        Ok(data)
    }

    /// Parse int64 data from bytes
    fn parse_int64_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        if bytes.len() % 8 != 0 {
            return Err(TensorError::invalid_argument(
                "Byte array length not divisible by 8 for int64 data".to_string(),
            ));
        }

        let num_elements = bytes.len() / 8;
        let mut data = Vec::with_capacity(num_elements);
        let is_little_endian = self.array_info.dtype.starts_with('<');

        for i in 0..num_elements {
            let byte_slice = &bytes[i * 8..(i + 1) * 8];
            let _value = if is_little_endian {
                i64::from_le_bytes([
                    byte_slice[0],
                    byte_slice[1],
                    byte_slice[2],
                    byte_slice[3],
                    byte_slice[4],
                    byte_slice[5],
                    byte_slice[6],
                    byte_slice[7],
                ])
            } else {
                i64::from_be_bytes([
                    byte_slice[0],
                    byte_slice[1],
                    byte_slice[2],
                    byte_slice[3],
                    byte_slice[4],
                    byte_slice[5],
                    byte_slice[6],
                    byte_slice[7],
                ])
            };

            // Convert to target type T using num_traits for safe conversion
            let converted =
                scirs2_core::num_traits::cast::NumCast::from(_value).unwrap_or_else(|| {
                    eprintln!(
                        "Warning: Failed to convert i64 {_value} to target type, using default"
                    );
                    T::default()
                });
            data.push(converted);
        }

        Ok(data)
    }

    /// Parse uint8 data from bytes
    fn parse_uint8_data(&self, bytes: &[u8]) -> Result<Vec<T>> {
        let mut data = Vec::with_capacity(bytes.len());

        for &_byte in bytes {
            // Convert to target type T using num_traits for safe conversion
            let converted =
                scirs2_core::num_traits::cast::NumCast::from(_byte).unwrap_or_else(|| {
                    eprintln!(
                        "Warning: Failed to convert u8 {_byte} to target type, using default"
                    );
                    T::default()
                });
            data.push(converted);
        }

        Ok(data)
    }

    /// Decompress chunk data based on compression type
    fn decompress_chunk_data(&self, compressed_data: &[u8]) -> Result<Vec<u8>> {
        match &self.array_info.compressor {
            Some(compressor) => {
                match compressor.as_str() {
                    #[cfg(feature = "compression")]
                    "gzip" => {
                        let mut gzip_reader = GzipReader::new(std::io::Cursor::new(
                            compressed_data,
                        ))
                        .map_err(|e| {
                            TensorError::invalid_argument(format!(
                                "Gzip decompression init failed: {e}"
                            ))
                        })?;
                        let decompressed = gzip_reader.decompress().map_err(|e| {
                            TensorError::invalid_argument(format!("Gzip decompression failed: {e}"))
                        })?;
                        Ok(decompressed)
                    }
                    "blosc" => {
                        // Blosc decompression would require the blosc crate
                        // For now, return the data as-is
                        Ok(compressed_data.to_vec())
                    }
                    "lz4" => {
                        // LZ4 decompression would require the lz4 crate
                        // For now, return the data as-is
                        Ok(compressed_data.to_vec())
                    }
                    "zstd" => {
                        // Zstandard decompression would require the zstd crate
                        // For now, return the data as-is
                        Ok(compressed_data.to_vec())
                    }
                    _ => {
                        // Unknown compressor, return data as-is
                        Ok(compressed_data.to_vec())
                    }
                }
            }
            None => {
                // No compression, return data as-is
                Ok(compressed_data.to_vec())
            }
        }
    }

    /// Calculate chunk coordinates for a given sample index
    fn sample_to_chunk_coords(&self, index: usize) -> Vec<usize> {
        let chunk_size = self.array_info.chunks[0];
        vec![index / chunk_size]
    }

    /// Get sample data by index
    pub fn get_sample_data(&self, index: usize) -> Result<(Vec<T>, Option<T>)> {
        // Calculate which chunk contains this sample
        let chunk_coords = self.sample_to_chunk_coords(index);

        // Load the chunk
        let chunk_data = self.load_chunk(&chunk_coords)?;

        // Extract the specific sample from the chunk
        let chunk_size = self.array_info.chunks[0];
        let sample_offset = index % chunk_size;
        let sample_size = self.array_info.shape[1..].iter().product::<usize>();

        let start_idx = sample_offset * sample_size;
        let end_idx = start_idx + sample_size;

        if end_idx > chunk_data.len() {
            return Err(TensorError::invalid_argument(format!(
                "Sample index {index} out of bounds"
            )));
        }

        let features = chunk_data[start_idx..end_idx].to_vec();

        // Load label if available
        let label = if self.labels_info.is_some() {
            // Load label from labels array (simplified)
            Some(T::default())
        } else {
            None
        };

        Ok((features, label))
    }
}

impl<T> Dataset<T> for ZarrDataset<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static
        + scirs2_core::num_traits::cast::NumCast,
{
    fn len(&self) -> usize {
        self.array_info.shape[0]
    }

    fn get(&self, index: usize) -> Result<(Tensor<T>, Tensor<T>)> {
        if index >= self.len() {
            return Err(TensorError::invalid_argument(format!(
                "Index {} out of bounds for dataset of size {}",
                index,
                self.len()
            )));
        }

        let (features, label) = self.get_sample_data(index)?;

        // Create feature tensor
        let feature_shape = self.array_info.shape[1..].to_vec();
        let feature_tensor = Tensor::from_vec(features, &feature_shape)?;

        // Create label tensor
        let label_tensor = if let Some(label_val) = label {
            Tensor::from_vec(vec![label_val], &[1])?
        } else {
            // Default label if no labels provided
            Tensor::from_vec(vec![T::default()], &[1])?
        };

        Ok((feature_tensor, label_tensor))
    }
}

/// Extension trait for easy Zarr dataset creation
pub trait ZarrDatasetExt<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static,
{
    /// Create a Zarr dataset from a directory path
    fn from_zarr_path<P: AsRef<Path>>(path: P) -> Result<ZarrDataset<T>>;

    /// Create a Zarr dataset with labels
    fn from_zarr_with_labels<P: AsRef<Path>>(
        array_path: P,
        labels_path: P,
    ) -> Result<ZarrDataset<T>>;
}

impl<T> ZarrDatasetExt<T> for ZarrDataset<T>
where
    T: Clone
        + Default
        + scirs2_core::numeric::Zero
        + scirs2_core::numeric::One
        + std::str::FromStr
        + Send
        + Sync
        + 'static
        + scirs2_core::num_traits::cast::NumCast,
{
    fn from_zarr_path<P: AsRef<Path>>(path: P) -> Result<ZarrDataset<T>> {
        ZarrDatasetBuilder::new().array_path(path).build()
    }

    fn from_zarr_with_labels<P: AsRef<Path>>(
        array_path: P,
        labels_path: P,
    ) -> Result<ZarrDataset<T>> {
        ZarrDatasetBuilder::new()
            .array_path(array_path)
            .labels_path(labels_path)
            .build()
    }
}

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

    #[test]
    fn test_zarr_config_creation() {
        let config = ZarrConfig::default();
        assert_eq!(config.chunk_cache_size, 100_000_000);
        assert!(config.lazy_loading);
        assert_eq!(config.max_parallel_chunks, 4);
        assert!(config.use_memory_mapping);
        #[cfg(feature = "cloud")]
        assert_eq!(config.cloud_backend, CloudBackend::Local);
        assert_eq!(config.compression, ZarrCompressionType::None);
        assert!(!config.async_io);
        assert_eq!(config.connection_timeout, 30);
        assert_eq!(config.retry_attempts, 3);
    }

    #[test]
    fn test_zarr_builder() {
        let builder = ZarrDatasetBuilder::<f32>::new()
            .array_path("/path/to/array")
            .chunk_cache_size(50_000_000)
            .lazy_loading(false)
            .max_parallel_chunks(8)
            .compression(ZarrCompressionType::None)
            .async_io(true)
            .connection_timeout(60)
            .retry_attempts(5);

        assert_eq!(builder.config.array_path, PathBuf::from("/path/to/array"));
        assert_eq!(builder.config.chunk_cache_size, 50_000_000);
        assert!(!builder.config.lazy_loading);
        assert_eq!(builder.config.max_parallel_chunks, 8);
        assert_eq!(builder.config.compression, ZarrCompressionType::None);
        assert!(builder.config.async_io);
        assert_eq!(builder.config.connection_timeout, 60);
        assert_eq!(builder.config.retry_attempts, 5);
    }

    #[test]
    fn test_chunk_coordinate_calculation() {
        let config = ZarrConfig {
            array_path: PathBuf::from("/test"),
            ..Default::default()
        };

        let array_info = ZarrArrayInfo {
            shape: vec![1000, 224, 224, 3],
            dtype: "<f4".to_string(),
            chunks: vec![100, 224, 224, 3],
            compressor: None,
            fill_value: None,
            order: "C".to_string(),
            zarr_format: 2,
        };

        let dataset = ZarrDataset::<f32> {
            config,
            array_info,
            labels_info: None,
            chunk_cache: Arc::new(std::sync::Mutex::new(HashMap::new())),
            _phantom: std::marker::PhantomData,
        };

        // Test chunk coordinate calculation
        assert_eq!(dataset.sample_to_chunk_coords(0), vec![0]);
        assert_eq!(dataset.sample_to_chunk_coords(50), vec![0]);
        assert_eq!(dataset.sample_to_chunk_coords(100), vec![1]);
        assert_eq!(dataset.sample_to_chunk_coords(250), vec![2]);
    }

    #[test]
    fn test_zarr_array_info() {
        let info = ZarrArrayInfo {
            shape: vec![1000, 224, 224, 3],
            dtype: "<f4".to_string(),
            chunks: vec![100, 224, 224, 3],
            compressor: Some("blosc".to_string()),
            fill_value: Some(0.0),
            order: "C".to_string(),
            zarr_format: 2,
        };

        assert_eq!(info.shape, vec![1000, 224, 224, 3]);
        assert_eq!(info.dtype, "<f4");
        assert_eq!(
            info.compressor
                .as_ref()
                .expect("test: value should be present"),
            "blosc"
        );
        assert_eq!(info.zarr_format, 2);
    }

    #[test]
    fn test_cloud_backend_configuration() {
        #[cfg(feature = "cloud")]
        {
            let s3_backend = CloudBackend::S3 {
                bucket: "my-bucket".to_string(),
                region: "us-west-2".to_string(),
                endpoint: None,
            };
            assert!(matches!(s3_backend, CloudBackend::S3 { .. }));

            let gcs_backend = CloudBackend::Gcs {
                bucket: "my-gcs-bucket".to_string(),
            };
            assert!(matches!(gcs_backend, CloudBackend::Gcs { .. }));

            let local_backend = CloudBackend::Local;
            assert_eq!(local_backend, CloudBackend::default());
        }
    }

    #[test]
    fn test_compression_types() {
        let none_compression = ZarrCompressionType::None;
        assert_eq!(none_compression, ZarrCompressionType::default());

        #[cfg(feature = "compression")]
        {
            let gzip_compression = ZarrCompressionType::Gzip;
            assert!(matches!(gzip_compression, ZarrCompressionType::Gzip));

            let blosc_compression = ZarrCompressionType::Blosc;
            assert!(matches!(blosc_compression, ZarrCompressionType::Blosc));
        }
    }

    #[test]
    fn test_enhanced_metadata_parsing() {
        let json_content = r#"{
            "chunks": [100, 224, 224, 3],
            "compressor": {"id": "blosc"},
            "dtype": "<f4",
            "fill_value": 0.0,
            "filters": null,
            "order": "C",
            "shape": [1000, 224, 224, 3],
            "zarr_format": 2
        }"#;

        let result = ZarrDataset::<f32>::parse_zarr_metadata(json_content);
        assert!(result.is_ok());

        let metadata = result.expect("test: operation should succeed");
        assert_eq!(metadata.shape, vec![1000, 224, 224, 3]);
        assert_eq!(metadata.dtype, "<f4");
        assert_eq!(metadata.chunks, vec![100, 224, 224, 3]);
        assert_eq!(metadata.order, "C");
        assert_eq!(metadata.zarr_format, 2);
    }
}