tomoxide 0.14.0

GPU/CPU tomographic reconstruction toolkit (tomopy + tomocupy, in Rust)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//! Per-voxel XANES peak-energy fitting.
//!
//! Given a stack of reconstructed volumes taken at a series of X-ray energies
//! straddling an absorption edge, this maps each voxel's absorption spectrum to
//! the energy of its edge/whiteline peak — a chemical-state map. The numerics
//! are a direct port of the reference pipeline (`txm_pal_core`, the crate the
//! beamline's Python XANES tool calls); see [`fit`] and [`filter`] for the
//! bit-for-bit provenance.
//!
//! Two layers:
//!
//! - [`fit_peak_energy`] — the per-voxel core: smooth the spectrum, locate the
//!   peak, and fit a [`FitMethod`] curve to a window around it, returning the
//!   fitted peak energy (or NaN if the fit leaves the `[start_e, stop_e]`
//!   window).
//! - [`fit_map`] — a `rayon` driver over a volume view (`(E, z, y, x)`),
//!   parallel across `z`. It reads `f32` views and never materialises the full
//!   `f64` stack, so a caller can stream one z-band at a time (a 40-energy
//!   500³ volume is ~40 GB as `f64`); [`CancelToken`] is polled between slices.
//!
//! Feature-gated behind `xanes` (pulls `levenberg-marquardt`, `nalgebra`,
//! `savgol-rs`, `median`); off by default so the core stays lean.

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;

/// Curve model fitted to the windowed peak.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FitMethod {
    /// Downward parabola; peak energy is the vertex `-b / 2a`. Robust and cheap
    /// — the default for whiteline-position mapping.
    #[default]
    Quadratic,
    /// Gaussian `a·exp(-(x-b)²/2c²) + d`; peak energy is the centre `b`.
    Gaussian,
}

/// Optional 1-D spectral smoother applied before peak finding.
///
/// One smoother is applied uniformly regardless of the [`FitMethod`]. This is a
/// deliberate divergence from the reference `TXM-Pal-core` fitter, whose two
/// fit functions key the median branch on different strings (`"median"` in the
/// quadratic path, `"medfilt"` in the gaussian path). Because the app passes a
/// single algorithm name, exactly one fit method there silently skips
/// smoothing — e.g. with `"median"` the gaussian fits run unsmoothed. The
/// enum here removes that string coupling so median smoothing always applies;
/// do not "restore parity" by reintroducing the split keys.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SmoothAlgo {
    /// No smoothing.
    #[default]
    None,
    /// Savitzky–Golay (`smooth_width` window, `smooth_order` polynomial).
    SavGol,
    /// Sliding-window median (`smooth_width`), zero-padded edges.
    Median,
    /// `smooth_width` iterations of a 3-point moving average.
    ThreePoint,
    /// Centred boxcar of width `smooth_width`.
    Boxcar,
}

/// Fit configuration shared by every voxel in a map.
#[derive(Debug, Clone, Copy)]
pub struct FitParams {
    /// Curve model.
    pub method: FitMethod,
    /// Number of energy samples in the fit window centred on the peak.
    pub points: usize,
    /// Lower energy bound: peak search and result validity both clamp here.
    pub start_e: f64,
    /// Upper energy bound.
    pub stop_e: f64,
    /// Pre-fit smoother.
    pub smooth: SmoothAlgo,
    /// Smoother window width (samples / iterations, per algorithm).
    pub smooth_width: usize,
    /// Savitzky–Golay polynomial order (ignored by other smoothers).
    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,
        }
    }
}

/// NaN-tolerant "argmax by value" — returns the index of the largest sample.
///
/// The reference uses `partial_cmp().unwrap()`, which panics on a NaN sample
/// (a single bad voxel would abort the whole parallel map). `total_cmp` orders
/// NaN deterministically instead; for all-finite spectra it is identical.
fn argmax(xs: &[f64]) -> Option<usize> {
    xs.iter()
        .enumerate()
        .max_by(|(_, a), (_, b)| a.total_cmp(b))
        .map(|(i, _)| i)
}

/// Same as [`argmax`] but for the minimum sample.
fn argmin(xs: &[f64]) -> Option<usize> {
    xs.iter()
        .enumerate()
        .min_by(|(_, a), (_, b)| a.total_cmp(b))
        .map(|(i, _)| i)
}

/// Fit one voxel spectrum and return its peak energy, or NaN.
///
/// `energy` and `spectrum` are parallel arrays (same length, energy ascending).
/// Mirrors the reference `quadfit_mc_3d` / `gaussianfit_mc_3d` inner body: the
/// spectrum is optionally smoothed, the peak is located within
/// `[start_e, stop_e]`, and a [`FitMethod`] curve is fitted to a `points`-wide
/// window around it. A fitted centre outside `[start_e, stop_e]` (or a
/// degenerate window) yields NaN so the caller can mask it out.
///
/// This is the low-level per-voxel core and does not *reject* bad parameters —
/// [`fit_map`] is the driver that validates them loudly. But it must never
/// panic on a direct caller's input: an invalid smoother width (SavGol failure,
/// or a zero/even width for the odd-window `Median`/`Boxcar` filters) also
/// yields NaN, honouring the same mask-it-out contract rather than panicking
/// (`median::Filter::new(0)`) or fabricating a value (boxcar `1/0`).
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;
    }

    // Energy window: first index at or past each bound (reference semantics).
    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;
    }

    // Smoothing.
    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,
            };
            // A silent fall-through to the unsmoothed spectrum would fit raw
            // data while the caller believes smoothing is on (plausible but
            // wrong). Surface it as NaN instead; the `fit_map` driver validates
            // the parameters up front so this is unreachable on that path.
            match savgol_filter(&input) {
                Ok(f) => slice = f,
                Err(_) => return f64::NAN,
            }
        }
        // Median and Boxcar are odd-window-only. A zero width panics medfilt
        // (`median::Filter::new(0)`) / makes boxcar divide by zero, and an even
        // width silently corrupts (tail zeros / (w+1)/w inflation). fit_map
        // rejects both up front, but this pub path must mask an invalid width as
        // NaN, not panic — the same way the SavGol arm surfaces its error.
        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);
        }
    }

    // Peak within the energy window.
    let sub = &slice[start_idx..stop_idx];
    let Some(rel_max) = argmax(sub) else {
        return f64::NAN;
    };
    let max_idx = rel_max + start_idx;

    // Fit window: `points` samples centred on the peak, clamped to the spectrum.
    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;
    }
    // A degenerate window (peak at an edge) has no data to fit / a zero slope
    // denominator — mask instead of dividing by zero.
    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 => {
            // Initial guess for a·x² + b·x + c.
            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 => {
            // Initial guess for a·exp(-(x-b)²/2c²) + d, from the window extrema.
            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
    }
}

