Skip to main content

fpm_rs/tabular/tables/
summary.rs

1use polars::prelude::*;
2
3use crate::{Result, reconstruction::ReconstructionResult};
4
5/// Stable identity/status columns shared by reconstruction and benchmark run tables.
6pub const COMMON_RUN_COLUMNS: &[&str] = &[
7    "run_id",
8    "case_id",
9    "algorithm",
10    "algorithm_configuration",
11    "dataset_name",
12    "dataset_version",
13    "random_seed",
14    "frame_count",
15    "completed_iterations",
16    "elapsed_seconds",
17    "final_objective",
18    "success",
19    "error",
20];
21
22/// Builds the required one-row reconstruction result summary table.
23pub fn summary_dataframe(run_id: &str, result: &ReconstructionResult) -> Result<DataFrame> {
24    let (reconstruction_height, reconstruction_width) = result.object.dim();
25    let (image_height, image_width) = result.recovered_pupil.shape();
26    let metadata = &result.metadata;
27    let optional = |key: &str| metadata.get(key).map(String::as_str);
28    let parse_u64 = |key: &str| metadata.get(key).and_then(|value| value.parse().ok());
29    let frame_count = parse_u64("frame_count").or_else(|| {
30        result
31            .recovered_frame_gains
32            .as_ref()
33            .map(|values| values.len() as u64)
34    });
35    Ok(df!(
36        "run_id" => [run_id],
37        "case_id" => [optional("case_id")],
38        "crate_version" => [env!("CARGO_PKG_VERSION")],
39        "dataset_name" => [optional("dataset_name")],
40        "dataset_version" => [optional("dataset_version")],
41        "preset_name" => [optional("preset_name")],
42        "algorithm" => [result.runtime.algorithm.as_str()],
43        "algorithm_configuration" => [optional("algorithm_configuration")],
44        "random_seed" => [parse_u64("random_seed")],
45        "frame_count" => [frame_count],
46        "image_width" => [image_width as u64],
47        "image_height" => [image_height as u64],
48        "reconstruction_width" => [reconstruction_width as u64],
49        "reconstruction_height" => [reconstruction_height as u64],
50        "completed_iterations" => [result.runtime.completed_iterations as u64],
51        "elapsed_seconds" => [result.runtime.elapsed_seconds],
52        "stopped_early" => [result.runtime.stopped_early],
53        "final_objective" => [result.trace.final_objective()],
54        "success" => [true],
55        "error" => [None::<&str>],
56    )?)
57}