oxirs-embed 0.4.0

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
//! Classification evaluation module
//!
//! This module provides comprehensive evaluation for classification tasks using
//! embedding models, including accuracy, precision, recall, F1-score, and
//! confusion matrix analysis.

use super::ApplicationEvalConfig;
use crate::EmbeddingModel;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Classification evaluation metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClassificationMetric {
    /// Accuracy
    Accuracy,
    /// Precision (macro-averaged)
    Precision,
    /// Recall (macro-averaged)
    Recall,
    /// F1 Score (macro-averaged)
    F1Score,
    /// ROC AUC (for binary classification)
    ROCAUC,
    /// Precision-Recall AUC
    PRAUC,
    /// Matthews Correlation Coefficient
    MCC,
}

/// Per-class classification results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassResults {
    /// Class label
    pub class_label: String,
    /// Precision
    pub precision: f64,
    /// Recall
    pub recall: f64,
    /// F1 score
    pub f1_score: f64,
    /// Support (number of instances)
    pub support: usize,
}

/// Classification report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassificationReport {
    /// Macro-averaged metrics
    pub macro_avg: ClassResults,
    /// Weighted-averaged metrics
    pub weighted_avg: ClassResults,
    /// Overall accuracy
    pub accuracy: f64,
    /// Total number of samples
    pub total_samples: usize,
}

/// Classification evaluation results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassificationResults {
    /// Metric scores
    pub metric_scores: HashMap<String, f64>,
    /// Per-class results
    pub per_class_results: HashMap<String, ClassResults>,
    /// Confusion matrix
    pub confusion_matrix: Vec<Vec<usize>>,
    /// Classification report
    pub classification_report: ClassificationReport,
}

/// Simple classifier for evaluation
#[allow(dead_code)]
pub struct SimpleClassifier {
    /// Class centroids
    class_centroids: HashMap<String, Vec<f32>>,
    /// Class counts
    class_counts: HashMap<String, usize>,
}

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

impl SimpleClassifier {
    /// Create a new simple classifier
    pub fn new() -> Self {
        Self {
            class_centroids: HashMap::new(),
            class_counts: HashMap::new(),
        }
    }

    /// Predict class for an embedding
    pub fn predict(&self, embedding: &[f32]) -> Option<String> {
        if self.class_centroids.is_empty() {
            return None;
        }

        let mut best_class = None;
        let mut best_distance = f32::INFINITY;

        for (class_name, centroid) in &self.class_centroids {
            let distance = self.euclidean_distance(embedding, centroid);
            if distance < best_distance {
                best_distance = distance;
                best_class = Some(class_name.clone());
            }
        }

        best_class
    }

    /// Calculate euclidean distance
    fn euclidean_distance(&self, v1: &[f32], v2: &[f32]) -> f32 {
        v1.iter()
            .zip(v2.iter())
            .map(|(a, b)| (a - b).powi(2))
            .sum::<f32>()
            .sqrt()
    }
}

/// Classification evaluator
pub struct ClassificationEvaluator {
    /// Training data with labels
    training_data: Vec<(String, String)>, // (entity, label)
    /// Test data with labels
    test_data: Vec<(String, String)>,
    /// Classification metrics
    metrics: Vec<ClassificationMetric>,
}

impl ClassificationEvaluator {
    /// Create a new classification evaluator
    pub fn new() -> Self {
        Self {
            training_data: Vec::new(),
            test_data: Vec::new(),
            metrics: vec![
                ClassificationMetric::Accuracy,
                ClassificationMetric::Precision,
                ClassificationMetric::Recall,
                ClassificationMetric::F1Score,
            ],
        }
    }

    /// Add training data
    pub fn add_training_data(&mut self, entity: String, label: String) {
        self.training_data.push((entity, label));
    }

    /// Add test data
    pub fn add_test_data(&mut self, entity: String, label: String) {
        self.test_data.push((entity, label));
    }

