scirs2-datasets 0.4.1

Datasets module for SciRS2 (scirs2-datasets)
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
//! Data sampling utilities for statistical analysis and machine learning
//!
//! This module provides various sampling strategies including random sampling,
//! stratified sampling, and importance-weighted sampling. These functions are
//! useful for creating representative subsets of datasets, bootstrap sampling,
//! and handling imbalanced data distributions.

use crate::error::{DatasetsError, Result};
use scirs2_core::ndarray::Array1;
use scirs2_core::random::prelude::*;
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::seq::SliceRandom;
use scirs2_core::random::Uniform;
use std::collections::HashMap;

/// Performs random sampling with or without replacement
///
/// This function creates random samples from a dataset using either bootstrap
/// sampling (with replacement) or standard random sampling (without replacement).
///
/// # Arguments
///
/// * `n_samples` - Total number of samples in the dataset
/// * `sample_size` - Number of samples to draw
/// * `replace` - Whether to sample with replacement (bootstrap)
/// * `random_seed` - Optional random seed for reproducible sampling
///
/// # Returns
///
/// A vector of indices representing the sampled data points
///
/// # Examples
///
/// ```rust
/// use scirs2_datasets::utils::random_sample;
///
/// // Sample 5 indices from 10 total samples without replacement
/// let indices = random_sample(10, 5, false, Some(42)).expect("Operation failed");
/// assert_eq!(indices.len(), 5);
/// assert!(indices.iter().all(|&i| i < 10));
///
/// // Bootstrap sampling (with replacement)
/// let bootstrap_indices = random_sample(10, 15, true, Some(42)).expect("Operation failed");
/// assert_eq!(bootstrap_indices.len(), 15);
/// ```
#[allow(dead_code)]
pub fn random_sample(
    n_samples: usize,
    sample_size: usize,
    replace: bool,
    random_seed: Option<u64>,
) -> Result<Vec<usize>> {
    if n_samples == 0 {
        return Err(DatasetsError::InvalidFormat(
            "Number of _samples must be > 0".to_string(),
        ));
    }

    if sample_size == 0 {
        return Err(DatasetsError::InvalidFormat(
            "Sample _size must be > 0".to_string(),
        ));
    }

    if !replace && sample_size > n_samples {
        return Err(DatasetsError::InvalidFormat(format!(
            "Cannot sample {sample_size} items from {n_samples} without replacement"
        )));
    }

    let mut rng = match random_seed {
        Some(_seed) => StdRng::seed_from_u64(_seed),
        None => {
            let mut r = thread_rng();
            StdRng::seed_from_u64(r.next_u64())
        }
    };

    let mut indices = Vec::with_capacity(sample_size);

    if replace {
        // Bootstrap sampling (with replacement)
        for _ in 0..sample_size {
            indices.push(rng.sample(Uniform::new(0, n_samples).expect("Operation failed")));
        }
    } else {
        // Sampling without replacement
        let mut available: Vec<usize> = (0..n_samples).collect();
        available.shuffle(&mut rng);
        indices.extend_from_slice(&available[0..sample_size]);
    }

    Ok(indices)
}

