examples/rips/rips-pairwise.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu Jul 30 10:35:49 2009 -0700 (2 years ago)
branchdev
changeset 147 d39a20acb253
parent 145ee096f207dfb
child 1811ee6edc17cb6
permissions -rw-r--r--
Switched PairwiseDistances in Python bindings to C++
     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 = []
    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     f = Filtration(simplices, 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(f)
    29     print time.asctime(), "Initialized StaticPersistence"
    30 
    31     p.pair_simplices()
    32     print time.asctime(), "Simplices paired"
    33 
    34     print "Outputting persistence diagram"
    35     for i in p:
    36         if i.sign():
    37             b = simplices[f[p(i)]]
    38 
    39             if b.dimension() >= skeleton: continue
    40 
    41             if i == i.pair:
    42                 print b.dimension(), b.data, "inf"
    43                 continue
    44 
    45             d = simplices[f[p(i.pair)]]
    46             print b.dimension(), b.data, d.data
    47 
    48 if __name__ == '__main__':
    49     if len(argv) < 4:
    50         print "Usage: %s POINTS SKELETON MAX" % argv[0]
    51         exit()
    52 
    53     filename = argv[1]
    54     skeleton = int(argv[2])
    55     max = float(argv[3])
    56 
    57     main(filename, skeleton, max)