dataprof 0.7.0

High-performance data profiler with ISO 8000/25012 quality metrics for CSV, JSON/JSONL, and Parquet files
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
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
use std::collections::HashMap;

/// Enhanced reservoir sampling implementation based on Vitter's algorithm
///
/// This is a proper implementation of Algorithm R with optimizations:
/// - True randomness with seedable RNG for reproducibility
/// - Optimized skip calculation using geometric distribution
/// - Memory-efficient storage of sample indices
/// - Support for weighted sampling
#[derive(Debug, Clone)]
pub struct ReservoirSampler {
    /// Maximum size of the reservoir
    capacity: usize,
    /// Current sample (stores row indices)
    reservoir: Vec<usize>,
    /// Total number of records processed
    total_processed: usize,
    /// Random number generator (seeded for reproducibility)
    rng: ChaCha8Rng,
    /// Skip optimization - next record to consider
    next_record: usize,
    /// Statistics for analysis
    stats: ReservoirStats,
}

/// Statistics for reservoir sampling performance
#[derive(Debug, Clone, Default)]
pub struct ReservoirStats {
    pub records_processed: usize,
    pub records_sampled: usize,
    pub replacement_count: usize,
    pub skip_count: usize,
    pub efficiency_ratio: f64,
}

impl ReservoirSampler {
    /// Create a new reservoir sampler with specified capacity
    pub fn new(capacity: usize) -> Self {
        Self::seed(capacity, 42) // Default seed for reproducibility
    }

    /// Create a new reservoir sampler with custom seed
    pub fn seed(capacity: usize, seed: u64) -> Self {
        Self {
            capacity,
            reservoir: Vec::with_capacity(capacity),
            total_processed: 0,
            rng: ChaCha8Rng::seed_from_u64(seed),
            next_record: 0,
            stats: ReservoirStats::default(),
        }
    }

    /// Process a new record and decide if it should be included
    /// Returns true if the record is selected for the sample
    pub fn process_record(&mut self, record_index: usize) -> bool {
        self.total_processed += 1;
        self.stats.records_processed += 1;

        // Phase 1: Fill the reservoir with first k records
        if self.reservoir.len() < self.capacity {
            self.reservoir.push(record_index);
            self.stats.records_sampled += 1;
            return true;
        }

        // Phase 2: Reservoir is full, use replacement algorithm
        self.apply_vitter_algorithm(record_index)
    }

    /// Apply Vitter's Algorithm R with skip optimization
    fn apply_vitter_algorithm(&mut self, record_index: usize) -> bool {
        // Skip records using geometric distribution for efficiency
        if self.total_processed < self.next_record {
            return false;
        }

        // Calculate if this record should replace one in the reservoir
        let random_index = self.rng.random_range(0..self.total_processed);

        if random_index < self.capacity {
            // Replace the record at random_index in reservoir
            let replace_position = random_index % self.capacity;
            self.reservoir[replace_position] = record_index;
            self.stats.replacement_count += 1;
            self.stats.records_sampled += 1;

            // Calculate next skip using geometric distribution
            self.calculate_next_skip();

            return true;
        }

        false
    }

    /// Calculate next skip distance using geometric distribution
    /// This optimizes performance by skipping records that won't be selected
    fn calculate_next_skip(&mut self) {
        // Use geometric distribution to calculate skip distance
        // This is based on Vitter's Algorithm S optimization
        let u: f64 = self.rng.random();
        let skip = if u > 0.0 {
            ((self.total_processed as f64) * (u.powf(1.0 / self.capacity as f64) - 1.0)) as usize
        } else {
            1
        };

        self.next_record = self.total_processed + skip.max(1);
        self.stats.skip_count += skip;
    }

    /// Get current sample as a vector of indices
    pub fn get_sample_indices(&self) -> &[usize] {
        &self.reservoir
    }

    /// Get current sample size
    pub fn sample_size(&self) -> usize {
        self.reservoir.len()
    }

