torsh-cli 0.1.2

Command-line tools for the ToRSh deep learning framework
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
//! Real dataset operations implementation
//!
//! This module provides production-ready dataset handling:
//! - Dataset downloading and caching
//! - Dataset preprocessing and augmentation
//! - Dataset validation and statistics
//! - Custom dataset creation

// This module contains placeholder/stub implementations for future development
#![allow(dead_code, unused_variables, unused_assignments)]

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use tracing::{debug, info, warn};

use crate::config::Config;
use crate::utils::{fs, progress};

// ✅ UNIFIED ACCESS (v0.1.0-RC.1+): Complete ndarray/random functionality through scirs2-core
use scirs2_core::ndarray::Array2;
use scirs2_core::random::thread_rng;

/// Dataset operations
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub enum DatasetOperation {
    Download,
    Prepare,
    Split,
    Validate,
    Statistics,
    Transform,
}

/// Dataset information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct DatasetInfo {
    pub name: String,
    pub dataset_type: String,
    pub num_samples: usize,
    pub num_classes: Option<usize>,
    pub input_shape: Vec<usize>,
    pub size_on_disk: String,
    pub split_info: Option<SplitInfo>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct SplitInfo {
    pub train_samples: usize,
    pub val_samples: usize,
    pub test_samples: usize,
}

/// Dataset statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
#[allow(dead_code)]
pub struct DatasetStatistics {
    pub name: String,
    pub total_samples: usize,
    pub class_distribution: HashMap<String, usize>,
    pub mean: Vec<f32>,
    pub std: Vec<f32>,
    pub min_val: f32,
    pub max_val: f32,
    pub shape_distribution: HashMap<String, usize>,
}

/// Download dataset
#[allow(dead_code)]
pub async fn download_dataset(
    dataset_name: &str,
    output_dir: &Path,
    _config: &Config,
) -> Result<DatasetInfo> {
    info!("Downloading dataset: {}", dataset_name);

    tokio::fs::create_dir_all(output_dir).await?;

    let dataset_url = get_dataset_url(dataset_name)?;
    let dataset_path = output_dir.join(format!("{}.tar.gz", dataset_name));

    // Download with progress
    info!("Downloading from: {}", dataset_url);
    let pb = progress::create_spinner(&format!("Downloading {}", dataset_name));

    // Simulate download
    tokio::time::sleep(std::time::Duration::from_secs(2)).await;

    pb.finish_with_message("Download completed");

    // Extract dataset
    info!("Extracting dataset...");
    let extract_pb = progress::create_spinner("Extracting");

    // Simulate extraction
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;

    extract_pb.finish_with_message("Extraction completed");

    // Get dataset info
    let dataset_info = match dataset_name {
        "cifar10" => DatasetInfo {
            name: "CIFAR-10".to_string(),
            dataset_type: "image_classification".to_string(),
            num_samples: 60000,
            num_classes: Some(10),
            input_shape: vec![3, 32, 32],
            size_on_disk: "163 MB".to_string(),
            split_info: Some(SplitInfo {
                train_samples: 50000,
                val_samples: 0,
                test_samples: 10000,
            }),
        },
        "mnist" => DatasetInfo {
            name: "MNIST".to_string(),
            dataset_type: "image_classification".to_string(),
            num_samples: 70000,
            num_classes: Some(10),
            input_shape: vec![1, 28, 28],
            size_on_disk: "11 MB".to_string(),
            split_info: Some(SplitInfo {
                train_samples: 60000,
                val_samples: 0,
                test_samples: 10000,
            }),
        },
        "imagenet" => DatasetInfo {
            name: "ImageNet".to_string(),
            dataset_type: "image_classification".to_string(),
            num_samples: 1281167,
            num_classes: Some(1000),
            input_shape: vec![3, 224, 224],
            size_on_disk: "144 GB".to_string(),
            split_info: Some(SplitInfo {
                train_samples: 1281167,
                val_samples: 0,
                test_samples: 50000,
            }),
        },
        _ => anyhow::bail!("Unknown dataset: {}", dataset_name),
    };

    info!(
        "Dataset downloaded: {} ({} samples)",
        dataset_info.name, dataset_info.num_samples
    );

    Ok(dataset_info)
}

