sklears-utils 0.1.1

Shared utilities for sklears: validation, data generation, helpers
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! Debug utilities for development and troubleshooting
//!
//! This module provides debugging helper functions, assertion macros,
//! test data generation, and diagnostic utilities for ML development.

use crate::{UtilsError, UtilsResult};
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::time::{Duration, Instant};

/// Debug context for collecting debugging information
#[derive(Debug, Clone)]
pub struct DebugContext {
    pub module: String,
    pub function: String,
    pub line: u32,
    pub timestamp: Instant,
    pub metadata: HashMap<String, String>,
}

impl DebugContext {
    /// Create a new debug context
    pub fn new(module: &str, function: &str, line: u32) -> Self {
        Self {
            module: module.to_string(),
            function: function.to_string(),
            line,
            timestamp: Instant::now(),
            metadata: HashMap::new(),
        }
    }

    /// Add metadata to the debug context
    pub fn with_metadata(mut self, key: &str, value: &str) -> Self {
        self.metadata.insert(key.to_string(), value.to_string());
        self
    }

    /// Format the debug context as a string
    pub fn format(&self) -> String {
        let mut result = format!("{}::{}:{}", self.module, self.function, self.line);
        if !self.metadata.is_empty() {
            result.push_str(" {");
            let metadata_strs: Vec<String> = self
                .metadata
                .iter()
                .map(|(k, v)| format!("{k}={v}"))
                .collect();
            result.push_str(&metadata_strs.join(", "));
            result.push('}');
        }
        result
    }
}

/// Macro for creating debug contexts easily
#[macro_export]
macro_rules! debug_context {
    () => {
        DebugContext::new(module_path!(), function_name!(), line!())
    };
    ($($key:expr => $value:expr),*) => {
        DebugContext::new(module_path!(), function_name!(), line!())
            $(.with_metadata($key, &$value.to_string()))*
    };
}

/// Enhanced assertion macro with debugging information
#[macro_export]
macro_rules! debug_assert_msg {
    ($condition:expr, $msg:expr) => {
        if !$condition {
            panic!(
                "Assertion failed at {}:{}:{}: {}\nCondition: {}",
                module_path!(),
                file!(),
                line!(),
                $msg,
                stringify!($condition)
            );
        }
    };
    ($condition:expr, $msg:expr, $($arg:tt)*) => {
        if !$condition {
            panic!(
                "Assertion failed at {}:{}:{}: {}\nCondition: {}",
                module_path!(),
                file!(),
                line!(),
                format!($msg, $($arg)*),
                stringify!($condition)
            );
        }
    };
}

/// Array debugging utilities
pub struct ArrayDebugger;

impl ArrayDebugger {
    /// Debug array statistics
    pub fn array_stats<T: fmt::Debug + Clone>(array: &[T]) -> String {
        format!(
            "Array[len={}, type={}]",
            array.len(),
            std::any::type_name::<T>()
        )
    }

    /// Debug array shape for multidimensional arrays
    pub fn array_shape_info(shape: &[usize]) -> String {
        format!(
            "Shape: {:?}, Total elements: {}",
            shape,
            shape.iter().product::<usize>()
        )
    }

    /// Find array differences for debugging
    pub fn compare_arrays<T: PartialEq + fmt::Debug>(a: &[T], b: &[T]) -> Vec<String> {
        let mut differences = Vec::new();

        if a.len() != b.len() {
            differences.push(format!("Length mismatch: {} vs {}", b.len(), a.len()));
        }

        let min_len = a.len().min(b.len());
        for i in 0..min_len {
            if a[i] != b[i] {
                differences.push(format!("Element {} differs: {:?} vs {:?}", i, a[i], b[i]));
            }
        }

        differences
    }

    /// Check for NaN and infinite values in float arrays
    pub fn check_float_array(array: &[f64]) -> Vec<String> {
        let mut issues = Vec::new();

        for (i, &value) in array.iter().enumerate() {
            if value.is_nan() {
                issues.push(format!("NaN at index {i}"));
            } else if value.is_infinite() {
                issues.push(format!("Infinite value at index {i}: {value}"));
            }
        }

        issues
    }
}

/// Memory debugging utilities
pub struct MemoryDebugger;

impl MemoryDebugger {
    /// Get memory usage information (simplified version)
    pub fn memory_info() -> String {
        // Note: In a real implementation, you'd use system calls or external crates
        // for actual memory information. This is a placeholder.
        "Memory debugging not fully implemented in this version".to_string()
    }

    /// Debug heap allocations (placeholder)
    pub fn heap_info() -> String {
        "Heap debugging requires external dependencies".to_string()
    }

