rexafs 0.2.11

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
575
576
577
578
579
580
581
582
583
584
585
use std::collections::BTreeMap;

use nalgebra::DVector;

use super::errors::FittingError;
use super::solver;
use super::types::{
    FeffFitDataset, FeffFitJacobianMode, FeffFitOptions, FeffFitResult, FeffFitSolverMethod,
    FeffFlavor, FeffPathModel, FitSpace, FitVariable, FitVariables, FitWarning, Param,
    PathParamSpec,
};
use super::variables::try_extract_symbols;
use crate::xafs::xafsutils::FTWindow;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParamRole {
    S02,
    Degen,
    Sigma2,
    E0,
    Ei,
    Deltar,
    Third,
    Fourth,
    UntypedExpr,
}

impl ParamRole {
    fn default_value(self) -> f64 {
        match self {
            Self::S02 | Self::Degen => 1.0,
            Self::Sigma2 => 0.003,
            Self::E0 | Self::Ei | Self::Deltar | Self::Third | Self::Fourth | Self::UntypedExpr => {
                0.0
            }
        }
    }

    fn as_str(self) -> &'static str {
        match self {
            Self::S02 => "s02",
            Self::Degen => "degen",
            Self::Sigma2 => "sigma2",
            Self::E0 => "e0",
            Self::Ei => "ei",
            Self::Deltar => "deltar",
            Self::Third => "third",
            Self::Fourth => "fourth",
            Self::UntypedExpr => "expr",
        }
    }
}

fn path_param_expr(spec: &PathParamSpec) -> Option<&str> {
    match spec {
        PathParamSpec::Expression(expr) => Some(expr.as_str()),
        PathParamSpec::Value(_) => None,
    }
}

/// Builder for one spectrum or a joint fit of several prepared χ(k) datasets.
///
/// Start with data(), add_path(), physically supported krange()/rrange(), and
/// explicit Param values. Configuration methods consume and return the builder;
/// fit() borrows it and fits copies, so it can be reused. Default settings are
/// documented by FeffFitDataset and FeffFitOptions. Unspecified noise means
/// a residual divisor of 1.0 rather than automatic estimation.
#[derive(Debug, Clone)]
pub struct FeffFit {
    datasets: Vec<FeffFitDataset>,
    variables: FitVariables,
    flavor: FeffFlavor,
    default_dataset: FeffFitDataset,
    has_default: bool,
    options: FeffFitOptions,
}

impl Default for FeffFit {
    fn default() -> Self {
        Self {
            datasets: Vec::new(),
            variables: FitVariables::new(),
            flavor: FeffFlavor::Feff85L,
            default_dataset: FeffFitDataset::default(),
            has_default: false,
            options: FeffFitOptions::default(),
        }
    }
}

impl FeffFit {
    /// Create a value with the defaults documented on this type.
    pub fn new() -> Self {
        Self::default()
    }

    /// Copy the prepared k grid (Å⁻¹) and dimensionless χ(k) arrays into the dataset.
    pub fn data(mut self, k: &DVector<f64>, chi: &DVector<f64>) -> Self {
        self.default_dataset = self.default_dataset.data(k, chi);
        self.has_default = true;
        self
    }

    /// Set the scalar k-space residual divisor; absent settings otherwise use 1.0.
    pub fn epsilon_k(mut self, value: f64) -> Self {
        self.default_dataset = self.default_dataset.epsilon_k(value);
        self.has_default = true;
        self
    }

    /// Append an owned theoretical path to the dataset.
    pub fn add_path(mut self, path: FeffPathModel) -> Self {
        self.default_dataset = self.default_dataset.add_path(path);
        self.has_default = true;
        self
    }

    /// Set the fit interval in k (Å⁻¹); ensure it is supported by measured data.
    pub fn krange(mut self, kmin: f64, kmax: f64) -> Self {
        self.default_dataset = self.default_dataset.krange(kmin, kmax);
        self.has_default = true;
        self
    }

    /// Set the fit interval in R (Å); these Fourier positions are not phase-corrected distances.
    pub fn rrange(mut self, rmin: f64, rmax: f64) -> Self {
        self.default_dataset = self.default_dataset.rrange(rmin, rmax);
        self.has_default = true;
        self
    }

