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
//! Download and extraction utilities for dataset files

use std::fs::File;
use std::io::{BufReader, Read, Write};
use std::path::Path;
use tenflowers_core::{Result, TensorError};

use super::common::{error_utils, ProgressTracker};

#[cfg(feature = "download")]
use reqwest::blocking::Client;

#[cfg(feature = "download")]
use oxiarc_archive::{GzipReader, TarReader};

/// Download utilities for dataset files
pub struct Downloader {
    #[cfg(feature = "download")]
    client: Client,
}

impl Downloader {
    /// Create a new downloader
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "download")]
            client: Client::builder()
                .timeout(std::time::Duration::from_secs(300)) // 5 minutes timeout
                .user_agent("tenflowers-dataset/0.1.1")
                .build()
                .unwrap_or_else(|_| Client::new()),
        }
    }

    /// Download a file from URL to destination
    #[cfg(feature = "download")]
    pub fn download_file(&self, url: &str, dest_path: &Path, description: &str) -> Result<()> {
        println!("Downloading {}: {}", description, url);

        let response = self.client.get(url).send().map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, e),
                &format!("Failed to start download from {}", url),
            )
        })?;

        if !response.status().is_success() {
            return Err(TensorError::invalid_argument(format!(
                "Failed to download {}: HTTP {}",
                description,
                response.status()
            )));
        }

        let total_size = response.content_length().unwrap_or(0);
        let mut tracker = ProgressTracker::new(total_size, format!("Downloading {}", description));

        let mut file = File::create(dest_path).map_err(|e| {
            error_utils::io_error_with_context(e, "Failed to create destination file")
        })?;

        let mut downloaded = 0u64;
        let mut buffer = [0; 8192];
        let mut reader = BufReader::new(response);

        loop {
            let bytes_read = reader.read(&mut buffer).map_err(|e| {
                error_utils::io_error_with_context(e, "Failed to read from download stream")
            })?;

            if bytes_read == 0 {
                break;
            }

            file.write_all(&buffer[..bytes_read]).map_err(|e| {
                error_utils::io_error_with_context(e, "Failed to write to destination file")
            })?;

            downloaded += bytes_read as u64;
            tracker.update(downloaded);
        }

        tracker.complete();
        println!("{} downloaded successfully!", description);
        Ok(())
    }

    /// Download a file when download feature is disabled
    #[cfg(not(feature = "download"))]
    pub fn download_file(&self, _url: &str, _dest_path: &Path, description: &str) -> Result<()> {
        Err(TensorError::invalid_argument(format!(
            "Download feature not enabled. Please enable the 'download' feature or manually download {} files.",
            description
        )))
    }

    /// Extract a gzipped file
    #[cfg(feature = "download")]
    pub fn extract_gzip(&self, gz_path: &Path, dest_path: &Path, description: &str) -> Result<()> {
        println!("Extracting {}", description);

        let gz_file = File::open(gz_path)
            .map_err(|e| error_utils::io_error_with_context(e, "Failed to open gzip file"))?;

        let mut gzip_reader = GzipReader::new(gz_file).map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                "Failed to open gzip file",
            )
        })?;
        let decompressed = gzip_reader.decompress().map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                "Failed to extract gzip file",
            )
        })?;
        let mut dest_file = File::create(dest_path).map_err(|e| {
            error_utils::io_error_with_context(e, "Failed to create destination file")
        })?;
        dest_file
            .write_all(&decompressed)
            .map_err(|e| error_utils::io_error_with_context(e, "Failed to write extracted file"))?;

        println!("{} extracted successfully!", description);
        Ok(())
    }

    /// Extract a gzipped file when download feature is disabled
    #[cfg(not(feature = "download"))]
    pub fn extract_gzip(
        &self,
        _gz_path: &Path,
        _dest_path: &Path,
        description: &str,
    ) -> Result<()> {
        Err(TensorError::invalid_argument(format!(
            "Download feature not enabled. Cannot extract {} files.",
            description
        )))
    }

    /// Extract a tar.gz archive
    #[cfg(feature = "download")]
    pub fn extract_tar_gz(
        &self,
        tar_gz_path: &Path,
        dest_dir: &Path,
        description: &str,
    ) -> Result<()> {
        println!("Extracting {} archive", description);

        let tar_gz_file = File::open(tar_gz_path)
            .map_err(|e| error_utils::io_error_with_context(e, "Failed to open tar.gz file"))?;

        let mut gzip_reader = GzipReader::new(tar_gz_file).map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                "Failed to open gzip file",
            )
        })?;
        let decompressed = gzip_reader.decompress().map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                "Failed to decompress tar.gz file",
            )
        })?;
        let mut tar_reader = TarReader::new(std::io::Cursor::new(decompressed)).map_err(|e| {
            error_utils::io_error_with_context(
                std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                "Failed to parse tar archive",
            )
        })?;
        // Clone entries first to avoid borrow conflict (Entry: Clone)
        let entries = tar_reader.entries().to_vec();
        for entry in &entries {
            let dest_path = dest_dir.join(&entry.name);
            if entry.name.ends_with('/') {
                std::fs::create_dir_all(&dest_path).map_err(|e| {
                    error_utils::io_error_with_context(e, "Failed to create directory")
                })?;
            } else {
                if let Some(parent) = dest_path.parent() {
                    std::fs::create_dir_all(parent).map_err(|e| {
                        error_utils::io_error_with_context(e, "Failed to create parent directory")
                    })?;
                }
                let data = tar_reader.extract_to_vec(entry).map_err(|e| {
                    error_utils::io_error_with_context(
                        std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                        "Failed to extract tar entry",
                    )
                })?;
                std::fs::write(&dest_path, &data).map_err(|e| {
                    error_utils::io_error_with_context(e, "Failed to write extracted file")
                })?;
            }
        }

        println!("{} archive extracted successfully!", description);
        Ok(())
    }

    /// Extract a tar.gz archive when download feature is disabled
    #[cfg(not(feature = "download"))]
    pub fn extract_tar_gz(
        &self,
        _tar_gz_path: &Path,
        _dest_dir: &Path,
        description: &str,
    ) -> Result<()> {
        Err(TensorError::invalid_argument(format!(
            "Download feature not enabled. Cannot extract {} archive.",
            description
        )))
    }

    /// Download and extract MNIST files
    pub fn download_mnist(&self, mnist_dir: &Path, train: bool) -> Result<()> {
        use super::common::{
            MNIST_TEST_IMAGES_URL, MNIST_TEST_LABELS_URL, MNIST_TRAIN_IMAGES_URL,
            MNIST_TRAIN_LABELS_URL,
        };

        let (images_url, labels_url, images_file, labels_file) = if train {
            (
                MNIST_TRAIN_IMAGES_URL,
                MNIST_TRAIN_LABELS_URL,
                "train-images-idx3-ubyte.gz",
                "train-labels-idx1-ubyte.gz",
            )
        } else {
            (
                MNIST_TEST_IMAGES_URL,
                MNIST_TEST_LABELS_URL,
                "t10k-images-idx3-ubyte.gz",
                "t10k-labels-idx1-ubyte.gz",
            )
        };

        let images_gz_path = mnist_dir.join(images_file);
        let labels_gz_path = mnist_dir.join(labels_file);

        // Download compressed files
        self.download_file(images_url, &images_gz_path, "MNIST images")?;
        self.download_file(labels_url, &labels_gz_path, "MNIST labels")?;

        // Extract files
        let images_path = mnist_dir.join(images_file.trim_end_matches(".gz"));
        let labels_path = mnist_dir.join(labels_file.trim_end_matches(".gz"));

        self.extract_gzip(&images_gz_path, &images_path, "MNIST images")?;
        self.extract_gzip(&labels_gz_path, &labels_path, "MNIST labels")?;

        // Clean up compressed files
        let _ = std::fs::remove_file(images_gz_path);
        let _ = std::fs::remove_file(labels_gz_path);

        Ok(())
    }

    /// Download and extract CIFAR-10 files
    pub fn download_cifar10(&self, cifar_dir: &Path) -> Result<()> {
        use super::common::CIFAR10_URL;

        let tar_gz_path = cifar_dir.join("cifar-10-binary.tar.gz");

        // Download archive
        self.download_file(CIFAR10_URL, &tar_gz_path, "CIFAR-10 dataset")?;

        // Extract archive
        self.extract_tar_gz(&tar_gz_path, cifar_dir, "CIFAR-10")?;

        // Clean up archive
        let _ = std::fs::remove_file(tar_gz_path);

        Ok(())
    }

    /// Download and extract ImageNet validation files
    pub fn download_imagenet_val(&self, imagenet_dir: &Path) -> Result<()> {
        use super::common::{IMAGENET_LABELS_URL, IMAGENET_VAL_URL};

        let val_tar_path = imagenet_dir.join("ILSVRC2012_img_val.tar");
        let labels_path = imagenet_dir.join("ILSVRC2012_validation_ground_truth.txt");

        // Download files
        self.download_file(
            IMAGENET_VAL_URL,
            &val_tar_path,
            "ImageNet validation images",
        )?;
        self.download_file(
            IMAGENET_LABELS_URL,
            &labels_path,
            "ImageNet validation labels",
        )?;

        // Extract validation images (tar, not tar.gz)
        #[cfg(feature = "download")]
        {
            let val_images_dir = imagenet_dir.join("val");
            std::fs::create_dir_all(&val_images_dir).map_err(|e| {
                error_utils::io_error_with_context(
                    e,
                    "Failed to create validation images directory",
                )
            })?;

            let tar_file = File::open(&val_tar_path).map_err(|e| {
                error_utils::io_error_with_context(e, "Failed to open validation tar file")
            })?;

            let mut tar_reader = TarReader::new(tar_file).map_err(|e| {
                error_utils::io_error_with_context(
                    std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                    "Failed to parse tar archive",
                )
            })?;
            // Clone entries first to avoid borrow conflict (Entry: Clone)
            let entries = tar_reader.entries().to_vec();
            for entry in &entries {
                let dest_path = val_images_dir.join(&entry.name);
                if entry.name.ends_with('/') {
                    std::fs::create_dir_all(&dest_path).map_err(|e| {
                        error_utils::io_error_with_context(e, "Failed to create directory")
                    })?;
                } else {
                    if let Some(parent) = dest_path.parent() {
                        std::fs::create_dir_all(parent).map_err(|e| {
                            error_utils::io_error_with_context(
                                e,
                                "Failed to create parent directory",
                            )
                        })?;
                    }
                    let data = tar_reader.extract_to_vec(entry).map_err(|e| {
                        error_utils::io_error_with_context(
                            std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)),
                            "Failed to extract tar entry",
                        )
                    })?;
                    std::fs::write(&dest_path, &data).map_err(|e| {
                        error_utils::io_error_with_context(e, "Failed to write extracted file")
                    })?;
                }
            }

            // Clean up tar file
            let _ = std::fs::remove_file(val_tar_path);
        }

        Ok(())
    }

    /// Download IMDB dataset
    pub fn download_imdb(&self, imdb_dir: &Path) -> Result<()> {
        use super::common::IMDB_URL;

        let tar_gz_path = imdb_dir.join("aclImdb_v1.tar.gz");

        // Download archive
        self.download_file(IMDB_URL, &tar_gz_path, "IMDB dataset")?;

        // Extract archive
        self.extract_tar_gz(&tar_gz_path, imdb_dir, "IMDB")?;

        // Clean up archive
        let _ = std::fs::remove_file(tar_gz_path);

        Ok(())
    }

    /// Download AG News dataset
    pub fn download_ag_news(&self, ag_news_dir: &Path) -> Result<()> {
        use super::common::{AG_NEWS_TEST_URL, AG_NEWS_TRAIN_URL};

        let train_path = ag_news_dir.join("train.csv");
        let test_path = ag_news_dir.join("test.csv");

        // Download CSV files
        self.download_file(AG_NEWS_TRAIN_URL, &train_path, "AG News training data")?;
        self.download_file(AG_NEWS_TEST_URL, &test_path, "AG News test data")?;

        Ok(())
    }
}

