def test_inf_deathtime(self): dgm = np.array([[1, 2]]) empty = np.array([[0, np.inf]]) with pytest.warns(UserWarning, match="dgm1 has points with non-finite death") as w: dist1 = wasserstein(empty, dgm) with pytest.warns(UserWarning, match="dgm2 has points with non-finite death") as w: dist2 = wasserstein(dgm, empty) assert (np.allclose(dist1, np.sqrt(2) / 2)) and (np.allclose( dist2, np.sqrt(2) / 2))
def test_single(self): d = wasserstein( np.array([[0.5, 1]]), np.array([[0.5, 1.1]]) ) # These are very loose bounds assert d == pytest.approx(0.1, 0.001)
def test_matching_to_self(self): # Matching a diagram to itself should yield 0 pd = np.array([[0. , 1.71858561], [0. , 1.74160683], [0. , 2.43430877], [0. , 2.56949258]]) dist = wasserstein(pd, pd) assert dist == 0
def mainp(queue, res_queue, df, rfs): print("thread initiated") while not queue.empty(): try: i = queue.get(True, 15) res_queue.put((i, df[0].apply(lambda x: wasserstein(x, rfs[i])))) # f = np.vectorize(lambda x:wasserstein(x, rfs[i])) # res_queue.put((i,f(rfs))) except Exception as err: print("process down") break if queue.empty(): quit() break
def test_matching(self): dgm1 = np.array([[0.5, 1], [0.6, 1.1]]) dgm2 = np.array([ [0.5, 1.1], [0.6, 1.1], [0.8, 1.1], [1.0, 1.1], ]) d, m = wasserstein(dgm1, dgm2, matching=True) u1 = np.unique(m[:, 0]) u1 = u1[u1 >= 0] u2 = np.unique(m[:, 1]) u2 = u2[u2 >= 0] assert u1.size == dgm1.shape[0] and u2.size == dgm2.shape[0]
print "\tSize:", curData.shape str(bn) + "," + str(h) + "," + str(ws) + "," #if(a == 0) # for i in range(0,d0_count): # outdata = np.vstack([outdata, [0,0,epsilon]]) if (len(curData.data) > 0): print "Metric", len(curData), a bn = persim.bottleneck(comparePers, curData, matching=False) h = persim.heat(comparePers, curData) ws = persim.wasserstein(comparePers, curData) outString += str(bn) + "," + str(h) + "," + str(ws) + "," end = time.time() stat_time = (end - start) outfile = file(os.getcwd() + "/aggResults.csv", 'a') outfile.write(outString + str(stat_time) + ",") outfile.close() else: print "ripser processing failed" ''' LHF configuration and execution ''' if (tdaType == "LHF"): #try: start = time.time()
def test_one_empty(self): dgm1 = np.array([[1, 2]]) empty = np.array([]) dist = wasserstein(dgm1, empty) assert np.allclose(dist, np.sqrt(2) / 2)
def test_single_point_same(self): dgm = np.array([[0.11371516, 4.45734882]]) dist = wasserstein(dgm, dgm) assert dist == 0
def match_wasserstein(dgms1, dgms2): return wasserstein(dgms1[1], dgms2[1], matching=True)
try: upscalePers = np.genfromtxt(outDir + "/upscaledPersistence.csv", delimiter=',') except: upscalePers = np.genfromtxt(outDir + "/ripser_Output.csv", delimiter=',') start = time.time() print "Computing bottlenecks..." bn = persim.bottleneck(originalPers, comparePers, matching=False) bn_u = persim.bottleneck(originalPers, upscalePers, matching=False) print "Computing heat kernel distance..." h = persim.heat(originalPers, comparePers) h_u = persim.heat(originalPers, upscalePers) print "Computing wasserstein..." ws = persim.wasserstein(originalPers, comparePers) ws_u = persim.wasserstein(originalPers, upscalePers) end = time.time() #gh = persim.gromov_hausdorff(originalPers, comparePers) #gh_u = persim.gromov_hausdorff(originalPers, upscalePers) print bn, bn_u, h, h_u, ws, ws_u stat_time = (end - start) with open("upscaleStats.csv", 'a') as f: f.write(SourcePers + "," + outDir + ",".join(str(x) for x in [bn, bn_u, h, h_u, ws, ws_u, stat_time]) + "\n")
def test_repeated(self): dgm1 = np.array([[0, 10], [0, 10]]) dgm2 = np.array([[0, 10]]) dist = wasserstein(dgm1, dgm2) assert dist == 5 * np.sqrt(2)