examples/rips/rips-pairwise.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Mon Jul 25 23:21:29 2011 -0700 (6 months ago)
branchdev
changeset 244 66235db8d8b7
parent 147d39a20acb253
permissions -rw-r--r--
Added cohomology/candidates counter to i/t/cohomology-persistence.hpp
     1 #/usr/bin/env python
     2 
     3 
     4 from    dionysus    import Rips, PairwiseDistances, StaticPersistence, Filtration, points_file, \
     5                            ExplicitDistances, data_dim_cmp
     6 from    sys         import argv, exit
     7 import  time
     8 
     9 def main(filename, skeleton, max):
    10     points = [p for p in points_file(filename)]
    11     distances = PairwiseDistances(points)
    12     # distances = ExplicitDistances(distances)           # speeds up generation of the Rips complex at the expense of memory usage
    13     rips = Rips(distances)
    14     print time.asctime(), "Rips initialized"
    15 
    16     simplices = Filtration()
    17     rips.generate(skeleton, max, simplices.append)
    18     print time.asctime(), "Generated complex: %d simplices" % len(simplices)
    19 
    20     # While this step is unnecessary (Filtration below can be passed rips.cmp), 
    21     # it greatly speeds up the running times
    22     for s in simplices: s.data = rips.eval(s)
    23     print time.asctime(), simplices[0], '...', simplices[-1]
    24 
    25     simplices.sort(data_dim_cmp)             # could be rips.cmp if s.data for s in simplices is not set
    26     print time.asctime(), "Set up filtration"
    27 
    28     p = StaticPersistence(simplices)
    29     print time.asctime(), "Initialized StaticPersistence"
    30 
    31     p.pair_simplices()
    32     print time.asctime(), "Simplices paired"
    33 
    34     print "Outputting persistence diagram"
    35     smap = p.make_simplex_map(simplices)
    36     for i in p:
    37         if i.sign():
    38             b = smap[i]
    39 
    40             if b.dimension() >= skeleton: continue
    41 
    42             if i.unpaired():
    43                 print b.dimension(), b.data, "inf"
    44                 continue
    45 
    46             d = smap[i.pair()]
    47             print b.dimension(), b.data, d.data
    48 
    49 if __name__ == '__main__':
    50     if len(argv) < 4:
    51         print "Usage: %s POINTS SKELETON MAX" % argv[0]
    52         exit()
    53 
    54     filename = argv[1]
    55     skeleton = int(argv[2])
    56     max = float(argv[3])
    57 
    58     main(filename, skeleton, max)