torsh-data 0.1.2

Data loading and preprocessing utilities for ToRSh
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
//! HDF5 integration for scientific data formats
//!
//! This module provides utilities for reading and writing HDF5 files,
//! which are commonly used in scientific computing and machine learning.

#[cfg(feature = "hdf5-support")]
use hdf5::{File, Group, H5Type};

use crate::{utils, Dataset};
use torsh_core::{
    device::DeviceType,
    dtype::TensorElement,
    error::{Result, TorshError},
};
use torsh_tensor::Tensor;

#[cfg(not(feature = "hdf5-support"))]
use std::marker::PhantomData;

/// HDF5 dataset for reading HDF5 files
#[cfg(feature = "hdf5-support")]
pub struct HDF5TensorDataset {
    file: File,
    dataset_path: String,
    shape: Vec<usize>,
    chunk_size: Option<usize>,
    cache_size: usize,
}

#[cfg(not(feature = "hdf5-support"))]
pub struct HDF5TensorDataset {
    _phantom: PhantomData<()>,
}

#[cfg(feature = "hdf5-support")]
impl HDF5TensorDataset {
    /// Create a new HDF5 dataset
    pub fn new<P: AsRef<std::path::Path>>(file_path: P, dataset_path: &str) -> Result<Self> {
        let file_path = file_path.as_ref();
        utils::validate_dataset_path(file_path, "HDF5 file")?;
        utils::validate_file_extension(file_path, &["h5", "hdf5", "hdf"])?;

        let file = File::open(file_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open HDF5 file: {}", e)))?;

        // Validate dataset exists
        let dataset = file.dataset(dataset_path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to open dataset '{}': {}", dataset_path, e))
        })?;

        let shape = dataset.shape();

        Ok(Self {
            file,
            dataset_path: dataset_path.to_string(),
            shape,
            chunk_size: None,
            cache_size: 100, // Default cache size
        })
    }

    /// Set chunk size for reading large datasets
    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
        self.chunk_size = Some(chunk_size);
        self
    }

    /// Set cache size for performance
    pub fn with_cache_size(mut self, cache_size: usize) -> Self {
        self.cache_size = cache_size;
        self
    }

    /// Read entire dataset as tensor
    pub fn read_full<T: TensorElement + H5Type>(&self) -> Result<Tensor<T>> {
        let dataset = self
            .file
            .dataset(&self.dataset_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open dataset: {}", e)))?;

        let data: Vec<T> = dataset.read_raw().map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to read HDF5 dataset: {}", e))
        })?;

        torsh_tensor::Tensor::from_data(data, self.shape.clone(), DeviceType::Cpu)
    }

    /// Read a slice of the dataset
    pub fn read_slice<T: TensorElement + H5Type>(
        &self,
        start: &[usize],
        count: &[usize],
    ) -> Result<Tensor<T>> {
        if start.len() != self.shape.len() || count.len() != self.shape.len() {
            return Err(TorshError::InvalidArgument(
                "Start and count must have same dimensionality as dataset".to_string(),
            ));
        }

        let dataset = self
            .file
            .dataset(&self.dataset_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open dataset: {}", e)))?;

        // Create selection
        let selection = dataset
            .read_slice_1d::<T, _>(start[0]..start[0] + count[0])
            .map_err(|e| {
                TorshError::InvalidArgument(format!("Failed to read HDF5 slice: {}", e))
            })?;

        torsh_tensor::Tensor::from_data(selection.to_vec(), count.to_vec(), DeviceType::Cpu)
    }

    /// Write tensor to HDF5 file
    pub fn write_tensor<T: TensorElement + H5Type>(
        file_path: &std::path::Path,
        dataset_path: &str,
        tensor: &Tensor<T>,
    ) -> Result<()> {
        let file = File::create(file_path).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create HDF5 file: {}", e))
        })?;

        let data = tensor.to_vec()?;
        let shape = tensor.shape().dims().to_vec();

        let dataset = file
            .new_dataset::<T>()
            .shape(shape)
            .create(dataset_path)
            .map_err(|e| {
                TorshError::InvalidArgument(format!("Failed to create HDF5 dataset: {}", e))
            })?;

        dataset.write_raw(&data).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to write HDF5 dataset: {}", e))
        })?;

        Ok(())
    }

    /// List all datasets in the file
    pub fn list_datasets(&self) -> Result<Vec<String>> {
        let mut datasets = Vec::new();
        self.collect_datasets_recursive(&self.file, "", &mut datasets)?;
        Ok(datasets)
    }

    fn collect_datasets_recursive(
        &self,
        group: &Group,
        prefix: &str,
        datasets: &mut Vec<String>,
    ) -> Result<()> {
        for name in group.member_names().map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to list group members: {}", e))
        })? {
            let full_path = if prefix.is_empty() {
                name.clone()
            } else {
                format!("{}/{}", prefix, name)
            };

            if group.dataset(&name).is_ok() {
                datasets.push(full_path);
            } else if let Ok(subgroup) = group.group(&name) {
                self.collect_datasets_recursive(&subgroup, &full_path, datasets)?;
            }
        }
        Ok(())
    }

    /// Get dataset metadata
    pub fn get_metadata(&self) -> Result<HDF5Metadata> {
        let dataset = self
            .file
            .dataset(&self.dataset_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open dataset: {}", e)))?;

        Ok(HDF5Metadata {
            shape: dataset.shape(),
            dtype: format!("{:?}", dataset.dtype()),
            size: dataset.size(),
            chunks: dataset.chunk(),
        })
    }
}

