WeightedRipsPersistence¶

class
gtda.homology.
WeightedRipsPersistence
(metric='euclidean', metric_params={}, homology_dimensions=0, 1, weights='DTM', weight_params=None, collapse_edges=False, coeff=2, max_edge_weight=inf, infinity_values=None, reduced_homology=True, n_jobs=None)[source]¶ Persistence diagrams resulting from weighted Vietoris–Rips filtrations as in 3.
Given a point cloud in Euclidean space, an abstract metric space encoded by a distance matrix, or the adjacency matrix of a weighted undirected graph, information about the appearance and disappearance of topological features (technically, homology classes) of various dimensions and at different scales is summarised in the corresponding persistence diagram.
Weighted (Vietoris–)Rips filtrations can be useful to highlight topological features against outliers and noise. Among them, the distancetomeasure (DTM) filtration is particularly suited to point clouds due to several favourable properties. This implementation follows the general framework described in 3. The idea is that, starting from a way to compute vertex weights \(\{w_i\}_i\) from an input point cloud/distance matrix/adjacency matrix, a modified adjacency matrix is determined whose diagonal entries are the \(\{w_i\}_i\), and whose edge weights are
\[\begin{split}w_{ij} = \begin{cases} \max\{ w_i, w_j \} &\text{if } 2\mathrm{dist}_{ij} \leq w_i^p  w_j^p^{\frac{1}{p}}, \\ t &\text{otherwise} \end{cases}\end{split}\]where \(t\) is the only positive root of
\[2 \mathrm{dist}_{ij} = (t^p  w_i^p)^\frac{1}{p} + (t^p  w_j^p)^\frac{1}{p}\]and \(p\) is a parameter (see metric_params). The modified adjacency matrices are then treated exactly as in
VietorisRipsPersistence
.Important notes:
Vertex and edge weights are twice the ones in 3 so that the same results as
VietorisRipsPersistence
are obtained when all vertex weights are zero.Persistence diagrams produced by this class must be interpreted with care due to the presence of padding triples which carry no information. See
transform
for additional information.
 Parameters
metric (string or callable, optional, default:
"euclidean"
) – If set to"precomputed"
, input data is to be interpreted as a collection of distance matrices or of adjacency matrices of weighted undirected graphs. Otherwise, input data is to be interpreted as a collection of point clouds (i.e. feature arrays), and metric determines a rule with which to calculate distances between pairs of points (i.e. row vectors). If metric is a string, it must be one of the options allowed byscipy.spatial.distance.pdist
for its metric parameter, or a metric listed insklearn.pairwise.PAIRWISE_DISTANCE_FUNCTIONS
, including"euclidean"
,"manhattan"
or"cosine"
. If metric is a callable, it should take pairs of vectors (1D arrays) as input and, for each two vectors in a pair, it should return a scalar indicating the distance/dissimilarity between them.metric_params (dict, optional, default:
{}
) – Additional parameters to be passed to the distance function.homology_dimensions (list or tuple, optional, default:
(0, 1)
) – Dimensions (nonnegative integers) of the topological features to be detected.weights (
"DTM"
or callable, optional, default:"DTM"
) –Function that will be applied to each input point cloud/distance matrix/adjacency matrix to compute a 1D array of vertex weights for the the modified adjacency matrices. The default
"DTM"
denotes the empirical distancetomeasure function defined, following 3, by\[w(x) = 2\left(\frac{1}{n+1} \sum_{k=1}^n \mathrm{dist}(x, x_k)^r\right)^{1/r}.\]Here, \(\mathrm{dist}\) is the distance metric used, \(x_k\) is the \(k\)th \(\mathrm{dist}\)nearest neighbour of \(x\) (\(x\) is not considered a neighbour of itself), \(n\) is the number of nearest neighbors to include, and \(r\) is a parameter (see weight_params). If a callable, it must return nonnegative 1D arrays.
weight_params (dict, optional, default:
None
) – Additional parameters for the weighted filtration."p"
determines the power to be used in computing edge weights from vertex weights. It can be one of1
,2
ornp.inf
and defaults to1
. If weights is"DTM"
, the additional keys"r"
(default:2
) and"n_neighbors"
(default:3
) are available (see weights, where the latter corresponds to \(n\)).coeff (int prime, optional, default:
2
) – Compute homology with coefficients in the prime field \(\mathbb{F}_p = \{ 0, \ldots, p  1 \}\) where \(p\) equals coeff.collapse_edges (bool, optional, default:
False
) – Whether to run the edge collapse algorithm in 2 prior to the persistent homology computation (see the Notes). Can reduce the runtime dramatically when the data or the maximum homology dimensions are large.max_edge_weight (float, optional, default:
numpy.inf
) – Maximum value of the filtration parameter in the modified adjacency matrix. Edges with weight greater than this value will be considered absent.infinity_values (float or None, default:
None
) – Which death value to assign to features which are still alive at filtration value max_edge_weight.None
means that this death value is declared to be equal to max_edge_weight.reduced_homology (bool, optional, default:
True
) – IfTrue
, the earliestborn triple in homology dimension 0 which has infinite death is discarded from each diagram computed intransform
.n_jobs (int or None, optional, default:
None
) – The number of jobs to use for the computation.None
means 1 unless in ajoblib.parallel_backend
context.1
means using all processors.

