summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryuichi-ike <yuichi.ike.1990@gmail.com>2020-05-11 10:45:02 +0900
committeryuichi-ike <yuichi.ike.1990@gmail.com>2020-05-11 10:45:02 +0900
commit5040c75893cb864f5e780b6644b8097f7beeb3a6 (patch)
tree66e9d7635f909c13a11935299ae2d03d5e845fe1
parent3685c604e78b1aa8b472a7a96716f7c126497a70 (diff)
document and comments added, weights modified
-rw-r--r--src/python/doc/rips_complex_ref.rst51
-rw-r--r--src/python/gudhi/weighted_rips_complex.py18
-rw-r--r--src/python/test/test_weighted_rips.py2
3 files changed, 63 insertions, 8 deletions
diff --git a/src/python/doc/rips_complex_ref.rst b/src/python/doc/rips_complex_ref.rst
index 22b5616c..8fc7e1b0 100644
--- a/src/python/doc/rips_complex_ref.rst
+++ b/src/python/doc/rips_complex_ref.rst
@@ -12,3 +12,54 @@ Rips complex reference manual
:show-inheritance:
.. automethod:: gudhi.RipsComplex.__init__
+
+======================================
+Weighted Rips complex reference manual
+======================================
+
+.. autoclass:: gudhi.WeightedRipsComplex
+ :members:
+ :undoc-members:
+ :show-inheritance:
+
+ .. automethod:: gudhi.WeightedRipsComplex.__init__
+
+Basic examples
+-------------
+
+The following example computes the weighted Rips filtration associated with a distance matrix and weights on vertices.
+
+.. testcode::
+
+ from gudhi.weighted_rips_complex import WeightedRipsComplex
+ dist = [[], [1]]
+ weights = [1, 100]
+ w_rips = WeightedRipsComplex(distance_matrix=dist, weights=weights)
+ st = w_rips.create_simplex_tree(max_dimension=2)
+ print(st.get_filtration())
+
+The output is:
+
+.. testoutput::
+
+ [([0], 2.0), ([1], 200.0), ([0, 1], 200.0)]
+
+Combining with DistanceToMeasure, one can compute the DTM-filtration of a point set, as in `this notebook <https://github.com/GUDHI/TDA-tutorial/blob/master/Tuto-GUDHI-DTM-filtrations.ipynb>`_.
+
+.. testcode::
+
+ import numpy as np
+ from scipy.spatial.distance import cdist
+ from gudhi.point_cloud.dtm import DistanceToMeasure
+ from gudhi.weighted_rips_complex import WeightedRipsComplex
+ pts = np.array([[2.0, 2.0], [0.0, 1.0], [3.0, 4.0]])
+ dist = cdist(pts,pts)
+ dtm = DistanceToMeasure(2, q=2, metric="precomputed")
+ r = dtm.fit_transform(dist)
+ w_rips = WeightedRipsComplex(distance_matrix=dist, weights=r)
+ st = w_rips.create_simplex_tree(max_dimension=2)
+ print(st.persistence())
+
+.. testoutput::
+
+ [(0, (3.1622776601683795, inf)), (0, (3.1622776601683795, 5.39834563766817)), (0, (3.1622776601683795, 5.39834563766817))]
diff --git a/src/python/gudhi/weighted_rips_complex.py b/src/python/gudhi/weighted_rips_complex.py
index 83fa82c5..7401c428 100644
--- a/src/python/gudhi/weighted_rips_complex.py
+++ b/src/python/gudhi/weighted_rips_complex.py
@@ -11,23 +11,26 @@ from gudhi import SimplexTree
class WeightedRipsComplex:
"""
- Class to generate a weighted Rips complex from a distance matrix and weights on vertices.
+ Class to generate a weighted Rips complex from a distance matrix and weights on vertices,
+ in the way described in the paper 'DTM-based filtrations' https://arxiv.org/abs/1811.04757.
+ Remark that the filtration value of a vertex is twice of its weight for the consistency with
+ RipsComplex, which is different from the definition in the paper.
"""
def __init__(self,
distance_matrix,
- weights="diagonal",
+ weights=None,
max_filtration=float('inf')):
"""
Args:
- distance_matrix (list of list of float): distance matrix (full square or lower triangular).
- weights (list of float): (one half of) weight for each vertex.
+ distance_matrix (Sequence[Sequence[float]]): distance matrix (full square or lower triangular).
+ weights (Sequence[float]): (one half of) weight for each vertex.
max_filtration (float): specifies the maximal filtration value to be considered.
"""
self.distance_matrix = distance_matrix
- if weights == "diagonal":
- self.weights = [distance_matrix[i][i] for i in range(len(distance_matrix))]
- else:
+ if weights is not None:
self.weights = weights
+ else:
+ self.weights = [0] * len(distance_matrix)
self.max_filtration = max_filtration
def create_simplex_tree(self, max_dimension):
@@ -47,6 +50,7 @@ class WeightedRipsComplex:
for i in range(num_pts):
for j in range(i):
value = max(2*F[i], 2*F[j], dist[i][j] + F[i] + F[j])
+ # max is needed when F is not 1-Lipschitz
if value <= self.max_filtration:
st.insert([i,j], filtration=value)
diff --git a/src/python/test/test_weighted_rips.py b/src/python/test/test_weighted_rips.py
index d3721115..59ec022a 100644
--- a/src/python/test/test_weighted_rips.py
+++ b/src/python/test/test_weighted_rips.py
@@ -51,7 +51,7 @@ def test_compatibility_with_filtered_rips():
assert st.num_vertices() == 4
def test_dtm_rips_complex():
- pts = np.array([[2.0, 2], [0, 1], [3, 4]])
+ pts = np.array([[2.0, 2.0], [0.0, 1.0], [3.0, 4.0]])
dist = cdist(pts,pts)
dtm = DistanceToMeasure(2, q=2, metric="precomputed")
r = dtm.fit_transform(dist)