    /// Set the nonnegative real exponent w in k^w; used when kweights is empty.
    pub fn kweight(mut self, value: f64) -> Self {
        self.default_dataset = self.default_dataset.kweight(value);
        self.has_default = true;
        self
    }

    /// Set the low-k window parameter; Kaiser–Bessel also uses it to control shape.
    pub fn dk(mut self, value: f64) -> Self {
        self.default_dataset = self.default_dataset.dk(value);
        self.has_default = true;
        self
    }

    /// Set the k-window family; K-space residuals do not apply this window.
    pub fn window(mut self, value: FTWindow) -> Self {
        self.default_dataset = self.default_dataset.window(value);
        self.has_default = true;
        self
    }

    /// Set the R-window family for R- and Q-space fits.
    pub fn rwindow(mut self, value: FTWindow) -> Self {
        self.default_dataset = self.default_dataset.rwindow(value);
        self.has_default = true;
        self
    }

    /// Set the low-R window parameter (Å for Hanning); default zero gives a hard edge.
    pub fn dr(mut self, value: f64) -> Self {
        self.default_dataset = self.default_dataset.dr(value);
        self.has_default = true;
        self
    }

    /// Fit several k-weights simultaneously (Larch `kweight=(1,2,3)`); the residual
    /// concatenates one transformed block per k-weight.
    pub fn kweights(mut self, values: &[f64]) -> Self {
        self.default_dataset = self.default_dataset.kweights(values);
        self.has_default = true;
        self
    }

    /// Select the fit space (`FitSpace::R` by default, or `K` / `Q`).
    pub fn fitspace(mut self, value: FitSpace) -> Self {
        self.default_dataset = self.default_dataset.fitspace(value);
        self.has_default = true;
        self
    }

    /// Per-k-weight noise estimates aligned with `kweights` (Larch multi-kweight `epsilon_k`).
    pub fn epsilon_ks(mut self, values: &[f64]) -> Self {
        self.default_dataset = self.default_dataset.epsilon_ks(values);
        self.has_default = true;
        self
    }

    /// Append an owned dataset for a joint fit; equal variable names are shared.
    pub fn add_dataset(mut self, dataset: FeffFitDataset) -> Self {
        self.datasets.push(dataset);
        self
    }

    /// Set or create a varying parameter and initial value; removes an expression constraint.
    pub fn set_init(mut self, name: impl Into<String>, value: f64) -> Self {
        let name = name.into();
        let entry = self
            .variables
            .vars
            .entry(name)
            .or_insert_with(|| FitVariable::new(value, true));
        entry.value = value;
        entry.init_value = value;
        entry.vary = true;
        if entry.expr.is_some() {
            entry.expr = None;
        }
        self
    }

    /// Apply set_init to each name/value pair, replacing any expression constraints.
    pub fn set_inits<I, S>(mut self, inits: I) -> Self
    where
        I: IntoIterator<Item = (S, f64)>,
        S: Into<String>,
    {
        for (name, value) in inits {
            self = self.set_init(name, value);
        }
        self
    }

    /// Set inclusive parameter bounds; creates a varying value initialized to zero if absent.
    pub fn set_bounds(mut self, name: impl Into<String>, min: f64, max: f64) -> Self {
        let name = name.into();
        let entry = self
            .variables
            .vars
            .entry(name)
            .or_insert_with(|| FitVariable::new(0.0, true));
        entry.min = Some(min);
        entry.max = Some(max);
        self
    }

    /// Set a constant parameter value, disable fitting, and remove any expression constraint.
    pub fn fix(mut self, name: impl Into<String>, value: f64) -> Self {
        let name = name.into();
        let entry = self
            .variables
            .vars
            .entry(name)
            .or_insert_with(|| FitVariable::new(value, false));
        entry.value = value;
        entry.init_value = value;
        entry.vary = false;
        entry.expr = None;
        self
    }