    /// Check for potential memory leaks (placeholder)
    pub fn leak_check() -> Vec<String> {
        vec!["Memory leak detection requires runtime support".to_string()]
    }
}

/// Performance debugging utilities
#[derive(Debug, Clone)]
pub struct PerformanceDebugger {
    timers: HashMap<String, Instant>,
    durations: HashMap<String, Vec<Duration>>,
}

impl Default for PerformanceDebugger {
    fn default() -> Self {
        Self::new()
    }
}

impl PerformanceDebugger {
    /// Create a new performance debugger
    pub fn new() -> Self {
        Self {
            timers: HashMap::new(),
            durations: HashMap::new(),
        }
    }

    /// Start timing an operation
    pub fn start_timer(&mut self, name: &str) {
        self.timers.insert(name.to_string(), Instant::now());
    }

    /// Stop timing and record duration
    pub fn stop_timer(&mut self, name: &str) -> Option<Duration> {
        if let Some(start) = self.timers.remove(name) {
            let duration = start.elapsed();
            self.durations
                .entry(name.to_string())
                .or_default()
                .push(duration);
            Some(duration)
        } else {
            None
        }
    }

    /// Get timing statistics
    pub fn timing_stats(&self, name: &str) -> Option<TimingStats> {
        self.durations.get(name).map(|durations| {
            let count = durations.len();
            let total: Duration = durations.iter().sum();
            let avg = total / count as u32;
            let min = *durations.iter().min().expect("operation should succeed");
            let max = *durations.iter().max().expect("operation should succeed");

            TimingStats {
                name: name.to_string(),
                count,
                total,
                average: avg,
                min,
                max,
            }
        })
    }

    /// Get all timing statistics
    pub fn all_stats(&self) -> Vec<TimingStats> {
        self.durations
            .keys()
            .filter_map(|name| self.timing_stats(name))
            .collect()
    }

    /// Format timing report
    pub fn timing_report(&self) -> String {
        let mut report = String::from("Performance Report:\n");

        for stats in self.all_stats() {
            report.push_str(&format!(
                "  {}: {} calls, avg: {:?}, min: {:?}, max: {:?}, total: {:?}\n",
                stats.name, stats.count, stats.average, stats.min, stats.max, stats.total
            ));
        }

        report
    }
}

/// Timing statistics for performance analysis
#[derive(Debug, Clone)]
pub struct TimingStats {
    pub name: String,
    pub count: usize,
    pub total: Duration,
    pub average: Duration,
    pub min: Duration,
    pub max: Duration,
}

/// Test data generation utilities
pub struct TestDataGenerator;

impl TestDataGenerator {
    /// Generate random integers in a range
    pub fn random_integers(count: usize, min: i32, max: i32) -> Vec<i32> {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::Hasher;

        let mut result = Vec::with_capacity(count);
        let range = (max - min + 1) as u64;

        for i in 0..count {
            let mut hasher = DefaultHasher::new();
            (i as u64 + 12345).hash(&mut hasher); // Add seed for better distribution
            let hash_value = hasher.finish();
            let value = min + ((hash_value % range) as i32);
            result.push(value);
        }

        result
    }

    /// Generate random floats in a range
    pub fn random_floats(count: usize, min: f64, max: f64) -> Vec<f64> {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::Hasher;

        let mut result = Vec::with_capacity(count);

        for i in 0..count {
            let mut hasher = DefaultHasher::new();
            (i as u64 + 54321).hash(&mut hasher); // Add different seed
            let hash_value = hasher.finish();
            let normalized = (hash_value as f64) / (u64::MAX as f64);
            let value = min + normalized * (max - min);
            result.push(value);
        }

        result
    }

    /// Generate test matrix data
    pub fn test_matrix(rows: usize, cols: usize) -> Vec<Vec<f64>> {
        let mut matrix = Vec::with_capacity(rows);

        for i in 0..rows {
            let mut row = Vec::with_capacity(cols);
            for j in 0..cols {
                let value = (i * cols + j) as f64 / (rows * cols) as f64;
                row.push(value);
            }
            matrix.push(row);
        }

        matrix
    }

    /// Generate test strings
    pub fn test_strings(count: usize) -> Vec<String> {
        let prefixes = ["test", "data", "sample", "debug", "item"];
        let mut result = Vec::with_capacity(count);

        for i in 0..count {
            let prefix = prefixes[i % prefixes.len()];
            result.push(format!("{prefix}_{i}"));
        }

        result
    }

