From af49fdd761bf1eccb5fdca760a99e2e250895f64 Mon Sep 17 00:00:00 2001 From: martinroyer-buntu Date: Fri, 3 Jul 2020 10:58:54 +0200 Subject: dummy test for code coverage --- src/python/test/test_representations.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/python/test/test_representations.py') diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 589cee00..6a09be48 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -4,6 +4,8 @@ import matplotlib.pyplot as plt import numpy as np import pytest +from sklearn.cluster import KMeans + def test_representations_examples(): # Disable graphics for testing purposes @@ -15,6 +17,7 @@ def test_representations_examples(): return None +from gudhi.representations.vector_methods import Atol from gudhi.representations.metrics import * from gudhi.representations.kernel_methods import * @@ -41,3 +44,16 @@ def test_multiple(): d2 = WassersteinDistance(order=2, internal_p=2, n_jobs=4).fit(l2).transform(l1) print(d1.shape, d2.shape) assert d1 == pytest.approx(d2, rel=.02) + + +def test_dummy_atol(): + a = np.array([[1, 2, 4], [1, 4, 0], [1, 0, 4]]) + b = np.array([[4, 2, 0], [4, 4, 0], [4, 0, 2]]) + c = np.array([[3, 2, -1], [1, 2, -1]]) + + for weighting_method in ["cloud", "iidproba"]: + for contrast in ["gaussian", "laplacian", "indicator"]: + atol_vectoriser = Atol(quantiser=KMeans(n_clusters=1, random_state=202006), weighting_method=weighting_method, contrast=contrast) + atol_vectoriser(a) + atol_vectoriser.transform(X=[a, b, c]) + -- cgit v1.2.3 From 96eb09e4f034fd71f5674f75e5e4584a7402b218 Mon Sep 17 00:00:00 2001 From: martinroyer-buntu Date: Fri, 3 Jul 2020 13:43:58 +0200 Subject: missing fit in dummy test --- src/python/test/test_representations.py | 1 + 1 file changed, 1 insertion(+) (limited to 'src/python/test/test_representations.py') diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 6a09be48..e5c211a0 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -54,6 +54,7 @@ def test_dummy_atol(): for weighting_method in ["cloud", "iidproba"]: for contrast in ["gaussian", "laplacian", "indicator"]: atol_vectoriser = Atol(quantiser=KMeans(n_clusters=1, random_state=202006), weighting_method=weighting_method, contrast=contrast) + atol_vectoriser.fit([a, b, c]) atol_vectoriser(a) atol_vectoriser.transform(X=[a, b, c]) -- cgit v1.2.3 From 0022442a303f297ac773e262abd2661d2ce0a614 Mon Sep 17 00:00:00 2001 From: Marc Glisse Date: Sun, 8 Nov 2020 22:08:21 +0100 Subject: Test BettiCurve with infinite value + black reformatting --- src/python/test/test_representations.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'src/python/test/test_representations.py') diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index e5c211a0..43c914f3 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -39,11 +39,11 @@ def test_multiple(): d2 = BottleneckDistance(epsilon=0.00001).fit_transform(l1) d3 = pairwise_persistence_diagram_distances(l1, l1b, e=0.00001, n_jobs=4) assert d1 == pytest.approx(d2) - assert d3 == pytest.approx(d2, abs=1e-5) # Because of 0 entries (on the diagonal) + assert d3 == pytest.approx(d2, abs=1e-5) # Because of 0 entries (on the diagonal) d1 = pairwise_persistence_diagram_distances(l1, l2, metric="wasserstein", order=2, internal_p=2) d2 = WassersteinDistance(order=2, internal_p=2, n_jobs=4).fit(l2).transform(l1) print(d1.shape, d2.shape) - assert d1 == pytest.approx(d2, rel=.02) + assert d1 == pytest.approx(d2, rel=0.02) def test_dummy_atol(): @@ -53,8 +53,22 @@ def test_dummy_atol(): for weighting_method in ["cloud", "iidproba"]: for contrast in ["gaussian", "laplacian", "indicator"]: - atol_vectoriser = Atol(quantiser=KMeans(n_clusters=1, random_state=202006), weighting_method=weighting_method, contrast=contrast) + atol_vectoriser = Atol( + quantiser=KMeans(n_clusters=1, random_state=202006), + weighting_method=weighting_method, + contrast=contrast, + ) atol_vectoriser.fit([a, b, c]) atol_vectoriser(a) atol_vectoriser.transform(X=[a, b, c]) + +from gudhi.representations.vector_methods import BettiCurve + + +def test_infinity(): + a = np.array([[1.0, 8.0], [2.0, np.inf], [3.0, 4.0]]) + c = BettiCurve(20, [0.0, 10.0])(a) + assert c[1] == 0 + assert c[7] == 3 + assert c[9] == 2 -- cgit v1.2.3 From 0afc650917ddf9fc4cf95fd86e0b6408f64a465d Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Mon, 11 Jan 2021 11:29:20 +0100 Subject: Remove sphinx doc test for atol as points order can be inverted and add it in a UT but sorted --- src/python/gudhi/representations/vector_methods.py | 14 +++++++------- src/python/test/test_representations.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) (limited to 'src/python/test/test_representations.py') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index cdcb1fde..d4449e7d 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -606,16 +606,16 @@ class Atol(BaseEstimator, TransformerMixin): >>> c = np.array([[3, 2, -1], [1, 2, -1]]) >>> atol_vectoriser = Atol(quantiser=KMeans(n_clusters=2, random_state=202006)) >>> atol_vectoriser.fit(X=[a, b, c]).centers - array([[ 2. , 0.66666667, 3.33333333], - [ 2.6 , 2.8 , -0.4 ]]) + >>> # array([[ 2. , 0.66666667, 3.33333333], + >>> # [ 2.6 , 2.8 , -0.4 ]]) >>> atol_vectoriser(a) - array([1.18168665, 0.42375966]) + >>> # array([1.18168665, 0.42375966]) >>> atol_vectoriser(c) - array([0.02062512, 1.25157463]) + >>> # array([0.02062512, 1.25157463]) >>> atol_vectoriser.transform(X=[a, b, c]) - array([[1.18168665, 0.42375966], - [0.29861028, 1.06330156], - [0.02062512, 1.25157463]]) + >>> # array([[1.18168665, 0.42375966], + >>> # [0.29861028, 1.06330156], + >>> # [0.02062512, 1.25157463]]) """ def __init__(self, quantiser, weighting_method="cloud", contrast="gaussian"): """ diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 43c914f3..1c8f8cdb 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -46,6 +46,24 @@ def test_multiple(): assert d1 == pytest.approx(d2, rel=0.02) +# Test sorted values as points order can be inverted, and sorted test is not documentation-friendly +def test_atol_doc(): + a = np.array([[1, 2, 4], [1, 4, 0], [1, 0, 4]]) + b = np.array([[4, 2, 0], [4, 4, 0], [4, 0, 2]]) + c = np.array([[3, 2, -1], [1, 2, -1]]) + + atol_vectoriser = Atol(quantiser=KMeans(n_clusters=2, random_state=202006)) + assert np.sort(atol_vectoriser.fit(X=[a, b, c]).centers, axis=0) == \ + pytest.approx(np.array([[2. , 0.66666667, -0.4], \ + [2.6, 2.8 , 3.33333333]])) + assert np.sort(atol_vectoriser(a)) == pytest.approx(np.array([0.42375966, 1.18168665])) + assert np.sort(atol_vectoriser(c)) == pytest.approx(np.array([0.02062512, 1.25157463])) + assert np.sort(atol_vectoriser.transform(X=[a, b, c]), axis=0) == \ + pytest.approx(np.array([[0.02062512, 0.42375966], \ + [0.29861028, 1.06330156], \ + [1.18168665, 1.25157463]])) + + def test_dummy_atol(): a = np.array([[1, 2, 4], [1, 4, 0], [1, 0, 4]]) b = np.array([[4, 2, 0], [4, 4, 0], [4, 0, 2]]) -- cgit v1.2.3 From 60907b0104a2807667f175d9a8a328fd3f7f4ec8 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent Date: Mon, 11 Jan 2021 16:25:18 +0100 Subject: Ignore doctest for atol doc. Rewrite unitary test for atol doc. To be synchronized --- src/python/gudhi/representations/vector_methods.py | 9 ++++---- src/python/test/test_representations.py | 26 ++++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) (limited to 'src/python/test/test_representations.py') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index 5ec2abd0..84bc99a2 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -605,18 +605,19 @@ class Atol(BaseEstimator, TransformerMixin): >>> b = np.array([[4, 2, 0], [4, 4, 0], [4, 0, 2]]) >>> c = np.array([[3, 2, -1], [1, 2, -1]]) >>> atol_vectoriser = Atol(quantiser=KMeans(n_clusters=2, random_state=202006)) - >>> atol_vectoriser.fit(X=[a, b, c]).centers #doctest: +SKIP + >>> atol_vectoriser.fit(X=[a, b, c]).centers # doctest: +SKIP >>> # array([[ 2. , 0.66666667, 3.33333333], >>> # [ 2.6 , 2.8 , -0.4 ]]) >>> atol_vectoriser(a) - >>> # array([1.18168665, 0.42375966]) #doctest: +SKIP + >>> # array([1.18168665, 0.42375966]) # doctest: +SKIP >>> atol_vectoriser(c) - >>> # array([0.02062512, 1.25157463]) #doctest: +SKIP - >>> atol_vectoriser.transform(X=[a, b, c]) #doctest: +SKIP + >>> # array([0.02062512, 1.25157463]) # doctest: +SKIP + >>> atol_vectoriser.transform(X=[a, b, c]) # doctest: +SKIP >>> # array([[1.18168665, 0.42375966], >>> # [0.29861028, 1.06330156], >>> # [0.02062512, 1.25157463]]) """ + # Note the example above must be up to date with the one in tests called test_atol_doc def __init__(self, quantiser, weighting_method="cloud", contrast="gaussian"): """ Constructor for the Atol measure vectorisation class. diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 1c8f8cdb..cda1a15b 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -47,21 +47,29 @@ def test_multiple(): # Test sorted values as points order can be inverted, and sorted test is not documentation-friendly +# Note the test below must be up to date with the Atol class documentation def test_atol_doc(): a = np.array([[1, 2, 4], [1, 4, 0], [1, 0, 4]]) b = np.array([[4, 2, 0], [4, 4, 0], [4, 0, 2]]) c = np.array([[3, 2, -1], [1, 2, -1]]) atol_vectoriser = Atol(quantiser=KMeans(n_clusters=2, random_state=202006)) - assert np.sort(atol_vectoriser.fit(X=[a, b, c]).centers, axis=0) == \ - pytest.approx(np.array([[2. , 0.66666667, -0.4], \ - [2.6, 2.8 , 3.33333333]])) - assert np.sort(atol_vectoriser(a)) == pytest.approx(np.array([0.42375966, 1.18168665])) - assert np.sort(atol_vectoriser(c)) == pytest.approx(np.array([0.02062512, 1.25157463])) - assert np.sort(atol_vectoriser.transform(X=[a, b, c]), axis=0) == \ - pytest.approx(np.array([[0.02062512, 0.42375966], \ - [0.29861028, 1.06330156], \ - [1.18168665, 1.25157463]])) + # Atol will do + # X = np.concatenate([a,b,c]) + # kmeans = KMeans(n_clusters=2, random_state=202006).fit(X) + # kmeans.labels_ will be : array([1, 0, 1, 0, 0, 1, 0, 0]) + first_cluster = np.asarray([a[0], a[2], b[2]]) + second_cluster = np.asarray([a[1], b[0], b[2], c[0], c[1]]) + + # Check the center of the first_cluster and second_cluster are in Atol centers + centers = atol_vectoriser.fit(X=[a, b, c]).centers + np.isclose(centers, first_cluster.mean(axis=0)).all(1).any() + np.isclose(centers, second_cluster.mean(axis=0)).all(1).any() + + vectorization = atol_vectoriser.transform(X=[a, b, c]) + assert np.allclose(vectorization[0], atol_vectoriser(a)) + assert np.allclose(vectorization[1], atol_vectoriser(b)) + assert np.allclose(vectorization[2], atol_vectoriser(c)) def test_dummy_atol(): -- cgit v1.2.3