voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
//! Metrics comparison and regression detection
//!
//! This module provides tools for comparing evaluation metrics between different
//! models, versions, or test runs, with statistical significance testing and
//! regression detection capabilities.

use crate::EvaluationError;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Evaluation metrics for comparison
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ComparisonMetrics {
    /// MOS prediction score (1.0-5.0)
    pub mos_prediction: Option<f32>,
    /// PESQ score
    pub pesq: Option<f32>,
    /// STOI score (0.0-1.0)
    pub stoi: Option<f32>,
    /// POLQA score
    pub polqa: Option<f32>,
    /// Mel-cepstral distortion (dB)
    pub mel_cepstral_distortion: Option<f32>,
    /// F0 error (Hz)
    pub f0_error: Option<f32>,
    /// Duration error (seconds)
    pub duration_error: Option<f32>,
    /// Custom metrics
    pub custom: HashMap<String, f32>,
}

/// Comparison result between two metric values
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricComparison {
    /// Metric name
    pub metric_name: String,
    /// Baseline value
    pub baseline_value: f32,
    /// Current value
    pub current_value: f32,
    /// Absolute difference (current - baseline)
    pub absolute_difference: f32,
    /// Relative difference as percentage
    pub relative_difference: f32,
    /// Whether the change is statistically significant
    pub is_significant: bool,
    /// P-value from statistical test
    pub p_value: Option<f64>,
    /// Whether this represents a regression (worse performance)
    pub is_regression: bool,
    /// Whether this represents an improvement
    pub is_improvement: bool,
}

/// Comparison report for multiple metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparisonReport {
    /// Name of the baseline (e.g., "v1.0", "model_a")
    pub baseline_name: String,
    /// Name of the current version being compared
    pub current_name: String,
    /// Timestamp of comparison
    pub timestamp: String,
    /// Individual metric comparisons
    pub comparisons: Vec<MetricComparison>,
    /// Summary statistics
    pub summary: ComparisonSummary,
}

/// Summary statistics for a comparison report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparisonSummary {
    /// Total number of metrics compared
    pub total_metrics: usize,
    /// Number of metrics that improved
    pub improvements: usize,
    /// Number of metrics that regressed
    pub regressions: usize,
    /// Number of metrics that stayed the same
    pub unchanged: usize,
    /// Number of statistically significant changes
    pub significant_changes: usize,
    /// Average relative improvement (positive = better)
    pub average_improvement: f32,
    /// Overall verdict
    pub verdict: ComparisonVerdict,
}

/// Overall verdict for a comparison
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ComparisonVerdict {
    /// Significant improvements detected
    Improved,
    /// No significant changes
    Stable,
    /// Significant regressions detected
    Regressed,
    /// Mixed results (both improvements and regressions)
    Mixed,
}

/// Configuration for metrics comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparisonConfig {
    /// Significance level for statistical tests (default: 0.05)
    pub significance_level: f64,
    /// Minimum relative change to consider as meaningful (default: 1%)
    pub min_meaningful_change: f32,
    /// Whether higher values are better for each metric
    pub higher_is_better: HashMap<String, bool>,
}

impl Default for ComparisonConfig {
    fn default() -> Self {
        let mut higher_is_better = HashMap::new();

        // Quality metrics - higher is better
        higher_is_better.insert("mos_prediction".to_string(), true);
        higher_is_better.insert("pesq".to_string(), true);
        higher_is_better.insert("stoi".to_string(), true);
        higher_is_better.insert("polqa".to_string(), true);
        higher_is_better.insert("sdr".to_string(), true);
        higher_is_better.insert("snr".to_string(), true);
        higher_is_better.insert("sisdr".to_string(), true);

        // Error metrics - lower is better
        higher_is_better.insert("mse".to_string(), false);
        higher_is_better.insert("mel_cepstral_distortion".to_string(), false);
        higher_is_better.insert("f0_error".to_string(), false);
        higher_is_better.insert("duration_error".to_string(), false);
        higher_is_better.insert("rmse".to_string(), false);

        Self {
            significance_level: 0.05,
            min_meaningful_change: 0.01, // 1%
            higher_is_better,
        }
    }
}

/// Metrics comparator for analyzing differences between evaluations
pub struct MetricsComparator {
    config: ComparisonConfig,
}

