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
//! Integration with scirs2-neural
//!
//! This module provides adapters and utilities for integrating scirs2-metrics
//! with scirs2-neural, allowing metrics to be used during model training,
//! validation, and evaluation.
//!
//! # Feature Flag
//!
//! This integration requires the `neural_common` feature flag to be enabled:
//!
//! ```toml
//! [dependencies]
//! scirs2-metrics = { version = "0.1.0", features = ["neural_common"] }
//! ```
//!
//! # Metric Adapters
//!
//! The [`NeuralMetricAdapter`] provides an adapter for using scirs2-metrics metrics
//! with scirs2-neural models:
//!
//! ```no_run
//! # #[cfg(feature = "neural_common")]
//! # {
//! use scirs2_metrics::integration::neural::NeuralMetricAdapter;
//! use scirs2_metrics::integration::traits::MetricComputation;
//! use scirs2_core::ndarray::{Array, IxDyn};
//!
//! // Create metric adapters
//! let accuracy = NeuralMetricAdapter::<f64>::accuracy();
//! let precision = NeuralMetricAdapter::<f64>::precision();
//! let f1_score = NeuralMetricAdapter::<f64>::f1_score();
//!
//! // Use with neural network predictions and targets
//! let predictions = Array::<f64, IxDyn>::zeros(IxDyn(&[10, 1]));
//! let targets = Array::<f64, IxDyn>::zeros(IxDyn(&[10, 1]));
//!
//! // Compute metrics
//! let acc = accuracy.compute(&predictions, &targets).expect("should succeed");
//! let prec = precision.compute(&predictions, &targets).expect("should succeed");
//! let f1 = f1_score.compute(&predictions, &targets).expect("should succeed");
//! # }
//! ```
//!
//! # Metrics Callback
//!
//! The [`MetricsCallback`] can be used to track metrics during neural network training:
//!
//! ```no_run
//! # #[cfg(feature = "neural_common")]
//! # {
//! use scirs2_metrics::integration::neural::{NeuralMetricAdapter, MetricsCallback};
//!
//! // Create metric adapters
//! let metrics = vec![
//! NeuralMetricAdapter::<f32>::accuracy(),
//! NeuralMetricAdapter::<f32>::precision(),
//! NeuralMetricAdapter::<f32>::f1_score(),
//! ];
//!
//! // Create callback
//! let mut callback = MetricsCallback::new(metrics, true);
//!
//! // In scirs2-neural, use with model training:
//! // model.fit(..., callbacks: &[&callback], ...);
//! # }
//! ```
//!
//! # Visualization
//!
//! Visualization utilities are provided for neural network metrics:
//!
//! ```no_run
//! # #[cfg(feature = "neural_common")]
//! # {
//! use scirs2_metrics::integration::neural::{
//! neural_roc_curve_visualization,
//! neural_precision_recall_curve_visualization,
//! neural_confusion_matrix_visualization,
//! training_history_visualization,
//! };
//! use scirs2_core::ndarray::{Array, IxDyn};
//! use std::collections::HashMap;
//!
//! // Example data
//! let y_true = Array::<f64, IxDyn>::zeros(IxDyn(&[100]));
//! let y_score = Array::<f64, IxDyn>::zeros(IxDyn(&[100]));
//! let history = vec![HashMap::from([
//! ("loss".to_string(), 0.5),
//! ("accuracy".to_string(), 0.85),
//! ])];
//!
//! // Create visualizations
//! let roc_viz = neural_roc_curve_visualization(&y_true, &y_score, Some(0.8)).expect("should succeed");
//! let pr_viz = neural_precision_recall_curve_visualization(&y_true, &y_score, Some(0.75)).expect("should succeed");
//! let cm_viz = neural_confusion_matrix_visualization(
//! &y_true, &y_score, Some(vec!["Class 0".to_string(), "Class 1".to_string()]), false
//! ).expect("should succeed");
//! let history_viz = training_history_visualization(
//! vec!["loss".to_string(), "accuracy".to_string()],
//! history,
//! None,
//! );
//! # }
//! ```
// Core implementation that's available with neural_common feature
pub use *;
// Neural-integration specific implementations that depend on neural crate
// Re-export only when feature is enabled
pub use *;
pub use *;
pub use *;