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
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;
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)]
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>,
}

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

    /// Should return the 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()],
        }
    }

    /// Get feature evaluator meta-information
    fn get_info(&self) -> &EvaluatorInfo;

    /// Should return the vector of feature names. The length and feature order should
    /// correspond to `eval()` output
    fn get_names(&self) -> Vec<&str>;

    /// Shoud return the vector of feature descriptions. The length and feature order should
    /// correspond to `eval()` output
    fn get_descriptions(&self) -> Vec<&str>;

    /// Should return the size of vectors returned by `eval()` and `get_names()`
    fn size_hint(&self) -> usize {
        self.get_info().size
    }

    /// Should return minimum time series length to successfully find feature value
    fn min_ts_length(&self) -> usize {
        self.get_info().min_ts_length
    }

    fn is_t_required(&self) -> bool {
        self.get_info().t_required
    }

    fn is_m_required(&self) -> bool {
        self.get_info().m_required
    }

    fn is_w_required(&self) -> bool {
        self.get_info().w_required
    }

    fn is_sorting_required(&self) -> bool {
        self.get_info().sorting_required
    }

    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)
    }
}