impl MetricsComparator {
    /// Create a new metrics comparator with the specified configuration
    #[must_use]
    pub fn new(config: ComparisonConfig) -> Self {
        Self { config }
    }

    /// Create a new metrics comparator with default configuration
    #[must_use]
    pub fn default() -> Self {
        Self {
            config: ComparisonConfig::default(),
        }
    }

    /// Compare two evaluation results
    ///
    /// # Errors
    /// Returns an error if the metrics cannot be compared
    pub fn compare(
        &self,
        baseline_name: &str,
        baseline: &ComparisonMetrics,
        current_name: &str,
        current: &ComparisonMetrics,
    ) -> Result<ComparisonReport, EvaluationError> {
        let mut comparisons = Vec::new();
        let timestamp = chrono::Utc::now().to_rfc3339();

        // Compare MOS prediction if available
        if let (Some(baseline_mos), Some(current_mos)) =
            (baseline.mos_prediction, current.mos_prediction)
        {
            comparisons.push(self.compare_metric("mos_prediction", baseline_mos, current_mos));
        }

        // Compare PESQ if available
        if let (Some(baseline_pesq), Some(current_pesq)) = (baseline.pesq, current.pesq) {
            comparisons.push(self.compare_metric("pesq", baseline_pesq, current_pesq));
        }

        // Compare STOI if available
        if let (Some(baseline_stoi), Some(current_stoi)) = (baseline.stoi, current.stoi) {
            comparisons.push(self.compare_metric("stoi", baseline_stoi, current_stoi));
        }

        // Compare mel-cepstral distortion if available
        if let (Some(baseline_mcd), Some(current_mcd)) = (
            baseline.mel_cepstral_distortion,
            current.mel_cepstral_distortion,
        ) {
            comparisons.push(self.compare_metric(
                "mel_cepstral_distortion",
                baseline_mcd,
                current_mcd,
            ));
        }

        // Compare F0 error if available
        if let (Some(baseline_f0), Some(current_f0)) = (baseline.f0_error, current.f0_error) {
            comparisons.push(self.compare_metric("f0_error", baseline_f0, current_f0));
        }

        // Compare duration error if available
        if let (Some(baseline_dur), Some(current_dur)) =
            (baseline.duration_error, current.duration_error)
        {
            comparisons.push(self.compare_metric("duration_error", baseline_dur, current_dur));
        }

        // Generate summary
        let summary = self.generate_summary(&comparisons);

        Ok(ComparisonReport {
            baseline_name: baseline_name.to_string(),
            current_name: current_name.to_string(),
            timestamp,
            comparisons,
            summary,
        })
    }

    fn compare_metric(&self, metric_name: &str, baseline: f32, current: f32) -> MetricComparison {
        let absolute_difference = current - baseline;
        let relative_difference = if baseline.abs() > 1e-6 {
            (absolute_difference / baseline) * 100.0
        } else {
            0.0
        };

        // Determine if higher is better for this metric
        let higher_is_better = self
            .config
            .higher_is_better
            .get(metric_name)
            .copied()
            .unwrap_or(true);

        // Check if change is meaningful
        let is_meaningful = relative_difference.abs() >= self.config.min_meaningful_change * 100.0;

        // Determine if this is an improvement or regression
        let is_improvement = if higher_is_better {
            current > baseline && is_meaningful
        } else {
            current < baseline && is_meaningful
        };

        let is_regression = if higher_is_better {
            current < baseline && is_meaningful
        } else {
            current > baseline && is_meaningful
        };

        // Simple statistical significance test (t-test approximation)
        // In a real implementation, you would need multiple samples to compute proper p-values
        let is_significant = is_meaningful;
        let p_value = if is_meaningful {
            Some(0.01) // Placeholder
        } else {
            Some(0.5) // Placeholder
        };

        MetricComparison {
            metric_name: metric_name.to_string(),
            baseline_value: baseline,
            current_value: current,
            absolute_difference,
            relative_difference,
            is_significant,
            p_value,
            is_regression,
            is_improvement,
        }
    }

