Skip to main content

fpm_rs/tabular/tables/
calibration.rs

1use polars::prelude::*;
2
3use crate::{Result, reconstruction::ReconstructionResult};
4
5/// Builds source-order `(row, column)` correction rows in Fourier-grid pixels.
6pub fn illumination_calibration_dataframe(
7    run_id: &str,
8    result: &ReconstructionResult,
9) -> Result<DataFrame> {
10    let corrections = result.calibrated_illumination.as_deref().unwrap_or(&[]);
11    let length = corrections.len();
12    let mut run_ids = Vec::with_capacity(length);
13    let mut source_indices = Vec::with_capacity(length);
14    let mut row_corrections = Vec::with_capacity(length);
15    let mut column_corrections = Vec::with_capacity(length);
16    for (source, &(row, column)) in corrections.iter().enumerate() {
17        run_ids.push(run_id);
18        source_indices.push(source as u64);
19        row_corrections.push(row);
20        column_corrections.push(column);
21    }
22    Ok(df!(
23        "run_id" => run_ids,
24        "source_index" => source_indices,
25        "row_correction_pixels" => row_corrections,
26        "column_correction_pixels" => column_corrections,
27    )?)
28}
29
30/// Builds acquisition-frame gain and additive-background rows.
31pub fn frame_calibration_dataframe(
32    run_id: &str,
33    result: &ReconstructionResult,
34) -> Result<DataFrame> {
35    let length = result
36        .recovered_frame_gains
37        .as_ref()
38        .map_or(0, Vec::len)
39        .max(result.recovered_background.as_ref().map_or(0, Vec::len));
40    let mut run_ids = Vec::with_capacity(length);
41    let mut frame_indices = Vec::with_capacity(length);
42    let mut gains = Vec::with_capacity(length);
43    let mut backgrounds = Vec::with_capacity(length);
44    for frame in 0..length {
45        run_ids.push(run_id);
46        frame_indices.push(frame as u64);
47        gains.push(
48            result
49                .recovered_frame_gains
50                .as_ref()
51                .and_then(|values| values.get(frame).copied()),
52        );
53        backgrounds.push(
54            result
55                .recovered_background
56                .as_ref()
57                .and_then(|values| values.get(frame).copied()),
58        );
59    }
60    Ok(df!(
61        "run_id" => run_ids,
62        "frame_index" => frame_indices,
63        "gain" => gains,
64        "background" => backgrounds,
65    )?)
66}