torsh-data 0.1.2

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
//! Batch sampling functionality
//!
//! This module provides utilities for converting individual samplers into
//! batch samplers that yield batches of indices instead of individual indices.

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

use super::core::{BatchSampler, Sampler};

/// Wrapper that converts any sampler into a batch sampler
///
/// This sampler takes an underlying sampler and groups its output into batches
/// of a specified size. The last batch may be smaller than the batch size
/// unless `drop_last` is set to true.
///
/// # Examples
///
/// ```rust,ignore
/// use torsh_data::sampler::{SequentialSampler, BatchingSampler, BatchSampler};
///
/// let base_sampler = SequentialSampler::new(10);
/// let batch_sampler = BatchingSampler::new(base_sampler, 3, false);
///
/// let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
/// assert_eq!(batches.len(), 4); // [0,1,2], [3,4,5], [6,7,8], [9]
/// ```
#[derive(Debug, Clone)]
pub struct BatchingSampler<S: Sampler> {
    sampler: S,
    batch_size: usize,
    drop_last: bool,
}

impl<S: Sampler> BatchingSampler<S> {
    /// Create a new batching sampler
    ///
    /// # Arguments
    ///
    /// * `sampler` - The underlying sampler to batch
    /// * `batch_size` - Size of each batch
    /// * `drop_last` - Whether to drop the last incomplete batch
    ///
    /// # Panics
    ///
    /// Panics if `batch_size` is 0
    pub fn new(sampler: S, batch_size: usize, drop_last: bool) -> Self {
        assert!(batch_size > 0, "Batch size must be positive");
        Self {
            sampler,
            batch_size,
            drop_last,
        }
    }

    /// Get the batch size
    pub fn batch_size(&self) -> usize {
        self.batch_size
    }

    /// Check if dropping last incomplete batch
    pub fn drop_last(&self) -> bool {
        self.drop_last
    }

    /// Get a reference to the underlying sampler
    pub fn sampler(&self) -> &S {
        &self.sampler
    }

    /// Get the underlying sampler by value
    pub fn into_sampler(self) -> S {
        self.sampler
    }

    /// Convert this batching sampler into a distributed version
    ///
    /// This creates a distributed wrapper around the underlying sampler
    /// and then wraps it with a new BatchingSampler.
    ///
    /// # Arguments
    ///
    /// * `num_replicas` - Total number of processes
    /// * `rank` - Current process rank (0-based)
    pub fn into_distributed(
        self,
        num_replicas: usize,
        rank: usize,
    ) -> BatchingSampler<super::distributed::DistributedWrapper<S>> {
        let distributed_sampler = self.sampler.into_distributed(num_replicas, rank);
        BatchingSampler::new(distributed_sampler, self.batch_size, self.drop_last)
    }
}

impl<S: Sampler> BatchSampler for BatchingSampler<S> {
    type Iter = BatchSamplerIter<S::Iter>;

    fn iter(&self) -> Self::Iter {
        BatchSamplerIter::new(self.sampler.iter(), self.batch_size, self.drop_last)
    }

    fn num_batches(&self) -> usize {
        let total_samples = self.sampler.len();
        if total_samples == 0 {
            return 0;
        }

        if self.drop_last {
            total_samples / self.batch_size
        } else {
            (total_samples + self.batch_size - 1) / self.batch_size
        }
    }
}

/// Iterator that groups indices from an underlying iterator into batches
#[derive(Debug)]
pub struct BatchSamplerIter<I: Iterator<Item = usize>> {
    inner: I,
    batch_size: usize,
    drop_last: bool,
}

impl<I: Iterator<Item = usize>> BatchSamplerIter<I> {
    /// Create a new batch sampler iterator
    pub fn new(inner: I, batch_size: usize, drop_last: bool) -> Self {
        Self {
            inner,
            batch_size,
            drop_last,
        }
    }

    /// Get the batch size
    pub fn batch_size(&self) -> usize {
        self.batch_size
    }

    /// Check if dropping last incomplete batch
    pub fn drop_last(&self) -> bool {
        self.drop_last
    }
}

impl<I: Iterator<Item = usize>> Iterator for BatchSamplerIter<I> {
    type Item = Vec<usize>;