    /// Check if reservoir is full
    pub fn is_full(&self) -> bool {
        self.reservoir.len() >= self.capacity
    }

    /// Get sampling statistics
    pub fn get_stats(&self) -> &ReservoirStats {
        &self.stats
    }

    /// Calculate current sampling ratio
    pub fn sampling_ratio(&self) -> f64 {
        if self.total_processed > 0 {
            self.reservoir.len() as f64 / self.total_processed as f64
        } else {
            0.0
        }
    }

    /// Reset the sampler for reuse
    pub fn reset(&mut self) {
        self.reservoir.clear();
        self.total_processed = 0;
        self.next_record = 0;
        self.stats = ReservoirStats::default();
    }

    /// Set new seed for reproducible results
    pub fn set_seed(&mut self, seed: u64) {
        self.rng = ChaCha8Rng::seed_from_u64(seed);
    }

    /// Update efficiency statistics
    pub fn update_efficiency_stats(&mut self) {
        self.stats.efficiency_ratio = if self.stats.records_processed > 0 {
            self.stats.records_sampled as f64 / self.stats.records_processed as f64
        } else {
            0.0
        };
    }
}

/// Weighted reservoir sampling for stratified sampling
#[derive(Debug, Clone)]
pub struct WeightedReservoirSampler {
    base_sampler: ReservoirSampler,
    /// Weights for each record type/stratum
    weights: HashMap<String, f64>,
    /// Total weight processed
    total_weight: f64,
}

impl WeightedReservoirSampler {
    pub fn new(capacity: usize, weights: HashMap<String, f64>) -> Self {
        Self {
            base_sampler: ReservoirSampler::new(capacity),
            weights,
            total_weight: 0.0,
        }
    }

    /// Process a record with associated weight category
    pub fn process_weighted_record(&mut self, record_index: usize, category: &str) -> bool {
        let weight = self.weights.get(category).copied().unwrap_or(1.0);
        self.total_weight += weight;

        // Adjust sampling probability based on weight
        let adjusted_probability = weight / self.total_weight;
        let u: f64 = self.base_sampler.rng.random();

        if u < adjusted_probability {
            self.base_sampler.process_record(record_index)
        } else {
            self.base_sampler.total_processed += 1;
            false
        }
    }

    pub fn get_sample_indices(&self) -> &[usize] {
        self.base_sampler.get_sample_indices()
    }

    pub fn sampling_ratio(&self) -> f64 {
        self.base_sampler.sampling_ratio()
    }
}

/// Multi-reservoir sampling for handling multiple data types
#[derive(Debug)]
pub struct MultiReservoirSampler {
    reservoirs: HashMap<String, ReservoirSampler>,
    default_capacity: usize,
}

impl MultiReservoirSampler {
    pub fn new(default_capacity: usize) -> Self {
        Self {
            reservoirs: HashMap::new(),
            default_capacity,
        }
    }

    /// Process a record for a specific category/type
    pub fn process_categorized_record(&mut self, record_index: usize, category: &str) -> bool {
        let reservoir = self
            .reservoirs
            .entry(category.to_string())
            .or_insert_with(|| ReservoirSampler::new(self.default_capacity));

        reservoir.process_record(record_index)
    }

    /// Get combined sample from all reservoirs
    pub fn get_combined_sample(&self) -> Vec<usize> {
        let mut combined = Vec::new();

        for reservoir in self.reservoirs.values() {
            combined.extend_from_slice(reservoir.get_sample_indices());
        }

        // Sort for consistent ordering
        combined.sort_unstable();
        combined
    }

    /// Get samples by category
    pub fn get_samples_by_category(&self) -> HashMap<String, Vec<usize>> {
        self.reservoirs
            .iter()
            .map(|(category, reservoir)| {
                (
                    category.to_string(),
                    reservoir.get_sample_indices().to_vec(),
                )
            })
            .collect()
    }