#[cfg(not(feature = "hdf5-support"))]
impl HDF5TensorDataset {
    /// Placeholder when HDF5 is not available
    pub fn new<P: AsRef<std::path::Path>>(_file_path: P, _dataset_path: &str) -> Result<Self> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled. Enable 'hdf5-support' feature flag.".to_string(),
        ))
    }

    /// Placeholder for reading
    pub fn read_full<T>(&self) -> Result<Tensor<T>> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled".to_string(),
        ))
    }
}

/// HDF5 dataset metadata
#[derive(Debug, Clone)]
pub struct HDF5Metadata {
    pub shape: Vec<usize>,
    pub dtype: String,
    pub size: usize,
    pub chunks: Option<Vec<usize>>,
}

#[cfg(feature = "hdf5-support")]
impl Dataset for HDF5TensorDataset {
    type Item = Vec<f32>; // Simplified for now

    fn len(&self) -> usize {
        if self.shape.is_empty() {
            0
        } else {
            self.shape[0]
        }
    }

    fn get(&self, index: usize) -> Result<Self::Item> {
        if index >= self.len() {
            return Err(utils::errors::invalid_index(index, self.len()));
        }

        let dataset = self
            .file
            .dataset(&self.dataset_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open dataset: {}", e)))?;

        // Read a single row/sample
        let row_data: Vec<f32> = dataset
            .read_slice_1d(index..index + 1)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to read HDF5 row: {}", e)))?
            .to_vec();

        Ok(row_data)
    }
}

#[cfg(not(feature = "hdf5-support"))]
impl Dataset for HDF5TensorDataset {
    type Item = ();

    fn len(&self) -> usize {
        0
    }

    fn get(&self, _index: usize) -> Result<Self::Item> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled".to_string(),
        ))
    }
}

/// Utility functions for HDF5 integration
pub mod hdf5_utils {
    use super::*;

    /// Check if HDF5 feature is available at compile time
    pub const fn is_hdf5_available() -> bool {
        cfg!(feature = "hdf5-support")
    }

    /// Create a sample HDF5 file for testing
    #[cfg(feature = "hdf5-support")]
    pub fn create_sample_file<P: AsRef<std::path::Path>>(path: P) -> Result<()> {
        use torsh_tensor::creation::arange;

        let tensor = arange::<f32>(0.0, 100.0, 1.0)
            .expect("failed to create arange tensor for sample HDF5 file")
            .reshape(&[10, 10])
            .expect("failed to reshape tensor to 10x10 for sample HDF5 file");

        HDF5TensorDataset::write_tensor(path.as_ref(), "data", &tensor)?;

        Ok(())
    }

    /// Batch read multiple datasets from HDF5 file
    #[cfg(feature = "hdf5-support")]
    pub fn batch_read_datasets<T: TensorElement + H5Type>(
        file_path: &std::path::Path,
        dataset_paths: &[&str],
    ) -> Result<Vec<Tensor<T>>> {
        let file = File::open(file_path)
            .map_err(|e| TorshError::InvalidArgument(format!("Failed to open HDF5 file: {}", e)))?;

        let mut tensors = Vec::new();

        for &dataset_path in dataset_paths {
            let dataset = file.dataset(dataset_path).map_err(|e| {
                TorshError::InvalidArgument(format!(
                    "Failed to open dataset '{}': {}",
                    dataset_path, e
                ))
            })?;

            let data: Vec<T> = dataset.read_raw().map_err(|e| {
                TorshError::InvalidArgument(format!(
                    "Failed to read dataset '{}': {}",
                    dataset_path, e
                ))
            })?;

            let shape = dataset.shape();
            let tensor = torsh_tensor::Tensor::from_data(data, shape, DeviceType::Cpu)?;
            tensors.push(tensor);
        }

        Ok(tensors)
    }