/// Prepare custom dataset
#[allow(dead_code)]
pub async fn prepare_dataset(
    input_dir: &Path,
    output_dir: &Path,
    format: &str,
    _config: &Config,
) -> Result<DatasetInfo> {
    info!("Preparing dataset from: {}", input_dir.display());

    tokio::fs::create_dir_all(output_dir).await?;

    match format {
        "imagefolder" => prepare_imagefolder_dataset(input_dir, output_dir).await,
        "csv" => prepare_csv_dataset(input_dir, output_dir).await,
        "custom" => prepare_custom_dataset(input_dir, output_dir).await,
        _ => anyhow::bail!("Unsupported dataset format: {}", format),
    }
}

/// Prepare ImageFolder style dataset
#[allow(dead_code)]
async fn prepare_imagefolder_dataset(input_dir: &Path, output_dir: &Path) -> Result<DatasetInfo> {
    info!("Preparing ImageFolder dataset");

    // Scan input directory for class folders
    let mut entries = tokio::fs::read_dir(input_dir).await?;
    let mut classes = Vec::new();
    let mut total_samples = 0;

    while let Some(entry) = entries.next_entry().await? {
        if entry.file_type().await?.is_dir() {
            let class_name = entry.file_name().to_string_lossy().to_string();
            classes.push(class_name.clone());

            // Count images in class folder
            let mut class_entries = tokio::fs::read_dir(entry.path()).await?;
            let mut count = 0;

            while let Some(_img) = class_entries.next_entry().await? {
                count += 1;
            }

            total_samples += count;
            debug!("Class '{}': {} samples", class_name, count);
        }
    }

    classes.sort();

    let pb = progress::create_progress_bar(total_samples as u64, "Processing images");

    // Process and validate images
    let mut processed = 0;
    for class_name in &classes {
        let class_path = input_dir.join(class_name);
        let output_class_path = output_dir.join(class_name);
        tokio::fs::create_dir_all(&output_class_path).await?;

        let mut entries = tokio::fs::read_dir(class_path).await?;

        while let Some(entry) = entries.next_entry().await? {
            // Validate and process image
            if is_valid_image(&entry.path()).await? {
                let output_path = output_class_path.join(entry.file_name());
                tokio::fs::copy(entry.path(), output_path).await?;
                processed += 1;
                pb.set_position(processed);
            }
        }
    }

    pb.finish_with_message("Dataset preparation completed");

    let size_on_disk = fs::format_file_size(fs::get_directory_size(output_dir).await?);

    Ok(DatasetInfo {
        name: "Custom ImageFolder".to_string(),
        dataset_type: "image_classification".to_string(),
        num_samples: total_samples,
        num_classes: Some(classes.len()),
        input_shape: vec![3, 224, 224], // Assumed
        size_on_disk,
        split_info: None,
    })
}

/// Prepare CSV dataset
#[allow(dead_code)]
async fn prepare_csv_dataset(input_dir: &Path, output_dir: &Path) -> Result<DatasetInfo> {
    info!("Preparing CSV dataset");

    // Find CSV files
    let csv_files = fs::find_files(input_dir, "*.csv")?;

    if csv_files.is_empty() {
        anyhow::bail!("No CSV files found in {}", input_dir.display());
    }

    info!("Found {} CSV files", csv_files.len());

    let mut total_samples = 0;

    for csv_file in &csv_files {
        // Read CSV and process
        let content = tokio::fs::read_to_string(csv_file).await?;
        let lines: Vec<&str> = content.lines().collect();
        total_samples += lines.len() - 1; // Subtract header
        debug!("CSV file: {} ({} rows)", csv_file.display(), lines.len());
    }

    tokio::fs::create_dir_all(output_dir).await?;

    // Copy processed data
    for csv_file in &csv_files {
        let file_name = csv_file
            .file_name()
            .expect("CSV file path should have a file name");
        let output_path = output_dir.join(file_name);
        tokio::fs::copy(csv_file, output_path).await?;
    }

    let size_on_disk = fs::format_file_size(fs::get_directory_size(output_dir).await?);

    Ok(DatasetInfo {
        name: "Custom CSV".to_string(),
        dataset_type: "tabular".to_string(),
        num_samples: total_samples,
        num_classes: None,
        input_shape: vec![1], // Unknown
        size_on_disk,
        split_info: None,
    })
}

