diff --git a/.travis.yml b/.travis.yml index 14c5169..a3c42c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,6 @@ install: - pip install coveralls # command to run tests script: - - pytest --cov=pysheds/ + - pytest --cov=rrcf/ after_success: - coveralls diff --git a/README.md b/README.md index 0a42e14..03d4291 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # rrcf 🌲🌲🌲 -[![Build Status](https://travis-ci.org/kLabUM/rrcf.svg?branch=master)](https://travis-ci.org/kLabUM/rrcf) [![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/) ![GitHub](https://img.shields.io/github/license/kLabUM/rrcf.svg) +[![Build Status](https://travis-ci.org/kLabUM/rrcf.svg?branch=master)](https://travis-ci.org/kLabUM/rrcf) [![Coverage Status](https://coveralls.io/repos/github/kLabUM/rrcf/badge.svg?branch=master)](https://coveralls.io/github/kLabUM/rrcf?branch=master) [![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/) ![GitHub](https://img.shields.io/github/license/kLabUM/rrcf.svg) Implementation of the *Robust Random Cut Forest Algorithm* for anomaly detection by [Guha et al. (2016)](http://proceedings.mlr.press/v48/guha16.pdf). diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_rrcf.py b/test/test_rrcf.py index ffbef6a..6b6cf25 100644 --- a/test/test_rrcf.py +++ b/test/test_rrcf.py @@ -5,7 +5,11 @@ n = 100 d = 3 X = np.random.randn(n, d) +Z = np.copy(X) +Z[90:, :] = 1 + tree = rrcf.RCTree(X) +duplicate_tree = rrcf.RCTree(Z) deck = np.arange(n, dtype=int) np.random.shuffle(deck) @@ -23,6 +27,16 @@ def test_batch(): bbox = tree.get_bbox(branch) assert (bbox == branch.b).all() +def test_codisp(): + for i in range(100): + codisp = tree.codisp(i) + assert codisp > 0 + +def test_disp(): + for i in range(100): + disp = tree.disp(i) + assert disp > 0 + def test_forget_batch(): # Check stored bounding boxes and leaf counts after forgetting points for index in indexes: @@ -71,3 +85,46 @@ def test_insert_batch(): print('Computed:\n', bbox) print('Stored:\n', branch.b) raise + +def test_batch_with_duplicates(): + # Instantiate tree with 10 duplicates + leafcount = duplicate_tree._count_leaves(tree.root) + assert (leafcount == n) + for i in range(90, 100): + try: + assert duplicate_tree.leaves[i].n == 10 + except: + print(i) + print(duplicate_tree.leaves[i].n) + raise + +def test_insert_duplicate(): + # Insert duplicate point + point = (1., 1., 1.) + leaf = duplicate_tree.insert_point(point, index=100) + assert leaf.n == 11 + for i in range(90, 100): + try: + assert duplicate_tree.leaves[i].n == 11 + except: + print(i) + print(duplicate_tree.leaves[i].n) + raise + +def test_find_duplicate(): + # Find duplicate point + point = (1, 1, 1) + duplicate = duplicate_tree.find_duplicate(point) + assert duplicate is not None + +def test_forget_duplicate(): + # Forget duplicate point + leaf = duplicate_tree.forget_point(100) + for i in range(90, 100): + assert duplicate_tree.leaves[i].n == 10 + +def test_shingle(): + shingle = rrcf.shingle(X, 3) + step_0 = next(shingle) + step_1 = next(shingle) + assert (step_0[1] == step_1[0]).all()