infinity_values_
¶ Effective death value to assign to features which are still alive at filtration value max_edge_weight.
 Type
float

effective_weight_params_
¶ Effective parameters involved in computing the weighted Rips filtration.
 Type
dict
See also
VietorisRipsPersistence
,SparseRipsPersistence
,FlagserPersistence
,WeakAlphaPersistence
,EuclideanCechPersistence
,ConsistentRescaling
,ConsecutiveRescaling
Notes
Ripser 1 is used as a C++ backend for computing Vietoris–Rips persistent homology. Python bindings were modified for performance from the ripser.py package.
GUDHI is used as a C++ backend for the edge collapse algorithm described in 2.
References
 1
U. Bauer, “Ripser: efficient computation of Vietoris–Rips persistence barcodes”, 2019; arXiv:1908.02518.
 2(1,2)
J.D. Boissonnat and S. Pritam, “Edge Collapse and Persistence of Flag Complexes”; in 36th International Symposium on Computational Geometry (SoCG 2020), pp. 19:1–19:15, Schloss DagstuhlLeibniz–Zentrum für Informatik, 2020; DOI: 10.4230/LIPIcs.SoCG.2020.19.
 3(1,2,3,4)
H. Anai et al, “DTMBased Filtrations”; in Topological Data Analysis (Abel Symposia, vol 15), Springer, 2020; DOI: 10.1007/9783030434083_2.

__init__
(metric='euclidean', metric_params={}, homology_dimensions=0, 1, weights='DTM', weight_params=None, collapse_edges=False, coeff=2, max_edge_weight=inf, infinity_values=None, reduced_homology=True, n_jobs=None)[source]¶ Initialize self. See help(type(self)) for accurate signature.

fit
(X, y=None)[source]¶ Calculate
infinity_values_
. Then, return the estimator.This method is here to implement the usual scikitlearn API and hence work in pipelines.
 Parameters
X (ndarray or list of length n_samples) –
Input data representing a collection of point clouds if metric was not set to
"precomputed"
, and of distance matrices or adjacency matrices of weighted undirected graphs otherwise. Can be either a 3D ndarray whose zeroth dimension has sizen_samples
, or a list containingn_samples
2D ndarrays/sparse matrices. Point cloud arrays have shape(n_points, n_dimensions)
, and if X is a list these shapes can vary between point clouds. If metric was set to"precomputed"
, then:All entries of X should not contain infinities or negative values (contrary to
VietorisRipsPersistence
).The diagonals of entries of X are ignored (after the vertex weights are computed, when weights is a callable).
If entries of X are dense, only their upper diagonal portions are considered.
If entries of X are sparse, they do not need to be upper diagonal or symmetric. If only one of entry (i, j) and (j, i) is stored, its value is taken as the weight of the undirected edge {i, j}. If both are stored, the value in the upper diagonal is taken. Offdiagonal entries which are not explicitly stored are treated as infinite, indicating absent edges.
y (None) – There is no need for a target in a transformer, yet the pipeline API requires this parameter.
 Returns
self
 Return type
object

fit_transform
(X, y=None, **fit_params)¶ Fit to data, then transform it.
Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.
 Parameters
X (ndarray or list of length n_samples) –
Input data representing a collection of point clouds if metric was not set to
"precomputed"
, and of distance matrices or adjacency matrices of weighted undirected graphs otherwise. Can be either a 3D ndarray whose zeroth dimension has sizen_samples
, or a list containingn_samples
2D ndarrays/sparse matrices. Point cloud arrays have shape(n_points, n_dimensions)
, and if X is a list these shapes can vary between point clouds. If metric was set to"precomputed"
, then:All entries of X should not contain infinities or negative values (contrary to
VietorisRipsPersistence
).The diagonals of entries of X are ignored (after the vertex weights are computed, when weights is a callable).
If entries of X are dense, only their upper diagonal portions are considered.
If entries of X are sparse, they do not need to be upper diagonal or symmetric. If only one of entry (i, j) and (j, i) is stored, its value is taken as the weight of the undirected edge {i, j}. If both are stored, the value in the upper diagonal is taken. Offdiagonal entries which are not explicitly stored are treated as infinite, indicating absent edges.
y (None) – There is no need for a target in a transformer, yet the pipeline API requires this parameter.
 Returns
