# HG changeset patch # User Dmitriy Morozov <dmitriy@mrzv.org> # Date 1264532451 28800 # Node ID 2d8fba6d1d5820efcc9ac16b574f699cad95f929 # Parent 6edd7c861bc00f63ef2b17f90ef42bbf292a12ae Moved lsfiltration.py into examples/pl-functions + added lscubes.py example (cubical lower-star filtration) diff -r 6edd7c861bc0 -r 2d8fba6d1d58 examples/lsfiltration.py --- a/examples/lsfiltration.py Tue Jan 12 14:36:38 2010 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#!/usr/bin/env python - -from dionysus import Simplex, Filtration, StaticPersistence, vertex_cmp -from sys import argv, exit - - -def max_vertex(s, vertices): - return max((vertices[v] for v in s.vertices)) - -def max_vertex_cmp(s1, s2, vertices): - m1 = max_vertex(s1, vertices) - m2 = max_vertex(s2, vertices) - return cmp(m1, m2) or cmp(s1.dimension(), s2.dimension()) - -def lsf(values_filename, simplices_filename): - # Read vertices - vertices = [] - with open(values_filename) as f: - for line in f: - if line.startswith('#'): continue - vertices.append(float(line.split()[1])) - - # Read simplices - fltr = Filtration() - with open(simplices_filename) as f: - for line in f: - if line.startswith('#'): continue - fltr.append(Simplex(map(int, line.split()))) - fltr.sort(lambda x,y: max_vertex_cmp(x,y,vertices)) - - # Compute persistence - p = StaticPersistence(fltr) - p.pair_simplices() - - # Output the persistence diagram - smap = p.make_simplex_map(fltr) - for i in p: - if not i.sign(): continue - - b = smap[i] - d = smap[i.pair()] - - if i.unpaired(): - print b.dimension(), max_vertex(b, vertices), "inf" - continue - - print b.dimension(), max_vertex(b, vertices), max_vertex(d, vertices) - - -if __name__ == '__main__': - if len(argv) < 3: - print "Usage: %s VERTICES SIMPLICES" % argv[0] - print - print "Computes persistence of the lower star filtration of the simplicial " - print "complex explicitly listed out in SIMPLICES with vertex values given in VERTICES." - exit() - - values = argv[1] - simplices = argv[2] - - lsf(values, simplices) diff -r 6edd7c861bc0 -r 2d8fba6d1d58 examples/pl-functions/cube.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/pl-functions/cube.py Tue Jan 26 11:00:51 2010 -0800 @@ -0,0 +1,40 @@ +def log2(x): + i = 0 + while x: + x >>= 1 + i += 1 + return i-1 + +class Cube: + def __init__(self, vertices): + self.vertices = vertices + + def dimension(self): + return log2(len(self.vertices)) + + def boundary(self): + for i in xrange(self.dimension()): + for side in [0,1]: + vertices = [] + for idx in xrange(len(self.vertices)/2): + # Insert i-th bit equal to side + v = (idx & ~(2**i-1)) << 1 + if side: v |= 2**i + v |= (idx & (2**i - 1)) + vertices.append(self.vertices[v]) + yield Cube(vertices) + + def __hash__(self): + return hash(tuple(self.vertices)) + + def __eq__(self, other): + return self.vertices == other.vertices + + def __repr__(self): + return " ".join(map(str, self.vertices)) + + +if __name__ == '__main__': + c = Cube(['a', 'b', 'c', 'd']) + print c + for sb in c.boundary(): print sb diff -r 6edd7c861bc0 -r 2d8fba6d1d58 examples/pl-functions/lscubes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/pl-functions/lscubes.py Tue Jan 26 11:00:51 2010 -0800 @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +from dionysus import CohomologyPersistence +from cube import Cube +from sys import argv, exit + + +def max_vertex(s, vertices): + return max((vertices[v] for v in s.vertices)) + +def max_vertex_cmp(s1, s2, vertices): + m1 = max_vertex(s1, vertices) + m2 = max_vertex(s2, vertices) + return cmp(m1, m2) or cmp(s1.dimension(), s2.dimension()) + +def lsf(values_filename, cubes_filename, prime = 11): + # Read vertices + vertices = [] + with open(values_filename) as f: + for line in f: + if line.startswith('#'): continue + vertices.append(float(line.split()[0])) + + # Read cubes + fltr = [] + with open(cubes_filename) as f: + for line in f: + if line.startswith('#'): continue + fltr.append(Cube(map(int, line.split()))) + fltr.sort(lambda x,y: max_vertex_cmp(x,y,vertices)) + for i,c in enumerate(fltr): c.data = i + + ch = CohomologyPersistence(prime) + complex = {} + + for c in fltr: + # print "%s: %s" % (c, " + ".join(map(str, c.boundary()))) + # print complex + i,d = ch.add([complex[cb] for cb in c.boundary()], c.data) + complex[c] = i + if d: + birth = d + print c.dimension() - 1, max_vertex(fltr[birth], vertices), max_vertex(c, vertices) + +if __name__ == '__main__': + if len(argv) < 3: + print "Usage: %s VERTICES CUBES" % argv[0] + print + print "Computes persistence of the lower star filtration of the cubical " + print "complex explicitly listed out in CUBES with vertex values given in VERTICES." + exit() + + values = argv[1] + cubes = argv[2] + + lsf(values, cubes) diff -r 6edd7c861bc0 -r 2d8fba6d1d58 examples/pl-functions/lsfiltration.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/pl-functions/lsfiltration.py Tue Jan 26 11:00:51 2010 -0800 @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +from dionysus import Simplex, Filtration, StaticPersistence, vertex_cmp +from sys import argv, exit + + +def max_vertex(s, vertices): + return max((vertices[v] for v in s.vertices)) + +def max_vertex_cmp(s1, s2, vertices): + m1 = max_vertex(s1, vertices) + m2 = max_vertex(s2, vertices) + return cmp(m1, m2) or cmp(s1.dimension(), s2.dimension()) + +def lsf(values_filename, simplices_filename): + # Read vertices + vertices = [] + with open(values_filename) as f: + for line in f: + if line.startswith('#'): continue + vertices.append(float(line.split()[0])) + + # Read simplices + fltr = Filtration() + with open(simplices_filename) as f: + for line in f: + if line.startswith('#'): continue + fltr.append(Simplex(map(int, line.split()))) + fltr.sort(lambda x,y: max_vertex_cmp(x,y,vertices)) + + # Compute persistence + p = StaticPersistence(fltr) + p.pair_simplices() + + # Output the persistence diagram + smap = p.make_simplex_map(fltr) + for i in p: + if not i.sign(): continue + + b = smap[i] + d = smap[i.pair()] + + if i.unpaired(): + print b.dimension(), max_vertex(b, vertices), "inf" + continue + + print b.dimension(), max_vertex(b, vertices), max_vertex(d, vertices) + + +if __name__ == '__main__': + if len(argv) < 3: + print "Usage: %s VERTICES SIMPLICES" % argv[0] + print + print "Computes persistence of the lower star filtration of the simplicial " + print "complex explicitly listed out in SIMPLICES with vertex values given in VERTICES." + exit() + + values = argv[1] + simplices = argv[2] + + lsf(values, simplices)