torsh-data 0.1.0

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
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
//! Weighted and subset sampling functionality
//!
//! This module provides sampling strategies that work with weighted datasets
//! and subset selections, useful for imbalanced datasets and custom data selection.

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

// ✅ SciRS2 Policy Compliant - Using scirs2_core for all random operations
use scirs2_core::random::Rng;

use super::core::{rng_utils, Sampler, SamplerIterator};

/// Weighted random sampler for imbalanced datasets
///
/// This sampler allows sampling indices according to specified weights,
/// which is particularly useful for handling imbalanced datasets or
/// implementing custom sampling strategies.
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_data::sampler::{WeightedRandomSampler, Sampler};
///
/// // Higher weight for the last class (imbalanced dataset)
/// let weights = vec![0.1, 0.1, 0.1, 0.1, 0.6];
/// let sampler = WeightedRandomSampler::new(weights, 100, true).with_generator(42);
///
/// let indices: Vec<usize> = sampler.iter().collect();
/// assert_eq!(indices.len(), 100);
/// ```
#[derive(Debug, Clone)]
pub struct WeightedRandomSampler {
    weights: Vec<f64>,
    num_samples: usize,
    replacement: bool,
    generator: Option<u64>,
}

impl WeightedRandomSampler {
    /// Create a new weighted random sampler
    ///
    /// # Arguments
    ///
    /// * `weights` - Sampling weights for each index
    /// * `num_samples` - Number of samples to generate
    /// * `replacement` - Whether to sample with replacement
    ///
    /// # Panics
    ///
    /// Panics if weights are empty, contain negative values, or don't sum to a positive finite value
    pub fn new(weights: Vec<f64>, num_samples: usize, replacement: bool) -> Self {
        assert!(!weights.is_empty(), "weights cannot be empty");
        assert!(
            weights.iter().all(|&w| w >= 0.0),
            "weights must be non-negative"
        );
        let weight_sum: f64 = weights.iter().sum();
        assert!(
            weight_sum > 0.0 && weight_sum.is_finite(),
            "weights must sum to a positive finite value, got {weight_sum}"
        );

        Self {
            weights,
            num_samples,
            replacement,
            generator: None,
        }
    }

    /// Set random generator seed
    ///
    /// # Arguments
    ///
    /// * `seed` - Random seed for reproducible sampling
    pub fn with_generator(mut self, seed: u64) -> Self {
        self.generator = Some(seed);
        self
    }

    /// Get the weights
    pub fn weights(&self) -> &[f64] {
        &self.weights
    }

    /// Get the number of samples
    pub fn num_samples(&self) -> usize {
        self.num_samples
    }

    /// Check if sampling with replacement
    pub fn replacement(&self) -> bool {
        self.replacement
    }

    /// Get the generator seed if set
    pub fn generator(&self) -> Option<u64> {
        self.generator
    }