    /// Generate pathological test cases
    pub fn pathological_cases() -> HashMap<String, Vec<f64>> {
        let mut cases = HashMap::new();

        cases.insert("empty".to_string(), vec![]);
        cases.insert("single".to_string(), vec![1.0]);
        cases.insert("zeros".to_string(), vec![0.0; 10]);
        cases.insert("ones".to_string(), vec![1.0; 10]);
        cases.insert("alternating".to_string(), vec![1.0, -1.0, 1.0, -1.0, 1.0]);
        cases.insert("large_values".to_string(), vec![1e10, 1e11, 1e12]);
        cases.insert("small_values".to_string(), vec![1e-10, 1e-11, 1e-12]);
        cases.insert(
            "mixed_signs".to_string(),
            vec![-100.0, -1.0, 0.0, 1.0, 100.0],
        );

        // Add special float values
        cases.insert(
            "special_floats".to_string(),
            vec![
                f64::INFINITY,
                f64::NEG_INFINITY,
                f64::NAN,
                f64::MIN,
                f64::MAX,
                f64::EPSILON,
            ],
        );

        cases
    }
}

/// Diagnostic utilities for troubleshooting
pub struct DiagnosticTools;

impl DiagnosticTools {
    /// Run basic system diagnostics
    pub fn system_check() -> Vec<String> {
        let mut checks = Vec::new();

        // Check basic Rust environment
        checks.push(format!("Target architecture: {}", std::env::consts::ARCH));
        checks.push(format!("Operating system: {}", std::env::consts::OS));
        checks.push(format!(
            "Profile: {}",
            if cfg!(debug_assertions) {
                "debug"
            } else {
                "release"
            }
        ));

        // Check available features
        checks.push(format!(
            "Float support: {}",
            if f64::INFINITY.is_infinite() {
                "OK"
            } else {
                "FAIL"
            }
        ));
        checks.push(format!(
            "Threading: {}",
            if std::thread::available_parallelism().is_ok() {
                "OK"
            } else {
                "LIMITED"
            }
        ));

        checks
    }

    /// Validate algorithm inputs
    pub fn validate_ml_inputs(
        features: &[Vec<f64>],
        targets: Option<&[f64]>,
    ) -> UtilsResult<Vec<String>> {
        let mut warnings = Vec::new();

        if features.is_empty() {
            return Err(UtilsError::InvalidParameter(
                "Empty feature matrix".to_string(),
            ));
        }

        let feature_count = features[0].len();

        // Check feature consistency
        for (i, row) in features.iter().enumerate() {
            if row.len() != feature_count {
                warnings.push(format!(
                    "Row {} has {} features, expected {}",
                    i,
                    row.len(),
                    feature_count
                ));
            }

            // Check for NaN/infinite values
            for (j, &value) in row.iter().enumerate() {
                if value.is_nan() {
                    warnings.push(format!("NaN found at row {i}, col {j}"));
                } else if value.is_infinite() {
                    warnings.push(format!("Infinite value found at row {i}, col {j}: {value}"));
                }
            }
        }

        // Check targets if provided
        if let Some(targets) = targets {
            if targets.len() != features.len() {
                warnings.push(format!(
                    "Target count ({}) doesn't match sample count ({})",
                    targets.len(),
                    features.len()
                ));
            }

            for (i, &target) in targets.iter().enumerate() {
                if target.is_nan() {
                    warnings.push(format!("NaN target at index {i}"));
                } else if target.is_infinite() {
                    warnings.push(format!("Infinite target at index {i}: {target}"));
                }
            }
        }

        Ok(warnings)
    }

    /// Visualize data distribution (simplified text histogram)
    pub fn text_histogram(data: &[f64], bins: usize) -> String {
        if data.is_empty() {
            return "No data to visualize".to_string();
        }

        let min = data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max = data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        if min == max {
            return format!("All values equal: {min}");
        }

        let bin_width = (max - min) / bins as f64;
        let mut counts = vec![0; bins];

        for &value in data {
            let bin = ((value - min) / bin_width).floor() as usize;
            let bin = bin.min(bins - 1);
            counts[bin] += 1;
        }

        let max_count = *counts.iter().max().expect("operation should succeed");
        let scale = 50.0 / max_count as f64;

        let mut result = String::from("Data Distribution:\n");
        for (i, &count) in counts.iter().enumerate() {
            let bin_start = min + i as f64 * bin_width;
            let bin_end = bin_start + bin_width;
            let bar_length = (count as f64 * scale) as usize;
            let bar = "".repeat(bar_length);

            result.push_str(&format!(
                "[{bin_start:8.2} - {bin_end:8.2}]: {count:4} {bar}\n"
            ));
        }

        result
    }
}

/// Macro for quick performance timing
#[macro_export]
macro_rules! time_it {
    ($name:expr, $code:block) => {{
        let start = std::time::Instant::now();
        let result = $code;
        let duration = start.elapsed();
        println!("{}: {:?}", $name, duration);
        result
    }};
}

