use crate::error::{Error, Result};
use crate::params::{PhaseMethod, StripeMethod};
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())))
}
pub fn stripe_method(&self) -> Result<StripeMethod> {
let auto = |n: usize| (n != 0).then_some(n);
Ok(
match self.remove_stripe_method.to_ascii_lowercase().as_str() {
"none" => StripeMethod::None,
"fw" => StripeMethod::Fw {
sigma: self.fw_sigma,
level: auto(self.fw_level),
},
"ti" => StripeMethod::Ti {
nblock: self.ti_nblock,
beta: self.ti_beta,
},
"sf" => StripeMethod::Sf { size: self.sf_size },
"vo-all" | "vo_all" | "voall" => StripeMethod::VoAll {
snr: self.vo_snr,
la_size: self.vo_la_size,
sm_size: self.vo_sm_size,
},
"vo-sort" | "vo_sort" => StripeMethod::VoSort {
size: auto(self.vo_sort_size),
dim: self.vo_sort_dim,
},
"vo-filter" | "vo_filter" => StripeMethod::VoFilter {
sigma: self.vo_filter_sigma,
size: auto(self.vo_filter_size),
dim: self.vo_filter_dim,
},
"vo-large" | "vo_large" => StripeMethod::VoLarge {
snr: self.vo_large_snr,
size: self.vo_large_size,
drop_ratio: self.vo_large_drop_ratio,
norm: self.vo_large_norm,
},
"vo-dead" | "vo_dead" => StripeMethod::VoDead {
snr: self.vo_dead_snr,
size: self.vo_dead_size,
norm: self.vo_dead_norm,
},
"vo-fit" | "vo_fit" => StripeMethod::VoFit {
order: self.vo_fit_order,
sigma: (self.vo_fit_sigma_x, self.vo_fit_sigma_y),
},
other => {
return Err(Error::InvalidParam(format!(
"unknown stripe method '{other}' \
(none|fw|ti|sf|vo-all|vo-sort|vo-filter|vo-large|vo-dead|vo-fit)"
)))
}
},
)
}
pub fn phase_method(&self) -> Result<PhaseMethod> {
let (pixel_size, dist, energy) = (
self.pixel_size as f32,
self.propagation_distance as f32,
self.energy as f32,
);
Ok(
match self.retrieve_phase_method.to_ascii_lowercase().as_str() {
"none" => PhaseMethod::None,
"paganin" => PhaseMethod::Paganin {
pixel_size,
dist,
energy,
alpha: self.alpha as f32,
},
"gpaganin" => PhaseMethod::GPaganin {
pixel_size,
dist,
energy,
db: self.db as f32,
w: self.w as f32,
},
"farago" => PhaseMethod::Farago {
pixel_size,
dist,
energy,
db: self.db as f32,
},
other => {
return Err(Error::InvalidParam(format!(
"unknown phase method '{other}' (none|paganin|Gpaganin|farago)"
)))
}
},
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stripe_method_parses_every_spelling() {
let mut c = Config {
vo_filter_size: 7,
vo_filter_dim: 2,
vo_dead_snr: 4.0,
vo_dead_size: 41,
vo_dead_norm: false,
..Config::default()
};
let mut m = |name: &str| {
c.remove_stripe_method = name.into();
c.stripe_method()
};
assert_eq!(m("none").unwrap(), StripeMethod::None);
assert_eq!(
m("FW").unwrap(),
StripeMethod::Fw {
sigma: 2.0,
level: None
}
);
assert_eq!(
m("vo_all").unwrap(),
StripeMethod::VoAll {
snr: 3.0,
la_size: 61,
sm_size: 21
}
);
assert_eq!(
m("vo-sort").unwrap(),
StripeMethod::VoSort { size: None, dim: 1 }
);
assert_eq!(
m("vo_filter").unwrap(),
StripeMethod::VoFilter {
sigma: 3.0,
size: Some(7),
dim: 2
}
);
assert_eq!(
m("vo-large").unwrap(),
StripeMethod::VoLarge {
snr: 3.0,
size: 51,
drop_ratio: 0.1,
norm: true
}
);
assert_eq!(
m("vo-dead").unwrap(),
StripeMethod::VoDead {
snr: 4.0,
size: 41,
norm: false
}
);
assert_eq!(
m("vo-fit").unwrap(),
StripeMethod::VoFit {
order: 3,
sigma: (5.0, 20.0)
}
);
let err = m("nope").unwrap_err().to_string();
assert!(err.contains("vo-fit"), "{err}");
}
#[test]
fn phase_method_parses_and_casts() {
let c = Config {
retrieve_phase_method: "Gpaganin".into(),
pixel_size: 0.65e-4,
..Config::default()
};
assert_eq!(
c.phase_method().unwrap(),
PhaseMethod::GPaganin {
pixel_size: 0.65e-4,
dist: 50.0,
energy: 30.0,
db: 1000.0,
w: 2e-4
}
);
let c = Config {
retrieve_phase_method: "nope".into(),
..Config::default()
};
assert!(c.phase_method().unwrap_err().to_string().contains("farago"));
}
}