impl Default for Downloader {
    fn default() -> Self {
        Self::new()
    }
}

/// Utility function to get file size
pub fn get_file_size(path: &Path) -> Result<u64> {
    let metadata = path
        .metadata()
        .map_err(|e| error_utils::io_error_with_context(e, "Failed to get file metadata"))?;
    Ok(metadata.len())
}

/// Utility function to verify checksum (simplified)
pub fn verify_checksum(path: &Path, expected_hash: Option<&str>) -> Result<bool> {
    if expected_hash.is_none() {
        return Ok(true); // Skip verification if no hash provided
    }

    // For now, just return true. In a real implementation, you would
    // compute and verify the actual checksum (MD5, SHA256, etc.)
    let _file_size = get_file_size(path)?;

    println!("Checksum verification skipped (not implemented)");
    Ok(true)
}

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

    #[test]
    fn test_downloader_creation() {
        let downloader = Downloader::new();
        // Just verify it can be created without panicking
        drop(downloader);
    }

    #[test]
    fn test_get_file_size() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let test_file = temp_dir.path().join("test.txt");

        // Create a test file
        std::fs::write(&test_file, b"Hello, World!").expect("test: write should succeed");

        let size = get_file_size(&test_file).expect("test: operation should succeed");
        assert_eq!(size, 13); // "Hello, World!" is 13 bytes
    }

    #[test]
    fn test_verify_checksum_no_hash() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let test_file = temp_dir.path().join("test.txt");

        // Create a test file
        std::fs::write(&test_file, b"test").expect("test: write should succeed");

        let result = verify_checksum(&test_file, None).expect("test: operation should succeed");
        assert!(result);
    }

    #[test]
    fn test_verify_checksum_with_hash() {
        let temp_dir = TempDir::new().expect("test: temp dir creation should succeed");
        let test_file = temp_dir.path().join("test.txt");

        // Create a test file
        std::fs::write(&test_file, b"test").expect("test: write should succeed");

        // For now, this should always return true
        let result = verify_checksum(&test_file, Some("dummy_hash"))
            .expect("test: operation should succeed");
        assert!(result);
    }
}