Ejemplo n.º 1
0
def featdist(g, u, v, key='label', dist_type=None):
    # compute the feat distance on graph

    if key == 'deg':
        res = abs(g.node[u][key] - g.node[v][key])

    elif key == 'dgm':
        if dist_type == 'bd':
            res = d.bottleneck_distance(g.node[u][key], g.node[v][key])
        elif dist_type == 'sw':
            tda_kernel = tda.SlicedWassersteinKernel(num_directions=10,
                                                     bandwidth=1)
            x = tda_kernel.fit(g.node[u][key])
            y = tda_kernel.transform(g.node[v][key])
            res = y[0, 0]  # get the number from (1, 1) array
        else:
            raise Exception('No such distance %s is defined.' % dist_type)

    elif key == 'label':
        v1, v2 = g.node[u][key], g.node[v][key]
        if dist_type == 'hamming':
            res = hamming(v1, v2)
        elif dist_type == 'jaccard':
            res = jaccard(v1, v2)
        else:
            raise Exception('No such distance %s is defined.' % dist_type)

    else:
        # print('All other keys are treated at vector')
        res = euclidean(g.node[u][key], g.node[v][key])

    return res
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def sw_kernel(diags1, diags2):
    """ check more kernels at
     https://github.com/MathieuCarriere/sklearn-tda/blob/master/example/ex_diagrams.py

     :param: diags1: a list of np.array (shape (n, 2))
     :param diags2: a list of np.array (shape (m, 2))
     :return a np.array of shape n * m
    """
    SW = tda.SlicedWassersteinKernel(num_directions=100, bandwidth=1.)
    X = SW.fit(diags1)
    Y = SW.transform(diags2)
    return Y.T
Ejemplo n.º 4
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]))
Ejemplo n.º 5
0
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])))