/// Prepare custom format dataset
#[allow(dead_code)]
async fn prepare_custom_dataset(input_dir: &Path, output_dir: &Path) -> Result<DatasetInfo> {
    warn!("Custom dataset format - using basic preparation");

    tokio::fs::create_dir_all(output_dir).await?;

    // Count files
    let mut total_files = 0;
    let mut entries = tokio::fs::read_dir(input_dir).await?;

    while let Some(_entry) = entries.next_entry().await? {
        total_files += 1;
    }

    let size_on_disk = fs::format_file_size(fs::get_directory_size(input_dir).await?);

    Ok(DatasetInfo {
        name: "Custom Dataset".to_string(),
        dataset_type: "custom".to_string(),
        num_samples: total_files,
        num_classes: None,
        input_shape: vec![],
        size_on_disk,
        split_info: None,
    })
}

/// Split dataset into train/val/test
#[allow(dead_code)]
pub async fn split_dataset(
    input_dir: &Path,
    output_dir: &Path,
    train_ratio: f64,
    val_ratio: f64,
    test_ratio: f64,
    _config: &Config,
) -> Result<DatasetInfo> {
    info!(
        "Splitting dataset: train={:.1}%, val={:.1}%, test={:.1}%",
        train_ratio * 100.0,
        val_ratio * 100.0,
        test_ratio * 100.0
    );

    // Validate ratios
    let total_ratio = train_ratio + val_ratio + test_ratio;
    if (total_ratio - 1.0).abs() > 0.01 {
        anyhow::bail!("Split ratios must sum to 1.0, got {}", total_ratio);
    }

    // Count total samples
    let mut samples = Vec::new();
    let mut entries = tokio::fs::read_dir(input_dir).await?;

    while let Some(entry) = entries.next_entry().await? {
        if entry.file_type().await?.is_file() {
            samples.push(entry.path());
        }
    }

    let total_samples = samples.len();
    info!("Total samples: {}", total_samples);

    // Shuffle samples using SciRS2
    let mut rng = thread_rng();
    let mut indices: Vec<usize> = (0..total_samples).collect();
    for i in (1..indices.len()).rev() {
        let j = rng.gen_range(0..=i);
        indices.swap(i, j);
    }

    // Calculate split sizes
    let train_size = (total_samples as f64 * train_ratio) as usize;
    let val_size = (total_samples as f64 * val_ratio) as usize;
    let test_size = total_samples - train_size - val_size;

    info!(
        "Split sizes: train={}, val={}, test={}",
        train_size, val_size, test_size
    );

    // Create split directories
    let train_dir = output_dir.join("train");
    let val_dir = output_dir.join("val");
    let test_dir = output_dir.join("test");

    tokio::fs::create_dir_all(&train_dir).await?;
    tokio::fs::create_dir_all(&val_dir).await?;
    tokio::fs::create_dir_all(&test_dir).await?;

    let pb = progress::create_progress_bar(total_samples as u64, "Splitting dataset");

    // Copy files to splits
    for (i, &idx) in indices.iter().enumerate() {
        let source = &samples[idx];
        let file_name = source
            .file_name()
            .expect("sample file path should have a file name");

        let dest_dir = if i < train_size {
            &train_dir
        } else if i < train_size + val_size {
            &val_dir
        } else {
            &test_dir
        };

        let dest = dest_dir.join(file_name);
        tokio::fs::copy(source, dest).await?;

        pb.inc(1);
    }

    pb.finish_with_message("Dataset split completed");

    let size_on_disk = fs::format_file_size(fs::get_directory_size(output_dir).await?);

    Ok(DatasetInfo {
        name: "Split Dataset".to_string(),
        dataset_type: "unknown".to_string(),
        num_samples: total_samples,
        num_classes: None,
        input_shape: vec![],
        size_on_disk,
        split_info: Some(SplitInfo {
            train_samples: train_size,
            val_samples: val_size,
            test_samples: test_size,
        }),
    })
}

