rexafs 0.2.12

Rust-powered X-ray absorption spectroscopy analysis and EXAFS fitting
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
//! Region-balanced MBACK with scaled SVD and bounded erfc variable projection.
use super::*;
use crate::atomic::AtomicTable;
use crate::xafs::normalization::PrePostEdge;
use errorfunctions::RealErrorFunctions;
use nalgebra::DMatrix;
use std::f64::consts::PI;

const RANK_TOL: f64 = 1e-10;
type Result<T> = std::result::Result<T, NormalizationError>;

struct LinearFit {
    coefficients: DVector<f64>,
    objective: f64,
    rank: usize,
    condition: f64,
}
fn scaled_solve(a: &DMatrix<f64>, b: &DVector<f64>) -> Result<LinearFit> {
    let (scaled, scales) = scale_columns(a)?;
    let svd = scaled.svd(true, true);
    let largest = svd.singular_values.max();
    let rank = svd
        .singular_values
        .iter()
        .filter(|&&s| s > largest * RANK_TOL)
        .count();
    if rank < a.ncols() {
        return Err(invalid(format!(
            "fit is not identifiable: rank {rank}/{}",
            a.ncols()
        )));
    }
    let condition = largest / svd.singular_values.min();
    let coefficients = svd
        .solve(b, largest * RANK_TOL)
        .map_err(invalid)?
        .component_div(&scales);
    if coefficients.iter().any(|v| !v.is_finite()) {
        return Err(invalid("linear fit overflow"));
    }
    let objective = (a * &coefficients - b).norm_squared();
    if !objective.is_finite() {
        return Err(invalid("objective overflow"));
    }
    Ok(LinearFit {
        coefficients,
        objective,
        rank,
        condition,
    })
}
fn scale_columns(a: &DMatrix<f64>) -> Result<(DMatrix<f64>, DVector<f64>)> {
    let scales = DVector::from_iterator(a.ncols(), (0..a.ncols()).map(|j| a.column(j).norm()));
    if scales
        .iter()
        .any(|s| !s.is_finite() || *s <= f64::MIN_POSITIVE)
    {
        return Err(invalid(
            "zero or nonfinite model column; reduce the model or revise the ranges",
        ));
    }
    let mut scaled = a.clone();
    for (j, &s) in scales.iter().enumerate() {
        scaled.column_mut(j).scale_mut(1. / s);
    }
    Ok((scaled, scales))
}
fn checked_region(name: &str, range: [f64; 2], energy: &[f64], e0: f64) -> Result<Vec<usize>> {
    if range.iter().any(|v| !v.is_finite()) || range[0] >= range[1] {
        return Err(invalid(format!(
            "{name} must have increasing finite offsets"
        )));
    }
    let (lo, hi) = (range[0] + e0, range[1] + e0);
    if lo < energy[0] || hi > *energy.last().unwrap() {
        return Err(invalid(format!(
            "{name} [{lo}, {hi}] eV is outside the scan; explicit intervals are not shortened"
        )));
    }
    let indices = energy
        .iter()
        .enumerate()
        .filter_map(|(i, &e)| (e >= lo && e <= hi).then_some(i))
        .collect::<Vec<_>>();
    // Auxiliary polynomial normalization excludes the upper endpoint, and needs
    // a line in pre-edge and a quadratic in post-edge. Do not let it expand a range.
    let minimum = if name == "pre-edge" { 3 } else { 4 };
    if indices.len() < minimum {
        return Err(invalid(format!(
            "{name} needs at least {minimum} measured points"
        )));
    }
    Ok(indices)
}