/// Fit a peak-energy map over a volume view, parallel across `z`.
///
/// `volume` is `(E, z, y, x)` and `mask` is `(z, y, x)`; a zero mask voxel is
/// skipped and left NaN. To stream, slice a z-band out of the full stack
/// (`volume.slice(s![.., z0..z1, .., ..])` with the matching mask band) and
/// call this per band — the `f32` views mean the full `f64` stack is never
/// held. `cancel`, if given, is polled once per z-slice; a fired token returns
/// [`Error::Cancelled`].
///
/// Returns a `(z, y, x)` `f64` map of fitted peak energies (NaN where masked,
/// unfittable, or out of the energy window).
///
/// Window contract: unlike the smoother parameters — which are rejected up
/// front with [`Error::InvalidParam`] — an inconsistent search window is *not*
/// an error. If `start_e >= stop_e`, or the window does not overlap `energy`
/// (`start_e` past the last energy), every voxel falls out of the window and
/// the whole map comes back NaN. This is by design (the NaN mask is the signal),
/// but it means an all-NaN result is the caller's cue to check that
/// `start_e < stop_e` and that `[start_e, stop_e]` overlaps the energy grid,
/// not evidence of a fit failure.
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()),
        });
    }
    // Validate the smoother parameters once, loudly, so no per-voxel smoother
    // call can panic inside the rayon map (aborting the whole reconstruction)
    // or silently NaN/inf the map. Per voxel a SavGol failure still falls back
    // to NaN (see `fit_peak_energy`); the width-driven smoothers are guarded
    // here by construction.
    match params.smooth {
        SmoothAlgo::SavGol => {
            // Validity depends on length, not values; probe a dummy spectrum of
            // the real length.
            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
                )));
            }
        }
        // Median builds a `median::Filter::new(width)` that panics on a
        // zero-length window; Boxcar divides by `width` (→ inf). Both need a
        // positive width. ThreePoint uses `smooth_width` as an iteration count,
        // where 0 is a harmless no-op, and None does not smooth.
        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)"
            )));
        }
        // Median and Boxcar are both centred, odd-window filters. medfilt's
        // radius is `width / 2`, so an even width mis-centres it and the
        // right-edge branch stores zero-padded medians (re-zeroing the tail).
        // boxcar sums `width + 1` samples for an even width — window
        // `[i - width/2, i + width/2]` — but normalises by `1 / width`, so it
        // inflates a constant signal by `(width + 1) / width` (e.g. ×1.25 at
        // width 4) instead of preserving it. Neither has a canonical even-window
        // form (SciPy `medfilt` rejects even kernels; the reference forces odd),
        // so reject even widths up front, like the SavGol branch above.
        // ThreePoint uses `smooth_width` as an iteration count (even is fine) and
        // None does not smooth.
        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; // left NaN
                    }
                    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};

    /// Sample a Gaussian whiteline peaked at `center` over `energy`.
    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> {
        // 8.30–8.40 keV, 21 points (5 eV step) — a typical Co K-edge scan.
        (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() {
        // fit_peak_energy is pub and does not validate params (fit_map does), but
        // it must honour its "degenerate -> NaN" contract rather than panicking:
        // Median width 0 would panic medfilt (median::Filter::new(0)) and an even
        // width silently corrupts; boxcar width 0 divides by zero. All must NaN.
        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}"
                );
            }
        }
        // A valid odd width still fits (proves the guard is not over-broad).
        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() {
        // A [start_e, stop_e] window past the whole scan has no sample at or
        // above start_e → no valid peak search → NaN, never a fabricated value.
        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() {
        // A pure linear ramp has no parabola vertex: the best fit drives a→0,
        // pushing the vertex -b/2a to ±∞, which fails the [start_e, stop_e]
        // range check → NaN. Guards against reporting an off-window edge as a
        // peak energy.
        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;
                    }
                }
            }
        }
        // Mask out one voxel.
        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() {
        // An even Savitzky–Golay window is invalid. The driver must reject it
        // loudly up front, not silently NaN every voxel.
        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, // even window → invalid
            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() {
        // A zero window would panic per voxel (Median → median::Filter::new(0))
        // or emit an all-inf map (Boxcar → 1/0). fit_map must reject it up
        // front, the same way it does an invalid SavGol window.
        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() {
        // medfilt (centred radius = width/2) mis-centres and re-zeros the tail
        // for an even width; boxcar sums width+1 samples but divides by width,
        // inflating a constant by (width+1)/width. Both are odd-window only, so
        // fit_map must reject an even width up front, the way it rejects even
        // SavGol windows.
        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:?}"
                );
            }
        }
        // An odd width is still accepted for both.
        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)));
    }
}