rustyml 0.11.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
Documentation
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#![cfg(feature = "machine_learning")]

use ndarray::{Array2, arr2};
use rustyml::error::ModelError;
use rustyml::machine_learning::isolation_forest::IsolationForest;

// Test basic constructor functionality
#[test]
fn test_new() {
    let forest = IsolationForest::new(50, 128, Some(6), Some(42)).unwrap();
    assert_eq!(forest.get_n_estimators(), 50);
    assert_eq!(forest.get_max_samples(), 128);
    assert_eq!(forest.get_max_depth(), 6);
    assert_eq!(forest.get_random_state(), Some(42));
    assert_eq!(forest.get_n_features(), 0);
}

// Test default constructor
#[test]
fn test_default() {
    let forest = IsolationForest::default();
    assert_eq!(forest.get_n_estimators(), 100);
    assert_eq!(forest.get_max_samples(), 256);
    assert_eq!(forest.get_max_depth(), 8);
    assert_eq!(forest.get_random_state(), None);
    assert!(forest.get_trees().is_none());
}

// Test constructor with automatic max_depth calculation
#[test]
fn test_new_with_auto_max_depth() {
    let forest = IsolationForest::new(10, 64, None, None).unwrap();
    // ceil(log2(64)) = ceil(6.0) = 6
    assert_eq!(forest.get_max_depth(), 6);

    let forest2 = IsolationForest::new(10, 100, None, None).unwrap();
    // ceil(log2(100)) = ceil(6.64) = 7
    assert_eq!(forest2.get_max_depth(), 7);
}

// Test fitting with valid data
#[test]
fn test_fit_valid_data() {
    let mut forest = IsolationForest::new(10, 50, Some(3), Some(42)).unwrap();
    let x = arr2(&[
        [1.0, 2.0],
        [2.0, 3.0],
        [3.0, 4.0],
        [100.0, 200.0], // potential outlier
    ]);

    let result = forest.fit(&x.view());
    assert!(result.is_ok());
    assert_eq!(forest.get_n_features(), 2);
    assert!(forest.get_trees().is_some());
    assert_eq!(forest.get_trees().as_ref().unwrap().len(), 10);
}