struct Problem<'a> {
    energy: &'a [f64],
    mu: &'a [f64],
    f2: &'a [f64],
    indices: &'a [usize],
    weights: &'a [f64],
    t: &'a [f64],
    degree: usize,
    emission: Option<f64>,
    erfc: Option<&'a MbackErfc>,
}
impl Problem<'_> {
    fn system(&self, width: Option<f64>) -> (DMatrix<f64>, DVector<f64>) {
        let columns = self.degree + 2 + usize::from(width.is_some());
        let mut a = DMatrix::zeros(self.indices.len(), columns);
        let mut b = DVector::zeros(self.indices.len());
        for (row, (&i, &w)) in self.indices.iter().zip(self.weights).enumerate() {
            a[(row, 0)] = self.mu[i] * w;
            for j in 0..=self.degree {
                a[(row, j + 1)] = -self.t[i].powi(j as i32) * w;
            }
            if let Some(width) = width {
                a[(row, columns - 1)] =
                    -RealErrorFunctions::erfc((self.energy[i] - self.emission.unwrap()) / width)
                        * w;
            }
            b[row] = self.f2[i] * w;
        }
        (a, b)
    }
    fn solve(&self, width: Option<f64>) -> Result<LinearFit> {
        let (a, b) = self.system(width);
        let mut fit = scaled_solve(&a, &b)?;
        if let Some(term) = self.erfc {
            let last = a.ncols() - 1;
            let amplitude = fit.coefficients[last].clamp(term.amplitude[0], term.amplitude[1]);
            if amplitude != fit.coefficients[last] {
                let reduced = a.columns(0, last).into_owned();
                let rhs = &b - a.column(last) * amplitude;
                let projected = scaled_solve(&reduced, &rhs)?;
                fit.coefficients
                    .rows_mut(0, last)
                    .copy_from(&projected.coefficients);
                fit.coefficients[last] = amplitude;
                fit.objective = (&a * &fit.coefficients - &b).norm_squared();
            }
        }
        if fit.coefficients[0] <= 0. {
            return Err(invalid(
                "matching requires a nonpositive scale; check signal direction and fit intervals",
            ));
        }
        Ok(fit)
    }
    fn profile(&self) -> Result<(LinearFit, Option<f64>, usize, Vec<String>)> {
        let Some(term) = self.erfc else {
            return Ok((self.solve(None)?, None, 1, Vec::new()));
        };
        let lo = term.width_ev[0].ln();
        let hi = term.width_ev[1].ln();
        let mut evaluations = 0;
        let mut evaluate = |x: f64| {
            evaluations += 1;
            self.solve(Some(x.exp())).ok()
        };
        // A deterministic logarithmic grid locates distinct candidate basins.
        // Refine each local minimum with bounded golden-section profiling; the
        // inner solve refits every linear coefficient, including active A bounds.
        let grid = (0..=64)
            .map(|i| lo + (hi - lo) * i as f64 / 64.)
            .collect::<Vec<_>>();
        let samples = grid.iter().map(|&x| evaluate(x)).collect::<Vec<_>>();
        let score = |f: &Option<LinearFit>| f.as_ref().map_or(f64::INFINITY, |f| f.objective);
        let mut best = samples
            .iter()
            .enumerate()
            .filter_map(|(i, f)| f.as_ref().map(|f| (grid[i], f.objective)))
            .min_by(|a, b| a.1.total_cmp(&b.1))
            .ok_or_else(|| {
                invalid("no identifiable positive-scale erfc fit inside width/amplitude bounds")
            })?;
        for i in 0..grid.len() {
            if samples[i].is_none()
                || (i > 0 && score(&samples[i]) > score(&samples[i - 1]))
                || (i + 1 < grid.len() && score(&samples[i]) > score(&samples[i + 1]))
            {
                continue;
            }
            let mut a = grid[i.saturating_sub(1)];
            let mut b = grid[(i + 1).min(64)];
            let ratio = (5f64.sqrt() - 1.) / 2.;
            let mut c = b - ratio * (b - a);
            let mut d = a + ratio * (b - a);
            let mut fc = score(&evaluate(c));
            let mut fd = score(&evaluate(d));
            for _ in 0..120 {
                if b - a < 1e-10 {
                    break;
                }
                if fc < fd {
                    b = d;
                    d = c;
                    fd = fc;
                    c = b - ratio * (b - a);
                    fc = score(&evaluate(c));
                } else {
                    a = c;
                    c = d;
                    fc = fd;
                    d = a + ratio * (b - a);
                    fd = score(&evaluate(d));
                }
            }
            for (x, value) in [(c, fc), (d, fd)] {
                if value < best.1 {
                    best = (x, value);
                }
            }
        }
        let width = best.0.exp();
        let fit = evaluate(best.0).ok_or_else(|| invalid("erfc final evaluation failed"))?;
        let mut warnings = Vec::new();
        if (best.0 - lo).abs() < 1e-8 || (best.0 - hi).abs() < 1e-8 {
            warnings.push(
                "The erfc width is at a requested bound; inspect background sensitivity.".into(),
            );
        }
        let amplitude = fit.coefficients[fit.coefficients.len() - 1];
        if amplitude == term.amplitude[0] || amplitude == term.amplitude[1] {
            warnings.push(
                "The erfc amplitude is at a requested bound; inspect background sensitivity."
                    .into(),
            );
        }
        Ok((fit, Some(width), evaluations, warnings))
    }
}

