Minor changes in distances.py + rips-pairwise.py wrapped into a main() function dev
authorDmitriy Morozov <dmitriy@mrzv.org>
Fri, 24 Jul 2009 14:18:16 -0700
branchdev
changeset 145 ee096f207dfb
parent 144 baa53970eb88
child 146 4e27f1f7c169
Minor changes in distances.py + rips-pairwise.py wrapped into a main() function
bindings/python/dionysus/distances.py
examples/rips/rips-pairwise.py
--- a/bindings/python/dionysus/distances.py	Wed Jun 17 16:02:52 2009 -0700
+++ b/bindings/python/dionysus/distances.py	Fri Jul 24 14:18:16 2009 -0700
@@ -1,9 +1,7 @@
 from    math        import sqrt
 
 def l2(p):
-    return sqrt(                                        # take the square root
-            reduce(lambda x, y: x + y,                  # add them all up
-                   map(lambda x: x**2, p)))             # square each coordinate
+    return sqrt(sum((x**2 for x in p)))
 
 # Pairwise distances between the elements of `points` with respect to some `norm`
 class PairwiseDistances:
@@ -37,5 +35,6 @@
 def points_file(filename):
     fd = open(filename)
     for line in fd.xreadlines():
+        if line.startswith('#'): continue
         yield map(float, line.strip().split())
     fd.close()
--- a/examples/rips/rips-pairwise.py	Wed Jun 17 16:02:52 2009 -0700
+++ b/examples/rips/rips-pairwise.py	Fri Jul 24 14:18:16 2009 -0700
@@ -6,49 +6,52 @@
 from    sys         import argv, exit
 import  time
 
+def main(filename, skeleton, max):
+    points = [p for p in points_file(filename)]
+    distances = PairwiseDistances(points)
+    distances = ExplicitDistances(distances)           # speeds up generation of the Rips complex at the expense of memory usage
+    rips = Rips(distances)
+    print time.asctime(), "Rips initialized"
 
-if len(argv) < 4:
-    print "Usage: %s POINTS SKELETON MAX" % argv[0]
-    exit()
-
-filename = argv[1]
-skeleton = int(argv[2])
-max = float(argv[3])
+    simplices = []
+    rips.generate(skeleton, max, simplices.append)
+    print time.asctime(), "Generated complex: %d simplices" % len(simplices)
 
-points = [p for p in points_file(filename)]
-distances = PairwiseDistances(points)
-distances = ExplicitDistances(distances)           # speeds up generation of the Rips complex at the expense of memory usage
-rips = Rips(distances)
-print time.asctime(), "Rips initialized"
+    # While this step is unnecessary (Filtration below can be passed rips.cmp), 
+    # it greatly speeds up the running times
+    for s in simplices: s.data = rips.eval(s)
+    print time.asctime(), simplices[0], '...', simplices[-1]
 
-simplices = []
-rips.generate(skeleton, max, simplices.append)
-print time.asctime(), "Generated complex: %d simplices" % len(simplices)
+    f = Filtration(simplices, data_dim_cmp)             # could be rips.cmp if s.data for s in simplices is not set
+    print time.asctime(), "Set up filtration"
+
+    p = StaticPersistence(f)
+    print time.asctime(), "Initialized StaticPersistence"
+
+    p.pair_simplices()
+    print time.asctime(), "Simplices paired"
 
-# While this step is unnecessary (Filtration below can be passed rips.cmp), 
-# it greatly speeds up the running times
-for s in simplices: s.data = rips.eval(s)
-print time.asctime(), simplices[0], '...', simplices[-1]
+    print "Outputting persistence diagram"
+    for i in p:
+        if i.sign():
+            b = simplices[f[p(i)]]
 
-f = Filtration(simplices, data_dim_cmp)             # could be rips.cmp if s.data for s in simplices is not set
-print time.asctime(), "Set up filtration"
+            if b.dimension() >= skeleton: continue
 
-p = StaticPersistence(f)
-print time.asctime(), "Initialized StaticPersistence"
-
-p.pair_simplices()
-print time.asctime(), "Simplices paired"
+            if i == i.pair:
+                print b.dimension(), b.data, "inf"
+                continue
 
-print "Outputting persistence diagram"
-for i in p:
-    if i.sign():
-        b = simplices[f[p(i)]]
-
-        if b.dimension() >= skeleton: continue
+            d = simplices[f[p(i.pair)]]
+            print b.dimension(), b.data, d.data
 
-        if i == i.pair:
-            print b.dimension(), b.data, "inf"
-            continue
+if __name__ == '__main__':
+    if len(argv) < 4:
+        print "Usage: %s POINTS SKELETON MAX" % argv[0]
+        exit()
 
-        d = simplices[f[p(i.pair)]]
-        print b.dimension(), b.data, d.data
+    filename = argv[1]
+    skeleton = int(argv[2])
+    max = float(argv[3])
+
+    main(filename, skeleton, max)