rcf3 0.5.0

A Rust implementation of the Random Cut Forest algorithm for anomaly detection.
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import math
import pickle
from collections.abc import Callable
from copy import deepcopy

import pytest
from hypothesis import given, settings
from hypothesis import strategies as st

from rcf3 import Forest

DEFAULT_NUM_TREES = 40
DEFAULT_CAPACITY = 64
DEFAULT_OUTPUT_AFTER = 1


FINITE_F32 = st.floats(
    min_value=-1_000.0,
    max_value=1_000.0,
    allow_nan=False,
    allow_infinity=False,
    width=32,
)


def vector_strategy(dim: int) -> st.SearchStrategy[list[float]]:
    return st.lists(FINITE_F32, min_size=dim, max_size=dim)


def make_forest(  # noqa: PLR0913
    *,
    input_dim: int,
    seed: int,
    shingle_size: int = 1,
    internal_shingling: bool = True,
    num_trees: int = DEFAULT_NUM_TREES,
    capacity: int = DEFAULT_CAPACITY,
    output_after: int = DEFAULT_OUTPUT_AFTER,
    initial_accept_fraction: float = 0.125,
) -> Forest:
    """Create a Forest test fixture with deterministic defaults."""
    return Forest(
        input_dim=input_dim,
        shingle_size=shingle_size,
        num_trees=num_trees,
        capacity=capacity,
        output_after=output_after,
        internal_shingling=internal_shingling,
        initial_accept_fraction=initial_accept_fraction,
        seed=seed,
    )


def warm_forest(
    *,
    input_dim: int,
    shingle_size: int = 1,
    internal_shingling: bool = True,
    seed: int = 7,
    updates: int = 64,
) -> Forest:
    """Create a forest and warm it with synthetic observations."""
    forest = make_forest(
        input_dim=input_dim,
        seed=seed,
        shingle_size=shingle_size,
        internal_shingling=internal_shingling,
    )
    obs_dim = input_dim if internal_shingling else input_dim * shingle_size
    base = [0.125] * obs_dim
    for i in range(updates):
        point = [x + (i % 5) * 0.01 for x in base]
        forest.update(point)
    return forest


@settings(max_examples=30, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=6),
    stream_len=st.integers(min_value=24, max_value=72),
    seed=st.integers(min_value=0, max_value=2**32 - 1),
    data=st.data(),
)
def test_seeded_stream_is_deterministic(
    input_dim: int,
    stream_len: int,
    seed: int,
    data: st.DataObject,
) -> None:
    stream = [
        data.draw(vector_strategy(input_dim), label=f"p{i}") for i in range(stream_len)
    ]
    queries = [data.draw(vector_strategy(input_dim), label=f"q{i}") for i in range(6)]

    f1 = make_forest(input_dim=input_dim, seed=seed)
    f2 = make_forest(input_dim=input_dim, seed=seed)

    for point in stream:
        f1.update(point)
        f2.update(point)

    assert f1.entries_seen() == f2.entries_seen()
    for q in queries:
        assert f1.score(q) == pytest.approx(f2.score(q), abs=1e-12)


@settings(max_examples=25, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=5),
    seed=st.integers(min_value=0, max_value=2**32 - 1),
    data=st.data(),
)
def test_update_and_score_matches_score_then_update(
    input_dim: int,
    seed: int,
    data: st.DataObject,
) -> None:
    warmup = [
        data.draw(vector_strategy(input_dim), label=f"warmup{i}") for i in range(32)
    ]
    point = data.draw(vector_strategy(input_dim), label="point")
    probe = data.draw(vector_strategy(input_dim), label="probe")

    manual = make_forest(input_dim=input_dim, seed=seed)
    for item in warmup:
        manual.update(item)
    fused = deepcopy(manual)

    expected = manual.score(point)
    manual.update(point)
    actual = fused.update_and_score(point)

    assert actual == pytest.approx(expected, abs=1e-12)
    assert fused.entries_seen() == manual.entries_seen()
    assert fused.score(probe) == pytest.approx(manual.score(probe), abs=1e-12)