// Test fitting with empty data
#[test]
fn test_fit_empty_data() {
    let mut forest = IsolationForest::default();
    let x = Array2::<f64>::zeros((0, 2));

    let result = forest.fit(&x.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test fitting with NaN values
#[test]
fn test_fit_nan_data() {
    let mut forest = IsolationForest::default();
    let x = arr2(&[[1.0, 2.0], [f64::NAN, 3.0], [4.0, 5.0]]);

    let result = forest.fit(&x.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test fitting with infinite values
#[test]
fn test_fit_infinite_data() {
    let mut forest = IsolationForest::default();
    let x = arr2(&[[1.0, 2.0], [f64::INFINITY, 3.0], [4.0, 5.0]]);

    let result = forest.fit(&x.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test anomaly score calculation for single samples
#[test]
fn test_anomaly_score() {
    let mut forest = IsolationForest::new(10, 50, Some(3), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 1.0], [1.1, 0.9], [0.9, 1.1], [1.0, 0.8], [0.8, 1.0]]);

    forest.fit(&x_train.view()).unwrap();

    // Test normal point (should have low anomaly score, close to 0.5)
    let normal_point = [1.0, 1.0];
    let normal_score = forest.anomaly_score(&normal_point).unwrap();
    assert!(normal_score >= 0.0 && normal_score <= 1.0);

    // Test outlier point (should have higher anomaly score)
    let outlier_point = [10.0, 10.0];
    let outlier_score = forest.anomaly_score(&outlier_point).unwrap();
    assert!(outlier_score >= 0.0 && outlier_score <= 1.0);

    // Outlier should typically have higher score than normal point
    // (though this isn't guaranteed due to randomness)
}

// Test anomaly score on unfitted model
#[test]
fn test_anomaly_score_not_fitted() {
    let forest = IsolationForest::default();
    let sample = [1.0, 2.0];

    let result = forest.anomaly_score(&sample);
    assert!(matches!(result, Err(ModelError::NotFitted)));
}

// Test anomaly score with wrong feature dimension
#[test]
fn test_anomaly_score_dimension_mismatch() {
    let mut forest = IsolationForest::new(5, 10, Some(2), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 2.0], [2.0, 3.0]]);

    forest.fit(&x_train.view()).unwrap();

    // Try with wrong number of features
    let wrong_sample = [1.0, 2.0, 3.0]; // 3 features instead of 2
    let result = forest.anomaly_score(&wrong_sample);
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test predict function with multiple samples
#[test]
fn test_predict() {
    let mut forest = IsolationForest::new(20, 50, Some(4), Some(42)).unwrap();
    let x_train = arr2(&[
        [0.0, 0.0],
        [0.1, 0.1],
        [0.0, 0.2],
        [0.2, 0.0],
        [-0.1, 0.1],
        [0.1, -0.1],
    ]);

    forest.fit(&x_train.view()).unwrap();

    let x_test = arr2(&[
        [0.05, 0.05], // normal point
        [5.0, 5.0],   // outlier
        [0.0, 0.0],   // training point
    ]);

    let scores = forest.predict(&x_test.view()).unwrap();
    assert_eq!(scores.len(), 3);

    // All scores should be between 0 and 1
    for &score in scores.iter() {
        assert!(score >= 0.0 && score <= 1.0);
    }
}

// Test predict on unfitted model
#[test]
fn test_predict_not_fitted() {
    let forest = IsolationForest::default();
    let x_test = arr2(&[[1.0, 2.0], [3.0, 4.0]]);

    let result = forest.predict(&x_test.view());
    assert!(matches!(result, Err(ModelError::NotFitted)));
}

// Test predict with empty data
#[test]
fn test_predict_empty_data() {
    let mut forest = IsolationForest::new(5, 10, Some(2), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 2.0], [3.0, 4.0]]);
    forest.fit(&x_train.view()).unwrap();

    let x_test = Array2::<f64>::zeros((0, 2));
    let result = forest.predict(&x_test.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test predict with dimension mismatch
#[test]
fn test_predict_dimension_mismatch() {
    let mut forest = IsolationForest::new(5, 10, Some(2), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 2.0], [3.0, 4.0]]);
    forest.fit(&x_train.view()).unwrap();

    let x_test = arr2(&[[1.0, 2.0, 3.0]]); // 3 features instead of 2
    let result = forest.predict(&x_test.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test predict with NaN values
#[test]
fn test_predict_nan_data() {
    let mut forest = IsolationForest::new(5, 10, Some(2), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 2.0], [3.0, 4.0]]);
    forest.fit(&x_train.view()).unwrap();

    let x_test = arr2(&[[1.0, f64::NAN]]);
    let result = forest.predict(&x_test.view());
    assert!(matches!(result, Err(ModelError::InputValidationError(_))));
}

// Test fit_predict function
#[test]
fn test_fit_predict() {
    let mut forest = IsolationForest::new(15, 30, Some(3), Some(123)).unwrap();
    let x = arr2(&[
        [1.0, 1.0],
        [1.2, 0.8],
        [0.8, 1.2],
        [1.1, 0.9],
        [10.0, 10.0], // outlier
    ]);

    let scores = forest.fit_predict(&x.view()).unwrap();
    assert_eq!(scores.len(), 5);

    // All scores should be between 0 and 1
    for &score in scores.iter() {
        assert!(score >= 0.0 && score <= 1.0);
    }

    // Verify the model is fitted
    assert_eq!(forest.get_n_features(), 2);
    assert!(forest.get_trees().is_some());
}

// Test with single feature data
#[test]
fn test_single_feature() {
    let mut forest = IsolationForest::new(10, 20, Some(3), Some(42)).unwrap();
    let x = arr2(&[
        [1.0],
        [1.1],
        [0.9],
        [1.05],
        [10.0], // outlier
    ]);

    let result = forest.fit(&x.view());
    assert!(result.is_ok());

    let scores = forest.predict(&x.view()).unwrap();
    assert_eq!(scores.len(), 5);

    for &score in scores.iter() {
        assert!(score >= 0.0 && score <= 1.0);
    }
}

// Test reproducibility with random seed
#[test]
fn test_reproducibility() {
    let x = arr2(&[[1.0, 2.0], [2.0, 1.0], [1.5, 1.5], [10.0, 10.0]]);

    // Train two models with same seed
    let mut forest1 = IsolationForest::new(10, 20, Some(3), Some(42)).unwrap();
    let mut forest2 = IsolationForest::new(10, 20, Some(3), Some(42)).unwrap();

    let scores1 = forest1.fit_predict(&x.view()).unwrap();
    let scores2 = forest2.fit_predict(&x.view()).unwrap();

    // Results should be identical with same seed
    for i in 0..scores1.len() {
        assert!((scores1[i] - scores2[i]).abs() < 1e-10);
    }
}

// Test different numbers of estimators
#[test]
fn test_different_n_estimators() {
    let x = arr2(&[[1.0, 2.0], [2.0, 1.0], [1.5, 1.5], [0.5, 2.5]]);

    for n_estimators in [1, 5, 10, 50] {
        let mut forest = IsolationForest::new(n_estimators, 10, Some(2), Some(42)).unwrap();
        let result = forest.fit(&x.view());
        assert!(result.is_ok());
        assert_eq!(forest.get_trees().as_ref().unwrap().len(), n_estimators);
    }
}

// Test different max_samples values
#[test]
fn test_different_max_samples() {
    let x = arr2(&[
        [1.0, 2.0],
        [2.0, 1.0],
        [1.5, 1.5],
        [0.5, 2.5],
        [2.5, 0.5],
        [1.8, 1.2],
    ]);

    for max_samples in [2, 4, 6, 10] {
        let mut forest = IsolationForest::new(5, max_samples, Some(3), Some(42)).unwrap();
        let result = forest.fit(&x.view());
        assert!(result.is_ok());
        assert_eq!(forest.get_max_samples(), max_samples);
    }
}

// Test path length calculation consistency
#[test]
fn test_path_length_consistency() {
    let mut forest = IsolationForest::new(1, 10, Some(3), Some(42)).unwrap(); // Single tree for predictable behavior
    let x = arr2(&[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]);

    forest.fit(&x.view()).unwrap();

    // Same sample should always give same score
    let sample = [1.5, 1.5];
    let score1 = forest.anomaly_score(&sample).unwrap();
    let score2 = forest.anomaly_score(&sample).unwrap();
    assert_eq!(score1, score2);
}

// Test with larger dataset
// Test with larger dataset
#[test]
fn test_larger_dataset() {
    let mut forest = IsolationForest::new(20, 100, Some(5), Some(42)).unwrap();

    // Generate normal data points around (0, 0)
    let mut data = Vec::new();
    for i in 0..100 {
        data.push((i as f64 * 0.01 - 1.0).sin());
        data.push((i as f64 * 0.01 - 1.0).cos());
    }
    // Add some outliers
    data.extend_from_slice(&[10.0, 10.0, -10.0, -10.0]);

    let x = Array2::from_shape_vec((102, 2), data).unwrap();

    let result = forest.fit(&x.view());
    assert!(result.is_ok());

    let scores = forest.predict(&x.view()).unwrap();
    assert_eq!(scores.len(), 102);

    // Check that all scores are valid
    for &score in scores.iter() {
        assert!(score >= 0.0 && score <= 1.0);
        assert!(!score.is_nan());
    }
}

// Test edge case with all identical points
#[test]
fn test_identical_points() {
    let mut forest = IsolationForest::new(10, 20, Some(3), Some(42)).unwrap();
    let x = arr2(&[[1.0, 2.0], [1.0, 2.0], [1.0, 2.0], [1.0, 2.0]]);

    let result = forest.fit(&x.view());
    assert!(result.is_ok());

    let scores = forest.predict(&x.view()).unwrap();
    assert_eq!(scores.len(), 4);

    // All identical points should have similar scores
    let first_score = scores[0];
    for &score in scores.iter() {
        assert!((score - first_score).abs() < 1e-10);
        assert!(score >= 0.0 && score <= 1.0);
    }
}

// Test parallel processing consistency
#[test]
fn test_parallel_consistency() {
    let x = arr2(&[
        [1.0, 2.0],
        [2.0, 3.0],
        [3.0, 4.0],
        [4.0, 5.0],
        [100.0, 200.0],
    ]);

    let mut forest = IsolationForest::new(10, 20, Some(4), Some(42)).unwrap();
    forest.fit(&x.view()).unwrap();

    // Run prediction multiple times - should be consistent
    let scores1 = forest.predict(&x.view()).unwrap();
    let scores2 = forest.predict(&x.view()).unwrap();

    for i in 0..scores1.len() {
        assert_eq!(scores1[i], scores2[i]);
    }
}

// Test memory efficiency with many small predictions
#[test]
fn test_memory_efficiency() {
    let mut forest = IsolationForest::new(5, 10, Some(3), Some(42)).unwrap();
    let x_train = arr2(&[[1.0, 2.0], [2.0, 1.0], [1.5, 1.5]]);

    forest.fit(&x_train.view()).unwrap();

    // Make many individual predictions
    for i in 0..100 {
        let test_point = [i as f64 * 0.1, i as f64 * 0.1];
        let score = forest.anomaly_score(&test_point).unwrap();
        assert!(score >= 0.0 && score <= 1.0);
    }
}