    /// Get statistics for all reservoirs
    pub fn get_all_stats(&self) -> HashMap<String, ReservoirStats> {
        self.reservoirs
            .iter()
            .map(|(category, reservoir)| (category.to_string(), reservoir.get_stats().clone()))
            .collect()
    }
}

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

    #[test]
    fn test_basic_reservoir_sampling() {
        let mut sampler = ReservoirSampler::new(10);

        // Process 100 records
        let mut selected_count = 0;
        for i in 0..100 {
            if sampler.process_record(i) {
                selected_count += 1;
            }
        }

        // Should have exactly 10 samples
        assert_eq!(sampler.sample_size(), 10);
        assert_eq!(sampler.get_sample_indices().len(), 10);
        assert!(selected_count >= 10); // May be more due to replacements
    }

    #[test]
    fn test_reservoir_filling_phase() {
        let mut sampler = ReservoirSampler::new(5);

        // First 5 records should all be selected
        for i in 0..5 {
            assert!(sampler.process_record(i));
        }

        assert_eq!(sampler.sample_size(), 5);
        assert!(sampler.is_full());
    }

    #[test]
    fn test_replacement_phase() {
        let mut sampler = ReservoirSampler::seed(3, 42); // Fixed seed for reproducibility

        // Fill reservoir
        for i in 0..3 {
            sampler.process_record(i);
        }

        // Process more records
        let _initial_sample = sampler.get_sample_indices().to_vec();

        for i in 3..20 {
            sampler.process_record(i);
        }

        let final_sample = sampler.get_sample_indices().to_vec();

        // Sample size should remain the same
        assert_eq!(final_sample.len(), 3);

        // Some replacements should have occurred
        assert!(sampler.get_stats().replacement_count > 0);
    }

    #[test]
    fn test_sampling_ratio() {
        let mut sampler = ReservoirSampler::new(10);

        for i in 0..100 {
            sampler.process_record(i);
        }

        let ratio = sampler.sampling_ratio();
        assert!((ratio - 0.1).abs() < 0.01); // Should be ~10%
    }

    #[test]
    fn test_reset_functionality() {
        let mut sampler = ReservoirSampler::new(5);

        for i in 0..10 {
            sampler.process_record(i);
        }

        assert_eq!(sampler.sample_size(), 5);
        assert!(sampler.total_processed > 0);

        sampler.reset();

        assert_eq!(sampler.sample_size(), 0);
        assert_eq!(sampler.total_processed, 0);
    }

    #[test]
    fn test_weighted_sampling() {
        let mut weights = HashMap::new();
        weights.insert("high".to_string(), 3.0);
        weights.insert("low".to_string(), 1.0);

        let mut sampler = WeightedReservoirSampler::new(10, weights);

        let mut _high_selected = 0;
        let mut _low_selected = 0;

        // Process records with different weights
        for i in 0..50 {
            let category = if i % 2 == 0 { "high" } else { "low" };
            if sampler.process_weighted_record(i, category) {
                if category == "high" {
                    _high_selected += 1;
                } else {
                    _low_selected += 1;
                }
            }
        }

        // High weight records should be selected more frequently
        // This is probabilistic, so we allow some variance
        assert!(sampler.get_sample_indices().len() <= 10);
    }

    #[test]
    fn test_multi_reservoir() {
        let mut sampler = MultiReservoirSampler::new(5);

        for i in 0..20 {
            let category = format!("type_{}", i % 3);
            sampler.process_categorized_record(i, &category);
        }

        let combined = sampler.get_combined_sample();
        assert!(combined.len() <= 15); // Max 5 per category * 3 categories

        let by_category = sampler.get_samples_by_category();
        assert_eq!(by_category.len(), 3); // Should have 3 categories
    }

    #[test]
    fn test_deterministic_with_seed() {
        let mut sampler1 = ReservoirSampler::seed(5, 123);
        let mut sampler2 = ReservoirSampler::seed(5, 123);

        for i in 0..50 {
            sampler1.process_record(i);
            sampler2.process_record(i);
        }

        // Same seed should produce identical samples
        assert_eq!(sampler1.get_sample_indices(), sampler2.get_sample_indices());
    }
}