/// Macro for conditional debugging output
#[macro_export]
macro_rules! debug_println {
    ($($arg:tt)*) => {
        if cfg!(debug_assertions) {
            println!("[DEBUG] {}", format!($($arg)*));
        }
    };
}

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

    #[test]
    fn test_debug_context() {
        let ctx = DebugContext::new("test_module", "test_function", 42)
            .with_metadata("key1", "value1")
            .with_metadata("key2", "value2");

        let formatted = ctx.format();
        assert!(formatted.contains("test_module::test_function:42"));
        assert!(formatted.contains("key1=value1"));
        assert!(formatted.contains("key2=value2"));
    }

    #[test]
    fn test_array_debugger() {
        let array = vec![1, 2, 3, 4, 5];
        let stats = ArrayDebugger::array_stats(&array);
        assert!(stats.contains("len=5"));

        let shape_info = ArrayDebugger::array_shape_info(&[2, 3, 4]);
        assert!(shape_info.contains("Total elements: 24"));

        let diffs = ArrayDebugger::compare_arrays(&[1, 2, 3], &[1, 2, 4]);
        assert_eq!(diffs.len(), 1);
        assert!(diffs[0].contains("Element 2 differs"));
    }

    #[test]
    fn test_float_array_check() {
        let array = vec![1.0, 2.0, f64::NAN, f64::INFINITY, 5.0];
        let issues = ArrayDebugger::check_float_array(&array);
        assert_eq!(issues.len(), 2);
        assert!(issues.iter().any(|s| s.contains("NaN")));
        assert!(issues.iter().any(|s| s.contains("Infinite")));
    }

    #[test]
    fn test_performance_debugger() {
        let mut debugger = PerformanceDebugger::new();

        debugger.start_timer("test_operation");
        std::thread::sleep(std::time::Duration::from_millis(1));
        let duration = debugger.stop_timer("test_operation");

        assert!(duration.is_some());
        assert!(duration.expect("operation should succeed").as_millis() >= 1);

        let stats = debugger.timing_stats("test_operation");
        assert!(stats.is_some());
        assert_eq!(stats.expect("operation should succeed").count, 1);
    }

    #[test]
    fn test_test_data_generator() {
        let integers = TestDataGenerator::random_integers(10, 1, 100);
        assert_eq!(integers.len(), 10);
        assert!(integers.iter().all(|&x| (1..=100).contains(&x)));

        let floats = TestDataGenerator::random_floats(10, 0.0, 1.0);
        assert_eq!(floats.len(), 10);
        assert!(floats.iter().all(|&x| (0.0..=1.0).contains(&x)));

        let matrix = TestDataGenerator::test_matrix(3, 4);
        assert_eq!(matrix.len(), 3);
        assert!(matrix.iter().all(|row| row.len() == 4));

        let strings = TestDataGenerator::test_strings(5);
        assert_eq!(strings.len(), 5);
        assert!(strings.iter().all(|s| !s.is_empty()));
    }

    #[test]
    fn test_pathological_cases() {
        let cases = TestDataGenerator::pathological_cases();
        assert!(cases.contains_key("empty"));
        assert!(cases.contains_key("special_floats"));
        assert_eq!(cases["empty"].len(), 0);
        assert_eq!(cases["single"].len(), 1);
    }

    #[test]
    fn test_diagnostic_tools() {
        let checks = DiagnosticTools::system_check();
        assert!(!checks.is_empty());
        assert!(checks.iter().any(|s| s.contains("Target architecture")));

        let features = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let targets = vec![1.0, 2.0];

        let warnings = DiagnosticTools::validate_ml_inputs(&features, Some(&targets))
            .expect("operation should succeed");
        assert!(warnings.is_empty());

        // Test with inconsistent data
        let bad_features = vec![vec![1.0, 2.0], vec![3.0]];
        let warnings = DiagnosticTools::validate_ml_inputs(&bad_features, Some(&targets))
            .expect("operation should succeed");
        assert!(!warnings.is_empty());
    }

    #[test]
    fn test_text_histogram() {
        let data = vec![1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0];
        let histogram = DiagnosticTools::text_histogram(&data, 5);
        assert!(histogram.contains("Data Distribution:"));
        assert!(histogram.contains(""));

        let empty_data = vec![];
        let empty_histogram = DiagnosticTools::text_histogram(&empty_data, 5);
        assert!(empty_histogram.contains("No data"));

        let uniform_data = vec![1.0, 1.0, 1.0];
        let uniform_histogram = DiagnosticTools::text_histogram(&uniform_data, 5);
        assert!(uniform_histogram.contains("All values equal"));
    }
}