/// Performs stratified random sampling
///
/// Maintains the same class distribution in the sample as in the original dataset.
/// This is particularly useful for classification tasks where you want to ensure
/// that all classes are represented proportionally in your sample.
///
/// # Arguments
///
/// * `targets` - Target values for stratification
/// * `sample_size` - Number of samples to draw
/// * `random_seed` - Optional random seed for reproducible sampling
///
/// # Returns
///
/// A vector of indices representing the stratified sample
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::Array1;
/// use scirs2_datasets::utils::stratified_sample;
///
/// let targets = Array1::from(vec![0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
/// let indices = stratified_sample(&targets, 6, Some(42)).expect("Operation failed");
/// assert_eq!(indices.len(), 6);
///
/// // Check that the sample maintains class proportions
/// let mut class_counts = std::collections::HashMap::new();
/// for &idx in &indices {
///     let class = targets[idx] as i32;
///     *class_counts.entry(class).or_insert(0) += 1;
/// }
/// ```
#[allow(dead_code)]
pub fn stratified_sample(
    targets: &Array1<f64>,
    sample_size: usize,
    random_seed: Option<u64>,
) -> Result<Vec<usize>> {
    if targets.is_empty() {
        return Err(DatasetsError::InvalidFormat(
            "Targets array cannot be empty".to_string(),
        ));
    }

    if sample_size == 0 {
        return Err(DatasetsError::InvalidFormat(
            "Sample _size must be > 0".to_string(),
        ));
    }

    if sample_size > targets.len() {
        return Err(DatasetsError::InvalidFormat(format!(
            "Cannot sample {} items from {} total samples",
            sample_size,
            targets.len()
        )));
    }

    // Group indices by target class
    let mut class_indices: HashMap<i64, Vec<usize>> = HashMap::new();
    for (i, &target) in targets.iter().enumerate() {
        let class = target.round() as i64;
        class_indices.entry(class).or_default().push(i);
    }

    let mut rng = match random_seed {
        Some(_seed) => StdRng::seed_from_u64(_seed),
        None => {
            let mut r = thread_rng();
            StdRng::seed_from_u64(r.next_u64())
        }
    };

    let mut stratified_indices = Vec::new();
    let n_classes = class_indices.len();
    let base_samples_per_class = sample_size / n_classes;
    let remainder = sample_size % n_classes;

    let mut class_list: Vec<_> = class_indices.keys().cloned().collect();
    class_list.sort();

    for (i, &class) in class_list.iter().enumerate() {
        let class_samples = class_indices.get(&class).expect("Operation failed");
        let samples_for_this_class = if i < remainder {
            base_samples_per_class + 1
        } else {
            base_samples_per_class
        };

        if samples_for_this_class > class_samples.len() {
            return Err(DatasetsError::InvalidFormat(format!(
                "Class {} has only {} samples but needs {} for stratified sampling",
                class,
                class_samples.len(),
                samples_for_this_class
            )));
        }

        // Sample from this class
        let sampled_indices = random_sample(
            class_samples.len(),
            samples_for_this_class,
            false,
            Some(rng.next_u64()),
        )?;

        for &idx in &sampled_indices {
            stratified_indices.push(class_samples[idx]);
        }
    }

    stratified_indices.shuffle(&mut rng);
    Ok(stratified_indices)
}

/// Performs importance sampling based on provided weights
///
/// Samples indices according to the provided probability weights. Higher weights
/// increase the probability of selection. This is useful for adaptive sampling
/// where some samples are more important than others for training.
///
/// # Arguments
///
/// * `weights` - Probability weights for each sample (must be non-negative)
/// * `sample_size` - Number of samples to draw
/// * `replace` - Whether to sample with replacement
/// * `random_seed` - Optional random seed for reproducible sampling
///
/// # Returns
///
/// A vector of indices representing the importance-weighted sample
///
/// # Examples
///
/// ```rust
/// use scirs2_core::ndarray::Array1;
/// use scirs2_datasets::utils::importance_sample;
///
/// // Give higher weights to the last few samples
/// let weights = Array1::from(vec![0.1, 0.1, 0.1, 0.8, 0.9, 1.0]);
/// let indices = importance_sample(&weights, 3, false, Some(42)).expect("Operation failed");
/// assert_eq!(indices.len(), 3);
///
/// // Higher weighted samples should be more likely to be selected
/// let mut high_weight_count = 0;
/// for &idx in &indices {
///     if idx >= 3 { // Last three samples have higher weights
///         high_weight_count += 1;
///     }
/// }
/// // This should be true with high probability
/// assert!(high_weight_count >= 1);
/// ```
#[allow(dead_code)]
pub fn importance_sample(
    weights: &Array1<f64>,
    sample_size: usize,
    replace: bool,
    random_seed: Option<u64>,
) -> Result<Vec<usize>> {
    if weights.is_empty() {
        return Err(DatasetsError::InvalidFormat(
            "Weights array cannot be empty".to_string(),
        ));
    }

    if sample_size == 0 {
        return Err(DatasetsError::InvalidFormat(
            "Sample _size must be > 0".to_string(),
        ));
    }

    if !replace && sample_size > weights.len() {
        return Err(DatasetsError::InvalidFormat(format!(
            "Cannot sample {} items from {} without replacement",
            sample_size,
            weights.len()
        )));
    }

    // Check for negative weights
    for &weight in weights.iter() {
        if weight < 0.0 {
            return Err(DatasetsError::InvalidFormat(
                "All weights must be non-negative".to_string(),
            ));
        }
    }

    let weight_sum: f64 = weights.sum();
    if weight_sum <= 0.0 {
        return Err(DatasetsError::InvalidFormat(
            "Sum of weights must be positive".to_string(),
        ));
    }

    let mut rng = match random_seed {
        Some(_seed) => StdRng::seed_from_u64(_seed),
        None => {
            let mut r = thread_rng();
            StdRng::seed_from_u64(r.next_u64())
        }
    };

    let mut indices = Vec::with_capacity(sample_size);
    let mut available_weights = weights.clone();
    let mut available_indices: Vec<usize> = (0..weights.len()).collect();

    for _ in 0..sample_size {
        let current_sum = available_weights.sum();
        if current_sum <= 0.0 {
            break;
        }

        // Generate random number between 0 and current_sum
        let random_value = rng.random_range(0.0..current_sum);

        // Find the index corresponding to this random value
        let mut cumulative_weight = 0.0;
        let mut selected_idx = 0;

        for (i, &weight) in available_weights.iter().enumerate() {
            cumulative_weight += weight;
            if random_value <= cumulative_weight {
                selected_idx = i;
                break;
            }
        }

        let original_idx = available_indices[selected_idx];
        indices.push(original_idx);

        if !replace {
            // Remove the selected item for sampling without replacement
            available_weights = Array1::from_iter(
                available_weights
                    .iter()
                    .enumerate()
                    .filter(|(i_, _)| *i_ != selected_idx)
                    .map(|(_, &w)| w),
            );
            available_indices.remove(selected_idx);
        }
    }

    Ok(indices)
}

