예제 #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
예제 #2
0
파일: dist.py 프로젝트: Chen-Cai-OSU/Esme
def bd_distance(dgms):
    n = len(dgms)
    m = np.zeros((n, n))
    for i in range(n):
        for j in range(i, n):
            m[i][j] = d.bottleneck_distance(dgms[i], dgms[j])
    return m + m.T
def compute_bottleneck_distance(all_seeds_rips_files,
                                remove_infinity=False,
                                compute_wass_distance=False,
                                use_persim=False,
                                M=10):
    matrix = []
    x = []
    y = []
    for file1 in all_seeds_rips_files:
        print('Computing file: {}'.format(file1))
        row = np.zeros(len(all_seeds_rips_files))
        # example file1: LTHT/remote_data/saves/alexnet_nmp/mnist/42/pickle/8.pickle
        split1_name = file1.split('/')
        # print(split1_name)
        seed, model_name, dataset, file1_name = split1_name[-5], split1_name[
            -7], split1_name[-6], split1_name[-1]
        # appending 'alexnet_nmp-mnist-42-8'
        x.append(model_name + "-" + dataset + "-" + seed + "-" +
                 file1_name.split(".")[0])

        rips1 = pickle.load(open(file1, 'rb'))
        if remove_infinity:
            l1 = list(rips1['dgms'][0][rips1['dgms'][0][:, 1] < np.inf])
        else:
            l1 = list(rips1['dgms'][0])
        d1 = dion.Diagram(l1)

        for i, file2 in enumerate(all_seeds_rips_files):
            rips2 = pickle.load(open(file2, 'rb'))

            if remove_infinity:
                l2 = list(rips2['dgms'][0][rips2['dgms'][0][:, 1] < np.inf])
            else:
                l2 = list(rips2['dgms'][0])

            d2 = dion.Diagram(l2)

            if compute_wass_distance:
                if use_persim:
                    wdist = persim.sliced_wasserstein_kernel(d1, d2, M=M)
                else:
                    wdist = dion.wasserstein_distance(d1, d2, q=2)
                row[i] = wdist
            else:
                if use_persim:
                    bdist = persim.bottleneck(d1, d2)
                else:
                    bdist = dion.bottleneck_distance(d1, d2)
                row[i] = bdist

        matrix.append(row)
    #
    x = list(
        map(
            lambda y: '{}-{} seed:{}-{}'.format(
                y.split('-')[0],
                y.split('-')[1],
                y.split('-')[2],
                y.split('-')[3]), x))
    return matrix, x
예제 #4
0
def compare_diagrams(n=16, negate=False, n_threads=4, seed=1, top_dim=2):
    # no wrap in Dionysus
    wrap = False

    # generate random grid data
    np.random.seed(seed)
    a = np.random.randn(n**3).reshape((n, n, n))

    # compute diagrams with Oineus
    oin_dgms = oin.compute_diagrams_ls(a, negate, wrap, top_dim, n_threads)

    # compute diagrams with Dionysis
    fil_us = dion.fill_freudenthal(a, reverse=negate)
    p = dion.homology_persistence(fil_us)
    dion_dgms = dion.init_diagrams(p, fil_us)

    dist = 0.0

    for dim in range(top_dim):
        # convert Oineus diagram to Dionysus format
        oin_dgm = dion.Diagram(oin_dgms[dim])
        dion_dgm = dion_dgms[dim]
        dist += dion.bottleneck_distance(oin_dgm, dion_dgm)

    print("total dist: ", dist)
    assert (dist < 0.001)
예제 #5
0
def bneck_mat(ds):
    n = len(ds)
    Ds = [remove_inf(d) for d in ds]
    dmat = np.zeros((n, n), dtype=float)
    for i, j in tqdm(list(combinations(range(n), 2)), '[ bottleneck'):
        d = max(dio.bottleneck_distance(a, b) for a, b in zip(Ds[i], Ds[j]))
        dmat[i, j] = dmat[j, i] = d
    return dmat
예제 #6
0
def bad_example():
    import dionysus as d
    dgm1 = d.Diagram([(1, 2.07464)])
    dgm1 = d.Diagram([(1, 2.04287)])
    dgm2 = d.Diagram([(1, 1.68001), (1, 1.68001), (1, 1.68001)])  # this one doesn't work
    dgm2 = d.Diagram([(1, 1.71035)])
    # dgm2 = d.Diagram([(1,1.68), (1,1.68), (1,1.68)]) # But this one works
    print((d.bottleneck_distance(dgm1, dgm2)))
    print((d.bottleneck_distance_with_edge(dgm1, dgm2)))
예제 #7
0
def bottleneck_distance(dia1, dia2):
    return dionysus.bottleneck_distance(dia1, dia2)
