use ndarray::{Array2, Array3, ArrayView1, ArrayView3, ArrayView4};
use rayon::prelude::*;
use savgol_rs::{savgol_filter, SavGolInput};
use crate::error::{Error, Result};
use crate::pipeline::CancelToken;
mod filter;
mod fit;
mod magnification;
mod reader;
mod result;
use filter::{boxcar, medfilt, multi_3point_average};
use fit::{gaussian_fit_center, quadratic_fit_center};
pub use magnification::{apply_magnification, magnification_corr_factors, MagnificationParams};
pub use reader::{EnergyLayer, MultiEnergyVolume};
pub use result::write_peak_map_h5;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FitMethod {
#[default]
Quadratic,
Gaussian,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SmoothAlgo {
#[default]
None,
SavGol,
Median,
ThreePoint,
Boxcar,
}
#[derive(Debug, Clone, Copy)]
pub struct FitParams {
pub method: FitMethod,
pub points: usize,
pub start_e: f64,
pub stop_e: f64,
pub smooth: SmoothAlgo,
pub smooth_width: usize,
pub smooth_order: usize,
}
impl Default for FitParams {
fn default() -> Self {
FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: f64::NEG_INFINITY,
stop_e: f64::INFINITY,
smooth: SmoothAlgo::None,
smooth_width: 5,
smooth_order: 2,
}
}
}
fn argmax(xs: &[f64]) -> Option<usize> {
xs.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.total_cmp(b))
.map(|(i, _)| i)
}
fn argmin(xs: &[f64]) -> Option<usize> {
xs.iter()
.enumerate()
.min_by(|(_, a), (_, b)| a.total_cmp(b))
.map(|(i, _)| i)
}
pub fn fit_peak_energy(energy: &[f64], spectrum: &[f64], p: &FitParams) -> f64 {
let n = energy.len();
if n < 2 || spectrum.len() != n {
return f64::NAN;
}
let Some(start_idx) = energy.iter().position(|&x| x >= p.start_e) else {
return f64::NAN;
};
let Some(stop_idx) = energy.iter().position(|&x| x >= p.stop_e) else {
return f64::NAN;
};
if start_idx >= stop_idx {
return f64::NAN;
}
let mut slice = spectrum.to_vec();
match p.smooth {
SmoothAlgo::None => {}
SmoothAlgo::SavGol => {
let input = SavGolInput {
data: &slice,
window_length: p.smooth_width,
poly_order: p.smooth_order,
derivative: 0,
};
match savgol_filter(&input) {
Ok(f) => slice = f,
Err(_) => return f64::NAN,
}
}
SmoothAlgo::Median => {
if p.smooth_width == 0 || p.smooth_width.is_multiple_of(2) {
return f64::NAN;
}
slice = medfilt(slice, p.smooth_width, "zeropadding");
}
SmoothAlgo::ThreePoint => slice = multi_3point_average(&slice, p.smooth_width),
SmoothAlgo::Boxcar => {
if p.smooth_width == 0 || p.smooth_width.is_multiple_of(2) {
return f64::NAN;
}
slice = boxcar(&slice, p.smooth_width);
}
}
let sub = &slice[start_idx..stop_idx];
let Some(rel_max) = argmax(sub) else {
return f64::NAN;
};
let max_idx = rel_max + start_idx;
let half = p.points / 2;
let fit_start = max_idx.saturating_sub(half);
let mut fit_end = max_idx + half + 1;
if fit_end > slice.len() - 1 {
fit_end = slice.len() - 1;
}
if fit_end <= fit_start {
return f64::NAN;
}
let xdata = energy[fit_start..fit_end].to_vec();
let ydata = slice[fit_start..fit_end].to_vec();
let center = match p.method {
FitMethod::Quadratic => {
let c = slice[fit_start];
let denom = energy[fit_end] - energy[fit_start];
if denom == 0.0 {
return f64::NAN;
}
let b = (slice[fit_end] - slice[fit_start]) / denom;
let mut a = -b / (2.0 * energy[max_idx]);
if a > 0.0 {
a = -a;
}
quadratic_fit_center(xdata, ydata, vec![a, b, c])
}
FitMethod::Gaussian => {
let (nrjmin, nrjmax) = energy[start_idx..stop_idx]
.iter()
.fold((f64::INFINITY, f64::NEG_INFINITY), |(mn, mx), &v| {
(mn.min(v), mx.max(v))
});
let Some(rel_min) = argmin(sub) else {
return f64::NAN;
};
let min_idx = rel_min + start_idx;
let maxy = slice[max_idx];
let miny = slice[min_idx];
let cen = energy[max_idx];
let height = (maxy - miny) * 3.0;
let sig = (nrjmax - nrjmin) / 6.0;
let amp = height * sig;
gaussian_fit_center(xdata, ydata, vec![amp, cen, sig, miny])
}
};
if center >= p.start_e && center <= p.stop_e {
center
} else {
f64::NAN
}
}
pub fn fit_map(
energy: ArrayView1<f64>,
volume: ArrayView4<f32>,
mask: ArrayView3<u8>,
params: &FitParams,
cancel: Option<&CancelToken>,
) -> Result<Array3<f64>> {
let (ne, nz, ny, nx) = volume.dim();
if energy.len() != ne {
return Err(Error::ShapeMismatch {
expected: format!("energy len == volume energy axis ({ne})"),
found: format!("energy len {}", energy.len()),
});
}
if mask.dim() != (nz, ny, nx) {
return Err(Error::ShapeMismatch {
expected: format!("mask {:?}", (nz, ny, nx)),
found: format!("mask {:?}", mask.dim()),
});
}
match params.smooth {
SmoothAlgo::SavGol => {
let probe = vec![0.0f64; ne];
let input = SavGolInput {
data: &probe,
window_length: params.smooth_width,
poly_order: params.smooth_order,
derivative: 0,
};
if let Err(e) = savgol_filter(&input) {
return Err(Error::InvalidParam(format!(
"Savitzky–Golay smoothing invalid for {ne} energies \
(window {}, order {}): {e}",
params.smooth_width, params.smooth_order
)));
}
}
SmoothAlgo::Median | SmoothAlgo::Boxcar if params.smooth_width == 0 => {
let algo = if matches!(params.smooth, SmoothAlgo::Median) {
"median"
} else {
"boxcar"
};
return Err(Error::InvalidParam(format!(
"{algo} smoothing needs a window width >= 1 (got 0)"
)));
}
SmoothAlgo::Median | SmoothAlgo::Boxcar if params.smooth_width.is_multiple_of(2) => {
let algo = if matches!(params.smooth, SmoothAlgo::Median) {
"median"
} else {
"boxcar"
};
return Err(Error::InvalidParam(format!(
"{algo} smoothing needs an odd window width (got {})",
params.smooth_width
)));
}
_ => {}
}
let energy = energy
.as_slice()
.map_or_else(|| energy.to_vec(), <[f64]>::to_vec);
let cancelled = std::sync::atomic::AtomicBool::new(false);
let ordering = std::sync::atomic::Ordering::Relaxed;
let slices: Vec<Array2<f64>> = (0..nz)
.into_par_iter()
.map(|iz| {
let mut out = Array2::from_elem((ny, nx), f64::NAN);
if cancel.is_some_and(CancelToken::is_cancelled) {
cancelled.store(true, ordering);
return out;
}
let mut spectrum = vec![0.0f64; ne];
for iy in 0..ny {
for ix in 0..nx {
if mask[[iz, iy, ix]] == 0 {
continue; }
for (e, s) in spectrum.iter_mut().enumerate() {
*s = volume[[e, iz, iy, ix]] as f64;
}
out[[iy, ix]] = fit_peak_energy(&energy, &spectrum, params);
}
}
out
})
.collect();
if cancelled.load(ordering) || cancel.is_some_and(CancelToken::is_cancelled) {
return Err(Error::Cancelled);
}
let mut vol = Array3::from_elem((nz, ny, nx), f64::NAN);
for (iz, s) in slices.into_iter().enumerate() {
vol.slice_mut(ndarray::s![iz, .., ..]).assign(&s);
}
Ok(vol)
}
#[cfg(test)]
mod tests {
use super::*;
use ndarray::{Array1, Array3, Array4};
fn synth_spectrum(energy: &[f64], center: f64, width: f64) -> Vec<f64> {
energy
.iter()
.map(|&e| (-0.5 * ((e - center) / width).powi(2)).exp())
.collect()
}
fn energy_axis() -> Vec<f64> {
(0..21).map(|i| 8.300 + i as f64 * 0.005).collect()
}
#[test]
fn quadratic_recovers_known_peak() {
let energy = energy_axis();
let truth = 8.352;
let spectrum = synth_spectrum(&energy, truth, 0.02);
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 8.30,
stop_e: 8.40,
..Default::default()
};
let got = fit_peak_energy(&energy, &spectrum, &p);
assert!(
(got - truth).abs() < 0.005,
"quadratic peak {got} vs truth {truth}"
);
}
#[test]
fn gaussian_recovers_known_peak() {
let energy = energy_axis();
let truth = 8.352;
let spectrum = synth_spectrum(&energy, truth, 0.02);
let p = FitParams {
method: FitMethod::Gaussian,
points: 9,
start_e: 8.30,
stop_e: 8.40,
..Default::default()
};
let got = fit_peak_energy(&energy, &spectrum, &p);
assert!(
(got - truth).abs() < 0.003,
"gaussian peak {got} vs truth {truth}"
);
}
#[test]
fn fit_peak_energy_masks_invalid_smoother_width_instead_of_panicking() {
let energy = energy_axis();
let spectrum = synth_spectrum(&energy, 8.352, 0.02);
for smooth in [SmoothAlgo::Median, SmoothAlgo::Boxcar] {
for width in [0usize, 2, 4] {
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 8.30,
stop_e: 8.40,
smooth,
smooth_width: width,
..Default::default()
};
let got = fit_peak_energy(&energy, &spectrum, &p);
assert!(
got.is_nan(),
"{smooth:?} width={width} must yield NaN, got {got}"
);
}
}
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 8.30,
stop_e: 8.40,
smooth: SmoothAlgo::Median,
smooth_width: 3,
..Default::default()
};
assert!(fit_peak_energy(&energy, &spectrum, &p).is_finite());
}
#[test]
fn empty_energy_window_is_nan() {
let energy = energy_axis();
let spectrum = synth_spectrum(&energy, 8.352, 0.02);
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 9.00,
stop_e: 9.10,
..Default::default()
};
assert!(fit_peak_energy(&energy, &spectrum, &p).is_nan());
}
#[test]
fn fitted_center_out_of_range_is_nan() {
let energy = energy_axis();
let spectrum: Vec<f64> = (0..energy.len()).map(|i| i as f64).collect();
let p = FitParams {
method: FitMethod::Quadratic,
points: 21,
start_e: 8.30,
stop_e: 8.40,
..Default::default()
};
let got = fit_peak_energy(&energy, &spectrum, &p);
assert!(got.is_nan(), "expected NaN for rampless vertex, got {got}");
}
#[test]
fn fit_map_masks_and_fits() {
let energy = energy_axis();
let ne = energy.len();
let (nz, ny, nx) = (2, 2, 3);
let truth = 8.352;
let spectrum = synth_spectrum(&energy, truth, 0.02);
let mut volume = Array4::<f32>::zeros((ne, nz, ny, nx));
for e in 0..ne {
for iz in 0..nz {
for iy in 0..ny {
for ix in 0..nx {
volume[[e, iz, iy, ix]] = spectrum[e] as f32;
}
}
}
}
let mut mask = Array3::<u8>::ones((nz, ny, nx));
mask[[0, 0, 0]] = 0;
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 8.30,
stop_e: 8.40,
..Default::default()
};
let map = fit_map(
Array1::from(energy).view(),
volume.view(),
mask.view(),
&p,
None,
)
.unwrap();
assert!(map[[0, 0, 0]].is_nan(), "masked voxel must be NaN");
for iz in 0..nz {
for iy in 0..ny {
for ix in 0..nx {
if (iz, iy, ix) == (0, 0, 0) {
continue;
}
let v = map[[iz, iy, ix]];
assert!((v - truth).abs() < 0.005, "voxel fit {v} vs {truth}");
}
}
}
}
#[test]
fn fit_map_rejects_invalid_savgol() {
let energy = energy_axis();
let ne = energy.len();
let volume = Array4::<f32>::zeros((ne, 1, 1, 2));
let mask = Array3::<u8>::ones((1, 1, 2));
let p = FitParams {
method: FitMethod::Quadratic,
points: 7,
start_e: 8.30,
stop_e: 8.40,
smooth: SmoothAlgo::SavGol,
smooth_width: 4, smooth_order: 2,
};
let r = fit_map(
Array1::from(energy).view(),
volume.view(),
mask.view(),
&p,
None,
);
assert!(
matches!(r, Err(Error::InvalidParam(_))),
"invalid SavGol window must be a loud error, got {r:?}"
);
}
#[test]
fn fit_map_rejects_zero_width_median_and_boxcar() {
let energy = energy_axis();
let ne = energy.len();
let volume = Array4::<f32>::zeros((ne, 1, 1, 1));
let mask = Array3::<u8>::ones((1, 1, 1));
for smooth in [SmoothAlgo::Median, SmoothAlgo::Boxcar] {
let p = FitParams {
smooth,
smooth_width: 0,
..FitParams::default()
};
let r = fit_map(
Array1::from(energy.clone()).view(),
volume.view(),
mask.view(),
&p,
None,
);
assert!(
matches!(r, Err(Error::InvalidParam(_))),
"{smooth:?} width=0 must be a loud error, got {r:?}"
);
}
}
#[test]
fn fit_map_rejects_even_width_median_and_boxcar() {
let energy = energy_axis();
let ne = energy.len();
let volume = Array4::<f32>::zeros((ne, 1, 1, 1));
let mask = Array3::<u8>::ones((1, 1, 1));
for smooth in [SmoothAlgo::Median, SmoothAlgo::Boxcar] {
for width in [2usize, 4, 6] {
let p = FitParams {
smooth,
smooth_width: width,
..FitParams::default()
};
let r = fit_map(
Array1::from(energy.clone()).view(),
volume.view(),
mask.view(),
&p,
None,
);
assert!(
matches!(r, Err(Error::InvalidParam(_))),
"{smooth:?} width={width} (even) must be a loud error, got {r:?}"
);
}
}
for smooth in [SmoothAlgo::Median, SmoothAlgo::Boxcar] {
let p = FitParams {
smooth,
smooth_width: 5,
..FitParams::default()
};
let r = fit_map(
Array1::from(energy.clone()).view(),
volume.view(),
mask.view(),
&p,
None,
);
assert!(
r.is_ok(),
"{smooth:?} odd width must be accepted, got {r:?}"
);
}
}
#[test]
fn fit_map_cancel_returns_cancelled() {
let energy = energy_axis();
let ne = energy.len();
let volume = Array4::<f32>::zeros((ne, 1, 1, 1));
let mask = Array3::<u8>::ones((1, 1, 1));
let token = CancelToken::new();
token.cancel();
let p = FitParams::default();
let r = fit_map(
Array1::from(energy).view(),
volume.view(),
mask.view(),
&p,
Some(&token),
);
assert!(matches!(r, Err(Error::Cancelled)));
}
}