    /// Evaluate classification performance
    pub async fn evaluate(
        &self,
        model: &dyn EmbeddingModel,
        _config: &ApplicationEvalConfig,
    ) -> Result<ClassificationResults> {
        if self.test_data.is_empty() {
            return Err(anyhow!(
                "No test data available for classification evaluation"
            ));
        }

        // Train a simple classifier on embeddings
        let classifier = self.train_classifier(model).await?;

        // Predict on test data
        let predictions = self.predict_test_data(model, &classifier).await?;

        // Calculate metrics
        let mut metric_scores = HashMap::new();
        for metric in &self.metrics {
            let score = self.calculate_classification_metric(metric, &predictions)?;
            metric_scores.insert(format!("{metric:?}"), score);
        }

        // Generate per-class results
        let per_class_results = self.calculate_per_class_results(&predictions)?;

        // Generate confusion matrix
        let confusion_matrix = self.generate_confusion_matrix(&predictions)?;

        // Generate classification report
        let classification_report =
            self.generate_classification_report(&per_class_results, &predictions)?;

        Ok(ClassificationResults {
            metric_scores,
            per_class_results,
            confusion_matrix,
            classification_report,
        })
    }

    /// Train a simple classifier
    async fn train_classifier(&self, model: &dyn EmbeddingModel) -> Result<SimpleClassifier> {
        let mut class_centroids = HashMap::new();
        let mut class_counts = HashMap::new();

        for (entity, label) in &self.training_data {
            if let Ok(embedding) = model.get_entity_embedding(entity) {
                let centroid = class_centroids
                    .entry(label.clone())
                    .or_insert_with(|| vec![0.0f32; embedding.values.len()]);

                for (i, &value) in embedding.values.iter().enumerate() {
                    centroid[i] += value;
                }

                *class_counts.entry(label.clone()).or_insert(0) += 1;
            }
        }

        // Average the centroids
        for (label, count) in &class_counts {
            if let Some(centroid) = class_centroids.get_mut(label) {
                for value in centroid.iter_mut() {
                    *value /= *count as f32;
                }
            }
        }

        Ok(SimpleClassifier {
            class_centroids,
            class_counts,
        })
    }

    /// Predict on test data
    async fn predict_test_data(
        &self,
        model: &dyn EmbeddingModel,
        classifier: &SimpleClassifier,
    ) -> Result<Vec<(String, String, Option<String>)>> {
        // (true_label, entity, predicted_label)
        let mut predictions = Vec::new();

        for (entity, true_label) in &self.test_data {
            if let Ok(embedding) = model.get_entity_embedding(entity) {
                let predicted_label = classifier.predict(&embedding.values);
                predictions.push((true_label.clone(), entity.clone(), predicted_label));
            }
        }

        Ok(predictions)
    }

    /// Calculate classification metric
    fn calculate_classification_metric(
        &self,
        metric: &ClassificationMetric,
        predictions: &[(String, String, Option<String>)],
    ) -> Result<f64> {
        match metric {
            ClassificationMetric::Accuracy => {
                let correct = predictions
                    .iter()
                    .filter(|(true_label, _, pred)| {
                        pred.as_ref().map(|p| p == true_label).unwrap_or(false)
                    })
                    .count();
                Ok(correct as f64 / predictions.len() as f64)
            }
            ClassificationMetric::Precision
            | ClassificationMetric::Recall
            | ClassificationMetric::F1Score => {
                // Macro-averaged over the real per-class precision/recall/F1
                // computed from the confusion matrix (true/false positives and
                // false negatives per class).
                let per_class = self.calculate_per_class_results(predictions)?;
                if per_class.is_empty() {
                    return Ok(0.0);
                }
                let sum: f64 = match metric {
                    ClassificationMetric::Precision => {
                        per_class.values().map(|c| c.precision).sum()
                    }
                    ClassificationMetric::Recall => per_class.values().map(|c| c.recall).sum(),
                    ClassificationMetric::F1Score => per_class.values().map(|c| c.f1_score).sum(),
                    _ => unreachable!("matched above"),
                };
                Ok(sum / per_class.len() as f64)
            }
            ClassificationMetric::ROCAUC
            | ClassificationMetric::PRAUC
            | ClassificationMetric::MCC => Err(anyhow!(
                "Classification metric {metric:?} is not yet implemented; requires per-class \
                     prediction scores, which SimpleClassifier does not currently expose"
            )),
        }
    }

