Skip to content

Commit

Permalink
Add tests for stateless score_one and fix doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekWadinger committed Oct 10, 2023
1 parent 26b990e commit f33e3ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
20 changes: 19 additions & 1 deletion river/anomaly/lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,25 @@ class LocalOutlierFactor(anomaly.base.AnomalyDetector):
... scores.append(lof.score_one(x))
>>> [round(score, 3) for score in scores]
[1.802, 1.937, 1.567, 1.181, 1.28]
[1.802, 1.936, 1.566, 1.181, 1.272]
>>> X = [0.5, 0.45, 0.43, 0.44, 0.445, 0.45, 0.0]
>>> lof = anomaly.LocalOutlierFactor()
>>> for x in X[:3]:
... lof.learn_one({'x': x}) # Warming up
>>> for x in X:
... features = {'x': x}
... print(f'Anomaly score for x={x:.3f}: {lof.score_one(features):.3f}')
... lof.learn_one(features)
Anomaly score for x=0.500: 0.000
Anomaly score for x=0.450: 0.000
Anomaly score for x=0.430: 0.000
Anomaly score for x=0.440: 1.020
Anomaly score for x=0.445: 1.032
Anomaly score for x=0.450: 0.000
Anomaly score for x=0.000: 0.980
References
----------
Expand Down
30 changes: 30 additions & 0 deletions river/anomaly/test_lof.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,33 @@ def test_issue_1328():
X = [{"a": 1, "b": 1}, {"a": 1, "b": 1}]
for x in X:
lof.learn_one(x)


def test_issue_1331():
import copy

from river import anomaly

lof = anomaly.LocalOutlierFactor()

X = [{"a": 1, "b": 1}, {"a": 1, "b": 1}]
for x in X:
lof.learn_one(x)

neighborhoods_ = lof.neighborhoods.copy()
rev_neighborhoods = lof.rev_neighborhoods.copy()
k_dist_ = lof.k_dist.copy()
reach_dist_ = copy.deepcopy(lof.reach_dist)
dist_dict_ = copy.deepcopy(lof.dist_dict)
local_reach_ = lof.local_reach.copy()
lof_ = lof.lof.copy()

lof.score_one({"a": 0.5, "b": 1})

assert neighborhoods_ == lof.neighborhoods
assert rev_neighborhoods == lof.rev_neighborhoods
assert k_dist_ == lof.k_dist
assert reach_dist_ == lof.reach_dist
assert dist_dict_ == lof.dist_dict
assert local_reach_ == lof.local_reach
assert lof_ == lof.lof

0 comments on commit f33e3ca

Please sign in to comment.