/// Generate bootstrap samples from indices
///
/// This is a convenience function that generates bootstrap samples (sampling with
/// replacement) which is commonly used for bootstrap confidence intervals and
/// ensemble methods.
///
/// # Arguments
///
/// * `n_samples` - Total number of samples in the dataset
/// * `n_bootstrap_samples` - Number of bootstrap samples to generate
/// * `random_seed` - Optional random seed for reproducible sampling
///
/// # Returns
///
/// A vector of bootstrap sample indices
///
/// # Examples
///
/// ```rust
/// use scirs2_datasets::utils::bootstrap_sample;
///
/// let bootstrap_indices = bootstrap_sample(100, 100, Some(42)).expect("Operation failed");
/// assert_eq!(bootstrap_indices.len(), 100);
///
/// // Some indices should appear multiple times (with high probability)
/// let mut unique_indices = bootstrap_indices.clone();
/// unique_indices.sort();
/// unique_indices.dedup();
/// assert!(unique_indices.len() < bootstrap_indices.len());
/// ```
#[allow(dead_code)]
pub fn bootstrap_sample(
    n_samples: usize,
    n_bootstrap_samples: usize,
    random_seed: Option<u64>,
) -> Result<Vec<usize>> {
    random_sample(n_samples, n_bootstrap_samples, true, random_seed)
}

