Skip to main content

fast_nnt/weights/
mod.rs

1pub mod active_set_weights;
2pub mod band;
3pub mod cg_weights;
4pub mod cgnr;
5pub mod nnls_ops;
6
7pub use cg_weights::CGSolveStats;
8
9use clap::ValueEnum;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum, Default)]
12#[clap(rename_all = "kebab-case")]
13pub enum InferenceMethod {
14    #[default]
15    #[clap(alias = "activeset", alias = "active_set")]
16    ActiveSet,
17    #[clap(
18        alias = "splitstree4",
19        alias = "splits-tree4",
20        alias = "splits_tree4",
21        alias = "conjugate-gradient",
22        alias = "conjugate_gradient"
23    )]
24    CG,
25}
26
27impl InferenceMethod {
28    pub fn as_str(&self) -> &'static str {
29        match self {
30            InferenceMethod::ActiveSet => "active-set",
31            InferenceMethod::CG => "cg",
32        }
33    }
34
35    pub fn from_option(opt: Option<&str>) -> anyhow::Result<Self> {
36        opt.map_or(Ok(InferenceMethod::default()), str::parse)
37    }
38}
39
40impl std::str::FromStr for InferenceMethod {
41    type Err = anyhow::Error;
42
43    fn from_str(s: &str) -> anyhow::Result<Self> {
44        let normalized: String = s.to_lowercase().replace(['-', '_'], "");
45        match normalized.as_str() {
46            "activeset" => Ok(InferenceMethod::ActiveSet),
47            "cg" | "conjugategradient" | "splitstree4" => Ok(InferenceMethod::CG),
48            _ => anyhow::bail!(
49                "unknown inference method '{}' (expected 'active-set' or 'cg')",
50                s
51            ),
52        }
53    }
54}