Skip to content

Commit bae916f

Browse files
committed
Store data in lists of dicts and use csv package
Output format is unchanged (except that csv.DictWriter insists on writing ints as 0.0 instead of 0)
1 parent f954c6b commit bae916f

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

tools/extra/parse_log.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
import extract_seconds
1212
import argparse
13+
import csv
1314

1415

1516
def get_line_type(line):
@@ -26,6 +27,13 @@ def get_line_type(line):
2627

2728
def parse_log(path_to_log):
2829
"""Parse log file
30+
Returns (train_dict_list, train_dict_names, test_dict_list, test_dict_names)
31+
32+
train_dict_list and test_dict_list are lists of dicts that define the table
33+
rows
34+
35+
train_dict_names and test_dict_names are ordered tuples of the column names
36+
for the two dict_lists
2937
"""
3038

3139
re_iteration = re.compile('Iteration (\d+)')
@@ -38,8 +46,10 @@ def parse_log(path_to_log):
3846
iteration = -1
3947
test_accuracy = -1
4048
learning_rate = float('NaN')
41-
train_list = []
42-
test_list = []
49+
train_dict_list = []
50+
test_dict_list = []
51+
train_dict_names = ('NumIters', 'Seconds', 'TrainingLoss', 'LearningRate')
52+
test_dict_names = ('NumIters', 'Seconds', 'TestAccuracy', 'TestLoss')
4353

4454
logfile_year = extract_seconds.get_log_created_year(path_to_log)
4555
with open(path_to_log) as f:
@@ -68,8 +78,10 @@ def parse_log(path_to_log):
6878
train_loss_match = re_train_loss.search(line)
6979
if train_loss_match:
7080
train_loss = float(train_loss_match.group(1))
71-
train_list.append((iteration, seconds, train_loss,
72-
learning_rate))
81+
train_dict_list.append({'NumIters': iteration,
82+
'Seconds': seconds,
83+
'TrainingLoss': train_loss,
84+
'LearningRate': learning_rate})
7385

7486
output_loss_match = re_output_loss.search(line)
7587
if output_loss_match and get_line_type(line) == 'test':
@@ -78,13 +90,16 @@ def parse_log(path_to_log):
7890
# loss for test data so the test_accuracy variable is already
7991
# correctly populated and (2) there's one and only one output
8092
# named "accuracy" for the test net
81-
test_list.append((iteration, seconds, test_accuracy, test_loss))
93+
test_dict_list.append({'NumIters': iteration,
94+
'Seconds': seconds,
95+
'TestAccuracy': test_accuracy,
96+
'TestLoss': test_loss})
8297

83-
return train_list, test_list
98+
return train_dict_list, train_dict_names, test_dict_list, test_dict_names
8499

85100

86-
def save_csv_files(logfile_path, output_dir, train_list, test_list,
87-
verbose=False):
101+
def save_csv_files(logfile_path, output_dir, train_dict_list, train_dict_names,
102+
test_dict_list, test_dict_names, verbose=False):
88103
"""Save CSV files to output_dir
89104
90105
If the input log file is, e.g., caffe.INFO, the names will be
@@ -93,23 +108,20 @@ def save_csv_files(logfile_path, output_dir, train_list, test_list,
93108

94109
log_basename = os.path.basename(logfile_path)
95110
train_filename = os.path.join(output_dir, log_basename + '.train')
96-
write_csv(train_filename, train_list, '%d,%f,%f,%f',
97-
'NumIters,Seconds,TrainingLoss,LearningRate', verbose)
111+
write_csv(train_filename, train_dict_list, train_dict_names, verbose)
98112

99113
test_filename = os.path.join(output_dir, log_basename + '.test')
100-
write_csv(test_filename, test_list, '%d,%f,%f,%f',
101-
'NumIters,Seconds,TestAccuracy,TestLoss', verbose)
114+
write_csv(test_filename, test_dict_list, test_dict_names, verbose)
102115

103116

104-
def write_csv(output_filename, list_of_tuples, format_string, header,
105-
verbose=False):
117+
def write_csv(output_filename, dict_list, header_names, verbose=False):
106118
"""Write a CSV file
107119
"""
120+
108121
with open(output_filename, 'w') as f:
109-
f.write(header + '\n')
110-
for row in list_of_tuples:
111-
line = format_string % row
112-
f.write(line + '\n')
122+
dict_writer = csv.DictWriter(f, header_names)
123+
dict_writer.writeheader()
124+
dict_writer.writerows(dict_list)
113125
if verbose:
114126
print 'Wrote %s' % output_filename
115127

@@ -135,8 +147,10 @@ def parse_args():
135147

136148
def main():
137149
args = parse_args()
138-
train_list, test_list = parse_log(args.logfile_path)
139-
save_csv_files(args.logfile_path, args.output_dir, train_list, test_list)
150+
train_dict_list, train_dict_names, test_dict_list, test_dict_names = \
151+
parse_log(args.logfile_path)
152+
save_csv_files(args.logfile_path, args.output_dir, train_dict_list,
153+
train_dict_names, test_dict_list, test_dict_names)
140154

141155

142156
if __name__ == '__main__':

0 commit comments

Comments
 (0)