#[test]
fn test_isolation_forest_score_samples() {
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 10.0, 10.0, -10.0, -10.0, ],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new().with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let scores = iforest.score_samples(&data);
assert_eq!(scores.len(), 6);
let normal_avg = (scores[0] + scores[1] + scores[2] + scores[3]) / 4.0;
let outlier_avg = (scores[4] + scores[5]) / 2.0;
assert!(outlier_avg < normal_avg);
}
#[test]
fn test_isolation_forest_contamination() {
let data = Matrix::from_vec(
10,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 2.1, 2.1, 1.8, 2.0, 2.2, 2.0, 2.0, 2.2, 10.0,
10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest_low = IsolationForest::new()
.with_contamination(0.1)
.with_random_state(42);
iforest_low
.fit(&data)
.expect("Isolation Forest fit should succeed");
let pred_low = iforest_low.predict(&data);
let anomalies_low = pred_low.iter().filter(|&&p| p == -1).count();
let mut iforest_high = IsolationForest::new()
.with_contamination(0.3)
.with_random_state(42);
iforest_high
.fit(&data)
.expect("Isolation Forest fit should succeed");
let pred_high = iforest_high.predict(&data);
let anomalies_high = pred_high.iter().filter(|&&p| p == -1).count();
assert!(anomalies_high >= anomalies_low);
}
#[test]
fn test_isolation_forest_n_estimators() {
let data = Matrix::from_vec(
8,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 2.1, 2.1, 1.8, 2.0, 10.0, 10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest_few = IsolationForest::new()
.with_n_estimators(10)
.with_random_state(42);
iforest_few
.fit(&data)
.expect("Isolation Forest fit should succeed");
let mut iforest_many = IsolationForest::new()
.with_n_estimators(100)
.with_random_state(42);
iforest_many
.fit(&data)
.expect("Isolation Forest fit should succeed");
let pred_few = iforest_few.predict(&data);
let pred_many = iforest_many.predict(&data);
assert_eq!(pred_few.len(), 8);
assert_eq!(pred_many.len(), 8);
}
#[test]
fn test_isolation_forest_max_samples() {
let data = Matrix::from_vec(
10,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 2.1, 2.1, 1.8, 2.0, 2.2, 2.0, 2.0, 2.2, 10.0,
10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_max_samples(5)
.with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let predictions = iforest.predict(&data);
assert_eq!(predictions.len(), 10);
}
#[test]
fn test_isolation_forest_reproducible() {
let data = Matrix::from_vec(
8,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 2.1, 2.1, 1.8, 2.0, 10.0, 10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest1 = IsolationForest::new().with_random_state(42);
iforest1
.fit(&data)
.expect("Isolation Forest fit should succeed");
let pred1 = iforest1.predict(&data);
let mut iforest2 = IsolationForest::new().with_random_state(42);
iforest2
.fit(&data)
.expect("Isolation Forest fit should succeed");
let pred2 = iforest2.predict(&data);
assert_eq!(pred1, pred2);
}
#[test]
fn test_isolation_forest_all_normal() {
let data = Matrix::from_vec(
6,
2,
vec![2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 2.1, 2.1, 1.8, 2.0],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_contamination(0.1)
.with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let predictions = iforest.predict(&data);
let n_normal = predictions.iter().filter(|&&p| p == 1).count();
assert!(n_normal >= 5);
}
#[test]
fn test_isolation_forest_score_samples_range() {
let data = Matrix::from_vec(4, 2, vec![2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 10.0, 10.0])
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new().with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let scores = iforest.score_samples(&data);
for &score in &scores {
assert!(score.is_finite());
}
}
#[test]
fn test_isolation_forest_path_length() {
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 10.0, 10.0, 2.05, 2.05, ],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_n_estimators(100)
.with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let scores = iforest.score_samples(&data);
let outlier_score = scores[4];
let normal_score = (scores[0] + scores[1] + scores[2] + scores[3] + scores[5]) / 5.0;
assert!(outlier_score < normal_score);
}
#[test]
fn test_isolation_forest_multidimensional() {
let data = Matrix::from_vec(
6,
3,
vec![
1.0, 2.0, 3.0, 1.1, 2.1, 3.1, 1.0, 2.0, 3.0, 0.9, 1.9, 2.9, 10.0, 10.0, 10.0, -10.0,
-10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_contamination(0.3)
.with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let predictions = iforest.predict(&data);
assert_eq!(predictions.len(), 6);
}
#[test]
fn test_isolation_forest_decision_function_consistency() {
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 10.0, 10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_contamination(0.3)
.with_random_state(42);
iforest
.fit(&data)
.expect("Isolation Forest fit should succeed");
let predictions = iforest.predict(&data);
let scores = iforest.score_samples(&data);
assert_eq!(predictions.len(), scores.len());
}
#[test]
#[should_panic(expected = "Model not fitted")]
fn test_isolation_forest_predict_before_fit() {
let data =
Matrix::from_vec(2, 2, vec![1.0, 1.0, 2.0, 2.0]).expect("Matrix creation should succeed");
let iforest = IsolationForest::new();
let _ = iforest.predict(&data); }
#[test]
#[should_panic(expected = "Model not fitted")]
fn test_isolation_forest_score_samples_before_fit() {
let data =
Matrix::from_vec(2, 2, vec![1.0, 1.0, 2.0, 2.0]).expect("Matrix creation should succeed");
let iforest = IsolationForest::new();
let _ = iforest.score_samples(&data); }
#[test]
fn test_isolation_forest_empty_after_construction() {
let iforest = IsolationForest::new();
assert!(!iforest.is_fitted());
}
#[test]
fn test_isolation_forest_default() {
let iforest = IsolationForest::default();
assert!(!iforest.is_fitted());
}
#[test]
fn test_isolation_forest_debug_impl() {
let iforest = IsolationForest::new()
.with_n_estimators(50)
.with_contamination(0.2);
let debug_str = format!("{:?}", iforest);
assert!(debug_str.contains("IsolationForest"));
assert!(debug_str.contains("n_estimators"));
}
#[test]
fn test_isolation_forest_clone() {
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 10.0, 10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new().with_random_state(42);
iforest.fit(&data).expect("Fit should succeed");
let iforest_clone = iforest.clone();
assert!(iforest_clone.is_fitted());
let scores_orig = iforest.score_samples(&data);
let scores_clone = iforest_clone.score_samples(&data);
assert_eq!(scores_orig, scores_clone);
}
#[test]
fn test_isolation_forest_contamination_clamping_high() {
let iforest = IsolationForest::new().with_contamination(0.9);
let debug_str = format!("{:?}", iforest);
assert!(debug_str.contains("0.5"));
}
#[test]
fn test_isolation_forest_contamination_clamping_low() {
let iforest = IsolationForest::new().with_contamination(-1.0);
let debug_str = format!("{:?}", iforest);
assert!(debug_str.contains("contamination: 0.0"));
}
#[test]
fn test_isolation_forest_identical_points() {
let data = Matrix::from_vec(5, 2, vec![3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0])
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_n_estimators(10)
.with_random_state(42);
iforest
.fit(&data)
.expect("Fit should succeed with identical points");
assert!(iforest.is_fitted());
let scores = iforest.score_samples(&data);
for &score in &scores {
assert!(score.is_finite());
}
}
#[test]
fn test_isolation_forest_single_point() {
let data = Matrix::from_vec(1, 2, vec![1.0, 2.0]).expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_n_estimators(10)
.with_random_state(42);
iforest
.fit(&data)
.expect("Fit should succeed with single point");
assert!(iforest.is_fitted());
let scores = iforest.score_samples(&data);
assert_eq!(scores.len(), 1);
}
#[test]
fn test_isolation_forest_two_points() {
let data =
Matrix::from_vec(2, 2, vec![0.0, 0.0, 10.0, 10.0]).expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new()
.with_n_estimators(10)
.with_max_samples(2)
.with_random_state(42);
iforest
.fit(&data)
.expect("Fit should succeed with two points");
assert!(iforest.is_fitted());
let scores = iforest.score_samples(&data);
assert_eq!(scores.len(), 2);
for &score in &scores {
assert!(score.is_finite());
}
}
#[test]
fn test_isolation_forest_without_random_state() {
let data = Matrix::from_vec(
6,
2,
vec![
2.0, 2.0, 2.1, 2.0, 1.9, 2.1, 2.0, 1.9, 10.0, 10.0, -10.0, -10.0,
],
)
.expect("Matrix creation should succeed");
let mut iforest = IsolationForest::new().with_n_estimators(10);
iforest.fit(&data).expect("Fit should succeed without seed");
assert!(iforest.is_fitted());
}