diff options
author | MathieuCarriere <mathieu.carriere3@gmail.com> | 2021-12-04 12:41:59 +0100 |
---|---|---|
committer | MathieuCarriere <mathieu.carriere3@gmail.com> | 2021-12-04 12:41:59 +0100 |
commit | 979d12e00b4ea71391d132589ee3304e378459b9 (patch) | |
tree | d2a6c8e3c8d344ee5d860cfad775fbef5d10b43d /src/python/gudhi | |
parent | e9b297ec86d79e2b5b2fd4ce63033f8697f053da (diff) |
added min persistence
Diffstat (limited to 'src/python/gudhi')
-rw-r--r-- | src/python/gudhi/tensorflow/cubical_layer.py | 12 | ||||
-rw-r--r-- | src/python/gudhi/tensorflow/lower_star_simplex_tree_layer.py | 14 | ||||
-rw-r--r-- | src/python/gudhi/tensorflow/rips_layer.py | 12 |
3 files changed, 16 insertions, 22 deletions
diff --git a/src/python/gudhi/tensorflow/cubical_layer.py b/src/python/gudhi/tensorflow/cubical_layer.py index d07a4cd8..8fe9cff0 100644 --- a/src/python/gudhi/tensorflow/cubical_layer.py +++ b/src/python/gudhi/tensorflow/cubical_layer.py @@ -8,7 +8,7 @@ from ..cubical_complex import CubicalComplex # The parameters of the model are the pixel values. -def _Cubical(Xflat, Xdim, dimensions): +def _Cubical(Xflat, Xdim, dimensions, min_persistence): # Parameters: Xflat (flattened image), # Xdim (shape of non-flattened image) # dimensions (homology dimensions) @@ -16,7 +16,7 @@ def _Cubical(Xflat, Xdim, dimensions): # Compute the persistence pairs with Gudhi # We reverse the dimensions because CubicalComplex uses Fortran ordering cc = CubicalComplex(dimensions=Xdim[::-1], top_dimensional_cells=Xflat) - cc.compute_persistence() + cc.compute_persistence(min_persistence=min_persistence) # Retrieve and ouput image indices/pixels corresponding to positive and negative simplices cof_pp = cc.cofaces_of_persistence_pairs() @@ -37,7 +37,7 @@ class CubicalLayer(tf.keras.layers.Layer): """ TensorFlow layer for computing cubical persistence out of a cubical complex """ - def __init__(self, dimensions, **kwargs): + def __init__(self, dimensions, min_persistence=0., **kwargs): """ Constructor for the CubicalLayer class @@ -46,9 +46,7 @@ class CubicalLayer(tf.keras.layers.Layer): """ super().__init__(dynamic=True, **kwargs) self.dimensions = dimensions - - def build(self): - super.build() + self.min_persistence = min_persistence def call(self, X): """ @@ -64,7 +62,7 @@ class CubicalLayer(tf.keras.layers.Layer): # Don't compute gradient for this operation Xflat = tf.reshape(X, [-1]) Xdim = X.shape - indices = _Cubical(Xflat.numpy(), Xdim, self.dimensions) + indices = _Cubical(Xflat.numpy(), Xdim, self.dimensions, self.min_persistence) # Get persistence diagram by simply picking the corresponding entries in the image self.dgms = [tf.reshape(tf.gather(Xflat, indice), [-1,2]) for indice in indices] return self.dgms diff --git a/src/python/gudhi/tensorflow/lower_star_simplex_tree_layer.py b/src/python/gudhi/tensorflow/lower_star_simplex_tree_layer.py index aa55604a..5902e4a1 100644 --- a/src/python/gudhi/tensorflow/lower_star_simplex_tree_layer.py +++ b/src/python/gudhi/tensorflow/lower_star_simplex_tree_layer.py @@ -7,7 +7,7 @@ import tensorflow as tf # The parameters of the model are the vertex function values of the simplex tree. -def _LowerStarSimplexTree(simplextree, filtration, dimensions): +def _LowerStarSimplexTree(simplextree, filtration, dimensions, min_persistence): # Parameters: simplextree (simplex tree on which to compute persistence) # filtration (function values on the vertices of st), # dimensions (homology dimensions), @@ -21,7 +21,7 @@ def _LowerStarSimplexTree(simplextree, filtration, dimensions): simplextree.make_filtration_non_decreasing() # Compute persistence diagram - simplextree.compute_persistence() + simplextree.compute_persistence(min_persistence=min_persistence) # Get vertex pairs for optimization. First, get all simplex pairs pairs = simplextree.lower_star_persistence_generators() @@ -43,7 +43,7 @@ class LowerStarSimplexTreeLayer(tf.keras.layers.Layer): """ TensorFlow layer for computing lower-star persistence out of a simplex tree """ - def __init__(self, simplextree, dimensions, **kwargs): + def __init__(self, simplextree, dimensions, min_persistence=0., **kwargs): """ Constructor for the LowerStarSimplexTreeLayer class @@ -54,10 +54,8 @@ class LowerStarSimplexTreeLayer(tf.keras.layers.Layer): super().__init__(dynamic=True, **kwargs) self.dimensions = dimensions self.simplextree = simplextree - - def build(self): - super.build() - + self.min_persistence = min_persistence + def call(self, filtration): """ Compute lower-star persistence diagram associated to a function defined on the vertices of the simplex tree @@ -69,7 +67,7 @@ class LowerStarSimplexTreeLayer(tf.keras.layers.Layer): dgms (list of tuple of TensorFlow variables): list of lower-star persistence diagrams of length self.dimensions, where each element of the list is a tuple that contains the finite and essential persistence diagrams of shapes [num_finite_points, 2] and [num_essential_points, 1] respectively """ # Don't try to compute gradients for the vertex pairs - indices = _LowerStarSimplexTree(self.simplextree, filtration.numpy(), self.dimensions) + indices = _LowerStarSimplexTree(self.simplextree, filtration.numpy(), self.dimensions, self.min_persistence) # Get persistence diagrams self.dgms = [] for idx_dim, dimension in enumerate(self.dimensions): diff --git a/src/python/gudhi/tensorflow/rips_layer.py b/src/python/gudhi/tensorflow/rips_layer.py index 472a418b..97f28d74 100644 --- a/src/python/gudhi/tensorflow/rips_layer.py +++ b/src/python/gudhi/tensorflow/rips_layer.py @@ -8,7 +8,7 @@ from ..rips_complex import RipsComplex # The parameters of the model are the point coordinates. -def _Rips(DX, max_edge, dimensions): +def _Rips(DX, max_edge, dimensions, min_persistence): # Parameters: DX (distance matrix), # max_edge (maximum edge length for Rips filtration), # dimensions (homology dimensions) @@ -16,7 +16,7 @@ def _Rips(DX, max_edge, dimensions): # Compute the persistence pairs with Gudhi rc = RipsComplex(distance_matrix=DX, max_edge_length=max_edge) st = rc.create_simplex_tree(max_dimension=max(dimensions)+1) - st.compute_persistence() + st.compute_persistence(min_persistence=min_persistence) pairs = st.flag_persistence_generators() L_indices = [] @@ -40,7 +40,7 @@ class RipsLayer(tf.keras.layers.Layer): """ TensorFlow layer for computing Rips persistence out of a point cloud """ - def __init__(self, dimensions, maximum_edge_length=12, **kwargs): + def __init__(self, dimensions, maximum_edge_length=12, min_persistence=0., **kwargs): """ Constructor for the RipsLayer class @@ -51,9 +51,7 @@ class RipsLayer(tf.keras.layers.Layer): super().__init__(dynamic=True, **kwargs) self.max_edge = maximum_edge_length self.dimensions = dimensions - - def build(self): - super.build() + self.min_persistence = min_persistence def call(self, X): """ @@ -69,7 +67,7 @@ class RipsLayer(tf.keras.layers.Layer): DX = tf.math.sqrt(tf.reduce_sum((tf.expand_dims(X, 1)-tf.expand_dims(X, 0))**2, 2)) # Compute vertices associated to positive and negative simplices # Don't compute gradient for this operation - indices = _Rips(DX.numpy(), self.max_edge, self.dimensions) + indices = _Rips(DX.numpy(), self.max_edge, self.dimensions, self.min_persistence) # Get persistence diagrams by simply picking the corresponding entries in the distance matrix self.dgms = [] for idx_dim, dimension in enumerate(self.dimensions): |