    /// Define a parameter expression and disable independent variation; units are not checked.
    pub fn var_expr(mut self, name: impl Into<String>, expr: impl Into<String>) -> Self {
        let name = name.into();
        let expr = expr.into();
        let entry = self
            .variables
            .vars
            .entry(name)
            .or_insert_with(|| FitVariable::new(0.0, false));
        entry.expr = Some(expr);
        entry.vary = false;
        self
    }

    /// Insert or replace the supplied parameter specifications by name.
    pub fn params<I>(mut self, params: I) -> Self
    where
        I: IntoIterator<Item = Param>,
    {
        for param in params {
            self.variables
                .vars
                .insert(param.name.clone(), param.to_fit_variable());
        }
        self
    }

    /// Store a parser-flavor preference. This currently does not change already-loaded paths or fit().
    pub fn set_flavor(mut self, flavor: FeffFlavor) -> Self {
        self.flavor = flavor;
        self
    }

    /// Select the numerical solver; the trust-region option requires its Cargo feature.
    pub fn solver_method(mut self, solver_method: FeffFitSolverMethod) -> Self {
        self.options.solver_method = solver_method;
        self
    }

    /// Select Jacobian assembly for supported solvers; does not change the objective.
    pub fn jacobian_mode(mut self, jacobian_mode: FeffFitJacobianMode) -> Self {
        self.options.jacobian_mode = jacobian_mode;
        self
    }

    /// Replace the numerical solver settings used by this fit.
    pub fn fit_options(mut self, options: FeffFitOptions) -> Self {
        self.options = options;
        self
    }

    fn collect_default_dataset(&self, datasets: &mut Vec<FeffFitDataset>) {
        let has_data = !self.default_dataset.k.is_empty() || !self.default_dataset.chi.is_empty();
        let has_paths = !self.default_dataset.paths.is_empty();
        if self.has_default && (has_data || has_paths) {
            datasets.push(self.default_dataset.clone());
        }
    }

    fn maybe_infer_symbols_for_expr(
        expr: &str,
        role: ParamRole,
        inferred: &mut BTreeMap<String, (ParamRole, f64)>,
        warnings: &mut Vec<FitWarning>,
    ) -> Result<(), FittingError> {
        for symbol in try_extract_symbols(expr)? {
            let default_value = role.default_value();
            if let Some((existing_role, existing_default)) = inferred.get(&symbol) {
                if (*existing_default - default_value).abs() > f64::EPSILON {
                    warnings.push(FitWarning {
                        symbol: symbol.clone(),
                        inferred_from: existing_role.as_str().to_string(),
                        default_value: *existing_default,
                        message: format!(
                            "conflicting inferred defaults: '{}'={} vs '{}'={}, kept first",
                            existing_role.as_str(),
                            existing_default,
                            role.as_str(),
                            default_value
                        ),
                    });
                }
                continue;
            }
            inferred.insert(symbol, (role, default_value));
        }
        Ok(())
    }

    fn auto_discover_variables(
        &self,
        datasets: &[FeffFitDataset],
        vars: &mut FitVariables,
        warnings: &mut Vec<FitWarning>,
    ) -> Result<(), FittingError> {
        let mut inferred = BTreeMap::new();

        for dataset in datasets {
            for path in &dataset.paths {
                let path_roles = [
                    (path_param_expr(&path.s02), ParamRole::S02),
                    (path_param_expr(&path.degen), ParamRole::Degen),
                    (path_param_expr(&path.sigma2), ParamRole::Sigma2),
                    (path_param_expr(&path.e0), ParamRole::E0),
                    (path_param_expr(&path.ei), ParamRole::Ei),
                    (path_param_expr(&path.deltar), ParamRole::Deltar),
                    (path_param_expr(&path.third), ParamRole::Third),
                    (path_param_expr(&path.fourth), ParamRole::Fourth),
                ];

                for (expr, role) in path_roles {
                    if let Some(expr) = expr {
                        Self::maybe_infer_symbols_for_expr(expr, role, &mut inferred, warnings)?;
                    }
                }
            }
        }

        for (symbol, (role, default_value)) in inferred.iter() {
            if vars.vars.contains_key(symbol) {
                continue;
            }
            vars.insert(symbol.clone(), FitVariable::new(*default_value, true));
            warnings.push(FitWarning {
                symbol: symbol.clone(),
                inferred_from: role.as_str().to_string(),
                default_value: *default_value,
                message: "auto-created symbol with typed default".to_string(),
            });
        }

        let var_exprs = vars
            .vars
            .iter()
            .filter_map(|(name, var)| var.expr.as_ref().map(|expr| (name.clone(), expr.clone())))
            .collect::<Vec<_>>();
        for (_name, expr) in var_exprs {
            for symbol in try_extract_symbols(&expr)? {
                if vars.vars.contains_key(&symbol) {
                    continue;
                }
                let default_value = ParamRole::UntypedExpr.default_value();
                vars.insert(symbol.clone(), FitVariable::new(default_value, true));
                warnings.push(FitWarning {
                    symbol,
                    inferred_from: ParamRole::UntypedExpr.as_str().to_string(),
                    default_value,
                    message: "auto-created symbol with untyped expression default".to_string(),
                });
            }
        }

        Ok(())
    }