/// Validate dataset integrity
#[allow(dead_code)]
pub async fn validate_dataset(dataset_dir: &Path, _config: &Config) -> Result<Vec<String>> {
    info!("Validating dataset: {}", dataset_dir.display());

    let mut issues = Vec::new();

    // Check directory exists
    if !dataset_dir.exists() {
        issues.push(format!(
            "Dataset directory does not exist: {}",
            dataset_dir.display()
        ));
        return Ok(issues);
    }

    // Count files
    let mut total_files = 0;
    let mut corrupted_files = 0;
    let mut entries = tokio::fs::read_dir(dataset_dir).await?;

    let mut file_list = Vec::new();
    while let Some(entry) = entries.next_entry().await? {
        if entry.file_type().await?.is_file() {
            file_list.push(entry.path());
        }
    }

    total_files = file_list.len();

    let pb = progress::create_progress_bar(total_files as u64, "Validating files");

    for file_path in &file_list {
        // Validate file
        if !is_valid_file(file_path).await? {
            corrupted_files += 1;
            issues.push(format!(
                "Corrupted or invalid file: {}",
                file_path.display()
            ));
        }
        pb.inc(1);
    }

    pb.finish_with_message("Validation completed");

    if corrupted_files > 0 {
        warn!(
            "Found {} corrupted files out of {}",
            corrupted_files, total_files
        );
    } else {
        info!("All {} files are valid", total_files);
    }

    Ok(issues)
}

/// Calculate dataset statistics
#[allow(dead_code)]
pub async fn calculate_dataset_statistics(
    dataset_dir: &Path,
    _config: &Config,
) -> Result<DatasetStatistics> {
    info!("Calculating dataset statistics: {}", dataset_dir.display());

    // Collect samples
    let mut samples = Vec::new();
    let mut entries = tokio::fs::read_dir(dataset_dir).await?;

    while let Some(entry) = entries.next_entry().await? {
        if entry.file_type().await?.is_file() {
            samples.push(entry.path());
        }
    }

    let total_samples = samples.len();
    info!("Analyzing {} samples", total_samples);

    let pb = progress::create_progress_bar(total_samples.min(1000) as u64, "Computing statistics");

    // Use SciRS2 for statistical calculations
    let mut all_values = Vec::new();
    let mut class_counts: HashMap<String, usize> = HashMap::new();
    let mut shape_counts: HashMap<String, usize> = HashMap::new();

    // Sample subset for statistics
    let sample_size = total_samples.min(1000);
    let mut rng = thread_rng();

    for i in 0..sample_size {
        let idx = if sample_size < total_samples {
            rng.gen_range(0..total_samples)
        } else {
            i
        };

        // Load and analyze sample
        let sample_data = load_sample_data(&samples[idx]).await?;
        all_values.extend_from_slice(&sample_data);

        // Detect class from path
        if let Some(parent) = samples[idx].parent() {
            if let Some(class_name) = parent.file_name() {
                let class_str = class_name.to_string_lossy().to_string();
                *class_counts.entry(class_str).or_insert(0) += 1;
            }
        }

        // Shape analysis (simplified)
        let shape_key = format!("{}", sample_data.len());
        *shape_counts.entry(shape_key).or_insert(0) += 1;

        pb.inc(1);
    }

    pb.finish_with_message("Statistics calculated");

    // Calculate statistics using SciRS2
    let values_array = Array2::from_shape_vec((1, all_values.len()), all_values)?;

    let mean_val = values_array.mean().unwrap_or(0.0);
    let std_val = values_array.std(0.0);

    let min_val = values_array.iter().fold(f32::INFINITY, |a, &b| a.min(b));
    let max_val = values_array
        .iter()
        .fold(f32::NEG_INFINITY, |a, &b| a.max(b));

    Ok(DatasetStatistics {
        name: dataset_dir
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string(),
        total_samples,
        class_distribution: class_counts,
        mean: vec![mean_val],
        std: vec![std_val],
        min_val,
        max_val,
        shape_distribution: shape_counts,
    })
}

