From 3b7bd4fd8b21fc8ce0d7b2848b5ac9bdd397c080 Mon Sep 17 00:00:00 2001 From: Manu Date: Mon, 14 Feb 2022 13:58:01 +0100 Subject: A bug in the Entropy function has been solved --- src/python/gudhi/representations/vector_methods.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index f8078d03..57ca5999 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -510,16 +510,19 @@ class Entropy(BaseEstimator, TransformerMixin): for i in range(num_diag): orig_diagram, diagram, num_pts_in_diag = X[i], new_X[i], X[i].shape[0] try: - new_diagram = DiagramScaler(use=True, scalers=[([1], MaxAbsScaler())]).fit_transform([diagram])[0] + #new_diagram = DiagramScaler(use=True, scalers=[([1], MaxAbsScaler())]).fit_transform([diagram])[0] + new_diagram = DiagramScaler().fit_transform([diagram])[0] except ValueError: # Empty persistence diagram case - https://github.com/GUDHI/gudhi-devel/issues/507 assert len(diagram) == 0 new_diagram = np.empty(shape = [0, 2]) - + + p = new_diagram[:,1] + L = sum(p) + p = p/L if self.mode == "scalar": - ent = - np.sum( np.multiply(new_diagram[:,1], np.log(new_diagram[:,1])) ) + ent = -np.dot(p, np.log(p)) Xfit.append(np.array([[ent]])) - else: ent = np.zeros(self.resolution) for j in range(num_pts_in_diag): @@ -527,7 +530,7 @@ class Entropy(BaseEstimator, TransformerMixin): min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.resolution) max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.resolution) for k in range(min_idx, max_idx): - ent[k] += (-1) * new_diagram[j,1] * np.log(new_diagram[j,1]) + ent[k] += (-1) * p[j] * np.log(p[j]) if self.normalized: ent = ent / np.linalg.norm(ent, ord=1) Xfit.append(np.reshape(ent,[1,-1])) -- cgit v1.2.3 From 758111506dfb99cdc59981395386926e178d447c Mon Sep 17 00:00:00 2001 From: Manu Date: Wed, 23 Feb 2022 19:20:06 +0100 Subject: a test for gudhi.representations.Entropy has been added --- src/python/gudhi/representations/vector_methods.py | 2 +- src/python/test/test_representations.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index 57ca5999..ef1329d0 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -532,7 +532,7 @@ class Entropy(BaseEstimator, TransformerMixin): for k in range(min_idx, max_idx): ent[k] += (-1) * p[j] * np.log(p[j]) if self.normalized: - ent = ent / np.linalg.norm(ent, ord=1) + ent = ent / (np.linalg.norm(ent, ord=1)) Xfit.append(np.reshape(ent,[1,-1])) Xfit = np.concatenate(Xfit, axis=0) diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 6a3dddc4..553ceba0 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -160,7 +160,17 @@ def test_entropy_miscalculation(): l = l/sum(l) return -np.dot(l, np.log(l)) sce = Entropy(mode="scalar") - assert [[pe_max(diag_ex)]] == sce.fit_transform([diag_ex]) + assert [[pe(diag_ex)]] == sce.fit_transform([diag_ex]) + sce = Entropy(mode="vector", resolution=4, normalized=False) + pef = [-1/4*np.log(1/4)-1/4*np.log(1/4)-1/2*np.log(1/2), + -1/4*np.log(1/4)-1/4*np.log(1/4)-1/2*np.log(1/2), + -1/2*np.log(1/2), + 0.0] + assert all(([pef] == sce.fit_transform([diag_ex]))[0]) + sce = Entropy(mode="vector", resolution=4, normalized=True) + pefN = (sce.fit_transform([diag_ex]))[0] + area = np.linalg.norm(pefN, ord=1) + assert area==1 def test_kernel_empty_diagrams(): empty_diag = np.empty(shape = [0, 2]) -- cgit v1.2.3 From a1e8821384c58f7d843a3271f909c31c26649032 Mon Sep 17 00:00:00 2001 From: Manu Date: Wed, 23 Feb 2022 19:27:36 +0100 Subject: Revert "a test for gudhi.representations.Entropy has been added" This reverts commit 758111506dfb99cdc59981395386926e178d447c. --- src/python/gudhi/representations/vector_methods.py | 2 +- src/python/test/test_representations.py | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index ef1329d0..57ca5999 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -532,7 +532,7 @@ class Entropy(BaseEstimator, TransformerMixin): for k in range(min_idx, max_idx): ent[k] += (-1) * p[j] * np.log(p[j]) if self.normalized: - ent = ent / (np.linalg.norm(ent, ord=1)) + ent = ent / np.linalg.norm(ent, ord=1) Xfit.append(np.reshape(ent,[1,-1])) Xfit = np.concatenate(Xfit, axis=0) diff --git a/src/python/test/test_representations.py b/src/python/test/test_representations.py index 553ceba0..6a3dddc4 100755 --- a/src/python/test/test_representations.py +++ b/src/python/test/test_representations.py @@ -160,17 +160,7 @@ def test_entropy_miscalculation(): l = l/sum(l) return -np.dot(l, np.log(l)) sce = Entropy(mode="scalar") - assert [[pe(diag_ex)]] == sce.fit_transform([diag_ex]) - sce = Entropy(mode="vector", resolution=4, normalized=False) - pef = [-1/4*np.log(1/4)-1/4*np.log(1/4)-1/2*np.log(1/2), - -1/4*np.log(1/4)-1/4*np.log(1/4)-1/2*np.log(1/2), - -1/2*np.log(1/2), - 0.0] - assert all(([pef] == sce.fit_transform([diag_ex]))[0]) - sce = Entropy(mode="vector", resolution=4, normalized=True) - pefN = (sce.fit_transform([diag_ex]))[0] - area = np.linalg.norm(pefN, ord=1) - assert area==1 + assert [[pe_max(diag_ex)]] == sce.fit_transform([diag_ex]) def test_kernel_empty_diagrams(): empty_diag = np.empty(shape = [0, 2]) -- cgit v1.2.3 From 77cd751b729d6b68a49ae99e86cff481220ec367 Mon Sep 17 00:00:00 2001 From: Manu Date: Mon, 7 Mar 2022 09:55:17 +0100 Subject: minor changes in entropy --- src/python/gudhi/representations/vector_methods.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index 57ca5999..7d6a7f27 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -518,8 +518,7 @@ class Entropy(BaseEstimator, TransformerMixin): new_diagram = np.empty(shape = [0, 2]) p = new_diagram[:,1] - L = sum(p) - p = p/L + p = p/np.sum(p) if self.mode == "scalar": ent = -np.dot(p, np.log(p)) Xfit.append(np.array([[ent]])) @@ -529,8 +528,7 @@ class Entropy(BaseEstimator, TransformerMixin): [px,py] = orig_diagram[j,:2] min_idx = np.clip(np.ceil((px - self.sample_range[0]) / step_x).astype(int), 0, self.resolution) max_idx = np.clip(np.ceil((py - self.sample_range[0]) / step_x).astype(int), 0, self.resolution) - for k in range(min_idx, max_idx): - ent[k] += (-1) * p[j] * np.log(p[j]) + ent[min_idx:max_idx]-=p[j]*np.log(p[j]) if self.normalized: ent = ent / np.linalg.norm(ent, ord=1) Xfit.append(np.reshape(ent,[1,-1])) -- cgit v1.2.3 From 3e703a44f81623049dd481537e1e2e3457d32943 Mon Sep 17 00:00:00 2001 From: Manu Date: Tue, 7 Jun 2022 15:52:26 +0200 Subject: DiagramScaler removed from Entropy --- src/python/gudhi/representations/vector_methods.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'src/python/gudhi') diff --git a/src/python/gudhi/representations/vector_methods.py b/src/python/gudhi/representations/vector_methods.py index 7d6a7f27..69ff5e1e 100644 --- a/src/python/gudhi/representations/vector_methods.py +++ b/src/python/gudhi/representations/vector_methods.py @@ -508,14 +508,7 @@ class Entropy(BaseEstimator, TransformerMixin): new_X = BirthPersistenceTransform().fit_transform(X) for i in range(num_diag): - orig_diagram, diagram, num_pts_in_diag = X[i], new_X[i], X[i].shape[0] - try: - #new_diagram = DiagramScaler(use=True, scalers=[([1], MaxAbsScaler())]).fit_transform([diagram])[0] - new_diagram = DiagramScaler().fit_transform([diagram])[0] - except ValueError: - # Empty persistence diagram case - https://github.com/GUDHI/gudhi-devel/issues/507 - assert len(diagram) == 0 - new_diagram = np.empty(shape = [0, 2]) + orig_diagram, new_diagram, num_pts_in_diag = X[i], new_X[i], X[i].shape[0] p = new_diagram[:,1] p = p/np.sum(p) -- cgit v1.2.3