    /// Fit owned copies of the datasets and variables without modifying the builder.
    ///
    /// Missing expression symbols are created as varying parameters with typed
    /// starting values: amplitude/degeneracy 1, σ² 0.003 Ų, and other roles 0.
    /// Inspect returned warnings; explicitly named parameters avoid inferred starts.
    /// Errors report invalid inputs/expressions/paths or unavailable solvers. An Ok
    /// result can still report nonconvergence in solver_report.
    pub fn fit(&self) -> Result<FeffFitResult, FittingError> {
        let _ = self.flavor;
        let mut datasets = self.datasets.clone();
        self.collect_default_dataset(&mut datasets);
        if datasets.is_empty() {
            return Err(FittingError::InvalidDataset {
                reason: "fit requires at least one dataset".to_string(),
            });
        }

        let mut vars = self.variables.clone();
        let mut warnings = Vec::new();
        self.auto_discover_variables(&datasets, &mut vars, &mut warnings)?;

        let mut result = solver::feffit_joint_with_options(&datasets, &vars, &self.options)?;
        result.warnings.extend(warnings);
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::xafs::fitting::ff2chi;
    use crate::xafs::fitting::path_model::feffpath;
    use crate::xafs::fitting::types::{FeffFlavor, PathParamSpec};
    use crate::xafs::tests::TOP_DIR;

    #[test]
    fn test_auto_discovery_uses_typed_defaults() {
        let path = FeffPathModel::default()
            .set_s02("amp")
            .set_sigma2("sig2")
            .set_e0("de0")
            .set_deltar("dr");
        let dataset = FeffFitDataset::new()
            .data(&DVector::zeros(3), &DVector::zeros(3))
            .add_path(path);
        let fit = FeffFit::new().add_dataset(dataset);

        let mut vars = FitVariables::new();
        let mut warnings = Vec::new();
        fit.auto_discover_variables(&fit.datasets, &mut vars, &mut warnings)
            .unwrap();

        assert_eq!(vars.get("amp").unwrap().value, 1.0);
        assert_eq!(vars.get("sig2").unwrap().value, 0.003);
        assert_eq!(vars.get("de0").unwrap().value, 0.0);
        assert_eq!(vars.get("dr").unwrap().value, 0.0);
        assert!(!warnings.is_empty());
    }

    #[test]
    fn test_auto_discovery_warns_on_conflicting_defaults() {
        let path = FeffPathModel::default()
            .set_s02("shared")
            .set_sigma2("shared");
        let dataset = FeffFitDataset::new()
            .data(&DVector::zeros(3), &DVector::zeros(3))
            .add_path(path);
        let fit = FeffFit::new().add_dataset(dataset);

        let mut vars = FitVariables::new();
        let mut warnings = Vec::new();
        fit.auto_discover_variables(&fit.datasets, &mut vars, &mut warnings)
            .unwrap();

        assert_eq!(vars.get("shared").unwrap().value, 1.0);
        assert!(warnings.iter().any(|w| w.message.contains("conflicting")));
    }

    #[test]
    fn test_auto_discovery_untyped_expr_defaults_to_zero() {
        let fit = FeffFit::new().var_expr("scale", "missing * 2");
        let mut vars = fit.variables.clone();
        let mut warnings = Vec::new();
        fit.auto_discover_variables(&[], &mut vars, &mut warnings)
            .unwrap();

        assert_eq!(vars.get("missing").unwrap().value, 0.0);
        assert!(warnings
            .iter()
            .any(|w| w.inferred_from == ParamRole::UntypedExpr.as_str()));
    }

    #[test]
    fn test_builder_single_dataset_fit() {
        let pathfile = format!("{TOP_DIR}/tests/testfiles/feffcu01.dat");
        let path = feffpath(pathfile, FeffFlavor::Feff85L)
            .unwrap()
            .set_s02("amp")
            .set_e0("de0")
            .set_sigma2("sig2")
            .set_deltar("dr");

        let k = DVector::from_iterator(260, (0..260).map(|i| 0.05 * (i as f64 + 1.0)));
        let mut truth = FitVariables::new();
        truth.insert("amp", FitVariable::new(0.9, false));
        truth.insert("de0", FitVariable::new(1.1, false));
        truth.insert("sig2", FitVariable::new(0.0030, false));
        truth.insert("dr", FitVariable::new(0.01, false));

        let synthetic = ff2chi(std::slice::from_ref(&path), &truth, &k).unwrap();
        let result = FeffFit::new()
            .data(&k, &synthetic.chi)
            .add_path(path)
            .set_inits([("amp", 0.95), ("de0", 0.0), ("sig2", 0.002), ("dr", 0.0)])
            .set_bounds("sig2", 0.0, 0.02)
            .krange(2.0, 14.0)
            .rrange(1.0, 3.0)
            .fit()
            .unwrap();

        assert!(result.chi_square.is_finite());
        assert_eq!(result.datasets.len(), 1);
        assert!(!result.path_contributions.is_empty());
    }

    #[test]
    fn test_builder_multi_dataset_fit() {
        let pathfile = format!("{TOP_DIR}/tests/testfiles/feffcu01.dat");
        let mut path1 = feffpath(pathfile.clone(), FeffFlavor::Feff85L).unwrap();
        path1.s02 = PathParamSpec::Expression("amp".to_string());
        path1.e0 = PathParamSpec::Expression("de0".to_string());
        path1.sigma2 = PathParamSpec::Expression("sig2".to_string());
        path1.deltar = PathParamSpec::Expression("dr".to_string());
        let path2 = path1.clone().set_sigma2("sig2_2");

        let k1 = DVector::from_iterator(220, (0..220).map(|i| 0.05 * (i as f64 + 1.0)));
        let k2 = DVector::from_iterator(240, (0..240).map(|i| 0.05 * (i as f64 + 1.0)));

        let mut truth = FitVariables::new();
        truth.insert("amp", FitVariable::new(0.9, false));
        truth.insert("de0", FitVariable::new(1.2, false));
        truth.insert("sig2", FitVariable::new(0.003, false));
        truth.insert("sig2_2", FitVariable::new(0.004, false));
        truth.insert("dr", FitVariable::new(0.01, false));

        let chi1 = ff2chi(std::slice::from_ref(&path1), &truth, &k1)
            .unwrap()
            .chi;
        let chi2 = ff2chi(std::slice::from_ref(&path2), &truth, &k2)
            .unwrap()
            .chi;
        let ds1 = FeffFitDataset::new()
            .data(&k1, &chi1)
            .add_path(path1)
            .krange(2.0, 14.0)
            .rrange(1.0, 3.0);
        let ds2 = FeffFitDataset::new()
            .data(&k2, &chi2)
            .add_path(path2)
            .krange(2.0, 14.0)
            .rrange(1.0, 3.0);

        let result = FeffFit::new()
            .add_dataset(ds1)
            .add_dataset(ds2)
            .set_inits([
                ("amp", 0.95),
                ("de0", 0.0),
                ("sig2", 0.002),
                ("sig2_2", 0.002),
                ("dr", 0.0),
            ])
            .fit()
            .unwrap();

        assert!(result.chi_square.is_finite());
        assert_eq!(result.datasets.len(), 2);
    }
}