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
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
//! Ground truth dataset management for evaluation validation
//!
//! This module provides comprehensive ground truth dataset management capabilities
//! for speech synthesis evaluation, including dataset cataloging, annotation support,
//! versioning, and validation against reference standards.

use crate::data_quality_validation::{DataQualityValidator, DatasetValidationReport};
use crate::VoirsError;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Ground truth dataset management errors
#[derive(Error, Debug)]
pub enum GroundTruthError {
    /// Dataset not found
    #[error("Dataset not found: {0}")]
    DatasetNotFound(String),
    /// Invalid dataset format
    #[error("Invalid dataset format: {0}")]
    InvalidFormat(String),
    /// Annotation validation failed
    #[error("Annotation validation failed: {0}")]
    AnnotationValidationFailed(String),
    /// Version conflict detected
    #[error("Version conflict detected: {0}")]
    VersionConflict(String),
    /// Dataset corruption detected
    #[error("Dataset corruption detected: {0}")]
    DatasetCorruption(String),
    /// IO error
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),
    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// Dataset annotation quality levels
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AnnotationQuality {
    /// Expert-level annotations with high confidence
    Expert,
    /// Professional-level annotations
    Professional,
    /// Community-contributed annotations
    Community,
    /// Automatically generated annotations
    Automatic,
    /// Research-quality annotations
    Research,
}

/// Ground truth annotation types
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum AnnotationType {
    /// Overall quality scores
    QualityScore,
    /// Pronunciation accuracy
    PronunciationAccuracy,
    /// Naturalness ratings
    Naturalness,
    /// Intelligibility scores
    Intelligibility,
    /// Emotional appropriateness
    EmotionalExpression,
    /// Prosodic correctness
    ProsodicAccuracy,
    /// Speaker similarity
    SpeakerSimilarity,
    /// Technical quality metrics
    TechnicalQuality,
}

/// Individual ground truth annotation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroundTruthAnnotation {
    /// Annotation ID
    pub id: String,
    /// Audio sample ID
    pub sample_id: String,
    /// Annotation type
    pub annotation_type: AnnotationType,
    /// Annotation value (0.0 - 1.0 or specific scale)
    pub value: f64,
    /// Scale used for annotation (e.g., "MOS_5", "binary", "percentage")
    pub scale: String,
    /// Annotator ID
    pub annotator_id: String,
    /// Annotation quality level
    pub quality_level: AnnotationQuality,
    /// Confidence score (0.0 - 1.0)
    pub confidence: f64,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Optional text description
    pub description: Option<String>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Audio sample in ground truth dataset
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroundTruthSample {
    /// Sample ID
    pub id: String,
    /// Original audio file path
    pub audio_path: PathBuf,
    /// Reference audio path (if available)
    pub reference_path: Option<PathBuf>,
    /// Transcript text
    pub transcript: String,
    /// Language code
    pub language: String,
    /// Speaker ID
    pub speaker_id: String,
    /// Sample rate
    pub sample_rate: u32,
    /// Duration in seconds
    pub duration: f64,
    /// Sample metadata
    pub metadata: HashMap<String, String>,
    /// Associated annotations
    pub annotations: Vec<GroundTruthAnnotation>,
    /// Quality validation status
    pub validation_status: ValidationStatus,
}

/// Dataset validation status
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ValidationStatus {
    /// Not yet validated
    Pending,
    /// Passed validation
    Valid,
    /// Failed validation
    Invalid,
    /// Validation in progress
    InProgress,
    /// Requires manual review
    NeedsReview,
}

