examples/pl-functions/lsfiltration.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu Sep 07 17:35:28 2017 -0700 (2 months ago)
changeset 290 21b56c6c9512
parent 191 2d8fba6d1d58
permissions -rwxr-xr-x
Add Vanessa Robins' periodic alpha shapes code
     1 #!/usr/bin/env python
     2 
     3 from    dionysus        import Simplex, Filtration, StaticPersistence, vertex_cmp
     4 from    sys             import argv, exit
     5 
     6 
     7 def max_vertex(s, vertices):
     8     return max((vertices[v] for v in s.vertices))
     9 
    10 def max_vertex_cmp(s1, s2, vertices):
    11     m1 = max_vertex(s1, vertices)
    12     m2 = max_vertex(s2, vertices)
    13     return cmp(m1, m2) or cmp(s1.dimension(), s2.dimension())
    14 
    15 def lsf(values_filename, simplices_filename):
    16     # Read vertices
    17     vertices = []
    18     with open(values_filename) as f:
    19         for line in f:
    20             if line.startswith('#'): continue
    21             vertices.append(float(line.split()[0]))
    22 
    23     # Read simplices
    24     fltr = Filtration()
    25     with open(simplices_filename) as f:
    26         for line in f:
    27             if line.startswith('#'): continue
    28             fltr.append(Simplex(map(int, line.split())))
    29     fltr.sort(lambda x,y: max_vertex_cmp(x,y,vertices))
    30 
    31     # Compute persistence
    32     p = StaticPersistence(fltr)
    33     p.pair_simplices()
    34     
    35     # Output the persistence diagram
    36     smap = p.make_simplex_map(fltr)
    37     for i in p:
    38         if not i.sign(): continue
    39 
    40         b = smap[i]
    41         d = smap[i.pair()]
    42 
    43         if i.unpaired():
    44             print b.dimension(), max_vertex(b, vertices), "inf"
    45             continue
    46 
    47         print b.dimension(), max_vertex(b, vertices), max_vertex(d, vertices)
    48 
    49 
    50 if __name__ == '__main__':
    51     if len(argv) < 3:
    52         print "Usage: %s VERTICES SIMPLICES" % argv[0]
    53         print 
    54         print "Computes persistence of the lower star filtration of the simplicial "
    55         print "complex explicitly listed out in SIMPLICES with vertex values given in VERTICES."
    56         exit()
    57 
    58     values = argv[1]
    59     simplices = argv[2]
    60 
    61     lsf(values, simplices)