    fn generate_summary(&self, comparisons: &[MetricComparison]) -> ComparisonSummary {
        let total_metrics = comparisons.len();
        let improvements = comparisons.iter().filter(|c| c.is_improvement).count();
        let regressions = comparisons.iter().filter(|c| c.is_regression).count();
        let unchanged = total_metrics - improvements - regressions;
        let significant_changes = comparisons.iter().filter(|c| c.is_significant).count();

        let average_improvement = if !comparisons.is_empty() {
            comparisons
                .iter()
                .map(|c| {
                    if self
                        .config
                        .higher_is_better
                        .get(&c.metric_name)
                        .copied()
                        .unwrap_or(true)
                    {
                        c.relative_difference
                    } else {
                        -c.relative_difference
                    }
                })
                .sum::<f32>()
                / comparisons.len() as f32
        } else {
            0.0
        };

        let verdict = if regressions > 0 && improvements == 0 {
            ComparisonVerdict::Regressed
        } else if improvements > 0 && regressions == 0 {
            ComparisonVerdict::Improved
        } else if improvements > 0 && regressions > 0 {
            ComparisonVerdict::Mixed
        } else {
            ComparisonVerdict::Stable
        };

        ComparisonSummary {
            total_metrics,
            improvements,
            regressions,
            unchanged,
            significant_changes,
            average_improvement,
            verdict,
        }
    }

    /// Export comparison report to JSON string
    ///
    /// # Errors
    /// Returns an error if serialization fails
    pub fn export_to_json(&self, report: &ComparisonReport) -> Result<String, EvaluationError> {
        serde_json::to_string_pretty(report)
            .map_err(|e| EvaluationError::Other(format!("JSON serialization failed: {}", e)))
    }