/// Ground truth dataset metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GroundTruthDataset {
    /// Dataset ID
    pub id: String,
    /// Dataset name
    pub name: String,
    /// Dataset version
    pub version: String,
    /// Dataset description
    pub description: String,
    /// Creation date
    pub created_at: DateTime<Utc>,
    /// Last modification date
    pub modified_at: DateTime<Utc>,
    /// Dataset creator/organization
    pub creator: String,
    /// Dataset license
    pub license: String,
    /// Supported languages
    pub languages: Vec<String>,
    /// Number of samples
    pub sample_count: usize,
    /// Total duration in seconds
    pub total_duration: f64,
    /// Dataset purpose/domain
    pub domain: String,
    /// Annotation guidelines URL or description
    pub annotation_guidelines: Option<String>,
    /// Dataset samples
    pub samples: Vec<GroundTruthSample>,
    /// Quality metrics
    pub quality_metrics: DatasetQualityMetrics,
    /// Dataset tags
    pub tags: Vec<String>,
    /// Dataset metadata
    pub metadata: HashMap<String, String>,
}

/// Dataset quality metrics summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetQualityMetrics {
    /// Overall quality score (0.0 - 1.0)
    pub overall_quality: f64,
    /// Annotation consistency score
    pub annotation_consistency: f64,
    /// Inter-annotator agreement (if multiple annotators)
    pub inter_annotator_agreement: Option<f64>,
    /// Audio quality score
    pub audio_quality: f64,
    /// Metadata completeness score
    pub metadata_completeness: f64,
    /// Validation completion percentage
    pub validation_completion: f64,
    /// Quality distribution by annotation type
    pub quality_by_type: HashMap<AnnotationType, f64>,
}

/// Dataset version information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetVersion {
    /// Version string
    pub version: String,
    /// Creation date
    pub created_at: DateTime<Utc>,
    /// Version description/changelog
    pub description: String,
    /// Changes made in this version
    pub changes: Vec<String>,
    /// Previous version (if any)
    pub previous_version: Option<String>,
    /// Dataset snapshot path
    pub dataset_path: PathBuf,
    /// Version metadata
    pub metadata: HashMap<String, String>,
}

/// Ground truth dataset manager
#[derive(Debug)]
pub struct GroundTruthManager {
    /// Base directory for datasets
    base_path: PathBuf,
    /// Dataset catalog
    catalog: HashMap<String, GroundTruthDataset>,
    /// Version history
    version_history: HashMap<String, Vec<DatasetVersion>>,
    /// Data quality validator
    validator: DataQualityValidator,
}

impl GroundTruthManager {
    /// Create a new ground truth dataset manager
    pub fn new(base_path: PathBuf) -> Self {
        Self {
            base_path,
            catalog: HashMap::new(),
            version_history: HashMap::new(),
            validator: DataQualityValidator::default(),
        }
    }

    /// Initialize manager and load existing datasets
    pub async fn initialize(&mut self) -> Result<(), GroundTruthError> {
        self.ensure_directory_structure().await?;
        self.load_catalog().await?;
        self.load_version_history().await?;
        Ok(())
    }

    /// Create directory structure for dataset management
    async fn ensure_directory_structure(&self) -> Result<(), GroundTruthError> {
        let datasets_dir = self.base_path.join("datasets");
        let versions_dir = self.base_path.join("versions");
        let annotations_dir = self.base_path.join("annotations");
        let exports_dir = self.base_path.join("exports");

        tokio::fs::create_dir_all(&datasets_dir).await?;
        tokio::fs::create_dir_all(&versions_dir).await?;
        tokio::fs::create_dir_all(&annotations_dir).await?;
        tokio::fs::create_dir_all(&exports_dir).await?;

        Ok(())
    }

    /// Load dataset catalog from disk
    async fn load_catalog(&mut self) -> Result<(), GroundTruthError> {
        let catalog_path = self.base_path.join("catalog.json");
        if catalog_path.exists() {
            let content = tokio::fs::read_to_string(&catalog_path).await?;
            self.catalog = serde_json::from_str(&content)?;
        }
        Ok(())
    }

    /// Save dataset catalog to disk
    async fn save_catalog(&self) -> Result<(), GroundTruthError> {
        let catalog_path = self.base_path.join("catalog.json");
        let content = serde_json::to_string_pretty(&self.catalog)?;
        tokio::fs::write(&catalog_path, content).await?;
        Ok(())
    }