@settings(max_examples=25, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=5),
    shingle_size=st.integers(min_value=1, max_value=4),
    capacity=st.integers(min_value=8, max_value=128),
    internal_shingling=st.booleans(),
    output_after=st.integers(min_value=0, max_value=20),
)
def test_is_ready_threshold_contract(
    input_dim: int,
    shingle_size: int,
    capacity: int,
    internal_shingling: bool,
    output_after: int,
) -> None:
    forest = make_forest(
        input_dim=input_dim,
        seed=19,
        shingle_size=shingle_size,
        num_trees=30,
        capacity=capacity,
        output_after=output_after,
        internal_shingling=internal_shingling,
    )

    effective_output_after = (1 + capacity // 4) if output_after == 0 else output_after
    needed = effective_output_after + (shingle_size - 1 if internal_shingling else 0)
    obs_dim = input_dim if internal_shingling else input_dim * shingle_size
    obs = [0.0] * obs_dim

    for _ in range(needed):
        forest.update(obs)
        assert not forest.is_ready()

    forest.update(obs)
    assert forest.is_ready()


class TestRoundTrip:
    """Round-trip serialization tests preserving state and scoring."""

    def _inner(
        self,
        input_dim: int,
        seed: int,
        data: st.DataObject,
        roundtrip: Callable[[Forest], Forest],
    ) -> None:
        """Assert that a round-trip transform preserves entries and score."""
        forest = make_forest(input_dim=input_dim, seed=seed)
        points = [
            data.draw(vector_strategy(input_dim), label=f"u{i}") for i in range(48)
        ]
        query = data.draw(vector_strategy(input_dim), label="query")

        for point in points:
            forest.update(point)

        before = forest.score(query)
        restored = roundtrip(forest)

        assert restored.entries_seen() == forest.entries_seen()
        assert restored.score(query) == pytest.approx(before, abs=1e-12)

    @settings(max_examples=20, deadline=None)
    @given(
        input_dim=st.integers(min_value=1, max_value=6),
        seed=st.integers(min_value=0, max_value=2**32 - 1),
        data=st.data(),
    )
    def test_json_roundtrip_preserves_state_and_scores(
        self,
        input_dim: int,
        seed: int,
        data: st.DataObject,
    ) -> None:
        self._inner(
            input_dim=input_dim,
            seed=seed,
            data=data,
            roundtrip=lambda f: Forest.from_json(f.to_json()),
        )

    @settings(max_examples=20, deadline=None)
    @given(
        input_dim=st.integers(min_value=1, max_value=6),
        seed=st.integers(min_value=0, max_value=2**32 - 1),
        protocol=st.integers(
            min_value=pickle.DEFAULT_PROTOCOL, max_value=pickle.HIGHEST_PROTOCOL
        ),
        data=st.data(),
    )
    def test_pickle_roundtrip_preserves_state_and_scores(
        self,
        input_dim: int,
        seed: int,
        protocol: int,
        data: st.DataObject,
    ) -> None:
        self._inner(
            input_dim=input_dim,
            seed=seed,
            data=data,
            roundtrip=lambda f: pickle.loads(pickle.dumps(f, protocol=protocol)),  # noqa: S301
        )

    @settings(max_examples=20, deadline=None)
    @given(
        input_dim=st.integers(min_value=1, max_value=6),
        seed=st.integers(min_value=0, max_value=2**32 - 1),
        data=st.data(),
    )
    def test_deepcopy_preserves_state_and_scores(
        self,
        input_dim: int,
        seed: int,
        data: st.DataObject,
    ) -> None:
        self._inner(
            input_dim=input_dim,
            seed=seed,
            data=data,
            roundtrip=deepcopy,
        )


@settings(max_examples=20, deadline=None)
@given(
    input_dim=st.integers(min_value=2, max_value=6),
    top_k=st.integers(min_value=1, max_value=12),
    percentile=st.integers(min_value=0, max_value=100),
    data=st.data(),
)
def test_near_neighbors_are_bounded_and_sorted(
    input_dim: int,
    top_k: int,
    percentile: int,
    data: st.DataObject,
) -> None:
    forest = make_forest(input_dim=input_dim, seed=23, num_trees=50)
    updates = [data.draw(vector_strategy(input_dim), label=f"u{i}") for i in range(56)]
    query = data.draw(vector_strategy(input_dim), label="query")

    for point in updates:
        forest.update(point)

    results = forest.near_neighbors(query, top_k=top_k, percentile=percentile)
    assert len(results) <= top_k
    assert all(len(item["point"]) == input_dim for item in results)

    distances = [item["distance"] for item in results]
    assert distances == sorted(distances)


@settings(max_examples=25, deadline=None)
@given(
    input_dim=st.integers(min_value=2, max_value=8),
    data=st.data(),
)
def test_impute_deterministic_with_centrality_one_and_preserves_observed(
    input_dim: int,
    data: st.DataObject,
) -> None:
    forest = warm_forest(input_dim=input_dim, seed=29)
    query = data.draw(vector_strategy(input_dim), label="query")
    missing = sorted(
        data.draw(
            st.sets(
                st.integers(min_value=0, max_value=input_dim - 1),
                min_size=1,
                max_size=input_dim - 1,
            ),
            label="missing",
        )
    )

    out1 = forest.impute(query, missing, centrality=1.0)
    out2 = forest.impute(query, missing, centrality=1.0)
    out3 = forest.impute(query, missing, centrality=1.0)

    assert len(out1) == input_dim
    assert out1 == pytest.approx(out2, abs=1e-7)
    assert out2 == pytest.approx(out3, abs=1e-7)

    missing_set = set(missing)
    for i, original in enumerate(query):
        if i not in missing_set:
            assert out1[i] == pytest.approx(original, abs=1e-6)
        assert math.isfinite(out1[i])


@settings(max_examples=15, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=4),
    shingle_size=st.integers(min_value=2, max_value=4),
    data=st.data(),
)
def test_extrapolate_shape_and_finite_values(
    input_dim: int,
    shingle_size: int,
    data: st.DataObject,
) -> None:
    look_ahead = data.draw(
        st.integers(min_value=0, max_value=shingle_size), label="look_ahead"
    )
    forest = warm_forest(
        input_dim=input_dim,
        shingle_size=shingle_size,
        internal_shingling=True,
        seed=31,
        updates=96,
    )
    out = forest.extrapolate(look_ahead)

    assert len(out) == look_ahead * input_dim
    assert all(math.isfinite(x) for x in out)


