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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! Mini-batch sampler for iterating over datasets in batches.
//!
//! Provides configurable sampling strategies including sequential, random,
//! stratified (proportional label representation per batch), and weighted
//! random sampling.

use crate::error::{DatasetsError, Result};

// ─────────────────────────────────────────────────────────────────────────────
// LCG helper (deterministic PRNG without external rand dependency)
// ─────────────────────────────────────────────────────────────────────────────

/// Minimal 64-bit LCG (Knuth parameters).
struct Lcg64 {
    state: u64,
}

impl Lcg64 {
    fn new(seed: u64) -> Self {
        Self {
            state: seed.wrapping_add(1),
        }
    }

    fn next_u64(&mut self) -> u64 {
        self.state = self
            .state
            .wrapping_mul(6_364_136_223_846_793_005)
            .wrapping_add(1_442_695_040_888_963_407);
        self.state
    }

    fn next_usize(&mut self, n: usize) -> usize {
        if n == 0 {
            return 0;
        }
        (self.next_u64() % n as u64) as usize
    }

    fn next_f64(&mut self) -> f64 {
        (self.next_u64() >> 11) as f64 / (1u64 << 53) as f64
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Public types
// ─────────────────────────────────────────────────────────────────────────────

/// Strategy used by the mini-batch sampler to select samples.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Default)]
pub enum SamplerStrategy {
    /// Iterate through the dataset in order.
    #[default]
    Sequential,
    /// Randomly shuffle all indices, then iterate sequentially over the shuffled order.
    Random,
    /// Each batch maintains the same label proportions as the full dataset.
    Stratified,
    /// Sample indices according to per-example weights (with replacement).
    WeightedRandom {
        /// Per-sample weight. Length must equal the number of samples.
        weights: Vec<f64>,
    },
}

/// Configuration for the [`MiniBatchSampler`].
#[derive(Debug, Clone)]
pub struct SamplerConfig {
    /// Number of samples per batch.
    pub batch_size: usize,
    /// Whether to shuffle the dataset before creating batches.
    pub shuffle: bool,
    /// If `true`, the last batch is dropped when it has fewer than `batch_size` samples.
    pub drop_last: bool,
    /// Random seed for reproducibility.
    pub seed: u64,
    /// Sampling strategy.
    pub strategy: SamplerStrategy,
}

impl Default for SamplerConfig {
    fn default() -> Self {
        Self {
            batch_size: 32,
            shuffle: true,
            drop_last: false,
            seed: 42,
            strategy: SamplerStrategy::default(),
        }
    }
}

/// A single mini-batch of data and labels.
#[derive(Debug, Clone)]
pub struct MiniBatch {
    /// Feature vectors for this batch.
    pub data: Vec<Vec<f64>>,
    /// Labels for this batch.
    pub labels: Vec<usize>,
    /// Indices into the original dataset for this batch.
    pub indices: Vec<usize>,
}

/// Mini-batch sampler that yields batches from a dataset.
///
/// Construct via [`MiniBatchSampler::new`] and iterate with [`iter_batches`](MiniBatchSampler::iter_batches).
#[derive(Debug, Clone)]
pub struct MiniBatchSampler {
    config: SamplerConfig,
}

impl MiniBatchSampler {
    /// Create a new sampler with the given configuration.
    pub fn new(config: SamplerConfig) -> Self {
        Self { config }
    }

    /// Return a reference to the current configuration.
    pub fn config(&self) -> &SamplerConfig {
        &self.config
    }

    /// Generate all mini-batches for the given data and labels.
    ///
    /// Returns a `Vec<MiniBatch>` where each batch has at most `config.batch_size`
    /// samples. When `drop_last` is `true`, the final partial batch is omitted.
    ///
    /// # Errors
    ///
    /// Returns an error if data and labels have different lengths, or if
    /// `batch_size` is zero.
    pub fn iter_batches(&self, data: &[Vec<f64>], labels: &[usize]) -> Result<Vec<MiniBatch>> {
        iter_batches(data, labels, &self.config)
    }
}

/// Generate mini-batches from a dataset according to the given configuration.
///
/// This is the free-function equivalent of [`MiniBatchSampler::iter_batches`].
///
/// # Errors
///
/// Returns an error when `data.len() != labels.len()` or `config.batch_size == 0`.
pub fn iter_batches(
    data: &[Vec<f64>],
    labels: &[usize],
    config: &SamplerConfig,
) -> Result<Vec<MiniBatch>> {
    let n = data.len();
    if n != labels.len() {
        return Err(DatasetsError::InvalidFormat(format!(
            "data length ({}) != labels length ({})",
            n,
            labels.len()
        )));
    }
    if config.batch_size == 0 {
        return Err(DatasetsError::InvalidFormat(
            "batch_size must be >= 1".into(),
        ));
    }
    if n == 0 {
        return Ok(Vec::new());
    }

    let indices = build_index_order(n, labels, config);
    let mut batches = Vec::new();
    let mut offset = 0;

    while offset < indices.len() {
        let end = (offset + config.batch_size).min(indices.len());
        let batch_indices: Vec<usize> = indices[offset..end].to_vec();

        if config.drop_last && batch_indices.len() < config.batch_size {
            break;
        }

        let batch_data: Vec<Vec<f64>> = batch_indices.iter().map(|&i| data[i].clone()).collect();
        let batch_labels: Vec<usize> = batch_indices.iter().map(|&i| labels[i]).collect();

        batches.push(MiniBatch {
            data: batch_data,
            labels: batch_labels,
            indices: batch_indices,
        });

        offset = end;
    }

    Ok(batches)
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Build the ordered index list according to the strategy.
fn build_index_order(n: usize, labels: &[usize], config: &SamplerConfig) -> Vec<usize> {
    match &config.strategy {
        SamplerStrategy::Sequential => {
            let mut indices: Vec<usize> = (0..n).collect();
            if config.shuffle {
                fisher_yates_shuffle(&mut indices, config.seed);
            }
            indices
        }

        SamplerStrategy::Random => {
            let mut indices: Vec<usize> = (0..n).collect();
            fisher_yates_shuffle(&mut indices, config.seed);
            indices
        }

        SamplerStrategy::Stratified => build_stratified_order(n, labels, config),

        SamplerStrategy::WeightedRandom { weights } => build_weighted_order(n, weights, config),
    }
}

/// Fisher-Yates shuffle using seeded LCG.
fn fisher_yates_shuffle(indices: &mut [usize], seed: u64) {
    let n = indices.len();
    if n <= 1 {
        return;
    }
    let mut rng = Lcg64::new(seed);
    for i in (1..n).rev() {
        let j = rng.next_usize(i + 1);
        indices.swap(i, j);
    }
}

/// Build an index order that groups samples so each batch has proportional
/// label representation.
fn build_stratified_order(n: usize, labels: &[usize], config: &SamplerConfig) -> Vec<usize> {
    if n == 0 {
        return Vec::new();
    }

    // Group indices by class.
    let max_class = labels.iter().copied().max().unwrap_or(0);
    let mut class_indices: Vec<Vec<usize>> = vec![Vec::new(); max_class + 1];
    for (i, &label) in labels.iter().enumerate() {
        class_indices[label].push(i);
    }

    // Optionally shuffle within each class.
    if config.shuffle {
        for (cls, indices) in class_indices.iter_mut().enumerate() {
            let class_seed = config.seed.wrapping_add(cls as u64 * 0x9e37_79b9_7f4a_7c15);
            fisher_yates_shuffle(indices, class_seed);
        }
    }

    // Interleave: round-robin across classes to ensure proportional representation
    // within each batch-sized chunk.
    let mut result = Vec::with_capacity(n);
    let mut cursors: Vec<usize> = vec![0; class_indices.len()];
    let mut remaining = n;

    while remaining > 0 {
        let mut added = false;
        for (cls, indices) in class_indices.iter().enumerate() {
            if cursors[cls] < indices.len() {
                result.push(indices[cursors[cls]]);
                cursors[cls] += 1;
                remaining -= 1;
                added = true;
                if remaining == 0 {
                    break;
                }
            }
        }
        if !added {
            break;
        }
    }

    result
}

/// Build an index order using weighted sampling with replacement.
fn build_weighted_order(n: usize, weights: &[f64], config: &SamplerConfig) -> Vec<usize> {
    if n == 0 || weights.is_empty() {
        return Vec::new();
    }

    let mut rng = Lcg64::new(config.seed);
    let actual_weights: Vec<f64> = if weights.len() >= n {
        weights[..n].to_vec()
    } else {
        // Pad with uniform weight 1.0.
        let mut w = weights.to_vec();
        w.resize(n, 1.0);
        w
    };

    // Build cumulative distribution.
    let total: f64 = actual_weights.iter().sum();
    if total <= 0.0 {
        // Fallback to uniform.
        return (0..n).collect();
    }
    let cumulative: Vec<f64> = actual_weights
        .iter()
        .scan(0.0, |acc, &w| {
            *acc += w / total;
            Some(*acc)
        })
        .collect();

    // Sample n indices with replacement.
    (0..n)
        .map(|_| {
            let u = rng.next_f64();
            // Binary search in cumulative distribution.
            match cumulative.binary_search_by(|probe| {
                probe.partial_cmp(&u).unwrap_or(std::cmp::Ordering::Equal)
            }) {
                Ok(idx) => idx.min(n - 1),
                Err(idx) => idx.min(n - 1),
            }
        })
        .collect()
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn make_simple_data(n: usize, n_features: usize) -> Vec<Vec<f64>> {
        (0..n)
            .map(|i| {
                (0..n_features)
                    .map(|j| (i * n_features + j) as f64)
                    .collect()
            })
            .collect()
    }

    #[test]
    fn test_sequential_batches_correct_size() {
        let data = make_simple_data(100, 5);
        let labels: Vec<usize> = (0..100).map(|i| i % 3).collect();
        let config = SamplerConfig {
            batch_size: 32,
            shuffle: false,
            drop_last: false,
            seed: 42,
            strategy: SamplerStrategy::Sequential,
        };
        let batches = iter_batches(&data, &labels, &config).expect("should succeed");
        // 100 / 32 = 3 full + 1 partial = 4 batches
        assert_eq!(batches.len(), 4);
        assert_eq!(batches[0].data.len(), 32);
        assert_eq!(batches[3].data.len(), 4); // remainder
    }

    #[test]
    fn test_drop_last() {
        let data = make_simple_data(50, 3);
        let labels: Vec<usize> = vec![0; 50];
        let config = SamplerConfig {
            batch_size: 16,
            shuffle: false,
            drop_last: true,
            seed: 0,
            strategy: SamplerStrategy::Sequential,
        };
        let batches = iter_batches(&data, &labels, &config).expect("should succeed");
        // 50 / 16 = 3 full batches (48), last partial (2) dropped
        assert_eq!(batches.len(), 3);
        for b in &batches {
            assert_eq!(b.data.len(), 16);
        }
    }

    #[test]
    fn test_random_shuffles_indices() {
        let data = make_simple_data(20, 2);
        let labels: Vec<usize> = vec![0; 20];
        let config = SamplerConfig {
            batch_size: 20,
            shuffle: true,
            drop_last: false,
            seed: 99,
            strategy: SamplerStrategy::Random,
        };
        let batches = iter_batches(&data, &labels, &config).expect("should succeed");
        assert_eq!(batches.len(), 1);
        // Indices should be a permutation of 0..20
        let mut sorted = batches[0].indices.clone();
        sorted.sort_unstable();
        assert_eq!(sorted, (0..20).collect::<Vec<_>>());
        // Very unlikely to be in natural order
        assert_ne!(batches[0].indices, (0..20).collect::<Vec<_>>());
    }

    #[test]
    fn test_stratified_label_proportions() {
        // 60 class-0, 40 class-1
        let n = 100;
        let mut labels: Vec<usize> = vec![0; 60];
        labels.extend(vec![1; 40]);
        let data = make_simple_data(n, 2);

        let config = SamplerConfig {
            batch_size: 20,
            shuffle: false,
            drop_last: false,
            seed: 42,
            strategy: SamplerStrategy::Stratified,
        };
        let batches = iter_batches(&data, &labels, &config).expect("should succeed");
        assert_eq!(batches.len(), 5); // 100 / 20

        // Check overall: total class proportions should be preserved
        let total_c0: usize = batches
            .iter()
            .map(|b| b.labels.iter().filter(|&&l| l == 0).count())
            .sum();
        let total_c1: usize = batches
            .iter()
            .map(|b| b.labels.iter().filter(|&&l| l == 1).count())
            .sum();
        assert_eq!(total_c0, 60);
        assert_eq!(total_c1, 40);

        // Each batch should have at least some representation of both classes
        // (round-robin interleaving distributes them across batches)
        let batches_with_both: usize = batches
            .iter()
            .filter(|b| {
                let c0 = b.labels.iter().filter(|&&l| l == 0).count();
                let c1 = b.labels.iter().filter(|&&l| l == 1).count();
                c0 > 0 && c1 > 0
            })
            .count();
        // At least 4 out of 5 batches should have both classes
        assert!(
            batches_with_both >= 4,
            "Expected most batches to have both classes, got {batches_with_both}"
        );
    }

    #[test]
    fn test_weighted_sampling() {
        let n = 50;
        let data = make_simple_data(n, 2);
        let labels: Vec<usize> = vec![0; n];
        // Give all weight to index 0
        let mut weights = vec![0.0; n];
        weights[0] = 1.0;

        let config = SamplerConfig {
            batch_size: 10,
            shuffle: false,
            drop_last: false,
            seed: 42,
            strategy: SamplerStrategy::WeightedRandom { weights },
        };
        let batches = iter_batches(&data, &labels, &config).expect("should succeed");
        // All samples should be index 0
        for batch in &batches {
            for &idx in &batch.indices {
                assert_eq!(idx, 0, "All indices should be 0 with weight=[1,0,0,...]");
            }
        }
    }

    #[test]
    fn test_reproducibility_same_seed() {
        let data = make_simple_data(40, 3);
        let labels: Vec<usize> = (0..40).map(|i| i % 2).collect();
        let config = SamplerConfig {
            batch_size: 10,
            shuffle: true,
            drop_last: false,
            seed: 777,
            strategy: SamplerStrategy::Random,
        };
        let b1 = iter_batches(&data, &labels, &config).expect("ok");
        let b2 = iter_batches(&data, &labels, &config).expect("ok");
        assert_eq!(b1.len(), b2.len());
        for (a, b) in b1.iter().zip(b2.iter()) {
            assert_eq!(a.indices, b.indices);
        }
    }

    #[test]
    fn test_mismatched_lengths_error() {
        let data = make_simple_data(10, 2);
        let labels: Vec<usize> = vec![0; 5];
        let config = SamplerConfig::default();
        assert!(iter_batches(&data, &labels, &config).is_err());
    }

    #[test]
    fn test_zero_batch_size_error() {
        let data = make_simple_data(10, 2);
        let labels: Vec<usize> = vec![0; 10];
        let config = SamplerConfig {
            batch_size: 0,
            ..Default::default()
        };
        assert!(iter_batches(&data, &labels, &config).is_err());
    }

    #[test]
    fn test_empty_dataset() {
        let data: Vec<Vec<f64>> = Vec::new();
        let labels: Vec<usize> = Vec::new();
        let config = SamplerConfig::default();
        let batches = iter_batches(&data, &labels, &config).expect("ok");
        assert!(batches.is_empty());
    }

    #[test]
    fn test_sampler_struct() {
        let data = make_simple_data(20, 2);
        let labels: Vec<usize> = vec![0; 20];
        let sampler = MiniBatchSampler::new(SamplerConfig {
            batch_size: 5,
            shuffle: false,
            drop_last: false,
            seed: 0,
            strategy: SamplerStrategy::Sequential,
        });
        let batches = sampler.iter_batches(&data, &labels).expect("ok");
        assert_eq!(batches.len(), 4);
        assert_eq!(sampler.config().batch_size, 5);
    }

    #[test]
    fn test_all_indices_covered_sequential() {
        let n = 37;
        let data = make_simple_data(n, 2);
        let labels: Vec<usize> = vec![0; n];
        let config = SamplerConfig {
            batch_size: 10,
            shuffle: false,
            drop_last: false,
            seed: 0,
            strategy: SamplerStrategy::Sequential,
        };
        let batches = iter_batches(&data, &labels, &config).expect("ok");
        let mut all_indices: Vec<usize> = batches
            .iter()
            .flat_map(|b| b.indices.iter().copied())
            .collect();
        all_indices.sort_unstable();
        assert_eq!(all_indices, (0..n).collect::<Vec<_>>());
    }

    #[test]
    fn test_batch_data_matches_original() {
        let data = make_simple_data(15, 3);
        let labels: Vec<usize> = (0..15).map(|i| i % 2).collect();
        let config = SamplerConfig {
            batch_size: 5,
            shuffle: false,
            drop_last: false,
            seed: 0,
            strategy: SamplerStrategy::Sequential,
        };
        let batches = iter_batches(&data, &labels, &config).expect("ok");
        for batch in &batches {
            for (pos, &idx) in batch.indices.iter().enumerate() {
                assert_eq!(batch.data[pos], data[idx]);
                assert_eq!(batch.labels[pos], labels[idx]);
            }
        }
    }
}