examples/consistency/make-zigzag-subsamples.py
author Christos Mantoulidis <cmad@stanford.edu>
Tue, 04 Aug 2009 13:23:16 -0700
branchdev
changeset 156 f75fb57d2831
parent 142 ae2b1702c936
child 161 4bfb7c31ab28
permissions -rwxr-xr-x
Changed implementation of WeightedRips to store simplex values (max distance between simplices' vertices) as an invisible layer on top of each simplex object, so that the data() field of WeightedRips has been freed for use by the users again.

#!/usr/bin/env python 

# Creates POINTS and SUBSAMPLES files from a list of points file in a format
# suitable for rips-consistency-zigzag.


from    sys     import argv, exit


def create_subsamples(points_fn, subsamples_fn, points_list):
    points = []
    count = []
    for pfn in points_list:
        count.append(0)
        with open(pfn) as f:
            for line in f:
                if line.startswith('#'): continue
                points.append(line)
                count[-1] += 1

    with open(points_fn, 'w') as f:
        for line in points:
            f.write(line)

    cur = 0
    counts = []
    for c in count:
        counts.append(' '.join(map(str, xrange(cur, cur+c))) + '\n')
        cur += c
        counts.append(' '.join(map(str, xrange(cur-c, cur+c))) + '\n')
    
    with open(subsamples_fn, 'w') as f:
        f.writelines(counts[:-1])


if __name__ == '__main__':
    if len(argv) < 4:
        print "Usage: %s POINTS SUBSAMPLES POINTS1 [POINTS2 [POINTS3 [...]]]" % argv[0]
        print
        print "Creates a file POINTS with the union of POINTS* and SUBSAMPLES which lists"
        print "the indices of the points and their pairwise unions, one per line"
        exit()

    points_fn = argv[1]
    subsamples_fn = argv[2]
    points_list = argv[3:]
    create_subsamples(points_fn, subsamples_fn, points_list)