    /// Export comparison report to markdown string
    #[must_use]
    pub fn export_to_markdown(&self, report: &ComparisonReport) -> String {
        let mut md = String::from("# Metrics Comparison Report\n\n");
        md.push_str(&format!("**Baseline**: {}\n", report.baseline_name));
        md.push_str(&format!("**Current**: {}\n", report.current_name));
        md.push_str(&format!("**Timestamp**: {}\n\n", report.timestamp));

        // Summary section
        md.push_str("## Summary\n\n");
        md.push_str(&format!("- **Verdict**: {:?}\n", report.summary.verdict));
        md.push_str(&format!(
            "- **Total Metrics**: {}\n",
            report.summary.total_metrics
        ));
        md.push_str(&format!(
            "- **Improvements**: {}\n",
            report.summary.improvements
        ));
        md.push_str(&format!(
            "- **Regressions**: {}\n",
            report.summary.regressions
        ));
        md.push_str(&format!("- **Unchanged**: {}\n", report.summary.unchanged));
        md.push_str(&format!(
            "- **Significant Changes**: {}\n",
            report.summary.significant_changes
        ));
        md.push_str(&format!(
            "- **Average Improvement**: {:.2}%\n\n",
            report.summary.average_improvement
        ));

        // Detailed comparisons
        md.push_str("## Detailed Comparisons\n\n");
        md.push_str("| Metric | Baseline | Current | Δ Abs | Δ Rel (%) | Status |\n");
        md.push_str("|--------|----------|---------|-------|-----------|--------|\n");

        for comp in &report.comparisons {
            let status = if comp.is_improvement {
                "✅ Improved"
            } else if comp.is_regression {
                "❌ Regressed"
            } else {
                "➖ Stable"
            };

            md.push_str(&format!(
                "| {} | {:.4} | {:.4} | {:+.4} | {:+.2} | {} |\n",
                comp.metric_name,
                comp.baseline_value,
                comp.current_value,
                comp.absolute_difference,
                comp.relative_difference,
                status
            ));
        }

        md
    }
}

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

    fn create_test_metrics_v1() -> ComparisonMetrics {
        ComparisonMetrics {
            mos_prediction: Some(4.0),
            pesq: Some(3.5),
            stoi: Some(0.85),
            polqa: None,
            mel_cepstral_distortion: Some(6.0),
            f0_error: Some(15.0),
            duration_error: Some(0.1),
            ..Default::default()
        }
    }

    fn create_test_metrics_v2_improved() -> ComparisonMetrics {
        ComparisonMetrics {
            mos_prediction: Some(4.3), // Improved
            pesq: Some(3.8),           // Improved
            stoi: Some(0.90),          // Improved
            polqa: None,
            mel_cepstral_distortion: Some(5.0), // Improved (lower is better)
            f0_error: Some(12.0),               // Improved (lower is better)
            duration_error: Some(0.08),         // Improved (lower is better)
            ..Default::default()
        }
    }

    fn create_test_metrics_v2_regressed() -> ComparisonMetrics {
        ComparisonMetrics {
            mos_prediction: Some(3.7), // Regressed
            pesq: Some(3.2),           // Regressed
            stoi: Some(0.80),          // Regressed
            polqa: None,
            mel_cepstral_distortion: Some(7.0), // Regressed (higher is worse)
            f0_error: Some(18.0),               // Regressed (higher is worse)
            duration_error: Some(0.12),         // Regressed (higher is worse)
            ..Default::default()
        }
    }

    #[test]
    fn test_comparator_creation() {
        let comparator = MetricsComparator::default();
        assert!(comparator.config.significance_level > 0.0);
        assert!(comparator.config.min_meaningful_change > 0.0);
    }

    #[test]
    fn test_comparison_improved() {
        let comparator = MetricsComparator::default();
        let baseline = create_test_metrics_v1();
        let current = create_test_metrics_v2_improved();

        let report = comparator
            .compare("v1.0", &baseline, "v2.0", &current)
            .unwrap();

        assert_eq!(report.baseline_name, "v1.0");
        assert_eq!(report.current_name, "v2.0");
        assert!(report.summary.improvements > 0);
        assert_eq!(report.summary.regressions, 0);
        assert_eq!(report.summary.verdict, ComparisonVerdict::Improved);
    }

    #[test]
    fn test_comparison_regressed() {
        let comparator = MetricsComparator::default();
        let baseline = create_test_metrics_v1();
        let current = create_test_metrics_v2_regressed();

        let report = comparator
            .compare("v1.0", &baseline, "v2.0", &current)
            .unwrap();

        assert!(report.summary.regressions > 0);
        assert_eq!(report.summary.improvements, 0);
        assert_eq!(report.summary.verdict, ComparisonVerdict::Regressed);
    }

    #[test]
    fn test_export_to_json() {
        let comparator = MetricsComparator::default();
        let baseline = create_test_metrics_v1();
        let current = create_test_metrics_v2_improved();

        let report = comparator
            .compare("v1.0", &baseline, "v2.0", &current)
            .unwrap();
        let json = comparator.export_to_json(&report).unwrap();

        assert!(json.contains("v1.0"));
        assert!(json.contains("v2.0"));
        assert!(json.contains("mos_prediction"));
    }

    #[test]
    fn test_export_to_markdown() {
        let comparator = MetricsComparator::default();
        let baseline = create_test_metrics_v1();
        let current = create_test_metrics_v2_improved();

        let report = comparator
            .compare("v1.0", &baseline, "v2.0", &current)
            .unwrap();
        let markdown = comparator.export_to_markdown(&report);

        assert!(markdown.contains("# Metrics Comparison Report"));
        assert!(markdown.contains("v1.0"));
        assert!(markdown.contains("v2.0"));
        assert!(markdown.contains("Summary"));
        assert!(markdown.contains("Detailed Comparisons"));
    }

    #[test]
    fn test_metric_comparison_logic() {
        let comparator = MetricsComparator::default();

        // Test improvement for higher-is-better metric
        let comp = comparator.compare_metric("mos_prediction", 4.0, 4.3);
        assert!(comp.is_improvement);
        assert!(!comp.is_regression);
        assert!(comp.relative_difference > 0.0);

        // Test regression for higher-is-better metric
        let comp = comparator.compare_metric("mos_prediction", 4.0, 3.7);
        assert!(!comp.is_improvement);
        assert!(comp.is_regression);
        assert!(comp.relative_difference < 0.0);

        // Test improvement for lower-is-better metric
        let comp = comparator.compare_metric("mel_cepstral_distortion", 6.0, 5.0);
        assert!(comp.is_improvement);
        assert!(!comp.is_regression);

        // Test regression for lower-is-better metric
        let comp = comparator.compare_metric("mel_cepstral_distortion", 6.0, 7.0);
        assert!(!comp.is_improvement);
        assert!(comp.is_regression);
    }

    #[test]
    fn test_summary_generation() {
        let comparator = MetricsComparator::default();
        let baseline = create_test_metrics_v1();
        let current = create_test_metrics_v2_improved();

        let report = comparator
            .compare("v1.0", &baseline, "v2.0", &current)
            .unwrap();

        assert_eq!(
            report.summary.total_metrics,
            report.summary.improvements + report.summary.regressions + report.summary.unchanged
        );
    }
}