-
Notifications
You must be signed in to change notification settings - Fork 17
/
split_output.py
executable file
·86 lines (74 loc) · 2.43 KB
/
split_output.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
#
# This file is part of 'Aleph - A Library for Exploring Persistent
# Homology'. Its purpose is to split the output of some tools into
# multiple parts, using an appropriate suffix.
#
# Example:
#
# The `network_analysis` tool separates all persistence diagrams by two
# new lines. Say you calculate the persistent homology of a graph:
#
# $ network_analysis Graph.gml 5 > /tmp/Graph.txt
#
# The file `Graph.txt` will now contain 5 persistence diagrams. To split
# them up, you can call this script as follows:
#
# $ split_output --prefix=d --digits=1 /tmp/Graph.txt
#
# This will result in the following files:
#
# - Graph_d0.txt
# - Graph_d1.txt
# - Graph_d2.txt
# - Graph_d3.txt
# - Graph_d4.txt
#
# You can use any string as a prefix and specify the number of digits to
# use for the prefix generation. By default, two digits will be used, as
# this covers most use-cases. Moreover, you can specify a start value as
# an index using '--start'.
#
# Original author: Bastian Rieck
import argparse
import os
import sys
parser = argparse.ArgumentParser()
parser.add_argument('FILE')
parser.add_argument('--prefix', type=str, default="")
parser.add_argument('--digits', type=int, default=2)
parser.add_argument('--start', type=int, default=0)
arguments = parser.parse_args()
filename = arguments.FILE
class States:
Regular, FirstNewLine, SecondNewLine = range(3)
# Keeps track of the current part of the file
index = 0
prefix = arguments.prefix
digits = arguments.digits
start = arguments.start
def make_output_filename(basename, prefix, digits, index, start):
return basename \
+ "_" \
+ prefix \
+ ("%0" + str(digits) + "d") % (index+start)\
+ ".txt"
with open(filename) as f:
basename = os.path.basename(filename)
basename = os.path.splitext(basename)[0]
g = open(make_output_filename(basename, prefix, digits, index, start), "w")
state = States.Regular
for line in f:
if line == "\n":
if state == States.Regular:
state = States.FirstNewLine
elif state == States.FirstNewLine:
state = States.SecondNewLine
if state != States.SecondNewLine:
g.write(line)
else:
g.close()
index = index + 1
g = open(make_output_filename(basename, prefix, digits, index, start), "w")
state = States.Regular
g.close()