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

feat: Add trait for anomaly detectors #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions src/anomaly/half_space_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};

use crate::common::{ClassifierOutput, ClassifierTarget, Observation};

use super::AnomalyDetector;

// Return the index of a node's left child node.
#[inline]
fn left_child(node: u32) -> u32 {
Expand Down Expand Up @@ -227,19 +229,24 @@ impl<F: Float + FromPrimitive + AddAssign + SubAssign + MulAssign + DivAssign> H
}
return None;
}
pub fn learn_one(&mut self, observation: &Observation<F>) {
self.update(observation, false, true);
}
pub fn score_one(&mut self, observation: &Observation<F>) -> Option<ClassifierOutput<F>> {
self.update(observation, true, false)
}
fn max_score(&self) -> F {
F::from(self.n_trees).unwrap()
* F::from(self.window_size).unwrap()
* (F::from(2.).unwrap().powi(self.height as i32 + 1) - F::one())
}
}

impl<F: Float + FromPrimitive + AddAssign + SubAssign + MulAssign + DivAssign> AnomalyDetector<F>
for HalfSpaceTree<F>
{
fn learn_one(&mut self, observation: &Observation<F>) {
self.update(observation, false, true);
}
fn score_one(&mut self, observation: &Observation<F>) -> Option<ClassifierOutput<F>> {
self.update(observation, true, false)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -275,7 +282,6 @@ mod tests {
}

mod tests {
use super::*;
#[test]
fn test_left_child() {
let node = 42;
Expand Down
10 changes: 10 additions & 0 deletions src/anomaly/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
use num::{Float, FromPrimitive};
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};

use crate::common::{ClassifierOutput, Observation};

pub mod half_space_tree;

trait AnomalyDetector<F: Float + FromPrimitive + AddAssign + SubAssign + MulAssign + DivAssign> {
fn learn_one(&mut self, observation: &Observation<F>);
fn score_one(&mut self, observation: &Observation<F>) -> Option<ClassifierOutput<F>>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We continue to discuss the output type of score one, I merge your pr when we decide that is the good format. Thank you for your contribution 😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha no problem. Let me know if it's a public discussion 😃

}