From 2fb0d594060958804239fcdad5336832ea5133d0 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Mon, 25 May 2020 22:56:04 +0200 Subject: Add test --- src/python/test/test_tomato.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 src/python/test/test_tomato.py (limited to 'src/python/test') diff --git a/src/python/test/test_tomato.py b/src/python/test/test_tomato.py new file mode 100755 index 00000000..0a33b86e --- /dev/null +++ b/src/python/test/test_tomato.py @@ -0,0 +1,27 @@ +""" This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT. + See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details. + Author(s): Marc Glisse + + Copyright (C) 2020 Inria + + Modification(s): + - YYYY/MM Author: Description of the modification +""" + +from gudhi.clustering.tomato import Tomato +import numpy as np +import pytest + + +def test_tomato_something(): + a = [(1, 2), (1.1, 1.9), (0.9, 1.8), (10, 0), (10.1, 0.05), (10.2, -0.1), (5.4, 0)] + t = Tomato(metric="euclidean", n_clusters=2, k=4, n_jobs=-1, eps=0.05) + assert np.array_equal(t.fit_predict(a), [1,1,1,0,0,0,0]) # or with swapped 0 and 1 + + t = Tomato(density_type='KDE', r=1, k=4) + t.fit(a) + assert np.array_equal(t.leaf_labels_, [1,1,1,0,0,0,0]) # or with swapped 0 and 1 + + t = Tomato(graph_type='radius', r=4.7, k=4) + t.fit(a) + assert t.max_weight_per_cc_.size == 2 -- cgit v1.2.3 From 7c0fde0501ba9873e27b8f6129034cf97642cd24 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Tue, 26 May 2020 11:37:18 +0200 Subject: More test --- src/python/test/test_tomato.py | 43 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src/python/test') diff --git a/src/python/test/test_tomato.py b/src/python/test/test_tomato.py index 0a33b86e..a4cab654 100755 --- a/src/python/test/test_tomato.py +++ b/src/python/test/test_tomato.py @@ -11,17 +11,52 @@ from gudhi.clustering.tomato import Tomato import numpy as np import pytest +import matplotlib.pyplot as plt + +# Disable graphics for testing purposes +plt.show = lambda: None def test_tomato_something(): a = [(1, 2), (1.1, 1.9), (0.9, 1.8), (10, 0), (10.1, 0.05), (10.2, -0.1), (5.4, 0)] t = Tomato(metric="euclidean", n_clusters=2, k=4, n_jobs=-1, eps=0.05) - assert np.array_equal(t.fit_predict(a), [1,1,1,0,0,0,0]) # or with swapped 0 and 1 + assert np.array_equal(t.fit_predict(a), [1, 1, 1, 0, 0, 0, 0]) # or with swapped 0 and 1 + assert np.array_equal(t.children_, [[0, 1]]) + + t = Tomato(density_type="KDE", r=1, k=4) + t.fit(a) + assert np.array_equal(t.leaf_labels_, [1, 1, 1, 0, 0, 0, 0]) # or with swapped 0 and 1 + assert t.n_clusters_ == 2 + t.merge_threshold_ = 10 + assert t.n_clusters_ == 1 + assert (t.labels_ == 0).all() - t = Tomato(density_type='KDE', r=1, k=4) + t = Tomato(metric="euclidean", graph_type="radius", r=4.7, k=4) t.fit(a) - assert np.array_equal(t.leaf_labels_, [1,1,1,0,0,0,0]) # or with swapped 0 and 1 + assert t.max_weight_per_cc_.size == 2 + assert np.array_equal(t.neighbors_, [[0, 1, 2], [0, 1, 2], [0, 1, 2], [3, 4, 5, 6], [3, 4, 5], [3, 4, 5], [3, 6]]) + t.plot_diagram() - t = Tomato(graph_type='radius', r=4.7, k=4) + t = Tomato(graph_type="radius", r=4.7, k=4, symmetrize_graph=True) t.fit(a) assert t.max_weight_per_cc_.size == 2 + assert [set(i) for i in t.neighbors_] == [{1, 2}, {0, 2}, {0, 1}, {4, 5, 6}, {3, 5}, {3, 4}, {3}] + + t = Tomato(n_clusters=2, k=4, symmetrize_graph=True) + t.fit(a) + assert [set(i) for i in t.neighbors_] == [ + {1, 2, 6}, + {0, 2, 6}, + {0, 1, 6}, + {4, 5, 6}, + {3, 5, 6}, + {3, 4, 6}, + {0, 1, 2, 3, 4, 5}, + ] + t.plot_diagram() + + t = Tomato(k=6, metric="manhattan") + t.fit(a) + assert t.diagram_.size == 0 + assert t.max_weight_per_cc_.size == 1 + t.plot_diagram() -- cgit v1.2.3 From 59763a7a4a7be162aee9552e5db4e86fa2225cb6 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Tue, 26 May 2020 12:06:40 +0200 Subject: test --- src/python/test/test_tomato.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/python/test') diff --git a/src/python/test/test_tomato.py b/src/python/test/test_tomato.py index a4cab654..ecab03c4 100755 --- a/src/python/test/test_tomato.py +++ b/src/python/test/test_tomato.py @@ -17,7 +17,7 @@ import matplotlib.pyplot as plt plt.show = lambda: None -def test_tomato_something(): +def test_tomato_1(): a = [(1, 2), (1.1, 1.9), (0.9, 1.8), (10, 0), (10.1, 0.05), (10.2, -0.1), (5.4, 0)] t = Tomato(metric="euclidean", n_clusters=2, k=4, n_jobs=-1, eps=0.05) assert np.array_equal(t.fit_predict(a), [1, 1, 1, 0, 0, 0, 0]) # or with swapped 0 and 1 @@ -31,6 +31,9 @@ def test_tomato_something(): assert t.n_clusters_ == 1 assert (t.labels_ == 0).all() + t = Tomato(graph_type="radius", r=0.1, metric="cosine", k=3) + assert np.array_equal(t.fit_predict(a), [1, 1, 1, 0, 0, 0, 0]) # or with swapped 0 and 1 + t = Tomato(metric="euclidean", graph_type="radius", r=4.7, k=4) t.fit(a) assert t.max_weight_per_cc_.size == 2 -- cgit v1.2.3