Calculating the persistent diagram of a point cloude
import numpy as np
import sklearn as sklearn
import ripser as ripser
import persim
from persim import plot_diagrams
import tadasets
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
from scipy import sparse
import time
#Size of plots
plt.rcParams['figure.figsize'] = [14, 9]
infty_sign_clean = tadasets.infty_sign(n=500, noise=0)
plt.scatter(infty_sign_clean[:,0], infty_sign_clean[:,1], label="clean data")
plt.rcParams['figure.figsize'] = [14, 9]
plt.show()
#H_0 is:
dgm_clean_0 = ripser.ripser(infty_sign_clean)['dgms'][0]
#dgm_noisy = ripser.ripser(data_noisy)['dgms'][0]
#H_1 is:
dgm_clean_1 = ripser.ripser(infty_sign_clean)['dgms'][1]
#dgm_noisy = ripser.ripser(data_noisy)['dgms'][1]
#persim.plot_diagrams([dgm_clean, dgm_noisy] , labels=['Clean $H_1$', 'Noisy $H_1$'])
persim.plot_diagrams([dgm_clean_0, dgm_clean_1] , labels=['Infinity sign $H_0$', 'Infinity sign $H_1$'])
plt.rcParams['figure.figsize'] = [14, 9]
plt.show()
infty_sign_noise_1 = tadasets.infty_sign(n=500, noise=0)
infty_sign_noise_2 = tadasets.infty_sign(n=500, noise=0.1)
plt.scatter(infty_sign_noise_1[:,0], infty_sign_noise_1[:,1], label="clean data")
plt.scatter(infty_sign_noise_2[:,0], infty_sign_noise_2[:,1], label="clean data")
plt.show()
plt.rcParams['figure.figsize'] = [14, 9]
dgm_noise_1 = ripser.ripser(infty_sign_noise_1)['dgms'][1]
dgm_noise_2 = ripser.ripser(infty_sign_noise_2)['dgms'][1]
persim.plot_diagrams([dgm_noise_1, dgm_noise_2] , labels=['Infinity sign clean $H_1$', 'Infinity sign w/noise $H_1$'])
plt.show()
distance_bottleneck, matching = persim.bottleneck(dgm_noise_1, dgm_noise_2, matching=True)
persim.bottleneck_matching(dgm_noise_1, dgm_noise_2, matching, labels=['Infinity sign clean $H_1$', 'Infinity sign w/noise $H_1$'])
plt.show()
plt.rcParams['figure.figsize'] = [14, 9]
print("The Bottleneck distance between the two data sets is ", distance_bottleneck)
The Bottleneck distance between the two data sets is 0.344890832901001
N = 100 #The number of points
a = -100
t = np.linspace(0, 5, N)
x = np.cos(2*t)+t
#test
s = np.linspace(0, 5, N)
y = np.cos(2.1*s)+s+0.1
plt.rcParams['figure.figsize'] = [14, 9]
plt.plot(t, x)
plt.plot(s,y)
plt.title("$\\cos(2 t) + t$ (in blue)")
plt.xlabel("t")
Text(0.5, 0, 't')
# Add edges between adjacent points in the time series, with the "distance"
# along the edge equal to the max value of the points it connects
from ripser import ripser
I = np.arange(N-1)
J = np.arange(1, N)
V = np.maximum(x[0:-1], x[1::])
# Add vertex birth times along the diagonal of the distance matrix
I = np.concatenate((I, np.arange(N)))
J = np.concatenate((J, np.arange(N)))
V = np.concatenate((V, x))
#Create the sparse distance matrix
D = sparse.coo_matrix((V, (I, J)), shape=(N, N)).tocsr()
dgm0 = ripser(D, maxdim=0, distance_matrix=True)['dgms'][0]
dgm0 = dgm0[dgm0[:, 1]-dgm0[:, 0] > 1e-3, :]
#test
# Add edges between adjacent points in the time series, with the "distance"
# along the edge equal to the max value of the points it connects
I1 = np.arange(N-1)
J1 = np.arange(1, N)
V1 = np.maximum(y[0:-1], y[1::])
# Add vertex birth times along the diagonal of the distance matrix
I1 = np.concatenate((I1, np.arange(N)))
J1 = np.concatenate((J1, np.arange(N)))
V1 = np.concatenate((V1, y))
#Create the sparse distance matrix
D1 = sparse.coo_matrix((V1, (I1, J1)), shape=(N, N)).tocsr()
dgm1 = ripser(D1, maxdim=0, distance_matrix=True)['dgms'][0]
dgm1 = dgm1[dgm1[:, 1]-dgm0[:, 0] > 1e-3, :]
######
allgrid = np.unique(dgm0.flatten())
allgrid = allgrid[allgrid < np.inf]
xs = np.unique(dgm0[:, 0])
ys = np.unique(dgm0[:, 1])
ys = ys[ys < np.inf]
#Plot the time series and the persistence diagram
plt.figure(figsize=(12, 6))
ylims = [-1, 6.5]
plt.subplot(121)
plt.plot(t, x)
plt.plot(s,y)
ax = plt.gca()
ax.set_yticks(allgrid)
ax.set_xticks([])
plt.ylim(ylims)
plt.grid(linewidth=1, linestyle='--', color='b')
plt.subplot(122)
ax = plt.gca()
ax.set_yticks(ys)
ax.set_xticks(xs)
plt.ylim(ylims)
plt.grid(linewidth=1, linestyle='--',color='b')
plot_diagrams([dgm0,dgm1], size=50)
#plot_diagrams(dgm1, size=50)
plt.title("Persistence Diagram")
plt.show()
distance_bottleneck, matching = persim.bottleneck(dgm0, dgm1, matching=True)
persim.bottleneck_matching(dgm0, dgm1, matching, labels=['cos(2 t) + t $H_1$', 'cos(2.1s)+s+0.1 $H_1$'])
plt.rcParams['figure.figsize'] = [14, 9]
plt.show()
C:\Users\sigur\anaconda3\lib\site-packages\persim\bottleneck.py:56: UserWarning: dgm1 has points with non-finite death times;ignoring those points warnings.warn( C:\Users\sigur\anaconda3\lib\site-packages\persim\bottleneck.py:66: UserWarning: dgm2 has points with non-finite death times;ignoring those points warnings.warn(
print("The Bottleneck distance between the two data sets is ", distance_bottleneck)
print("Exersise: for f and g as above, vertify the Stability Theorem")
The Bottleneck distance between the two data sets is 0.11215949058532715 Exersise: for f and g as above, vertify the Stability Theorem
from sklearn import datasets
import ripser as ripser
circle_with_center = datasets.make_circles(n_samples=160, factor=0.99, noise = 0.1)[0]
circle_without_center = circle_with_center
temp = circle_with_center.tolist()
temp.append([0,0])
circle_with_center = np.array(temp)
circle = datasets.make_circles(n_samples=160, factor = 0.99, noise = 0.1)[0]
plt.scatter(circle[:,0], circle[:,1])
plt.scatter(circle_with_center[:,0], circle_with_center[:,1])
plt.show()
dgm_noise_1 = ripser.ripser(circle_with_center)['dgms'][1]
dgm_noise_2 = ripser.ripser(circle)['dgms'][1]
dgm_noise_3 = ripser.ripser(circle_without_center)['dgms'][1]
persim.plot_diagrams([dgm_noise_2, dgm_noise_1, dgm_noise_3] , labels=['Circle $H_1$', 'Circle with center $H_1$', 'Circle without center $H_1'])
plt.show()
#persim.plot_diagrams([dgm_noise_1, dgm_noise_2] , labels=['Circle $H_1$', 'Circle with center $H_1$'])
#distance_bottleneck, matching = persim.bottleneck(dgm_noise_1, dgm_noise_2, matching=True)
#persim.bottleneck_matching(dgm_noise_1, dgm_noise_2, matching, labels=['Circle $H_1$', 'Circle with center $H_1$'])
plt.rcParams['figure.figsize'] = [14, 9]
plt.show()
#print("The Bottleneck distance between the two data sets is ", distance_bottleneck)
from IPython.display import Image
Image(filename='dia0.png')
#display.Image("/Users\sigur\Desktop\TDA/dia1.png")
Image(filename='dia1.png')
#display.Image("/Users\sigur\Desktop\TDA/dia2.png"
Image(filename='dia2.png')
#display.Image("/Users\sigur\Desktop\TDA/dia1.png"
Image(filename='dia3.png')
#display.Image("/Users\sigur\Desktop\TDA/dia1.png"
Image(filename='dia4.png')
#display.Image("/Users\sigur\Desktop\TDA/dia1.png"