examples/cohomology/rips-pairwise-cohomology.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Sun Dec 20 07:45:29 2009 -0800 (2 years ago)
branchdev
changeset 181 1ee6edc17cb6
parent 1464e27f1f7c169
child 182451748b3c888
permissions -rw-r--r--
Python bindings work again
     1 #!/usr/bin/env python
     2 
     3 from    dionysus        import Simplex, CohomologyPersistence, points_file, PairwiseDistances, ExplicitDistances, Rips, data_dim_cmp
     4 from    sys             import argv, exit
     5 import  time
     6 
     7 def main(filename, skeleton, max, prime = 11):
     8     points = [p for p in points_file(filename)]
     9     print '#', time.asctime(), "Points read"
    10     distances = PairwiseDistances(points)
    11     distances = ExplicitDistances(distances)           # speeds up generation of the Rips complex at the expense of memory usage
    12     rips = Rips(distances)
    13     print '#', time.asctime(), "Rips initialized"
    14 
    15     simplices = []
    16     rips.generate(skeleton, max, simplices.append)
    17     print '#', time.asctime(), "Generated complex: %d simplices" % len(simplices)
    18 
    19     # While this step is unnecessary (Filtration below can be passed rips.cmp), 
    20     # it greatly speeds up the running times
    21     for s in simplices: s.data = rips.eval(s)
    22     print time.asctime(), simplices[0], '...', simplices[-1]
    23 
    24     simplices.sort(data_dim_cmp)
    25     print '#', time.asctime(), "Simplices sorted"
    26 
    27     ch = CohomologyPersistence(prime)
    28     complex = {}
    29 
    30     for s in simplices:
    31         i,d = ch.add([complex[sb] for sb in s.boundary], (s.dimension(), s.data))
    32         complex[s] = i
    33         if d: 
    34             dimension, birth = d
    35             print dimension, birth, s.data
    36         # else birth
    37 
    38     for ccl in ch:
    39         dimension, birth = ccl.birth
    40         if dimension >= skeleton: continue
    41         print dimension, birth, 'inf'         # dimension, simplex data = birth
    42         print "# Cocycle at (dim=%d, birth=%f)" % ccl.birth
    43         for e in ccl:
    44             print "#  ", e.si.order(), normalized(e.coefficient, prime)
    45 
    46 def normalized(coefficient, prime):
    47     if coefficient > prime/2:
    48         return coefficient - prime
    49     return coefficient
    50 
    51 if __name__ == '__main__':
    52     if len(argv) < 4:
    53         print "Usage: %s POINTS SKELETON MAX [PRIME=11]" % argv[0]
    54         exit()
    55 
    56     filename = argv[1]
    57     skeleton = int(argv[2])
    58     max = float(argv[3])
    59     prime = (len(argv) > 4 and argv[4]) or 11
    60 
    61     main(filename, skeleton, max, prime)