    /// Calculate per-class results from real true-positive/false-positive/
    /// false-negative counts derived from the predictions.
    fn calculate_per_class_results(
        &self,
        predictions: &[(String, String, Option<String>)],
    ) -> Result<HashMap<String, ClassResults>> {
        let mut results = HashMap::new();

        // Consider every class that appears either as ground truth or as a
        // prediction, so classes the classifier over/under-predicts are not
        // silently dropped from the report.
        let classes: std::collections::HashSet<String> = predictions
            .iter()
            .map(|(true_label, _, _)| true_label.clone())
            .chain(predictions.iter().filter_map(|(_, _, pred)| pred.clone()))
            .collect();

        for class in classes {
            let true_positives = predictions
                .iter()
                .filter(|(true_label, _, pred)| {
                    true_label == &class && pred.as_deref() == Some(class.as_str())
                })
                .count();
            let false_positives = predictions
                .iter()
                .filter(|(true_label, _, pred)| {
                    true_label != &class && pred.as_deref() == Some(class.as_str())
                })
                .count();
            let false_negatives = predictions
                .iter()
                .filter(|(true_label, _, pred)| {
                    true_label == &class && pred.as_deref() != Some(class.as_str())
                })
                .count();
            let support = predictions
                .iter()
                .filter(|(true_label, _, _)| true_label == &class)
                .count();

            let precision = if true_positives + false_positives > 0 {
                true_positives as f64 / (true_positives + false_positives) as f64
            } else {
                0.0
            };
            let recall = if true_positives + false_negatives > 0 {
                true_positives as f64 / (true_positives + false_negatives) as f64
            } else {
                0.0
            };
            let f1_score = if precision + recall > 0.0 {
                2.0 * precision * recall / (precision + recall)
            } else {
                0.0
            };

            results.insert(
                class.clone(),
                ClassResults {
                    class_label: class,
                    precision,
                    recall,
                    f1_score,
                    support,
                },
            );
        }

        Ok(results)
    }

    /// Generate a real multi-class confusion matrix, indexed by a
    /// deterministic (sorted) ordering over every class seen as ground truth
    /// or prediction. `matrix[i][j]` is the count of true-class-`i` samples
    /// predicted as class `j`.
    fn generate_confusion_matrix(
        &self,
        predictions: &[(String, String, Option<String>)],
    ) -> Result<Vec<Vec<usize>>> {
        let mut classes: Vec<String> = predictions
            .iter()
            .map(|(true_label, _, _)| true_label.clone())
            .chain(predictions.iter().filter_map(|(_, _, pred)| pred.clone()))
            .collect::<std::collections::HashSet<_>>()
            .into_iter()
            .collect();
        classes.sort();

        let class_index: HashMap<&str, usize> = classes
            .iter()
            .enumerate()
            .map(|(i, c)| (c.as_str(), i))
            .collect();

        let mut matrix = vec![vec![0usize; classes.len()]; classes.len()];
        for (true_label, _, pred) in predictions {
            let Some(&true_idx) = class_index.get(true_label.as_str()) else {
                continue;
            };
            if let Some(predicted_label) = pred {
                if let Some(&pred_idx) = class_index.get(predicted_label.as_str()) {
                    matrix[true_idx][pred_idx] += 1;
                }
            }
        }

        Ok(matrix)
    }