@settings(max_examples=10, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=5),
    look_ahead=st.integers(min_value=1, max_value=5),
)
def test_extrapolate_requires_internal_shingling(
    input_dim: int, look_ahead: int
) -> None:
    forest = warm_forest(
        input_dim=input_dim,
        shingle_size=2,
        internal_shingling=False,
        seed=37,
        updates=96,
    )
    with pytest.raises(ValueError, match="internal_shingling"):
        forest.extrapolate(look_ahead)


@settings(max_examples=10, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=5),
    look_ahead=st.integers(min_value=1, max_value=5),
)
def test_extrapolate_requires_shingle_size_gt_one(
    input_dim: int, look_ahead: int
) -> None:
    forest = warm_forest(
        input_dim=input_dim,
        shingle_size=1,
        internal_shingling=True,
        seed=41,
        updates=96,
    )
    with pytest.raises(ValueError, match="shingle_size"):
        forest.extrapolate(look_ahead)


@settings(max_examples=12, deadline=None)
@given(
    input_dim=st.integers(min_value=1, max_value=5),
    shingle_size=st.integers(min_value=2, max_value=5),
    extra=st.integers(min_value=1, max_value=5),
)
def test_extrapolate_rejects_look_ahead_beyond_shingle_window(
    input_dim: int,
    shingle_size: int,
    extra: int,
) -> None:
    forest = warm_forest(
        input_dim=input_dim,
        shingle_size=shingle_size,
        internal_shingling=True,
        seed=43,
        updates=96,
    )
    with pytest.raises(ValueError, match="look_ahead"):
        forest.extrapolate(shingle_size + extra)


@settings(max_examples=15, deadline=None)
@given(input_dim=st.integers(min_value=2, max_value=8), data=st.data())
def test_dimension_mismatch_raises_value_error(
    input_dim: int, data: st.DataObject
) -> None:
    forest = make_forest(input_dim=input_dim, seed=11)
    bad_point = data.draw(vector_strategy(input_dim + 1), label="bad")

    with pytest.raises(ValueError, match="dimension mismatch"):
        forest.update(bad_point)


def test_initial_accept_fraction_constructor_argument_is_accepted() -> None:
    forest = make_forest(input_dim=2, seed=17, initial_accept_fraction=1.0)

    forest.update([0.1, 0.2])

    assert forest.entries_seen() == 1


@pytest.mark.parametrize(
    "value",
    [-0.1, 1.1, math.nan, math.inf, -math.inf],
)
def test_invalid_initial_accept_fraction_raises_value_error(value: float) -> None:
    with pytest.raises(ValueError, match="initial_accept_fraction"):
        make_forest(input_dim=2, seed=17, initial_accept_fraction=value)


def test_impute_not_ready_raises_runtime_error() -> None:
    forest = make_forest(input_dim=4, seed=3)

    assert not forest.is_ready()
    with pytest.raises(RuntimeError):
        forest.impute([0.1, 0.2, 0.3, 0.4], [1], centrality=1.0)