# How to use the gudhi.bottleneck_distance function in gudhi

## To help you get started, we’ve selected a few gudhi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

MathieuCarriere / sklearn-tda / sklearn_tda / clustering.py View on Github
``````# Randomly select points
idxs = np.random.choice(num_pts, size=num_pts, replace=True)
Xboot = X[idxs,:] if self.input == "point cloud" else X[idxs,:][:,idxs]
f_boot, c_boot = self.filters[idxs,:], self.colors[idxs,:]
Mboot = self.__class__(filters=f_boot, filter_bnds=self.filter_bnds, colors=c_boot, resolutions=self.resolutions, gains=self.gains, inp=self.input, clustering=self.clustering).fit(Xboot)

# Compute the corresponding persistence diagrams
dgm_boot = Mboot.compute_persistence_diagrams()

# Compute the bottleneck distances between them and keep the maximum
df = 0.
for i in range(len(dgm)):
npts, npts_boot = len(dgm[i]), len(dgm_boot[i])
D1 = np.array([[dgm[i][pt][1][0], dgm[i][pt][1][1]] for pt in range(npts) if dgm[i][pt][0] &lt;= 1])
D2 = np.array([[dgm_boot[i][pt][1][0], dgm_boot[i][pt][1][1]] for pt in range(npts_boot) if dgm_boot[i][pt][0] &lt;= 1])
bottle = gd.bottleneck_distance(D1, D2)
df = max(df, bottle)
distribution.append(df)

return np.sort(distribution)``````
GUDHI / gudhi-devel / src / python / example / bottleneck_basic_example.py View on Github
``````__author__ = "Francois Godi, Vincent Rouvreau"

diag1 = [[2.7, 3.7], [9.6, 14.0], [34.2, 34.974], [3.0, float("Inf")]]

diag2 = [[2.8, 4.45], [9.5, 14.1], [3.2, float("Inf")]]

message = "diag1=" + repr(diag1)
print(message)

message = "diag2=" + repr(diag2)
print(message)

message = "Bottleneck distance approximation=" + repr(
gudhi.bottleneck_distance(diag1, diag2, 0.1)
)
print(message)

message = "Bottleneck distance exact value=" + repr(
gudhi.bottleneck_distance(diag1, diag2)
)
print(message)``````
GUDHI / gudhi-devel / src / python / example / bottleneck_basic_example.py View on Github
``````diag2 = [[2.8, 4.45], [9.5, 14.1], [3.2, float("Inf")]]

message = "diag1=" + repr(diag1)
print(message)

message = "diag2=" + repr(diag2)
print(message)

message = "Bottleneck distance approximation=" + repr(
gudhi.bottleneck_distance(diag1, diag2, 0.1)
)
print(message)

message = "Bottleneck distance exact value=" + repr(
gudhi.bottleneck_distance(diag1, diag2)
)
print(message)``````
MathieuCarriere / sklearn-tda / sklearn_tda / metrics.py View on Github
``````Parameters:
X (list of n x 2 numpy arrays): input persistence diagrams.

Returns:
Xfit (numpy array of shape (number of diagrams in **diagrams**) x (number of diagrams in X)): matrix of pairwise bottleneck distances.
"""
num_diag1 = len(X)

if len(self.diagrams_) == len(X) and np.all([np.array_equal(self.diagrams_[i], X[i]) for i in range(len(X))]):
matrix = np.zeros((num_diag1, num_diag1))

if USE_GUDHI:
for i in range(num_diag1):
for j in range(i+1, num_diag1):
matrix[i,j] = bottleneck_distance(X[i], X[j], self.epsilon)
matrix[j,i] = matrix[i,j]
else:
print("Gudhi required---returning null matrix")

else:
num_diag2 = len(self.diagrams_)
matrix = np.zeros((num_diag1, num_diag2))

if USE_GUDHI:
for i in range(num_diag1):
for j in range(num_diag2):
matrix[i,j] = bottleneck_distance(X[i], self.diagrams_[j], self.epsilon)
else:
print("Gudhi required---returning null matrix")

Xfit = matrix``````
GUDHI / gudhi-devel / src / cython / example / alpha_rips_persistence_bottleneck_distance.py View on Github
``````message = "Number of simplices=" + repr(alpha_stree.num_simplices())
print(message)

alpha_diag = alpha_stree.persistence()

max_b_distance = 0.0
for dim in range(args.max_dimension):
# Alpha persistence values needs to be transform because filtration
# values are alpha square values
funcs = [math.sqrt, math.sqrt]
alpha_intervals = []
for interval in alpha_stree.persistence_intervals_in_dimension(dim):
alpha_intervals.append(map(lambda func,value: func(value), funcs, interval))

rips_intervals = rips_stree.persistence_intervals_in_dimension(dim)
bottleneck_distance = gudhi.bottleneck_distance(rips_intervals, alpha_intervals)
message = "In dimension " + repr(dim) + ", bottleneck distance = " + repr(bottleneck_distance)
print(message)
max_b_distance = max(bottleneck_distance, max_b_distance)

print("================================================================================")
message = "Bottleneck distance is " + repr(max_b_distance)
print(message)

else:
print(args.file, "is not a valid OFF file")

f.close()``````

## gudhi

The Gudhi library is an open source library for Computational Topology and Topological Data Analysis (TDA).

MIT