pub(super) fn fit(
    model: &MBack,
    energy: &[f64],
    mu: &[f64],
    data: &dyn AtomicDataProvider,
) -> Result<MbackResult> {
    if energy.len() != mu.len() || energy.len() < 8 {
        return Err(invalid(
            "provide matching energy/mu arrays with at least eight points",
        ));
    }
    if energy.iter().any(|v| !v.is_finite() || *v <= 0.)
        || mu.iter().any(|v| !v.is_finite())
        || energy.windows(2).any(|w| w[0] >= w[1])
    {
        return Err(invalid(
            "energy must be finite, positive and strictly increasing; mu must be finite",
        ));
    }
    let options = &model.options;
    if options.element.is_empty() || options.edge.is_empty() {
        return Err(invalid(
            "select absorber and edge with MBack::for_edge(element, edge)",
        ));
    }
    if options.degree > 5 {
        return Err(invalid("background degree must be 0 through 5"));
    }
    let edge = data
        .edge(&options.element, &options.edge)
        .map_err(invalid)?;
    let energy_vec = DVector::from_column_slice(energy);
    let e0 = match model.e0 {
        Some(e) => e,
        None => super::super::xafsutils::find_e0(&energy_vec, &DVector::from_column_slice(mu))
            .map_err(invalid)?,
    };
    if !e0.is_finite() || e0 <= energy[0] || e0 >= *energy.last().unwrap() {
        return Err(invalid(
            "E0 must be strictly inside the measured energy interval",
        ));
    }
    let edges = data.edges(&edge.element).map_err(invalid)?;
    let mut pre_lo = energy[0] - e0;
    let mut post_hi = energy[energy.len() - 1] - e0;
    for other in &edges {
        if other.edge == edge.edge {
            continue;
        }
        let offset = other.energy_ev - e0;
        if offset < 0. {
            pre_lo = pre_lo.max(offset + 10.);
        } else {
            post_hi = post_hi.min(offset - 10.);
        }
    }
    let pre = options.pre_edge.unwrap_or([pre_lo, pre_lo * 0.2]);
    let post = options.post_edge.unwrap_or([post_hi * 0.2, post_hi]);
    if pre[1] >= 0. || post[0] <= 0. || pre[1] >= post[0] {
        return Err(invalid(
            "pre-edge must lie below E0 and post-edge above E0, without overlap",
        ));
    }
    if post[1] - post[0] < 10. {
        return Err(invalid(
            "post-edge interval must span at least 10 eV for auxiliary normalization",
        ));
    }
    let pre_indices = checked_region("pre-edge", pre, energy, e0)?;
    let post_indices = checked_region("post-edge", post, energy, e0)?;
    for other in &edges {
        if other.edge != edge.edge
            && [pre, post]
                .iter()
                .any(|r| other.energy_ev >= r[0] + e0 && other.energy_ev <= r[1] + e0)
        {
            return Err(invalid(format!(
                "neighboring {} {} edge at {} eV lies in a fit region; revise the interval",
                other.element, other.edge, other.energy_ev
            )));
        }
    }
    if edge.energy_ev >= pre[0] + e0 && edge.energy_ev <= pre[1] + e0
        || edge.energy_ev >= post[0] + e0 && edge.energy_ev <= post[1] + e0
    {
        return Err(invalid("the selected tabulated edge lies in a fit region; check E0/calibration and the excluded interval"));
    }
    let mut indices = pre_indices.clone();
    indices.extend(&post_indices);
    let weights = pre_indices
        .iter()
        .map(|_| 1. / (pre_indices.len() as f64).sqrt())
        .chain(
            post_indices
                .iter()
                .map(|_| 1. / (post_indices.len() as f64).sqrt()),
        )
        .collect::<Vec<_>>();
    let parameters = options.degree + 2 + if options.erfc.is_some() { 2 } else { 0 };
    if indices.len() <= parameters {
        return Err(invalid(
            "not enough included points for the selected background",
        ));
    }
    let reference = data.f2(&edge.element, energy).map_err(invalid)?;
    if reference.reference.table != AtomicTable::ChantlerF2LogLogV1
        || reference.reference.data != *data.identity()
    {
        return Err(invalid("provider returned an incompatible f2 reference"));
    }
    if options
        .reference
        .as_ref()
        .is_some_and(|r| r != &reference.reference)
    {
        return Err(invalid(
            "the archived atomic reference is unavailable; exact recomputation is blocked",
        ));
    }
    if reference.energy_ev != energy
        || reference.values.len() != energy.len()
        || reference.values.iter().any(|v| !v.is_finite() || *v <= 0.)
    {
        return Err(invalid("provider returned invalid reference samples"));
    }
    let emission = if let Some(term) = &options.erfc {
        if term.width_ev.iter().any(|v| !v.is_finite() || *v <= 0.)
            || term.width_ev[0] >= term.width_ev[1]
            || term.amplitude.iter().any(|v| !v.is_finite())
            || term.amplitude[0] >= term.amplitude[1]
        {
            return Err(invalid("erfc needs increasing positive finite width bounds and increasing finite amplitude bounds"));
        }
        let line = data
            .emission(&edge.element, &term.emission)
            .map_err(invalid)?;
        if line.lines.is_empty()
            || line.lines.iter().any(|l| l.initial_level != edge.edge)
            || line.energy_ev >= edge.energy_ev
        {
            return Err(invalid(
                "erfc emission must originate at the selected absorber edge and lie below it",
            ));
        }
        Some(line)
    } else {
        None
    };
    let energy_scale = pre[0].abs().max(post[1].abs());
    let t = energy
        .iter()
        .map(|e| (e - e0) / energy_scale)
        .collect::<Vec<_>>();
    let problem = Problem {
        energy,
        mu,
        f2: &reference.values,
        indices: &indices,
        weights: &weights,
        t: &t,
        degree: options.degree,
        emission: emission.as_ref().map(|l| l.energy_ev),
        erfc: options.erfc.as_ref(),
    };
    let (fit, width, evaluations, mut warnings) = problem.profile()?;
    let scale = fit.coefficients[0];
    let coefficients = fit
        .coefficients
        .rows(1, options.degree + 1)
        .iter()
        .copied()
        .collect::<Vec<_>>();
    let amplitude = if width.is_some() {
        fit.coefficients[fit.coefficients.len() - 1]
    } else {
        0.
    };
    let (rank, condition) = if let Some(width) = width {
        let (linear, _) = problem.system(Some(width));
        let mut jac = DMatrix::zeros(linear.nrows(), linear.ncols() + 1);
        jac.columns_mut(0, linear.ncols()).copy_from(&linear);
        for (row, (&i, &w)) in indices.iter().zip(&weights).enumerate() {
            let z = (energy[i] - emission.as_ref().unwrap().energy_ev) / width;
            jac[(row, linear.ncols())] =
                -amplitude * 2. * z * (-z * z).exp() / (PI.sqrt() * width) * w;
        }
        let (scaled, _) = scale_columns(&jac)?;
        let singular = scaled.svd(false, false).singular_values;
        let rank = singular
            .iter()
            .filter(|&&s| s > singular.max() * RANK_TOL)
            .count();
        if rank < parameters {
            return Err(invalid(format!(
                "erfc width/background are not identifiable: rank {rank}/{parameters}"
            )));
        }
        (rank, singular.max() / singular.min())
    } else {
        (fit.rank, fit.condition)
    };
    if condition > 1e6 {
        warnings.push(format!("Column-scaled condition number is {condition:.3e}; inspect model and range sensitivity."));
    }
    let background = t
        .iter()
        .zip(energy)
        .map(|(&t, &e)| {
            let poly = coefficients
                .iter()
                .rev()
                .fold(0., |value, &c| value * t + c);
            poly + width.map_or(0., |xi| {
                amplitude
                    * RealErrorFunctions::erfc((e - emission.as_ref().unwrap().energy_ev) / xi)
            })
        })
        .collect::<Vec<_>>();
    let matched = reference
        .values
        .iter()
        .zip(&background)
        .map(|(f, b)| f + b)
        .collect::<Vec<_>>();
    let mut auxiliary = PrePostEdge {
        e0: Some(e0),
        pre_edge_start: Some(pre[0]),
        pre_edge_end: Some(pre[1]),
        norm_start: Some(post[0]),
        norm_end: Some(post[1]),
        norm_polyorder: Some(2),
        n_victoreen: Some(0),
        ..PrePostEdge::new()
    };
    auxiliary.normalize(&energy_vec, &DVector::from_vec(matched))?;
    let pre_curve = auxiliary
        .pre_edge
        .as_ref()
        .unwrap()
        .iter()
        .copied()
        .collect::<Vec<_>>();
    let post_curve = auxiliary
        .post_edge
        .as_ref()
        .unwrap()
        .iter()
        .copied()
        .collect::<Vec<_>>();
    let ie0 = energy
        .iter()
        .enumerate()
        .min_by(|(_, a), (_, b)| (*a - e0).abs().total_cmp(&(*b - e0).abs()))
        .unwrap()
        .0;
    let delta = post_curve[ie0] - pre_curve[ie0];
    if !delta.is_finite()
        || delta <= 32. * f64::EPSILON * post_curve[ie0].abs().max(pre_curve[ie0].abs()).max(1.)
    {
        return Err(invalid(
            "auxiliary atomic edge step is nonpositive or numerically singular",
        ));
    }
    let edge_step = delta / scale;
    if !edge_step.is_finite() || edge_step <= 0. {
        return Err(invalid(
            "fitted absorption step is nonpositive or nonfinite",
        ));
    }
    let norm = mu
        .iter()
        .zip(&pre_curve)
        .map(|(mu, p)| (scale * mu - p) / delta)
        .collect::<Vec<_>>();
    let flat = norm
        .iter()
        .enumerate()
        .map(|(i, &n)| {
            if i < ie0 {
                n
            } else {
                n - (post_curve[i] - pre_curve[i]) / delta + 1.
            }
        })
        .collect::<Vec<_>>();
    let fpp = mu
        .iter()
        .zip(&background)
        .map(|(m, b)| scale * m - b)
        .collect::<Vec<_>>();
    let residual = reference
        .values
        .iter()
        .zip(&fpp)
        .map(|(f, m)| f - m)
        .collect::<Vec<_>>();
    if background
        .iter()
        .chain(&norm)
        .chain(&flat)
        .chain(&fpp)
        .chain(&residual)
        .any(|v| !v.is_finite())
    {
        return Err(invalid("normalization produced nonfinite values"));
    }
    Ok(MbackResult {
        method: "mback_chantler_v1".into(),
        requested: options.clone(),
        requested_e0: model.e0,
        reference: reference.reference,
        tabulated_edge_ev: edge.energy_ev,
        e0,
        pre_edge: pre,
        post_edge: post,
        energy: energy.to_vec(),
        fit_indices: indices,
        weights,
        scale,
        energy_scale,
        coefficients,
        emission,
        erfc_width: width,
        erfc_amplitude: amplitude,
        background,
        f2: reference.values,
        fpp,
        residual,
        objective: fit.objective,
        rank,
        condition,
        evaluations,
        edge_step,
        pre_curve,
        post_curve,
        auxiliary_method: "rexafs_prepost_linear_quadratic_v1".into(),
        norm,
        flat,
        warnings,
    })
}