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
use transformer::*;
pub use transformer::{Transformer, TransformerPropsTrait, TransformerTrait};

use paste::paste;

pub mod bazin_fit;
pub mod clipped_lg;
pub mod composed;
pub mod linexp_fit;
pub mod transformer;
pub mod villar_fit;

macro_rules! transformer {
    ($module:ident, $structure:ident, $func:expr, $names:expr, $descriptions:expr, $doc:literal $(,)?) => {
        pub mod $module {
            use super::*;

            #[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
            pub struct $structure {}

            impl $structure {
                pub fn new() -> Self {
                    Self {}
                }

                pub const fn doc() -> &'static str {
                    $doc
                }
            }

            impl Default for $structure {
                fn default() -> Self {
                    Self::new()
                }
            }

            impl TransformerPropsTrait for $structure {
                #[inline]
                fn is_size_valid(&self, _size: usize) -> bool {
                    true
                }

                #[inline]
                fn size_hint(&self, size: usize) -> usize {
                    size
                }

                fn names(&self, names: &[&str]) -> Vec<String> {
                    let func = $names;
                    func(names)
                }

                fn descriptions(&self, desc: &[&str]) -> Vec<String> {
                    let func = $descriptions;
                    func(desc)
                }
            }

            impl<T: Float> TransformerTrait<T> for $structure {
                #[inline]
                fn transform(&self, v: Vec<T>) -> Vec<T> {
                    let func = $func;
                    func(v)
                }
            }

            #[cfg(test)]
            mod tests {
                use super::*;

                check_transformer!($structure);
            }
        }
    };
}

// into_iter().map().collect() should create no allocations
macro_rules! transformer_from_per_element_fn {
    ($module:ident, $func:expr, $prefix_name:literal, $prefix_description:literal, $doc:literal $(,)?) => {
        paste! {
            transformer!(
                $module,
                [<$module:camel Transformer>],
                |v: Vec<T>| v.into_iter().map($func).collect::<Vec<T>>(),
                |names: &[&str]| {
                    names
                        .iter()
                        .map(|name| format!("{}_{}", $prefix_name, name))
                        .collect::<Vec<_>>()
                },
                |desc: &[&str]| {
                    desc.iter()
                        .map(|name| format!("{} of {}", $prefix_description, name))
                        .collect::<Vec<_>>()
                },
                $doc
            );
        }
    };
}

transformer!(
    identity,
    IdentityTransformer,
    |x| x,
    |names: &[&str]| names.iter().map(|x| x.to_string()).collect(),
    |desc: &[&str]| desc.iter().map(|x| x.to_string()).collect(),
    "Identity feature transformer",
);

transformer_from_per_element_fn!(
    arcsinh,
    T::asinh,
    "arcsinh",
    "hyperbolic arcsine",
    "Hyperbolic arcsine feature transformer",
);
transformer_from_per_element_fn!(
    ln1p,
    T::ln_1p,
    "ln1p",
    "natural logarithm of unity plus",
    "ln(1+x) feature transformer",
);
transformer_from_per_element_fn!(
    lg,
    T::log10,
    "lg",
    "decimal logarithm",
    "Decimal logarithm feature transformer"
);
transformer_from_per_element_fn!(
    sqrt,
    T::sqrt,
    "sqrt",
    "square root",
    "Square root feature transformer"
);