light_curve_feature/features/
duration.rs1use crate::evaluator::*;
2
3macro_const! {
4 const DOC: &str = r#"
5Time-series duration
6
7$$
8t_{N-1} - t_0.
9$$
10
11Note: cadence-dependent feature.
12
13- Depends on: **time**
14- Minimum number of observations: **1**
15- Number of features: **1**
16"#;
17}
18
19#[doc = DOC!()]
20#[derive(Clone, Default, Debug, Deserialize, Serialize, JsonSchema)]
21pub struct Duration {}
22
23impl Duration {
24 pub fn new() -> Self {
25 Self {}
26 }
27
28 pub const fn doc() -> &'static str {
29 DOC
30 }
31}
32
33lazy_info!(
34 DURATION_INFO,
35 Duration,
36 size: 1,
37 min_ts_length: 1,
38 t_required: true,
39 m_required: false,
40 w_required: false,
41 sorting_required: true,
42);
43
44impl FeatureNamesDescriptionsTrait for Duration {
45 fn get_names(&self) -> Vec<&str> {
46 vec!["duration"]
47 }
48
49 fn get_descriptions(&self) -> Vec<&str> {
50 vec!["time-series duration"]
51 }
52}
53
54impl<T> FeatureEvaluator<T> for Duration
55where
56 T: Float,
57{
58 fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
59 self.check_ts_length(ts)?;
60 Ok(vec![ts.t.sample[ts.lenu() - 1] - ts.t.sample[0]])
61 }
62}
63
64#[cfg(test)]
65#[allow(clippy::unreadable_literal)]
66#[allow(clippy::excessive_precision)]
67mod tests {
68 use super::*;
69 use crate::tests::*;
70
71 check_feature!(Duration);
72
73 feature_test!(
74 duration,
75 [Duration::new()],
76 [4.0],
77 [0.0_f32, 1.0, 2.0, 3.0, 4.0],
78 );
79}