Last active
November 20, 2017 23:54
-
-
Save sauln/da65fa1d699c9857bb9ba845973e6745 to your computer and use it in GitHub Desktop.
This a brief (and naive) script to convert the output of the ripser library into a json format so it can be used elsewhere.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import click | |
@click.command() | |
@click.argument('infile') | |
@click.argument('outfile') | |
def parse_barcode(infile, outfile): | |
""" This function will parse the result of the Ripser (https://github.com/Ripser/ripser) barcodes | |
and output the barcodes in json format. | |
To use: | |
> ripser your_distance_matrix.mat > tmp_barcode_file.bc | |
> python parse_ripser_output.py tmp_barcode_file barcode.json | |
""" | |
print("Convert ripser output into json") | |
with open(infile, "rb") as f: | |
lines = f.readlines() | |
persistence_diagram = {} | |
# find dim N start and finish | |
for line in lines[3:]: | |
line = line.decode('utf-8') | |
if line.startswith("persistence"): | |
current_diagram = line.replace("persistence intervals in ", "").replace(":\n", "") | |
persistence_diagram[current_diagram] = [] | |
else: | |
start, finish = line.split(",") | |
start = float(start.replace(" [", "")) | |
if finish[:2] == ' )': | |
finish = 99 | |
else: | |
finish = float(finish.replace(")\n", "")) | |
persistence_diagram[current_diagram].append([start, finish]) | |
with open(outfile, 'w') as fp: | |
json.dump(persistence_diagram, fp) | |
if __name__ == "__main__": | |
parse_barcode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment