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
pub use crate::error::EvaluatorError;
pub use crate::float_trait::Float;
pub use crate::time_series::TimeSeries;

use enum_dispatch::enum_dispatch;
pub use lazy_static::lazy_static;
pub use macro_const::macro_const;
use ndarray::Array1;
pub use schemars::JsonSchema;
use serde::de::DeserializeOwned;
pub use serde::{Deserialize, Serialize};
pub use std::fmt::Debug;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EvaluatorInfo {
    pub size: usize,
    pub min_ts_length: usize,
    pub t_required: bool,
    pub m_required: bool,
    pub w_required: bool,
    pub sorting_required: bool,
}

#[derive(Clone, Debug)]
pub struct EvaluatorProperties {
    pub info: EvaluatorInfo,
    pub names: Vec<String>,
    pub descriptions: Vec<String>,
}

// pub trait EvaluatorPropertiesTrait {
//     fn get_properties(&self) -> &EvaluatorProperties;
// }

#[enum_dispatch]
pub trait EvaluatorInfoTrait {
    /// Get feature evaluator meta-information
    fn get_info(&self) -> &EvaluatorInfo;

    /// Size of vectors returned by [eval()](FeatureEvaluator::eval),
    /// [get_names()](FeatureEvaluator::get_names) and
    /// [get_descriptions()](FeatureEvaluator::get_descriptions)
    fn size_hint(&self) -> usize {
        self.get_info().size
    }

    /// Minimum time series length required to successfully evaluate feature
    fn min_ts_length(&self) -> usize {
        self.get_info().min_ts_length
    }

    /// If time array used by the feature
    fn is_t_required(&self) -> bool {
        self.get_info().t_required
    }

    /// If magnitude array is used by the feature
    fn is_m_required(&self) -> bool {
        self.get_info().m_required
    }

    /// If weight array is used by the feature
    fn is_w_required(&self) -> bool {
        self.get_info().w_required
    }

    /// If feature requires time-sorting on the input [TimeSeries]
    fn is_sorting_required(&self) -> bool {
        self.get_info().sorting_required
    }
}

// impl<P> EvaluatorInfoTrait for P
// where
//     P: EvaluatorPropertiesTrait,
// {
//     fn get_info(&self) -> &EvaluatorInfo {
//         &self.get_properties().info
//     }
// }

#[enum_dispatch]
pub trait FeatureNamesDescriptionsTrait {
    /// Vector of feature names. The length and feature order corresponds to
    /// [eval()](FeatureEvaluator::eval) output
    fn get_names(&self) -> Vec<&str>;

    /// Vector of feature descriptions. The length and feature order corresponds to
    /// [eval()](FeatureEvaluator::eval) output
    fn get_descriptions(&self) -> Vec<&str>;
}

// impl<P> FeatureNamesDescriptionsTrait for P
// where
//     P: EvaluatorPropertiesTrait,
// {
//     fn get_names(&self) -> Vec<&str> {
//         self.get_properties()
//             .names
//             .iter()
//             .map(|name| name.as_str())
//             .collect()
//     }
//
//     fn get_descriptions(&self) -> Vec<&str> {
//         self.get_properties()
//             .descriptions
//             .iter()
//             .map(|descr| descr.as_str())
//             .collect()
//     }
// }

/// The trait each feature should implement
#[enum_dispatch]
pub trait FeatureEvaluator<T: Float>:
    FeatureNamesDescriptionsTrait
    + EvaluatorInfoTrait
    + Send
    + Clone
    + Debug
    + Serialize
    + DeserializeOwned
    + JsonSchema
{
    /// Vector of feature values or `EvaluatorError`
    fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError>;

    /// Returns vector of feature values and fill invalid components with given value
    fn eval_or_fill(&self, ts: &mut TimeSeries<T>, fill_value: T) -> Vec<T> {
        match self.eval(ts) {
            Ok(v) => v,
            Err(_) => vec![fill_value; self.size_hint()],
        }
    }

    /// Checks if [TimeSeries] has enough points to evaluate the feature
    fn check_ts_length(&self, ts: &TimeSeries<T>) -> Result<usize, EvaluatorError> {
        let length = ts.lenu();
        if length < self.min_ts_length() {
            Err(EvaluatorError::ShortTimeSeries {
                actual: length,
                minimum: self.min_ts_length(),
            })
        } else {
            Ok(length)
        }
    }
}

pub fn get_nonzero_m_std<T: Float>(ts: &mut TimeSeries<T>) -> Result<T, EvaluatorError> {
    let std = ts.m.get_std();
    if std.is_zero() || ts.is_plateau() {
        Err(EvaluatorError::FlatTimeSeries)
    } else {
        Ok(std)
    }
}

pub fn get_nonzero_m_std2<T: Float>(ts: &mut TimeSeries<T>) -> Result<T, EvaluatorError> {
    let std2 = ts.m.get_std2();
    if std2.is_zero() || ts.is_plateau() {
        Err(EvaluatorError::FlatTimeSeries)
    } else {
        Ok(std2)
    }
}

pub fn get_nonzero_reduced_chi2<T: Float>(ts: &mut TimeSeries<T>) -> Result<T, EvaluatorError> {
    let reduced_chi2 = ts.get_m_reduced_chi2();
    if reduced_chi2.is_zero() || ts.is_plateau() {
        Err(EvaluatorError::FlatTimeSeries)
    } else {
        Ok(reduced_chi2)
    }
}

pub trait OwnedArrays<T>
where
    T: Float,
{
    fn ts(self) -> TimeSeries<'static, T>;
}

pub struct TmArrays<T> {
    pub t: Array1<T>,
    pub m: Array1<T>,
}

impl<T> OwnedArrays<T> for TmArrays<T>
where
    T: Float,
{
    fn ts(self) -> TimeSeries<'static, T> {
        TimeSeries::new_without_weight(self.t, self.m)
    }
}

pub struct TmwArrays<T> {
    pub t: Array1<T>,
    pub m: Array1<T>,
    pub w: Array1<T>,
}

impl<T> OwnedArrays<T> for TmwArrays<T>
where
    T: Float,
{
    fn ts(self) -> TimeSeries<'static, T> {
        TimeSeries::new(self.t, self.m, self.w)
    }
}