1use core::cmp::Ordering;
2use std::{
3 fmt::Display,
4 path::{Path, PathBuf},
5};
6
7use crate::{
8 logger::FileMetricLogger,
9 metric::store::{Aggregate, EventStore, LogEventStore, Split},
10};
11
12pub struct MetricEntry {
14 pub step: usize,
16 pub value: f64,
18}
19
20pub struct MetricSummary {
22 pub name: String,
24 pub entries: Vec<MetricEntry>,
26}
27
28impl MetricSummary {
29 fn new<E: EventStore>(
30 event_store: &mut E,
31 metric: &str,
32 split: Split,
33 num_epochs: usize,
34 ) -> Option<Self> {
35 let entries = (1..=num_epochs)
36 .filter_map(|epoch| {
37 event_store
38 .find_metric(metric, epoch, Aggregate::Mean, split)
39 .map(|value| MetricEntry { step: epoch, value })
40 })
41 .collect::<Vec<_>>();
42
43 if entries.is_empty() {
44 None
45 } else {
46 Some(Self {
47 name: metric.to_string(),
48 entries,
49 })
50 }
51 }
52}
53
54pub struct SummaryMetrics {
56 pub train: Vec<MetricSummary>,
58 pub valid: Vec<MetricSummary>,
60}
61
62pub struct LearnerSummary {
64 pub epochs: usize,
66 pub metrics: SummaryMetrics,
68 pub(crate) model: Option<String>,
70}
71
72impl LearnerSummary {
73 pub fn new<S: AsRef<str>>(directory: impl AsRef<Path>, metrics: &[S]) -> Result<Self, String> {
80 let directory = directory.as_ref();
81 if !directory.exists() {
82 return Err(format!(
83 "Artifact directory does not exist at: {}",
84 directory.display()
85 ));
86 }
87 let train_dir = directory.join("train");
88 let valid_dir = directory.join("valid");
89 if !train_dir.exists() & !valid_dir.exists() {
90 return Err(format!(
91 "No training or validation artifacts found at: {}",
92 directory.display()
93 ));
94 }
95
96 let mut event_store = LogEventStore::default();
97
98 let train_logger = FileMetricLogger::new(train_dir.to_str().unwrap());
99 let valid_logger = FileMetricLogger::new(valid_dir.to_str().unwrap());
100
101 let epochs = train_logger.epochs();
103
104 event_store.register_logger_train(train_logger);
105 event_store.register_logger_valid(valid_logger);
106
107 let train_summary = metrics
108 .iter()
109 .filter_map(|metric| {
110 MetricSummary::new(&mut event_store, metric.as_ref(), Split::Train, epochs)
111 })
112 .collect::<Vec<_>>();
113
114 let valid_summary = metrics
115 .iter()
116 .filter_map(|metric| {
117 MetricSummary::new(&mut event_store, metric.as_ref(), Split::Valid, epochs)
118 })
119 .collect::<Vec<_>>();
120
121 Ok(Self {
122 epochs,
123 metrics: SummaryMetrics {
124 train: train_summary,
125 valid: valid_summary,
126 },
127 model: None,
128 })
129 }
130
131 pub(crate) fn with_model(mut self, name: String) -> Self {
132 self.model = Some(name);
133 self
134 }
135}
136
137impl Display for LearnerSummary {
138 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
139 let split_train = "Train";
141 let split_valid = "Valid";
142 let max_split_len = "Split".len().max(split_train.len()).max(split_valid.len());
143 let mut max_metric_len = "Metric".len();
144 for metric in self.metrics.train.iter() {
145 max_metric_len = max_metric_len.max(metric.name.len());
146 }
147 for metric in self.metrics.valid.iter() {
148 max_metric_len = max_metric_len.max(metric.name.len());
149 }
150
151 writeln!(
153 f,
154 "{:=>width_symbol$} Learner Summary {:=>width_symbol$}",
155 "",
156 "",
157 width_symbol = 24,
158 )?;
159
160 if let Some(model) = &self.model {
161 writeln!(f, "Model:\n{model}")?;
162 }
163 writeln!(f, "Total Epochs: {epochs}\n\n", epochs = self.epochs)?;
164
165 writeln!(
167 f,
168 "| {:<width_split$} | {:<width_metric$} | Min. | Epoch | Max. | Epoch |\n|{:->width_split$}--|{:->width_metric$}--|----------|----------|----------|----------|",
169 "Split", "Metric", "", "",
170 width_split = max_split_len,
171 width_metric = max_metric_len,
172 )?;
173
174 fn cmp_f64(a: &f64, b: &f64) -> Ordering {
176 match (a.is_nan(), b.is_nan()) {
177 (true, true) => Ordering::Equal,
178 (true, false) => Ordering::Greater,
179 (false, true) => Ordering::Less,
180 _ => a.partial_cmp(b).unwrap(),
181 }
182 }
183
184 fn fmt_val(val: f64) -> String {
185 if val < 1e-2 {
186 format!("{:<9.3e}", val)
188 } else {
189 format!("{:<9.3}", val)
190 }
191 }
192
193 let mut write_metrics_summary =
194 |metrics: &[MetricSummary], split: &str| -> std::fmt::Result {
195 for metric in metrics.iter() {
196 if metric.entries.is_empty() {
197 continue; }
199
200 let metric_min = metric
202 .entries
203 .iter()
204 .min_by(|a, b| cmp_f64(&a.value, &b.value))
205 .unwrap();
206 let metric_max = metric
207 .entries
208 .iter()
209 .max_by(|a, b| cmp_f64(&a.value, &b.value))
210 .unwrap();
211
212 writeln!(
213 f,
214 "| {:<width_split$} | {:<width_metric$} | {}| {:<9?}| {}| {:<9?}|",
215 split,
216 metric.name,
217 fmt_val(metric_min.value),
218 metric_min.step,
219 fmt_val(metric_max.value),
220 metric_max.step,
221 width_split = max_split_len,
222 width_metric = max_metric_len,
223 )?;
224 }
225
226 Ok(())
227 };
228
229 write_metrics_summary(&self.metrics.train, split_train)?;
230 write_metrics_summary(&self.metrics.valid, split_valid)?;
231
232 Ok(())
233 }
234}
235
236pub(crate) struct LearnerSummaryConfig {
237 pub(crate) directory: PathBuf,
238 pub(crate) metrics: Vec<String>,
239}
240
241impl LearnerSummaryConfig {
242 pub fn init(&self) -> Result<LearnerSummary, String> {
243 LearnerSummary::new(&self.directory, &self.metrics[..])
244 }
245}
246
247#[cfg(test)]
248mod tests {
249 use super::*;
250
251 #[test]
252 #[should_panic = "Summary artifacts should exist"]
253 fn test_artifact_dir_should_exist() {
254 let dir = "/tmp/learner-summary-not-found";
255 let _summary = LearnerSummary::new(dir, &["Loss"]).expect("Summary artifacts should exist");
256 }
257
258 #[test]
259 #[should_panic = "Summary artifacts should exist"]
260 fn test_train_valid_artifacts_should_exist() {
261 let dir = "/tmp/test-learner-summary-empty";
262 std::fs::create_dir_all(dir).ok();
263 let _summary = LearnerSummary::new(dir, &["Loss"]).expect("Summary artifacts should exist");
264 }
265
266 #[test]
267 fn test_summary_should_be_empty() {
268 let dir = Path::new("/tmp/test-learner-summary-empty-metrics");
269 std::fs::create_dir_all(dir).unwrap();
270 std::fs::create_dir_all(dir.join("train/epoch-1")).unwrap();
271 std::fs::create_dir_all(dir.join("valid/epoch-1")).unwrap();
272 let summary = LearnerSummary::new(dir.to_str().unwrap(), &["Loss"])
273 .expect("Summary artifacts should exist");
274
275 assert_eq!(summary.epochs, 1);
276
277 assert_eq!(summary.metrics.train.len(), 0);
278 assert_eq!(summary.metrics.valid.len(), 0);
279
280 std::fs::remove_dir_all(dir).unwrap();
281 }
282
283 #[test]
284 fn test_summary_should_be_collected() {
285 let dir = Path::new("/tmp/test-learner-summary");
286 let train_dir = dir.join("train/epoch-1");
287 let valid_dir = dir.join("valid/epoch-1");
288 std::fs::create_dir_all(dir).unwrap();
289 std::fs::create_dir_all(&train_dir).unwrap();
290 std::fs::create_dir_all(&valid_dir).unwrap();
291
292 std::fs::write(train_dir.join("Loss.log"), "1.0\n2.0").expect("Unable to write file");
293 std::fs::write(valid_dir.join("Loss.log"), "1.0").expect("Unable to write file");
294
295 let summary = LearnerSummary::new(dir.to_str().unwrap(), &["Loss"])
296 .expect("Summary artifacts should exist");
297
298 assert_eq!(summary.epochs, 1);
299
300 assert_eq!(summary.metrics.train.len(), 1);
302 assert_eq!(summary.metrics.valid.len(), 1);
303
304 let train_metric = &summary.metrics.train[0];
306 assert_eq!(train_metric.name, "Loss");
307 assert_eq!(train_metric.entries.len(), 1);
308 let entry = &train_metric.entries[0];
309 assert_eq!(entry.step, 1); assert_eq!(entry.value, 1.5); let valid_metric = &summary.metrics.valid[0];
314 assert_eq!(valid_metric.name, "Loss");
315 assert_eq!(valid_metric.entries.len(), 1);
316 let entry = &valid_metric.entries[0];
317 assert_eq!(entry.step, 1); assert_eq!(entry.value, 1.0);
319
320 std::fs::remove_dir_all(dir).unwrap();
321 }
322}