    fn next(&mut self) -> Option<Self::Item> {
        let mut batch = Vec::with_capacity(self.batch_size);

        // Collect items for this batch
        for _ in 0..self.batch_size {
            if let Some(item) = self.inner.next() {
                batch.push(item);
            } else {
                break;
            }
        }

        if batch.is_empty() {
            None
        } else if batch.len() < self.batch_size && self.drop_last {
            None
        } else {
            Some(batch)
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (lower, upper) = self.inner.size_hint();

        let lower_batches = if self.drop_last {
            lower / self.batch_size
        } else {
            (lower + self.batch_size - 1) / self.batch_size
        };

        let upper_batches = upper.map(|u| {
            if self.drop_last {
                u / self.batch_size
            } else {
                (u + self.batch_size - 1) / self.batch_size
            }
        });

        (lower_batches, upper_batches)
    }
}

/// Create a batch sampler from any sampler
///
/// Convenience function for creating a batch sampler.
///
/// # Arguments
///
/// * `sampler` - The underlying sampler
/// * `batch_size` - Size of each batch
/// * `drop_last` - Whether to drop the last incomplete batch
pub fn batch<S: Sampler>(sampler: S, batch_size: usize, drop_last: bool) -> BatchingSampler<S> {
    BatchingSampler::new(sampler, batch_size, drop_last)
}

/// Create a batch sampler that keeps the last incomplete batch
///
/// Convenience function for creating a batch sampler that doesn't drop
/// the last batch even if it's incomplete.
///
/// # Arguments
///
/// * `sampler` - The underlying sampler
/// * `batch_size` - Size of each batch
pub fn batch_keep_last<S: Sampler>(sampler: S, batch_size: usize) -> BatchingSampler<S> {
    BatchingSampler::new(sampler, batch_size, false)
}

/// Create a batch sampler that drops the last incomplete batch
///
/// Convenience function for creating a batch sampler that drops
/// the last batch if it's incomplete.
///
/// # Arguments
///
/// * `sampler` - The underlying sampler
/// * `batch_size` - Size of each batch
pub fn batch_drop_last<S: Sampler>(sampler: S, batch_size: usize) -> BatchingSampler<S> {
    BatchingSampler::new(sampler, batch_size, true)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sampler::basic::SequentialSampler;

    #[test]
    fn test_batching_sampler_basic() {
        let base_sampler = SequentialSampler::new(10);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, false);

        assert_eq!(batch_sampler.batch_size(), 3);
        assert!(!batch_sampler.drop_last());
        assert_eq!(batch_sampler.num_batches(), 4); // 10 items, 3 per batch = 4 batches

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 4);
        assert_eq!(batches[0], vec![0, 1, 2]);
        assert_eq!(batches[1], vec![3, 4, 5]);
        assert_eq!(batches[2], vec![6, 7, 8]);
        assert_eq!(batches[3], vec![9]); // Last incomplete batch
    }

    #[test]
    fn test_batching_sampler_drop_last() {
        let base_sampler = SequentialSampler::new(10);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, true);

        assert!(batch_sampler.drop_last());
        assert_eq!(batch_sampler.num_batches(), 3); // Drops last incomplete batch

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 3);
        assert_eq!(batches[0], vec![0, 1, 2]);
        assert_eq!(batches[1], vec![3, 4, 5]);
        assert_eq!(batches[2], vec![6, 7, 8]);
        // Last batch [9] is dropped
    }

    #[test]
    fn test_batching_sampler_exact_division() {
        let base_sampler = SequentialSampler::new(9);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, true);

        assert_eq!(batch_sampler.num_batches(), 3);

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 3);
        assert_eq!(batches[0], vec![0, 1, 2]);
        assert_eq!(batches[1], vec![3, 4, 5]);
        assert_eq!(batches[2], vec![6, 7, 8]);
    }

    #[test]
    fn test_batching_sampler_empty() {
        let base_sampler = SequentialSampler::new(0);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, false);

        assert_eq!(batch_sampler.num_batches(), 0);
        assert!(batch_sampler.is_empty());

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 0);
    }

    #[test]
    fn test_batching_sampler_single_item() {
        let base_sampler = SequentialSampler::new(1);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, false);

        assert_eq!(batch_sampler.num_batches(), 1);

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 1);
        assert_eq!(batches[0], vec![0]);
    }

    #[test]
    fn test_batching_sampler_single_item_drop_last() {
        let base_sampler = SequentialSampler::new(1);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, true);

        assert_eq!(batch_sampler.num_batches(), 0);

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 0);
    }

    #[test]
    #[should_panic(expected = "Batch size must be positive")]
    fn test_batching_sampler_zero_batch_size() {
        let base_sampler = SequentialSampler::new(10);
        BatchingSampler::new(base_sampler, 0, false);
    }

    #[test]
    fn test_batch_sampler_iter_size_hint() {
        let base_sampler = SequentialSampler::new(10);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, false);

        let iter = batch_sampler.iter();
        assert_eq!(iter.size_hint(), (4, Some(4)));

        let batch_sampler_drop = BatchingSampler::new(SequentialSampler::new(10), 3, true);
        let iter_drop = batch_sampler_drop.iter();
        assert_eq!(iter_drop.size_hint(), (3, Some(3)));
    }

    #[test]
    fn test_batching_sampler_into_sampler() {
        let base_sampler = SequentialSampler::new(5);
        let batch_sampler = BatchingSampler::new(base_sampler, 2, false);

        let recovered_sampler = batch_sampler.into_sampler();
        assert_eq!(recovered_sampler.len(), 5);
    }

    #[test]
    fn test_convenience_functions() {
        let base_sampler = SequentialSampler::new(10);

        let batch_keep = batch_keep_last(base_sampler.clone(), 3);
        assert!(!batch_keep.drop_last());
        assert_eq!(batch_keep.num_batches(), 4);

        let batch_drop = batch_drop_last(base_sampler.clone(), 3);
        assert!(batch_drop.drop_last());
        assert_eq!(batch_drop.num_batches(), 3);

        let batch_generic = batch(base_sampler, 3, true);
        assert!(batch_generic.drop_last());
        assert_eq!(batch_generic.num_batches(), 3);
    }

    #[test]
    fn test_batch_sampler_iter_properties() {
        let base_sampler = SequentialSampler::new(7);
        let batch_sampler = BatchingSampler::new(base_sampler, 3, false);

        let mut iter = batch_sampler.iter();
        assert_eq!(iter.batch_size(), 3);
        assert!(!iter.drop_last());

        // Test collecting batches one by one
        let batch1 = iter.next().expect("iterator should have a next element");
        assert_eq!(batch1, vec![0, 1, 2]);

        let batch2 = iter.next().expect("iterator should have a next element");
        assert_eq!(batch2, vec![3, 4, 5]);

        let batch3 = iter.next().expect("iterator should have a next element");
        assert_eq!(batch3, vec![6]);

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_batch_sizes() {
        // Test various batch sizes
        let test_cases = vec![
            (10, 1, false, 10), // Each item is its own batch
            (10, 10, false, 1), // Single batch with all items
            (10, 15, false, 1), // Batch size larger than dataset
            (0, 5, false, 0),   // Empty dataset
        ];

        for (dataset_size, batch_size, drop_last, expected_batches) in test_cases {
            if dataset_size == 0 && batch_size > 0 {
                // Skip invalid combinations handled by SequentialSampler
                continue;
            }

            let base_sampler = SequentialSampler::new(dataset_size);
            let batch_sampler = BatchingSampler::new(base_sampler, batch_size, drop_last);

            assert_eq!(
                batch_sampler.num_batches(),
                expected_batches,
                "Failed for dataset_size={}, batch_size={}, drop_last={}",
                dataset_size,
                batch_size,
                drop_last
            );

            let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
            assert_eq!(
                batches.len(),
                expected_batches,
                "Actual batch count doesn't match for dataset_size={}, batch_size={}, drop_last={}",
                dataset_size,
                batch_size,
                drop_last
            );
        }
    }

    #[test]
    fn test_edge_case_large_batch_size() {
        let base_sampler = SequentialSampler::new(3);
        let batch_sampler = BatchingSampler::new(base_sampler, 100, false);

        let batches: Vec<Vec<usize>> = batch_sampler.iter().collect();
        assert_eq!(batches.len(), 1);
        assert_eq!(batches[0], vec![0, 1, 2]);
    }
}