Add scientific notation formatting for small metric values (#2136)

This commit is contained in:
Guillaume Lagrange 2024-08-08 16:25:34 -04:00 committed by GitHub
parent 723c9d1a2e
commit 724bfbc73b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 42 additions and 34 deletions

View File

@ -181,9 +181,17 @@ impl Display for LearnerSummary {
}
}
let mut write_metrics_summary = |metrics: &[MetricSummary],
split: &str|
-> std::fmt::Result {
fn fmt_val(val: f64) -> String {
if val < 1e-2 {
// Use scientific notation for small values which would otherwise be truncated
format!("{:<9.3e}", val)
} else {
format!("{:<9.3}", val)
}
}
let mut write_metrics_summary =
|metrics: &[MetricSummary], split: &str| -> std::fmt::Result {
for metric in metrics.iter() {
if metric.entries.is_empty() {
continue; // skip metrics with no recorded values
@ -203,12 +211,12 @@ impl Display for LearnerSummary {
writeln!(
f,
"| {:<width_split$} | {:<width_metric$} | {:<9.3?}| {:<9?}| {:<9.3?}| {:<9.3?}|",
"| {:<width_split$} | {:<width_metric$} | {}| {:<9?}| {}| {:<9?}|",
split,
metric.name,
metric_min.value,
fmt_val(metric_min.value),
metric_min.step,
metric_max.value,
fmt_val(metric_max.value),
metric_max.step,
width_split = max_split_len,
width_metric = max_metric_len,