Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

roms feature added #160

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ where
Periodogram(Periodogram<T, Self>),
_PeriodogramPeaks,
ReducedChi2,
Roms,
Skew,
StandardDeviation,
StetsonK,
Expand Down
3 changes: 3 additions & 0 deletions src/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub use periodogram::PeriodogramPeaks as _PeriodogramPeaks;
mod reduced_chi2;
pub use reduced_chi2::ReducedChi2;

mod roms;
pub use roms::Roms;

mod skew;
pub use skew::Skew;

Expand Down
107 changes: 107 additions & 0 deletions src/features/roms.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use crate::evaluator::*;

macro_const! {
const DOC: &str = r"
Robust median statistic

$$
\frac1{N-1} \sum_{i=0}^{N-1} \frac{|m_i - \mathrm{median}(m_i)|}{\sigma_i}
$$
For non-variable data, it should be less than one.

- Depends on: **magnitude**, **errors**
- Minimum number of observations: **2**
- Number of features: **1**

Enoch, Brown, Burgasser 2003. [DOI:10.1086/376598](https://www.doi.org/10.1086/376598)
";
}

#[doc = DOC!()]
#[derive(Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]

pub struct Roms {}

impl Roms {
pub fn new() -> Self {
Self {}
}

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

Check warning on line 32 in src/features/roms.rs

View check run for this annotation

Codecov / codecov/patch

src/features/roms.rs#L30-L32

Added lines #L30 - L32 were not covered by tests
}

lazy_info!(
ROMS_INFO,
Roms,
size: 1,
min_ts_length: 2,
t_required: false,
m_required: true,
w_required: true,
sorting_required: false,
);

impl FeatureNamesDescriptionsTrait for Roms {
fn get_names(&self) -> Vec<&str> {
vec!["roms"]
}

fn get_descriptions(&self) -> Vec<&str> {
vec!["robust median statistic"]
}
}

impl<T> FeatureEvaluator<T> for Roms
where
T: Float,
{
fn eval(&self, ts: &mut TimeSeries<T>) -> Result<Vec<T>, EvaluatorError> {
self.check_ts_length(ts)?;
let n = ts.lenf();
let m_median = ts.m.get_median();
let tmp_sum =
ts.m.as_slice()
.iter()
.zip(ts.w.as_slice().iter())
.map(|(&m, &w)| ((m - m_median).abs() * w.sqrt()))
.filter(|&x| x.is_finite())
.sum::<T>();
let value = tmp_sum / (n - T::one());
Ok(vec![value])
}
}

#[cfg(test)]
#[allow(clippy::unreadable_literal)]
#[allow(clippy::excessive_precision)]

mod tests {
use super::*;
use crate::extractor::FeatureExtractor;
use crate::tests::*;

check_feature!(Roms);

feature_test!(
roms,
[Roms::new()],
[2.6035533],
[1.0_f32, 2.0, 3.0, 4.0, 5.0],
[1.0_f32, 1.0, 2.0, 3.0, 5.0],
[1.0_f32, 4.0, 1.0, 2.0, 4.0],
);

#[test]
fn roms_const_data() {
let eval = Roms::default();
let x = linspace(0.0_f32, 10.0, 10);
let y = vec![1.0_f32, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
let z = vec![1.0_f32, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
let mut ts = TimeSeries::new(&x, &y, &z);
let actual = eval.eval(&mut ts).unwrap();
let desired = [0.0_f32];
all_close(&actual, &desired, 1e-10);
}
}
Loading