    /// Convert HDF5 file to multiple tensor files
    #[cfg(feature = "hdf5-support")]
    pub fn hdf5_to_tensors<T: TensorElement + H5Type>(
        hdf5_path: &std::path::Path,
        output_dir: &std::path::Path,
    ) -> Result<()> {
        let dataset = HDF5TensorDataset::new(hdf5_path, "data")?;
        let datasets = dataset.list_datasets()?;

        std::fs::create_dir_all(output_dir).map_err(|e| {
            TorshError::InvalidArgument(format!("Failed to create output directory: {}", e))
        })?;

        for dataset_path in datasets {
            let tensor: Tensor<T> = dataset.read_full()?;

            // Save as tensor file (simplified)
            let filename = dataset_path.replace("/", "_") + ".tensor";
            let output_path = output_dir.join(filename);

            // Here you would save the tensor in your preferred format
            // For now, we'll save the shape and data info
            let info = format!(
                "Shape: {:?}, Size: {}",
                tensor.shape().dims(),
                tensor.numel()
            );
            std::fs::write(output_path, info).map_err(|e| {
                TorshError::InvalidArgument(format!("Failed to write tensor info: {}", e))
            })?;
        }

        Ok(())
    }

    #[cfg(not(feature = "hdf5-support"))]
    pub fn create_sample_file<P: AsRef<std::path::Path>>(_path: P) -> Result<()> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled".to_string(),
        ))
    }

    #[cfg(not(feature = "hdf5-support"))]
    pub fn batch_read_datasets<T>(
        _file_path: &std::path::Path,
        _dataset_paths: &[&str],
    ) -> Result<Vec<Tensor<T>>> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled".to_string(),
        ))
    }

    #[cfg(not(feature = "hdf5-support"))]
    pub fn hdf5_to_tensors<T>(
        _hdf5_path: &std::path::Path,
        _output_dir: &std::path::Path,
    ) -> Result<()> {
        Err(TorshError::InvalidArgument(
            "HDF5 support not enabled".to_string(),
        ))
    }
}

/// Builder for HDF5 datasets with configuration options
pub struct HDF5DatasetBuilder {
    file_path: std::path::PathBuf,
    dataset_path: String,
    chunk_size: Option<usize>,
    cache_size: usize,
    read_only: bool,
}

impl HDF5DatasetBuilder {
    /// Create a new HDF5 dataset builder
    pub fn new<P: AsRef<std::path::Path>>(file_path: P, dataset_path: &str) -> Self {
        Self {
            file_path: file_path.as_ref().to_path_buf(),
            dataset_path: dataset_path.to_string(),
            chunk_size: None,
            cache_size: 100,
            read_only: true,
        }
    }

    /// Set chunk size for reading
    pub fn chunk_size(mut self, size: usize) -> Self {
        self.chunk_size = Some(size);
        self
    }

    /// Set cache size
    pub fn cache_size(mut self, size: usize) -> Self {
        self.cache_size = size;
        self
    }

    /// Enable write mode
    pub fn writable(mut self) -> Self {
        self.read_only = false;
        self
    }

    /// Build the dataset
    pub fn build(self) -> Result<HDF5TensorDataset> {
        let mut dataset = HDF5TensorDataset::new(&self.file_path, &self.dataset_path)?;

        if let Some(chunk_size) = self.chunk_size {
            dataset = dataset.with_chunk_size(chunk_size);
        }

        dataset = dataset.with_cache_size(self.cache_size);

        Ok(dataset)
    }
}

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

    #[test]
    fn test_hdf5_availability() {
        assert!(hdf5_utils::is_hdf5_available() || !hdf5_utils::is_hdf5_available());
    }

    #[cfg(feature = "hdf5-support")]
    #[test]
    fn test_hdf5_dataset_builder() {
        let builder = HDF5DatasetBuilder::new("test.h5", "data")
            .chunk_size(1000)
            .cache_size(200)
            .writable();

        // Would need an actual HDF5 file to test building
        assert_eq!(builder.chunk_size, Some(1000));
        assert_eq!(builder.cache_size, 200);
        assert!(!builder.read_only);
    }

    #[cfg(not(feature = "hdf5-support"))]
    #[test]
    fn test_hdf5_disabled() {
        let result = HDF5TensorDataset::new("test.h5", "data");
        assert!(result.is_err());
    }
}