def sw(dgms1, dgms2, kernel_type='sw', parallel_flag=False, **featkwargs): """ :param dgms1: numpy array :param dgms2: numpy array :param parallel_flag: :param kernel_type: :param featkwargs: when kernel_type is sw, kwargs has n_d, bw when kernel_type is pss, kwargs has bw when kernel_type is wg, kwargs has bw, K,p :return: a computed kernel """ def arctan(C, p): return lambda x: C * np.arctan(np.power(x[1], p)) import signal def signal_handler(signum, frame): raise Exception("Timed out!") signal.signal(signal.SIGALRM, signal_handler) time_limit = max(int(len(dgms1) * len(dgms2) * 0.2), 100) signal.alarm(time_limit) if parallel_flag == False: if kernel_type == 'sw': assert_names(['n_directions', 'bw'], featkwargs) tda_kernel = tda.SlicedWassersteinKernel( num_directions=featkwargs['n_directions'], bandwidth=featkwargs['bw']) elif kernel_type == 'pss': assert_names(['bw'], featkwargs) tda_kernel = tda.PersistenceScaleSpaceKernel( bandwidth=featkwargs['bw']) elif kernel_type == 'wg': assert_names(['bw', 'K', 'p'], featkwargs) tda_kernel = tda.PersistenceWeightedGaussianKernel( bandwidth=featkwargs['bw'], weight=arctan(featkwargs['K'], featkwargs['p'])) elif kernel_type == 'pf': assert_names(['bw', 'bwf'], featkwargs) # try: tda_kernel = tda.PersistenceFisherKernel( bandwidth_fisher=featkwargs['bwf'], bandwidth=featkwargs['bw']) # except RuntimeWarning: # tda_kernel = 0 # print('RuntimeWarning catched') # print(f'dgm1', dgms1) # print(f'dgm2', dgms2) else: print('Unknown kernel for function sw') diags1 = dgms1 diags2 = dgms2 X = tda_kernel.fit(diags1) Y = tda_kernel.transform(diags2) Y = np.nan_to_num(Y) # todo wait for mathieu's reply return Y
plt.plot([0.0, 10.0], [0.0, 10.0]) plt.show() diags2 = [D] SW = tda.SlicedWassersteinKernel(num_directions=10, bandwidth=1.0) X = SW.fit(diags) Y = SW.transform(diags2) print("SW kernel is " + str(Y[0][0])) PWG = tda.PersistenceWeightedGaussianKernel(bandwidth=1.0, weight=arctan(1.0, 1.0)) X = PWG.fit(diags) Y = PWG.transform(diags2) print("PWG kernel is " + str(Y[0][0])) PSS = tda.PersistenceScaleSpaceKernel(bandwidth=1.0) X = PSS.fit(diags) Y = PSS.transform(diags2) print("PSS kernel is " + str(Y[0][0])) W = tda.WassersteinDistance(wasserstein=1, delta=0.001) X = W.fit(diags) Y = W.transform(diags2) print("Wasserstein-1 distance is " + str(Y[0][0])) sW = tda.SlicedWassersteinDistance(num_directions=10) X = sW.fit(diags) Y = sW.transform(diags2) print("sliced Wasserstein distance is " + str(Y[0][0]))
diags2 = [D] def arctan(C,p): return lambda x: C*np.arctan(np.power(x[1], p)) PWG = tda.PersistenceWeightedGaussianKernel(bandwidth=1., kernel_approx=None, weight=arctan(1.,1.)) X = PWG.fit(diags) Y = PWG.transform(diags2) print("PWG kernel is " + str(Y[0][0])) PWG = tda.PersistenceWeightedGaussianKernel(kernel_approx=RBFSampler(gamma=1./2, n_components=100000).fit(np.ones([1,2])), weight=arctan(1.,1.)) X = PWG.fit(diags) Y = PWG.transform(diags2) print("Approximate PWG kernel is " + str(Y[0][0])) PSS = tda.PersistenceScaleSpaceKernel(bandwidth=1.) X = PSS.fit(diags) Y = PSS.transform(diags2) print("PSS kernel is " + str(Y[0][0])) PSS = tda.PersistenceScaleSpaceKernel(kernel_approx=RBFSampler(gamma=1./2, n_components=100000).fit(np.ones([1,2]))) X = PSS.fit(diags) Y = PSS.transform(diags2) print("Approximate PSS kernel is " + str(Y[0][0])) sW = tda.SlicedWassersteinDistance(num_directions=100) X = sW.fit(diags) Y = sW.transform(diags2) print("SW distance is " + str(Y[0][0])) SW = tda.SlicedWassersteinKernel(num_directions=100, bandwidth=1.)
def persistence_scale_kernel(X, Y, PSS_kernel=None): if PSS_kernel is None: PSS_kernel = tda.PersistenceScaleSpaceKernel(bandwidth=1.) X_fit = PSS_kernel.fit([np.asarray(X)]) Y_fit = PSS_kernel.transform([np.asarray(Y)]) return Y_fit[0][0], PSS_kernel
def sklearn_tda(): def arctan(C, p): return lambda x: C * np.arctan(np.power(x[1], p)) D = np.array([[0.0, 4.0], [1.0, 2.0], [3.0, 8.0], [6.0, 8.0]]) plt.scatter(D[:, 0], D[:, 1]) plt.plot([0.0, 10.0], [0.0, 10.0]) plt.show() diags = [D] LS = tda.Landscape(resolution=1000) L = LS.fit_transform(diags) plt.plot(L[0][:1000]) plt.plot(L[0][1000:2000]) plt.plot(L[0][2000:3000]) plt.show() SH = tda.Silhouette(resolution=1000, weight=lambda x: np.power(x[1] - x[0], 5)) S = SH.fit_transform(diags) plt.plot(S[0]) plt.show() BC = tda.BettiCurve(resolution=1000) B = BC.fit_transform(diags) plt.plot(B[0]) plt.show() diagsT = tda.DiagramPreprocessor(use=True, scaler=tda.BirthPersistenceTransform()).fit_transform(diags) PI = tda.PersistenceImage(bandwidth=1.0, weight=arctan(1.0, 1.0), im_range=[0, 10, 0, 10], resolution=[100, 100]) I = PI.fit_transform(diagsT) plt.imshow(np.flip(np.reshape(I[0], [100, 100]), 0)) plt.show() plt.scatter(D[:, 0], D[:, 1]) D = np.array([[1.0, 5.0], [3.0, 6.0], [2.0, 7.0]]) plt.scatter(D[:, 0], D[:, 1]) plt.plot([0.0, 10.0], [0.0, 10.0]) plt.show() diags2 = [D] SW = tda.SlicedWassersteinKernel(num_directions=10, bandwidth=1.0) X = SW.fit(diags) Y = SW.transform(diags2) print(("SW kernel is " + str(Y[0][0]))) PWG = tda.PersistenceWeightedGaussianKernel(bandwidth=1.0, weight=arctan(1.0, 1.0)) X = PWG.fit(diags) Y = PWG.transform(diags2) print(("PWG kernel is " + str(Y[0][0]))) PSS = tda.PersistenceScaleSpaceKernel(bandwidth=1.0) X = PSS.fit(diags) Y = PSS.transform(diags2) print(("PSS kernel is " + str(Y[0][0]))) W = tda.WassersteinDistance(wasserstein=1, delta=0.001) X = W.fit(diags) Y = W.transform(diags2) print(("Wasserstein-1 distance is " + str(Y[0][0]))) sW = tda.SlicedWassersteinDistance(num_directions=10) X = sW.fit(diags) Y = sW.transform(diags2) print(("sliced Wasserstein distance is " + str(Y[0][0])))