use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
pub file_name: String,
pub backend: String,
pub algorithm: String,
pub filter_name: String,
pub rotation_axis: Option<f32>,
pub lamino_angle: Option<f32>,
pub remove_stripe_method: String,
pub retrieve_phase_method: String,
pub num_iter: usize,
pub ext_pad: bool,
pub reg_par: Vec<f32>,
pub nsino_per_chunk: usize,
pub save_format: String,
pub dtype: String,
pub output: Option<String>,
pub fw_sigma: f32,
pub fw_level: usize,
pub ti_nblock: usize,
pub ti_beta: f32,
pub sf_size: usize,
pub vo_snr: f32,
pub vo_la_size: usize,
pub vo_sm_size: usize,
pub vo_sort_size: usize,
pub vo_sort_dim: u8,
pub vo_filter_sigma: f32,
pub vo_filter_size: usize,
pub vo_filter_dim: u8,
pub vo_large_snr: f32,
pub vo_large_size: usize,
pub vo_large_drop_ratio: f32,
pub vo_large_norm: bool,
pub vo_dead_snr: f32,
pub vo_dead_size: usize,
pub vo_dead_norm: bool,
pub vo_fit_order: usize,
pub vo_fit_sigma_x: f32,
pub vo_fit_sigma_y: f32,
pub pixel_size: f64,
pub propagation_distance: f64,
pub energy: f64,
pub alpha: f64,
pub db: f64,
pub w: f64,
}
impl Default for Config {
fn default() -> Self {
Config {
file_name: String::new(),
backend: "auto".into(),
algorithm: "fbp".into(),
filter_name: "parzen".into(),
rotation_axis: None,
lamino_angle: None,
remove_stripe_method: "none".into(),
retrieve_phase_method: "none".into(),
num_iter: 1,
ext_pad: false,
reg_par: Vec::new(),
nsino_per_chunk: 8,
save_format: "tiff".into(),
dtype: "float32".into(),
output: None,
fw_sigma: 2.0,
fw_level: 0,
ti_nblock: 0,
ti_beta: 1.5,
sf_size: 5,
vo_snr: 3.0,
vo_la_size: 61,
vo_sm_size: 21,
vo_sort_size: 0,
vo_sort_dim: 1,
vo_filter_sigma: 3.0,
vo_filter_size: 0,
vo_filter_dim: 1,
vo_large_snr: 3.0,
vo_large_size: 51,
vo_large_drop_ratio: 0.1,
vo_large_norm: true,
vo_dead_snr: 3.0,
vo_dead_size: 51,
vo_dead_norm: true,
vo_fit_order: 3,
vo_fit_sigma_x: 5.0,
vo_fit_sigma_y: 20.0,
pixel_size: 1e-4,
propagation_distance: 50.0,
energy: 30.0,
alpha: 1e-3,
db: 1000.0,
w: 2e-4,
}
}
}
impl Config {
pub fn to_toml(&self) -> Result<String> {
toml::to_string_pretty(self)
.map_err(|e| Error::InvalidParam(format!("config serialize: {e}")))
}
pub fn write(&self, path: &Path) -> Result<()> {
std::fs::write(path, self.to_toml()?)
.map_err(|e| Error::Io(format!("writing config {}: {e}", path.display())))
}
pub fn load(path: &Path) -> Result<Self> {
let text = std::fs::read_to_string(path)
.map_err(|e| Error::Io(format!("reading config {}: {e}", path.display())))?;
toml::from_str(&text)
.map_err(|e| Error::InvalidParam(format!("config parse {}: {e}", path.display())))
}
}