Xt – Array of persistence diagrams computed from the feature arrays or distance matrices in X.
n_features
equals \(\sum_q n_q\), where \(n_q\) is the maximum number of topological features in dimension \(q\) across all samples in X. Return type
ndarray of shape (n_samples, n_features, 3)

fit_transform_plot
(X, y=None, sample=0, **plot_params)¶ Fit to data, then apply
transform_plot
. Parameters
X (ndarray of shape (n_samples, ..)) – Input data.
y (ndarray of shape (n_samples,) or None) – Target values for supervised problems.
sample (int) – Sample to be plotted.
**plot_params – Optional plotting parameters.
 Returns
Xt – Transformed onesample slice from the input.
 Return type
ndarray of shape (1, ..)

get_params
(deep=True)¶ Get parameters for this estimator.
 Parameters
deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.
 Returns
params – Parameter names mapped to their values.
 Return type
mapping of string to any

static
plot
(Xt, sample=0, homology_dimensions=None, plotly_params=None)[source]¶ Plot a sample from a collection of persistence diagrams, with homology in multiple dimensions.
 Parameters
Xt (ndarray of shape (n_samples, n_features, 3)) – Collection of persistence diagrams, such as returned by
transform
.sample (int, optional, default:
0
) – Index of the sample in Xt to be plotted.homology_dimensions (list, tuple or None, optional, default:
None
) – Which homology dimensions to include in the plot.None
means plotting all dimensions present inXt[sample]
.plotly_params (dict or None, optional, default:
None
) – Custom parameters to configure the plotly figure. Allowed keys are"traces"
and"layout"
, and the corresponding values should be dictionaries containing keyword arguments as would be fed to theupdate_traces
andupdate_layout
methods ofplotly.graph_objects.Figure
.
 Returns
fig – Plotly figure.
 Return type
plotly.graph_objects.Figure
object

set_params
(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form
<component>__<parameter>
so that it’s possible to update each component of a nested object. Parameters
**params (dict) – Estimator parameters.
 Returns
self – Estimator instance.
 Return type
object

transform
(X, y=None)[source]¶ For each point cloud or distance matrix in X, compute the relevant persistence diagram as an array of triples [b, d, q]. Each triple represents a persistent topological feature in dimension q (belonging to homology_dimensions) which is born at b and dies at d. Only triples in which b < d are meaningful. Triples in which b and d are equal (“diagonal elements”) may be artificially introduced during the computation for padding purposes, since the number of nontrivial persistent topological features is typically not constant across samples. They carry no information and hence should be effectively ignored by any further computation.
 Parameters
X (ndarray or list of length n_samples) –
Input data representing a collection of point clouds if metric was not set to
"precomputed"
, and of distance matrices or adjacency matrices of weighted undirected graphs otherwise. Can be either a 3D ndarray whose zeroth dimension has sizen_samples
, or a list containingn_samples
2D ndarrays/sparse matrices. Point cloud arrays have shape(n_points, n_dimensions)
, and if X is a list these shapes can vary between point clouds. If metric was set to"precomputed"
, then:All entries of X should not contain infinities or negative values (contrary to
VietorisRipsPersistence
).The diagonals of entries of X are ignored (after the vertex weights are computed, when weights is a callable).
If entries of X are dense, only their upper diagonal portions are considered.
If entries of X are sparse, they do not need to be upper diagonal or symmetric. If only one of entry (i, j) and (j, i) is stored, its value is taken as the weight of the undirected edge {i, j}. If both are stored, the value in the upper diagonal is taken. Offdiagonal entries which are not explicitly stored are treated as infinite, indicating absent edges.
y (None) – There is no need for a target in a transformer, yet the pipeline API requires this parameter.
 Returns
Xt – Array of persistence diagrams computed from the feature arrays or distance matrices in X.
n_features
equals \(\sum_q n_q\), where \(n_q\) is the maximum number of topological features in dimension \(q\) across all samples in X. Return type
ndarray of shape (n_samples, n_features, 3)

transform_plot
(X, sample=0, **plot_params)¶ Take a onesample slice from the input collection and transform it. Before returning the transformed object, plot the transformed sample.
 Parameters
X (ndarray of shape (n_samples, ..)) – Input data.
sample (int) – Sample to be plotted.
**plot_params – Optional plotting parameters.
 Returns
Xt – Transformed onesample slice from the input.
 Return type
ndarray of shape (1, ..)