    /// Sample indices according to weights with replacement
    fn sample_with_replacement(&self) -> Vec<usize> {
        // ✅ SciRS2 Policy Compliant - Using scirs2_core for random operations
        let mut rng = rng_utils::create_rng(self.generator);

        // Normalize weights to create cumulative distribution
        let weight_sum: f64 = self.weights.iter().sum();
        let mut cumulative_weights = Vec::with_capacity(self.weights.len());
        let mut cumsum = 0.0;

        for &weight in &self.weights {
            cumsum += weight / weight_sum;
            cumulative_weights.push(cumsum);
        }

        // Ensure the last value is exactly 1.0 to handle floating point precision
        if let Some(last) = cumulative_weights.last_mut() {
            *last = 1.0;
        }

        // Sample using inverse transform sampling
        (0..self.num_samples)
            .map(|_| {
                let rand_val: f64 = rng.random();
                // Binary search for the first cumulative weight >= rand_val
                cumulative_weights
                    .binary_search_by(|&x| {
                        x.partial_cmp(&rand_val)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .unwrap_or_else(|i| i)
                    .min(self.weights.len() - 1)
            })
            .collect()
    }

    /// Sample indices according to weights without replacement
    fn sample_without_replacement(&self) -> Vec<usize> {
        if self.num_samples >= self.weights.len() {
            // Return all indices if we need more samples than available
            return (0..self.weights.len()).collect();
        }

        // ✅ SciRS2 Policy Compliant - Using scirs2_core for random operations
        let mut rng = rng_utils::create_rng(self.generator);

        // Use weighted reservoir sampling for sampling without replacement
        let mut selected_indices = Vec::new();
        let mut remaining_weights = self.weights.clone();
        let mut remaining_indices: Vec<usize> = (0..self.weights.len()).collect();

        for _ in 0..self.num_samples {
            if remaining_indices.is_empty() {
                break;
            }

            // Normalize remaining weights
            let weight_sum: f64 = remaining_weights.iter().sum();
            if weight_sum <= 0.0 {
                break;
            }

            let mut cumsum = 0.0;
            let rand_val: f64 = rng.random::<f64>() * weight_sum;

            let mut selected_idx = 0;
            for (i, &weight) in remaining_weights.iter().enumerate() {
                cumsum += weight;
                if cumsum >= rand_val {
                    selected_idx = i;
                    break;
                }
            }

            // Add the selected index to results
            selected_indices.push(remaining_indices[selected_idx]);

            // Remove the selected index and weight
            remaining_indices.remove(selected_idx);
            remaining_weights.remove(selected_idx);
        }

        selected_indices
    }
}

impl Sampler for WeightedRandomSampler {
    type Iter = SamplerIterator;

    fn iter(&self) -> Self::Iter {
        let indices = if self.replacement {
            self.sample_with_replacement()
        } else {
            self.sample_without_replacement()
        };

        SamplerIterator::new(indices)
    }

    fn len(&self) -> usize {
        self.num_samples
    }
}

/// Random sampler that samples from a subset of indices
///
/// This sampler takes a predefined subset of indices and samples from them
/// in random order. Useful for creating custom data splits or working with
/// filtered datasets.
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_data::sampler::{SubsetRandomSampler, Sampler};
///
/// // Sample from odd indices only
/// let subset_indices = vec![1, 3, 5, 7, 9];
/// let sampler = SubsetRandomSampler::new(subset_indices).with_generator(42);
///
/// let indices: Vec<usize> = sampler.iter().collect();
/// assert_eq!(indices.len(), 5);
/// ```
#[derive(Debug, Clone)]
pub struct SubsetRandomSampler {
    indices: Vec<usize>,
    generator: Option<u64>,
}

impl SubsetRandomSampler {
    /// Create a new subset random sampler
    ///
    /// # Arguments
    ///
    /// * `indices` - The subset of indices to sample from
    pub fn new(indices: Vec<usize>) -> Self {
        Self {
            indices,
            generator: None,
        }
    }

    /// Set random generator seed
    ///
    /// # Arguments
    ///
    /// * `seed` - Random seed for reproducible sampling
    pub fn with_generator(mut self, seed: u64) -> Self {
        self.generator = Some(seed);
        self
    }

    /// Get the subset indices
    pub fn indices(&self) -> &[usize] {
        &self.indices
    }

    /// Get the generator seed if set
    pub fn generator(&self) -> Option<u64> {
        self.generator
    }
}

impl Sampler for SubsetRandomSampler {
    type Iter = SamplerIterator;

    fn iter(&self) -> Self::Iter {
        let mut shuffled_indices = self.indices.clone();
        rng_utils::shuffle_indices(&mut shuffled_indices, self.generator);
        SamplerIterator::new(shuffled_indices)
    }

    fn len(&self) -> usize {
        self.indices.len()
    }
}

/// Create a weighted random sampler
///
/// Convenience function for creating a weighted random sampler.
///
/// # Arguments
///
/// * `weights` - Sampling weights for each index
/// * `num_samples` - Number of samples to generate
/// * `replacement` - Whether to sample with replacement
/// * `seed` - Optional random seed for reproducible sampling
pub fn weighted_random(
    weights: Vec<f64>,
    num_samples: usize,
    replacement: bool,
    seed: Option<u64>,
) -> WeightedRandomSampler {
    let mut sampler = WeightedRandomSampler::new(weights, num_samples, replacement);
    if let Some(s) = seed {
        sampler = sampler.with_generator(s);
    }
    sampler
}

/// Create a subset random sampler
///
/// Convenience function for creating a subset random sampler.
///
/// # Arguments
///
/// * `indices` - The subset of indices to sample from
/// * `seed` - Optional random seed for reproducible sampling
pub fn subset_random(indices: Vec<usize>, seed: Option<u64>) -> SubsetRandomSampler {
    let mut sampler = SubsetRandomSampler::new(indices);
    if let Some(s) = seed {
        sampler = sampler.with_generator(s);
    }
    sampler
}

/// Create a balanced weighted sampler for class imbalance
///
/// Creates weights that are inversely proportional to class frequencies,
/// providing balanced sampling for imbalanced datasets.
///
/// # Arguments
///
/// * `class_counts` - Number of samples per class
/// * `num_samples` - Total number of samples to generate
/// * `seed` - Optional random seed for reproducible sampling
pub fn balanced_weighted(
    class_counts: &[usize],
    num_samples: usize,
    seed: Option<u64>,
) -> WeightedRandomSampler {
    let total_samples: usize = class_counts.iter().sum();
    let num_classes = class_counts.len();

    // Calculate inverse frequency weights
    let weights: Vec<f64> = class_counts
        .iter()
        .map(|&count| {
            if count > 0 {
                total_samples as f64 / (num_classes as f64 * count as f64)
            } else {
                0.0
            }
        })
        .collect();

    weighted_random(weights, num_samples, true, seed)
}

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

    #[test]
    fn test_weighted_sampler_basic() {
        let weights = vec![0.1, 0.1, 0.1, 0.1, 0.6]; // Last element has higher weight
        let sampler = WeightedRandomSampler::new(weights.clone(), 100, true).with_generator(42);

        assert_eq!(sampler.len(), 100);
        assert_eq!(sampler.weights(), &weights);
        assert_eq!(sampler.num_samples(), 100);
        assert!(sampler.replacement());
        assert_eq!(sampler.generator(), Some(42));

        let indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(indices.len(), 100);

        // All indices should be in valid range
        for &idx in &indices {
            assert!(idx < 5);
        }
    }

    #[test]
    fn test_weighted_sampler_without_replacement() {
        let weights = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let sampler = WeightedRandomSampler::new(weights, 3, false).with_generator(42);

        assert!(!sampler.replacement());

        let indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(indices.len(), 3);

        // All indices should be unique
        let mut sorted_indices = indices.clone();
        sorted_indices.sort();
        sorted_indices.dedup();
        assert_eq!(sorted_indices.len(), 3);

        // All indices should be in valid range
        for &idx in &indices {
            assert!(idx < 5);
        }
    }

    #[test]
    fn test_weighted_sampler_uniform_weights() {
        let weights = vec![1.0; 10];
        let sampler = WeightedRandomSampler::new(weights, 50, true).with_generator(42);

        let indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(indices.len(), 50);

        // With uniform weights, should get reasonably balanced distribution
        let mut counts = [0; 10];
        for &idx in &indices {
            counts[idx] += 1;
        }

        // Each index should appear at least once in 50 samples
        for count in counts {
            assert!(count > 0);
        }
    }

    #[test]
    fn test_weighted_sampler_extreme_weights() {
        let weights = vec![0.0, 0.0, 0.0, 1.0]; // Only last index has weight
        let sampler = WeightedRandomSampler::new(weights, 10, true).with_generator(42);

        let indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(indices.len(), 10);

        // All samples should be from index 3
        for &idx in &indices {
            assert_eq!(idx, 3);
        }
    }

    #[test]
    #[should_panic(expected = "weights cannot be empty")]
    fn test_weighted_sampler_empty_weights() {
        WeightedRandomSampler::new(vec![], 10, true);
    }

    #[test]
    #[should_panic(expected = "weights must be non-negative")]
    fn test_weighted_sampler_negative_weights() {
        WeightedRandomSampler::new(vec![1.0, -1.0, 1.0], 10, true);
    }

    #[test]
    #[should_panic(expected = "weights must sum to a positive finite value")]
    fn test_weighted_sampler_zero_sum() {
        WeightedRandomSampler::new(vec![0.0, 0.0, 0.0], 10, true);
    }

    #[test]
    fn test_subset_random_sampler() {
        // Test with a subset of indices
        let subset_indices = vec![1, 3, 5, 7, 9];
        let sampler = SubsetRandomSampler::new(subset_indices.clone()).with_generator(42);

        assert_eq!(sampler.len(), 5);
        assert_eq!(sampler.indices(), &subset_indices);
        assert_eq!(sampler.generator(), Some(42));

        let sampled_indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(sampled_indices.len(), 5);

        // Check that all sampled indices are from the original subset
        for idx in &sampled_indices {
            assert!(subset_indices.contains(idx));
        }

        // Check that we have all indices (just shuffled)
        let mut sorted_sampled = sampled_indices.clone();
        sorted_sampled.sort();
        let mut sorted_original = subset_indices;
        sorted_original.sort();
        assert_eq!(sorted_sampled, sorted_original);
    }

    #[test]
    fn test_subset_random_sampler_empty() {
        let sampler = SubsetRandomSampler::new(vec![]);
        assert_eq!(sampler.len(), 0);
        assert!(sampler.is_empty());

        let indices: Vec<usize> = sampler.iter().collect();
        assert!(indices.is_empty());
    }

    #[test]
    fn test_subset_random_sampler_single() {
        let sampler = SubsetRandomSampler::new(vec![42]);
        assert_eq!(sampler.len(), 1);

        let indices: Vec<usize> = sampler.iter().collect();
        assert_eq!(indices, vec![42]);
    }

    #[test]
    fn test_subset_random_sampler_reproducible() {
        let subset_indices = vec![10, 20, 30, 40, 50];
        let sampler1 = SubsetRandomSampler::new(subset_indices.clone()).with_generator(123);
        let sampler2 = SubsetRandomSampler::new(subset_indices).with_generator(123);

        let indices1: Vec<usize> = sampler1.iter().collect();
        let indices2: Vec<usize> = sampler2.iter().collect();

        assert_eq!(indices1, indices2);
    }

    #[test]
    fn test_convenience_functions() {
        // Test weighted_random convenience function
        let weights = vec![1.0, 2.0, 3.0];
        let weighted = weighted_random(weights.clone(), 10, true, Some(42));
        assert_eq!(weighted.weights(), &weights);
        assert_eq!(weighted.num_samples(), 10);
        assert!(weighted.replacement());
        assert_eq!(weighted.generator(), Some(42));

        // Test subset_random convenience function
        let indices = vec![1, 3, 5];
        let subset = subset_random(indices.clone(), Some(42));
        assert_eq!(subset.indices(), &indices);
        assert_eq!(subset.generator(), Some(42));

        // Test balanced_weighted convenience function
        let class_counts = vec![100, 50, 25]; // Imbalanced classes
        let balanced = balanced_weighted(&class_counts, 30, Some(42));
        assert_eq!(balanced.num_samples(), 30);
        assert!(balanced.replacement());

        // Verify that balanced weights are inversely proportional to class counts
        let weights = balanced.weights();
        assert!(weights[2] > weights[1]); // Smallest class has highest weight
        assert!(weights[1] > weights[0]); // Medium class has medium weight
    }

    #[test]
    fn test_balanced_weighted_edge_cases() {
        // Test with zero counts
        let class_counts = vec![100, 0, 50];
        let balanced = balanced_weighted(&class_counts, 20, Some(42));
        let weights = balanced.weights();

        assert!(weights[0] > 0.0);
        assert_eq!(weights[1], 0.0); // Zero count should give zero weight
        assert!(weights[2] > 0.0);
        assert!(weights[2] > weights[0]); // Smaller class should have higher weight

        // Test with single class
        let class_counts = vec![100];
        let balanced = balanced_weighted(&class_counts, 10, Some(42));
        assert_eq!(balanced.weights().len(), 1);
        assert!(balanced.weights()[0] > 0.0);
    }

    #[test]
    fn test_weighted_sampler_clone() {
        let weights = vec![1.0, 2.0, 3.0];
        let sampler = WeightedRandomSampler::new(weights.clone(), 10, true).with_generator(42);
        let cloned = sampler.clone();

        assert_eq!(sampler.weights(), cloned.weights());
        assert_eq!(sampler.num_samples(), cloned.num_samples());
        assert_eq!(sampler.replacement(), cloned.replacement());
        assert_eq!(sampler.generator(), cloned.generator());
    }

    #[test]
    fn test_subset_sampler_clone() {
        let indices = vec![1, 3, 5, 7];
        let sampler = SubsetRandomSampler::new(indices.clone()).with_generator(42);
        let cloned = sampler.clone();

        assert_eq!(sampler.indices(), cloned.indices());
        assert_eq!(sampler.generator(), cloned.generator());
    }
}