/// Generate multiple bootstrap samples
///
/// Creates multiple independent bootstrap samples, useful for ensemble methods
/// like bagging or for computing bootstrap confidence intervals.
///
/// # Arguments
///
/// * `n_samples` - Total number of samples in the dataset
/// * `sample_size` - Size of each bootstrap sample
/// * `n_bootstrap_rounds` - Number of bootstrap samples to generate
/// * `random_seed` - Optional random seed for reproducible sampling
///
/// # Returns
///
/// A vector of bootstrap sample vectors
///
/// # Examples
///
/// ```rust
/// use scirs2_datasets::utils::multiple_bootstrap_samples;
///
/// let bootstrap_samples = multiple_bootstrap_samples(50, 50, 10, Some(42)).expect("Operation failed");
/// assert_eq!(bootstrap_samples.len(), 10);
/// assert!(bootstrap_samples.iter().all(|sample| sample.len() == 50));
/// ```
#[allow(dead_code)]
pub fn multiple_bootstrap_samples(
    n_samples: usize,
    sample_size: usize,
    n_bootstrap_rounds: usize,
    random_seed: Option<u64>,
) -> Result<Vec<Vec<usize>>> {
    if n_bootstrap_rounds == 0 {
        return Err(DatasetsError::InvalidFormat(
            "Number of bootstrap _rounds must be > 0".to_string(),
        ));
    }

    let mut rng = match random_seed {
        Some(_seed) => StdRng::seed_from_u64(_seed),
        None => {
            let mut r = thread_rng();
            StdRng::seed_from_u64(r.next_u64())
        }
    };

    let mut bootstrap_samples = Vec::with_capacity(n_bootstrap_rounds);

    for _ in 0..n_bootstrap_rounds {
        let sample = random_sample(n_samples, sample_size, true, Some(rng.next_u64()))?;
        bootstrap_samples.push(sample);
    }

    Ok(bootstrap_samples)
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::array;
    use scirs2_core::random::Uniform;
    use std::collections::HashSet;

    #[test]
    fn test_random_sample_without_replacement() {
        let indices = random_sample(10, 5, false, Some(42)).expect("Operation failed");

        assert_eq!(indices.len(), 5);
        assert!(indices.iter().all(|&i| i < 10));

        // All indices should be unique (no replacement)
        let unique_indices: HashSet<_> = indices.iter().cloned().collect();
        assert_eq!(unique_indices.len(), 5);
    }

    #[test]
    fn test_random_sample_with_replacement() {
        let indices = random_sample(5, 10, true, Some(42)).expect("Operation failed");

        assert_eq!(indices.len(), 10);
        assert!(indices.iter().all(|&i| i < 5));

        // Some indices might be repeated (with replacement)
        let unique_indices: HashSet<_> = indices.iter().cloned().collect();
        assert!(unique_indices.len() <= 10);
    }

    #[test]
    fn test_random_sample_invalid_params() {
        // Zero samples
        assert!(random_sample(0, 5, false, None).is_err());

        // Zero sample size
        assert!(random_sample(10, 0, false, None).is_err());

        // Too many samples without replacement
        assert!(random_sample(5, 10, false, None).is_err());
    }

    #[test]
    fn test_stratified_sample() {
        let targets = array![0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0]; // 2, 3, 3 samples per class
        let indices = stratified_sample(&targets, 6, Some(42)).expect("Operation failed");

        assert_eq!(indices.len(), 6);

        // Count samples per class in the result
        let mut class_counts = HashMap::new();
        for &idx in &indices {
            let class = targets[idx] as i32;
            *class_counts.entry(class).or_insert(0) += 1;
        }

        // Should maintain rough proportions
        assert!(class_counts.len() <= 3); // At most 3 classes
    }

    #[test]
    fn test_stratified_sample_insufficient_samples() {
        let targets = array![0.0, 1.0]; // Only 1 sample per class
                                        // Requesting 4 samples but only 2 total
        assert!(stratified_sample(&targets, 4, Some(42)).is_err());
    }

    #[test]
    fn test_importance_sample() {
        let weights = array![0.1, 0.1, 0.1, 0.8, 0.9, 1.0]; // Higher weights at the end
        let indices = importance_sample(&weights, 3, false, Some(42)).expect("Operation failed");

        assert_eq!(indices.len(), 3);
        assert!(indices.iter().all(|&i| i < 6));

        // All indices should be unique (no replacement)
        let unique_indices: HashSet<_> = indices.iter().cloned().collect();
        assert_eq!(unique_indices.len(), 3);
    }

    #[test]
    fn test_importance_sample_negative_weights() {
        let weights = array![0.5, -0.1, 0.3]; // Contains negative weight
        assert!(importance_sample(&weights, 2, false, None).is_err());
    }

    #[test]
    fn test_importance_sample_zero_weights() {
        let weights = array![0.0, 0.0, 0.0]; // All zero weights
        assert!(importance_sample(&weights, 2, false, None).is_err());
    }

    #[test]
    fn test_bootstrap_sample() {
        let indices = bootstrap_sample(20, 20, Some(42)).expect("Operation failed");

        assert_eq!(indices.len(), 20);
        assert!(indices.iter().all(|&i| i < 20));

        // Should likely have some repeated indices
        let unique_indices: HashSet<_> = indices.iter().cloned().collect();
        assert!(unique_indices.len() < 20); // Very likely with size 20
    }

    #[test]
    fn test_multiple_bootstrap_samples() {
        let samples = multiple_bootstrap_samples(10, 8, 5, Some(42)).expect("Operation failed");

        assert_eq!(samples.len(), 5);
        assert!(samples.iter().all(|sample| sample.len() == 8));
        assert!(samples.iter().all(|sample| sample.iter().all(|&i| i < 10)));

        // Different bootstrap samples should be different
        assert_ne!(samples[0], samples[1]); // Very likely to be different
    }

    #[test]
    fn test_multiple_bootstrap_samples_invalid_params() {
        assert!(multiple_bootstrap_samples(10, 10, 0, None).is_err());
    }
}