summaryrefslogtreecommitdiff
path: root/debian/tests/examples.py
blob: a5a4836fba25292ffbf2d561bdb052aea497fb2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python3

import numpy as np
import subprocess

def parse_output(s):
    ret = []
    current_dim = None
    for line in s.splitlines():
        if line.startswith("persistence intervals in dim"):
            splitline = line.rstrip().rstrip(":").split(" ")
            assert(len(splitline) == 5)
            current_dim = int(splitline[-1])
            while len(ret) < current_dim + 1:
                ret.append(([], []))
        elif line.startswith(" ["):
            splitline = line.lstrip(" [").rstrip().rstrip(")").split(",")
            assert(len(splitline) == 2)
            if splitline[1].strip() == "":
                ret[current_dim][1].append(float(splitline[0]))
            else:
                ret[current_dim][0].append((float(splitline[0]), float(splitline[1])))

    return ret

def main():
    tests = [(None, "lower-distance", 2, "sphere_3_192.lower_distance_matrix"),
             (None, "distance", 2, "projective_plane.csv"),
             (None, "dipha", 2, "projective_plane.dipha"),
             (None, "lower-distance", 2, "projective_plane.lower_distance_matrix"),
             (2, "lower-distance", 2, "sphere_3_192.lower_distance_matrix"),
             (2, "distance", 2, "projective_plane.csv"),
             (2, "dipha", 2, "projective_plane.dipha"),
             (2, "lower-distance", 2, "projective_plane.lower_distance_matrix")]
             
    for (coeff, format, dim, prefix) in tests:
        if coeff is None:
            print("Running test %s." %(prefix))
            proc = subprocess.Popen(["/usr/bin/ripser", "--dim", str(dim), "--format", format, "examples/%s" %(prefix)], stdout=subprocess.PIPE)
        else:
            print("Running test %s with coefficients in Z/%dZ." %(prefix, coeff))
            proc = subprocess.Popen(["/usr/bin/ripser-coeff", "--modulus", str(coeff), "--dim", str(dim), "--format", format, "examples/%s" %(prefix)], stdout=subprocess.PIPE)
            
        output = proc.communicate()[0].decode("utf-8")
        assert(proc.returncode == 0)
        pd = parse_output(output)

        with open("debian/tests/reference/%s.txt" %(prefix), "r") as f:
            pd_ref = parse_output(f.read())

        assert(len(pd) == len(pd_ref))

        for d in range(0, len(pd)):
            fin = np.array(sorted(pd[d][0]))
            inf = np.array(sorted(pd[d][1]))
            print("Finite bars in dimension %d:" %(d))
            print(fin)
            print("Infinite bars in dimension %d:" %(d))
            print(inf)
            
            fin_ref = np.array(sorted(pd_ref[d][0]))
            inf_ref = np.array(sorted(pd_ref[d][1]))

            assert(fin.shape == fin_ref.shape)
            assert(inf.shape == inf_ref.shape)

            np.testing.assert_allclose(fin, fin_ref)
            np.testing.assert_allclose(inf, inf_ref)

            print("------")

        print("-------------------------------------------------")
        

if __name__ == "__main__":
    main()