    /// Load version history from disk
    async fn load_version_history(&mut self) -> Result<(), GroundTruthError> {
        let versions_path = self.base_path.join("versions.json");
        if versions_path.exists() {
            let content = tokio::fs::read_to_string(&versions_path).await?;
            self.version_history = serde_json::from_str(&content)?;
        }
        Ok(())
    }

    /// Save version history to disk
    async fn save_version_history(&self) -> Result<(), GroundTruthError> {
        let versions_path = self.base_path.join("versions.json");
        let content = serde_json::to_string_pretty(&self.version_history)?;
        tokio::fs::write(&versions_path, content).await?;
        Ok(())
    }

    /// Create a new ground truth dataset
    pub async fn create_dataset(
        &mut self,
        name: String,
        description: String,
        creator: String,
        license: String,
        domain: String,
        languages: Vec<String>,
    ) -> Result<String, GroundTruthError> {
        let id = uuid::Uuid::new_v4().to_string();
        let now = Utc::now();

        let dataset = GroundTruthDataset {
            id: id.clone(),
            name,
            version: "1.0.0".to_string(),
            description,
            created_at: now,
            modified_at: now,
            creator,
            license,
            languages,
            sample_count: 0,
            total_duration: 0.0,
            domain,
            annotation_guidelines: None,
            samples: Vec::new(),
            quality_metrics: DatasetQualityMetrics {
                overall_quality: 0.0,
                annotation_consistency: 0.0,
                inter_annotator_agreement: None,
                audio_quality: 0.0,
                metadata_completeness: 0.0,
                validation_completion: 0.0,
                quality_by_type: HashMap::new(),
            },
            tags: Vec::new(),
            metadata: HashMap::new(),
        };

        // Create dataset directory
        let dataset_dir = self.base_path.join("datasets").join(&id);
        tokio::fs::create_dir_all(&dataset_dir).await?;

        // Save dataset
        self.catalog.insert(id.clone(), dataset);
        self.save_catalog().await?;

        // Create initial version
        self.create_version(
            &id,
            "1.0.0",
            "Initial dataset creation".to_string(),
            Vec::new(),
        )
        .await?;

        Ok(id)
    }

