author Dmitriy Morozov <dmitriy@mrzv.org>
Fri, 06 Nov 2009 14:19:08 -0800
changeset 173 5fd3f43e6fbf
parent 142 ae2b1702c936
child 181 1ee6edc17cb6
permissions -rwxr-xr-x
Added arbitrary coefficients in boundaries (to C++ and Python) + cleaned up CohomologyPersistence.add overloading in Python

#!/usr/bin/env python

from    dionysus        import Simplex, Filtration, StaticPersistence, vertex_cmp
from    sys             import argv, exit

def max_vertex(s, vertices):
    return max((vertices[v] for v in s.vertices))

def max_vertex_cmp(s1, s2, vertices):
    m1 = max_vertex(s1, vertices)
    m2 = max_vertex(s2, vertices)
    return cmp(m1, m2) or cmp(s1.dimension(), s2.dimension())

def lsf(values_filename, simplices_filename):
    # Read vertices
    vertices = []
    with open(values_filename) as f:
        for line in f:
            if line.startswith('#'): continue

    # Read simplices
    simplices = []            
    with open(simplices_filename) as f:
        for line in f:
            if line.startswith('#'): continue
            simplices.append(map(int, line.split()))

    # Setup the complex
    complex = [Simplex(s) for s in simplices]

    # Compute persistence
    f = Filtration(complex, lambda x,y: max_vertex_cmp(x,y,vertices))
    p = StaticPersistence(f)
    # Output the persistence diagram
    for i in p:
        if not i.sign(): continue

        b = complex[f[p(i)]]
        d = complex[f[p(i.pair)]]

        if i == i.pair:
            print b.dimension(), max_vertex(b, vertices), "inf"

        print b.dimension(), max_vertex(b, vertices), max_vertex(d, vertices)

if __name__ == '__main__':
    if len(argv) < 3:
        print "Usage: %s VERTICES SIMPLICES" % argv[0]
        print "Computes persistence of the lower star filtration of the simplicial "
        print "complex explicitly listed out in SIMPLICES with vertex values given in VERTICES."

    values = argv[1]
    simplices = argv[2]

    lsf(values, simplices)