예제 #8
0
    for key, value in simplices.items():
        simplex = list(map(int, key))
        filtlist.append(simplex)
        times.append(value)

    filt = d.Filtration(filtlist)
    zz, dgms, cells = d.zigzag_homology_persistence(filt, times)

    #if you want to print the barcodes in a given dimension:
    #for i in dgms[0]:
    #print(i)

    return dgms


if __name__ == "__main__":
    filename1 = sys.argv[-2]
    filename2 = sys.argv[-1]

    dgms1 = compute_barcodes(filename1)
    dgms2 = compute_barcodes(filename2)

    bdist0 = d.bottleneck_distance(dgms1[0], dgms2[0])
    bdist1 = d.bottleneck_distance(dgms1[1], dgms2[1])

    print("0-dimensional bottleneck distance is:")
    print(bdist0)
    print("1-dimensional bottleneck distance is:")
    print(bdist1)
예제 #9
0
#alpha filtration TDA
test_comp = circ_alpha2[0]
test_bts = circ_alpha2[1]
test_f = d.Filtration()
for j in range(len(test_comp)):
    test_f.append(d.Simplex(test_comp[j],test_bts[j]))
p = d.homology_persistence(test_f)
dgms2 = d.init_diagrams(p, test_f)

#scatters
d.plot.plot_diagram(dgms2[0])
d.plot.plot_diagram(dgms2[1])
plt.show()

d.bottleneck_distance(dgms1[0], dgms2[0])

#get the nearest neighbors for the perturbed points
nn1 = np.sort([np.linalg.norm(pt - circ_data[-1]) for pt in circ_data])
nn2 = np.sort([np.linalg.norm(pt - circ_data2[-1]) for pt in circ_data2])

#plot the alpha complex for circ_data1
last_index = 1
pts = circ_data
bts = circ_alpha1[1][:-last_index]
current_simps = circ_alpha1[0][:-last_index]

fig = plt.figure()
ax = fig.add_subplot(111)

ax.set_xlim(-1.1,1.1)
예제 #10
0
파일: dist.py 프로젝트: Chen-Cai-OSU/Esme
 def bd_dist(self):
     return precision_format(d.bottleneck_distance(self.dgm1, self.dgm2))
예제 #11
0
        #######Thresholding the features on the persistent diagrams describing persistent cohomology of kPCA-VR complex(kPCA)
        #############Here we use the same thresholding for fairness. But this configuration is easy to change.
        PCA_thres = thres
        #############
        dgms_k_tmp = denoise_dgm(dgms_k, PCA_thres)  #thresholding CC diagrams
        dgms_k_0 = dionysus._dionysus.Diagram(
        )  #take persistent points of dim 0
        dgms_k_1 = dionysus._dionysus.Diagram(
        )  #take persistent points of dim 1
        #pybind issue, need to be correct instead of succint.
        for itr in range(len(dgms_k_tmp[0])):
            dgms_k_0.append(dgms_k_tmp[0][itr])
        for itr in range(len(dgms_k_tmp[1])):
            dgms_k_1.append(dgms_k_tmp[1][itr])
        #print(type(dgms_base[0]))
        #dionysus.bottleneck_distance(dgms_base_0, dgms_base_1)
        #######
        bdist_0 = dionysus.bottleneck_distance(dgms_base_0, dgms_k_0)
        bdist_1 = dionysus.bottleneck_distance(dgms_base_1, dgms_k_1)
        #dionysus.plot.plot_diagram(dgms_k[1], show=True)
        newrow = [k + 1, thres, bdist_0, bdist_1]
        print(newrow)
        A = np.vstack([A, newrow])
print(A)
#Column names of A
#1 number of principal components k;
#2 threshold used for filtering out topological features by persistence;
#3 Bottleneck distance between dimension 0 diagrams of CC and PCA
#3 Bottleneck distance between dimension 1 diagrams of CC and PCA
np.savetxt('ExamplePCA_gauss_100.txt', A)
예제 #12
0
def test_distance():
    dgm1 = d.Diagram([(1,2), (3,4), (1., float('inf'))])
    dgm2 = d.Diagram([(0,2), (3,5), (2., float('inf'))])
    dist = d.bottleneck_distance(dgm1,dgm2)
    assert dist == 1
예제 #13
0
def bottleneck_distance(barcode1, barcode2, delta=0.01):
    dg1, dg2 = barcode_to_diagram(barcode1), barcode_to_diagram(barcode2)
    return di.bottleneck_distance(dg1, dg2, delta)
예제 #14
0
def bottleneck_distance(dia1, dia2):
    return dionysus.bottleneck_distance(dia1, dia2)