examples/alphashapes/alphashapes.py
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu Apr 16 00:23:01 2009 -0700 (3 years ago)
branchdev
changeset 131 d9e050258358
child 1811ee6edc17cb6
permissions -rw-r--r--
Alphashapes and Python bindings
* converted alphashapes2d to the new format
* added Python bindings for alphashapes
* added sample alphashapes.py that dynamically distinguishes between 2D and 3D
     1 # Computes the persistence diagram of the alpha shapes in both 2D and 3D 
     2 # (decided dynamically based on the input file)
     3 
     4 from    dionysus        import Filtration, StaticPersistence, data_dim_cmp, vertex_cmp, \
     5                                fill_alpha3D_complex, fill_alpha2D_complex, points_file
     6 from    sys             import argv, exit
     7 from    math            import sqrt
     8 
     9 
    10 if len(argv) < 2:
    11     print "Usage: %s POINTS" % argv[0]
    12     exit()
    13 
    14 points = [p for p in points_file(argv[1])]
    15 simplices = []
    16 if   len(points[0]) == 2:           # 2D
    17     fill_alpha2D_complex(points, simplices)
    18 elif len(points[1]) == 3:           # 3D
    19     fill_alpha3D_complex(points, simplices)
    20 
    21 simplices.sort(vertex_cmp)                      # Must ensure lexicographic ordering
    22 print "Total number of simplices:", len(simplices)
    23 
    24 f = Filtration(simplices, data_dim_cmp)
    25 print "Filtration initialized"
    26 
    27 p = StaticPersistence(f)
    28 print "StaticPersistence initialized" 
    29 
    30 p.pair_simplices()
    31 print "Simplices paired"
    32 
    33 print "Outputting persistence diagram"
    34 for i in p:
    35     if i.sign():
    36         b = simplices[f[p(i)]]
    37         if i == i.pair:
    38             print b.dimension(), sqrt(b.data), "inf"
    39             continue
    40 
    41         d = simplices[f[p(i.pair)]]
    42         print b.dimension(), sqrt(b.data), sqrt(d.data)