pub struct SemanticAnomalyDetector { /* private fields */ }Expand description
Detects anomalous embeddings using distance-from-centroid analysis.
Supports three detection methods:
- ZScore: z-score of distances to centroid
- IQR: interquartile range on distances
- DistanceBased: mean + multiplier * std_dev threshold on distances
§Example
use ipfrs_semantic::anomaly_detector::{
SemanticAnomalyDetector, AnomalyConfig, SemanticAnomalyMethod,
};
let config = AnomalyConfig::default();
let mut detector = SemanticAnomalyDetector::new(config);
// Add some normal embeddings
for i in 0..20 {
detector.add_embedding(&format!("doc_{i}"), vec![0.1 * i as f64, 0.0, 0.0]);
}
// Detect anomalies
let results = detector.detect_all();Implementations§
Source§impl SemanticAnomalyDetector
impl SemanticAnomalyDetector
Sourcepub fn new(config: AnomalyConfig) -> Self
pub fn new(config: AnomalyConfig) -> Self
Create a new detector with the given configuration.
Sourcepub fn add_embedding(&mut self, doc_id: &str, embedding: Vec<f64>)
pub fn add_embedding(&mut self, doc_id: &str, embedding: Vec<f64>)
Add an embedding and incrementally recompute the centroid.
Sourcepub fn remove_embedding(&mut self, doc_id: &str) -> bool
pub fn remove_embedding(&mut self, doc_id: &str) -> bool
Remove an embedding by doc_id. Returns true if found and removed.
Recomputes the centroid from scratch after removal.
Sourcepub fn detect_all(&mut self) -> Vec<SemanticAnomalyResult>
pub fn detect_all(&mut self) -> Vec<SemanticAnomalyResult>
Run detection on all stored embeddings using the configured method.
Sourcepub fn detect_single(&self, embedding: &[f64]) -> SemanticAnomalyResult
pub fn detect_single(&self, embedding: &[f64]) -> SemanticAnomalyResult
Check if a single new embedding is anomalous against existing data.
Does not add the embedding to the detector.
Sourcepub fn compute_centroid(embeddings: &[(String, Vec<f64>)]) -> Vec<f64>
pub fn compute_centroid(embeddings: &[(String, Vec<f64>)]) -> Vec<f64>
Compute the centroid (mean vector) of the given embeddings.
Sourcepub fn euclidean_distance(a: &[f64], b: &[f64]) -> f64
pub fn euclidean_distance(a: &[f64], b: &[f64]) -> f64
Euclidean distance between two vectors.
Sourcepub fn distances_to_centroid(&self) -> Vec<(String, f64)>
pub fn distances_to_centroid(&self) -> Vec<(String, f64)>
Compute (doc_id, distance) pairs for all stored embeddings to centroid.
Sourcepub fn embedding_count(&self) -> usize
pub fn embedding_count(&self) -> usize
Number of stored embeddings.
Sourcepub fn stats(&self) -> AnomalyDetectorStats
pub fn stats(&self) -> AnomalyDetectorStats
Get detector statistics.
Auto Trait Implementations§
impl Freeze for SemanticAnomalyDetector
impl RefUnwindSafe for SemanticAnomalyDetector
impl Send for SemanticAnomalyDetector
impl Sync for SemanticAnomalyDetector
impl Unpin for SemanticAnomalyDetector
impl UnsafeUnpin for SemanticAnomalyDetector
impl UnwindSafe for SemanticAnomalyDetector
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.