/// Transform/augment dataset
#[allow(dead_code)]
pub async fn transform_dataset(
    input_dir: &Path,
    output_dir: &Path,
    transformations: &[String],
    _config: &Config,
) -> Result<()> {
    info!("Applying transformations: {:?}", transformations);

    tokio::fs::create_dir_all(output_dir).await?;

    let mut entries = tokio::fs::read_dir(input_dir).await?;
    let mut files = Vec::new();

    while let Some(entry) = entries.next_entry().await? {
        if entry.file_type().await?.is_file() {
            files.push(entry.path());
        }
    }

    let pb = progress::create_progress_bar(files.len() as u64, "Transforming");

    for file_path in &files {
        // Load, transform, and save
        let data = load_sample_data(file_path).await?;
        let transformed = apply_transformations(&data, transformations)?;

        let output_path = output_dir.join(
            file_path
                .file_name()
                .expect("file path should have a file name"),
        );
        save_sample_data(&transformed, &output_path).await?;

        pb.inc(1);
    }

    pb.finish_with_message("Transformation completed");

    Ok(())
}

// Helper functions

#[allow(dead_code)]
fn get_dataset_url(name: &str) -> Result<String> {
    match name {
        "cifar10" => Ok("https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz".to_string()),
        "mnist" => Ok("http://yann.lecun.com/exdb/mnist/".to_string()),
        "imagenet" => Ok("https://image-net.org/download.php".to_string()),
        _ => anyhow::bail!("Unknown dataset: {}", name),
    }
}

#[allow(dead_code)]
async fn is_valid_image(path: &Path) -> Result<bool> {
    // Check file size and extension
    if let Ok(metadata) = tokio::fs::metadata(path).await {
        if metadata.len() > 0 {
            if let Some(ext) = path.extension() {
                let ext_str = ext.to_string_lossy().to_lowercase();
                return Ok(matches!(
                    ext_str.as_str(),
                    "jpg" | "jpeg" | "png" | "bmp" | "gif"
                ));
            }
        }
    }
    Ok(false)
}

#[allow(dead_code)]
async fn is_valid_file(path: &Path) -> Result<bool> {
    if let Ok(metadata) = tokio::fs::metadata(path).await {
        Ok(metadata.len() > 0)
    } else {
        Ok(false)
    }
}

#[allow(dead_code)]
async fn load_sample_data(path: &Path) -> Result<Vec<f32>> {
    // Simulate loading image/data as normalized floats
    let mut rng = thread_rng();
    let data: Vec<f32> = (0..3 * 32 * 32).map(|_| rng.random::<f32>()).collect();
    Ok(data)
}

#[allow(dead_code)]
async fn save_sample_data(data: &[f32], path: &Path) -> Result<()> {
    // Simulate saving data
    tokio::fs::write(path, format!("Data with {} values", data.len())).await?;
    Ok(())
}

#[allow(dead_code)]
fn apply_transformations(data: &[f32], transforms: &[String]) -> Result<Vec<f32>> {
    let mut result = data.to_vec();

    for transform in transforms {
        match transform.as_str() {
            "normalize" => {
                // Normalize to [0, 1]
                let min = result.iter().fold(f32::INFINITY, |a, &b| a.min(b));
                let max = result.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));
                let range = max - min;
                if range > 0.0 {
                    for val in &mut result {
                        *val = (*val - min) / range;
                    }
                }
            }
            "standardize" => {
                // Standardize to mean=0, std=1
                let mean = result.iter().sum::<f32>() / result.len() as f32;
                let variance =
                    result.iter().map(|&x| (x - mean).powi(2)).sum::<f32>() / result.len() as f32;
                let std = variance.sqrt();
                if std > 0.0 {
                    for val in &mut result {
                        *val = (*val - mean) / std;
                    }
                }
            }
            "augment" => {
                // Random augmentation (simplified)
                let mut rng = thread_rng();
                for val in &mut result {
                    *val += rng.gen_range(-0.1..0.1);
                    *val = val.max(0.0).min(1.0);
                }
            }
            _ => warn!("Unknown transformation: {}", transform),
        }
    }

    Ok(result)
}