    /// Add a sample to a dataset
    pub async fn add_sample(
        &mut self,
        dataset_id: &str,
        audio_path: PathBuf,
        reference_path: Option<PathBuf>,
        transcript: String,
        language: String,
        speaker_id: String,
        metadata: HashMap<String, String>,
    ) -> Result<String, GroundTruthError> {
        let dataset = self
            .catalog
            .get_mut(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        // Validate audio file exists
        if !audio_path.exists() {
            return Err(GroundTruthError::InvalidFormat(format!(
                "Audio file not found: {:?}",
                audio_path
            )));
        }

        // Basic audio validation (could be enhanced with actual audio loading)
        let sample_id = uuid::Uuid::new_v4().to_string();
        let sample = GroundTruthSample {
            id: sample_id.clone(),
            audio_path,
            reference_path,
            transcript,
            language,
            speaker_id,
            sample_rate: 16000, // Default, should be detected from audio
            duration: 0.0,      // Should be calculated from audio
            metadata,
            annotations: Vec::new(),
            validation_status: ValidationStatus::Pending,
        };

        dataset.samples.push(sample);
        dataset.sample_count = dataset.samples.len();
        dataset.modified_at = Utc::now();

        self.save_catalog().await?;
        Ok(sample_id)
    }

    /// Add annotation to a sample
    pub async fn add_annotation(
        &mut self,
        dataset_id: &str,
        sample_id: &str,
        annotation_type: AnnotationType,
        value: f64,
        scale: String,
        annotator_id: String,
        quality_level: AnnotationQuality,
        confidence: f64,
        description: Option<String>,
        metadata: HashMap<String, String>,
    ) -> Result<String, GroundTruthError> {
        let dataset = self
            .catalog
            .get_mut(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        let sample = dataset
            .samples
            .iter_mut()
            .find(|s| s.id == sample_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(sample_id.to_string()))?;

        let annotation_id = uuid::Uuid::new_v4().to_string();
        let annotation = GroundTruthAnnotation {
            id: annotation_id.clone(),
            sample_id: sample_id.to_string(),
            annotation_type,
            value,
            scale,
            annotator_id,
            quality_level,
            confidence,
            created_at: Utc::now(),
            description,
            metadata,
        };

        sample.annotations.push(annotation);
        dataset.modified_at = Utc::now();

        self.save_catalog().await?;
        Ok(annotation_id)
    }

    /// Validate dataset against quality standards
    pub async fn validate_dataset(
        &mut self,
        dataset_id: &str,
    ) -> Result<DatasetValidationReport, GroundTruthError> {
        let dataset = self
            .catalog
            .get_mut(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        // Prepare data for validation
        let mut audio_samples = Vec::new();
        let mut metadata_samples = Vec::new();

        for sample in &dataset.samples {
            // For now, create dummy audio data (in real implementation, load from file)
            let dummy_audio = vec![0.1_f32; 16000]; // 1 second of dummy audio
            audio_samples.push((dummy_audio, sample.sample_rate));

            // Convert sample metadata to required format
            let mut sample_metadata = sample.metadata.clone();
            sample_metadata.insert("language".to_string(), sample.language.clone());
            sample_metadata.insert("speaker".to_string(), sample.speaker_id.clone());
            sample_metadata.insert("transcript".to_string(), sample.transcript.clone());
            metadata_samples.push(sample_metadata);
        }

        // Run validation
        let validation_report = self
            .validator
            .validate_dataset(&dataset.name, &audio_samples, &metadata_samples)
            .map_err(|e| GroundTruthError::AnnotationValidationFailed(e.to_string()))?;

        // Update dataset quality metrics
        dataset.quality_metrics.overall_quality = validation_report.quality_score;
        dataset.quality_metrics.audio_quality = validation_report.quality_score;
        dataset.quality_metrics.metadata_completeness = validation_report
            .metadata_validation
            .values()
            .map(|&valid| if valid { 1.0 } else { 0.0 })
            .sum::<f64>()
            / validation_report.metadata_validation.len() as f64;

        // Update sample validation statuses
        for (i, sample) in dataset.samples.iter_mut().enumerate() {
            let sample_issues = validation_report
                .audio_issues
                .iter()
                .filter(|issue| issue.description.starts_with(&format!("Sample {}:", i)))
                .count();

            sample.validation_status = if sample_issues == 0 {
                ValidationStatus::Valid
            } else {
                ValidationStatus::Invalid
            };
        }

        dataset.modified_at = Utc::now();
        self.save_catalog().await?;

        Ok(validation_report)
    }

    /// Create a new version of a dataset
    pub async fn create_version(
        &mut self,
        dataset_id: &str,
        version: &str,
        description: String,
        changes: Vec<String>,
    ) -> Result<(), GroundTruthError> {
        let dataset = self
            .catalog
            .get(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        // Get previous version
        let previous_version = self
            .version_history
            .get(dataset_id)
            .and_then(|versions| versions.last())
            .map(|v| v.version.clone());

        // Check for version conflicts
        if let Some(versions) = self.version_history.get(dataset_id) {
            if versions.iter().any(|v| v.version == version) {
                return Err(GroundTruthError::VersionConflict(format!(
                    "Version {} already exists for dataset {}",
                    version, dataset_id
                )));
            }
        }

        // Save dataset snapshot
        let version_dir = self.base_path.join("versions").join(dataset_id);
        tokio::fs::create_dir_all(&version_dir).await?;

        let snapshot_path = version_dir.join(format!("{}.json", version));
        let dataset_content = serde_json::to_string_pretty(dataset)?;
        tokio::fs::write(&snapshot_path, dataset_content).await?;

        // Create version record
        let version_record = DatasetVersion {
            version: version.to_string(),
            created_at: Utc::now(),
            description,
            changes,
            previous_version,
            dataset_path: snapshot_path,
            metadata: HashMap::new(),
        };

        // Add to version history
        self.version_history
            .entry(dataset_id.to_string())
            .or_insert_with(Vec::new)
            .push(version_record);

        self.save_version_history().await?;
        Ok(())
    }

    /// Get dataset by ID
    pub fn get_dataset(&self, dataset_id: &str) -> Option<&GroundTruthDataset> {
        self.catalog.get(dataset_id)
    }

    /// List all datasets
    pub fn list_datasets(&self) -> Vec<&GroundTruthDataset> {
        self.catalog.values().collect()
    }

    /// Search datasets by criteria
    pub fn search_datasets(
        &self,
        language: Option<&str>,
        domain: Option<&str>,
        creator: Option<&str>,
        tags: Option<&[String]>,
    ) -> Vec<&GroundTruthDataset> {
        self.catalog
            .values()
            .filter(|dataset| {
                if let Some(lang) = language {
                    if !dataset.languages.contains(&lang.to_string()) {
                        return false;
                    }
                }
                if let Some(dom) = domain {
                    if dataset.domain != dom {
                        return false;
                    }
                }
                if let Some(cre) = creator {
                    if dataset.creator != cre {
                        return false;
                    }
                }
                if let Some(search_tags) = tags {
                    if !search_tags.iter().all(|tag| dataset.tags.contains(tag)) {
                        return false;
                    }
                }
                true
            })
            .collect()
    }

    /// Export dataset to a specified format
    pub async fn export_dataset(
        &self,
        dataset_id: &str,
        export_path: &Path,
        format: DatasetExportFormat,
    ) -> Result<(), GroundTruthError> {
        let dataset = self
            .catalog
            .get(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        match format {
            DatasetExportFormat::Json => {
                let content = serde_json::to_string_pretty(dataset)?;
                tokio::fs::write(export_path, content).await?;
            }
            DatasetExportFormat::Csv => {
                // Convert to CSV format (simplified)
                let mut csv_content = String::from(
                    "id,transcript,language,speaker_id,audio_path,annotations_count\n",
                );

                for sample in &dataset.samples {
                    csv_content.push_str(&format!(
                        "{},{},{},{},{},{}\n",
                        sample.id,
                        sample.transcript.replace(',', ";"),
                        sample.language,
                        sample.speaker_id,
                        sample.audio_path.display(),
                        sample.annotations.len()
                    ));
                }

                tokio::fs::write(export_path, csv_content).await?;
            }
        }

        Ok(())
    }

    /// Calculate inter-annotator agreement
    pub fn calculate_inter_annotator_agreement(
        &self,
        dataset_id: &str,
        annotation_type: &AnnotationType,
    ) -> Result<f64, GroundTruthError> {
        let dataset = self
            .catalog
            .get(dataset_id)
            .ok_or_else(|| GroundTruthError::DatasetNotFound(dataset_id.to_string()))?;

        let mut sample_agreements = Vec::new();

        for sample in &dataset.samples {
            let annotations: Vec<_> = sample
                .annotations
                .iter()
                .filter(|ann| ann.annotation_type == *annotation_type)
                .collect();

            if annotations.len() >= 2 {
                // Calculate pairwise agreement (simplified Pearson correlation)
                let values: Vec<f64> = annotations.iter().map(|ann| ann.value).collect();
                let mean = values.iter().sum::<f64>() / values.len() as f64;

                let variance =
                    values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;

                // Use inverse of variance as agreement measure (lower variance = higher agreement)
                let agreement = if variance > 0.0 {
                    1.0 / (1.0 + variance)
                } else {
                    1.0
                };
                sample_agreements.push(agreement);
            }
        }

        if sample_agreements.is_empty() {
            Ok(0.0)
        } else {
            Ok(sample_agreements.iter().sum::<f64>() / sample_agreements.len() as f64)
        }
    }
}

/// Dataset export formats
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DatasetExportFormat {
    /// JSON format
    Json,
    /// CSV format
    Csv,
}

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

    #[tokio::test]
    async fn test_ground_truth_manager_creation() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager = GroundTruthManager::new(temp_dir.path().to_path_buf());

        manager.initialize().await.unwrap();
        assert!(manager.catalog.is_empty());
    }

    #[tokio::test]
    async fn test_dataset_creation() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager = GroundTruthManager::new(temp_dir.path().to_path_buf());
        manager.initialize().await.unwrap();

        let dataset_id = manager
            .create_dataset(
                "Test Dataset".to_string(),
                "A test dataset".to_string(),
                "Test Creator".to_string(),
                "MIT".to_string(),
                "speech".to_string(),
                vec!["en".to_string()],
            )
            .await
            .unwrap();

        assert!(!dataset_id.is_empty());
        assert!(manager.catalog.contains_key(&dataset_id));
    }

    #[tokio::test]
    async fn test_annotation_addition() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager = GroundTruthManager::new(temp_dir.path().to_path_buf());
        manager.initialize().await.unwrap();

        let dataset_id = manager
            .create_dataset(
                "Test Dataset".to_string(),
                "A test dataset".to_string(),
                "Test Creator".to_string(),
                "MIT".to_string(),
                "speech".to_string(),
                vec!["en".to_string()],
            )
            .await
            .unwrap();

        // Create a temporary audio file
        let audio_file = temp_dir.path().join("test.wav");
        tokio::fs::write(&audio_file, b"dummy audio content")
            .await
            .unwrap();

        let sample_id = manager
            .add_sample(
                &dataset_id,
                audio_file,
                None,
                "Hello world".to_string(),
                "en".to_string(),
                "speaker1".to_string(),
                HashMap::new(),
            )
            .await
            .unwrap();

        let annotation_id = manager
            .add_annotation(
                &dataset_id,
                &sample_id,
                AnnotationType::QualityScore,
                0.85,
                "MOS_5".to_string(),
                "annotator1".to_string(),
                AnnotationQuality::Expert,
                0.9,
                Some("High quality sample".to_string()),
                HashMap::new(),
            )
            .await
            .unwrap();

        assert!(!annotation_id.is_empty());

        let dataset = manager.get_dataset(&dataset_id).unwrap();
        assert_eq!(dataset.samples.len(), 1);
        assert_eq!(dataset.samples[0].annotations.len(), 1);
    }

    #[tokio::test]
    async fn test_dataset_search() {
        let temp_dir = TempDir::new().unwrap();
        let mut manager = GroundTruthManager::new(temp_dir.path().to_path_buf());
        manager.initialize().await.unwrap();

        let _dataset_id1 = manager
            .create_dataset(
                "English Dataset".to_string(),
                "English speech dataset".to_string(),
                "Creator1".to_string(),
                "MIT".to_string(),
                "speech".to_string(),
                vec!["en".to_string()],
            )
            .await
            .unwrap();

        let _dataset_id2 = manager
            .create_dataset(
                "Spanish Dataset".to_string(),
                "Spanish speech dataset".to_string(),
                "Creator2".to_string(),
                "MIT".to_string(),
                "music".to_string(),
                vec!["es".to_string()],
            )
            .await
            .unwrap();

        let english_datasets = manager.search_datasets(Some("en"), None, None, None);
        assert_eq!(english_datasets.len(), 1);
        assert_eq!(english_datasets[0].name, "English Dataset");

        let speech_datasets = manager.search_datasets(None, Some("speech"), None, None);
        assert_eq!(speech_datasets.len(), 1);
        assert_eq!(speech_datasets[0].domain, "speech");
    }
}