    /// Generate classification report with macro- and support-weighted
    /// averages computed from the real per-class results.
    fn generate_classification_report(
        &self,
        per_class_results: &HashMap<String, ClassResults>,
        predictions: &[(String, String, Option<String>)],
    ) -> Result<ClassificationReport> {
        let accuracy = predictions
            .iter()
            .filter(|(true_label, _, pred)| pred.as_ref().map(|p| p == true_label).unwrap_or(false))
            .count() as f64
            / predictions.len().max(1) as f64;

        let num_classes = per_class_results.len().max(1);
        let total_support: usize = per_class_results.values().map(|c| c.support).sum();

        let macro_precision =
            per_class_results.values().map(|c| c.precision).sum::<f64>() / num_classes as f64;
        let macro_recall =
            per_class_results.values().map(|c| c.recall).sum::<f64>() / num_classes as f64;
        let macro_f1 =
            per_class_results.values().map(|c| c.f1_score).sum::<f64>() / num_classes as f64;

        let (weighted_precision, weighted_recall, weighted_f1) = if total_support > 0 {
            let weighted_precision = per_class_results
                .values()
                .map(|c| c.precision * c.support as f64)
                .sum::<f64>()
                / total_support as f64;
            let weighted_recall = per_class_results
                .values()
                .map(|c| c.recall * c.support as f64)
                .sum::<f64>()
                / total_support as f64;
            let weighted_f1 = per_class_results
                .values()
                .map(|c| c.f1_score * c.support as f64)
                .sum::<f64>()
                / total_support as f64;
            (weighted_precision, weighted_recall, weighted_f1)
        } else {
            (0.0, 0.0, 0.0)
        };

        let macro_avg = ClassResults {
            class_label: "macro avg".to_string(),
            precision: macro_precision,
            recall: macro_recall,
            f1_score: macro_f1,
            support: predictions.len(),
        };

        let weighted_avg = ClassResults {
            class_label: "weighted avg".to_string(),
            precision: weighted_precision,
            recall: weighted_recall,
            f1_score: weighted_f1,
            support: predictions.len(),
        };

        Ok(ClassificationReport {
            macro_avg,
            weighted_avg,
            accuracy,
            total_samples: predictions.len(),
        })
    }
}

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

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

    /// Regression test for the P1 finding: Precision/Recall/F1Score must be
    /// computed from the real confusion matrix, not hardcoded 0.75/0.73/0.74.
    #[test]
    fn test_calculate_classification_metric_precision_recall_f1_are_real() -> Result<()> {
        let evaluator = ClassificationEvaluator::new();
        // 2 correct "cat" predictions, 1 correct "dog", 1 "cat" mispredicted
        // as "dog".
        let predictions = vec![
            ("cat".to_string(), "e1".to_string(), Some("cat".to_string())),
            ("cat".to_string(), "e2".to_string(), Some("cat".to_string())),
            ("cat".to_string(), "e3".to_string(), Some("dog".to_string())),
            ("dog".to_string(), "e4".to_string(), Some("dog".to_string())),
        ];

        let precision = evaluator
            .calculate_classification_metric(&ClassificationMetric::Precision, &predictions)?;
        let recall = evaluator
            .calculate_classification_metric(&ClassificationMetric::Recall, &predictions)?;
        let f1 = evaluator
            .calculate_classification_metric(&ClassificationMetric::F1Score, &predictions)?;

        // cat: TP=2, FP=0, FN=1 -> precision=1.0, recall=2/3
        // dog: TP=1, FP=1, FN=0 -> precision=0.5, recall=1.0
        // macro precision = (1.0 + 0.5) / 2 = 0.75; macro recall = (2/3 + 1.0) / 2 ≈ 0.8333
        assert!((precision - 0.75).abs() < 1e-9, "precision = {precision}");
        assert!((recall - 0.8333333333).abs() < 1e-6, "recall = {recall}");
        assert!(f1 > 0.0 && f1 < 1.0, "f1 = {f1}");

        // These must NOT be the old hardcoded constants for a *different*
        // confusion matrix (sanity: values change with the data).
        let all_correct = vec![
            ("cat".to_string(), "e1".to_string(), Some("cat".to_string())),
            ("dog".to_string(), "e2".to_string(), Some("dog".to_string())),
        ];
        let perfect_precision = evaluator
            .calculate_classification_metric(&ClassificationMetric::Precision, &all_correct)?;
        assert!(
            (perfect_precision - 1.0).abs() < 1e-9,
            "perfect_precision = {perfect_precision}"
        );

        Ok(())
    }

    #[test]
    fn test_calculate_classification_metric_unsupported_metrics_error() {
        let evaluator = ClassificationEvaluator::new();
        let predictions = vec![("cat".to_string(), "e1".to_string(), Some("cat".to_string()))];

        for metric in [
            ClassificationMetric::ROCAUC,
            ClassificationMetric::PRAUC,
            ClassificationMetric::MCC,
        ] {
            assert!(
                evaluator
                    .calculate_classification_metric(&metric, &predictions)
                    .is_err(),
                "metric = {metric:?}"
            );
        }
    }

    #[test]
    fn test_calculate_per_class_results_computes_real_confusion_counts() {
        let evaluator = ClassificationEvaluator::new();
        let predictions = vec![
            ("cat".to_string(), "e1".to_string(), Some("cat".to_string())),
            ("cat".to_string(), "e2".to_string(), Some("dog".to_string())),
        ];

        let results = evaluator
            .calculate_per_class_results(&predictions)
            .expect("should succeed");
        let cat_results = &results["cat"];
        assert_eq!(cat_results.support, 2);
        assert!((cat_results.precision - 1.0).abs() < 1e-9);
        assert!((cat_results.recall - 0.5).abs() < 1e-9);
    }
}