Skip to content

Commit 81501d6

Browse files
Produce output csv with aggregate results (#150)
1 parent b9b3487 commit 81501d6

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

consensus_decentralization/analyze.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,23 @@ def analyze(projects, aggregated_data_filename, output_dir):
2121
"""
2222
logging.info('Calculating metrics on aggregated data..')
2323
metrics = hlp.get_metrics_config()
24+
metric_names = list(metrics.keys())
25+
26+
aggregate_output = {}
2427

2528
csv_contents = {}
26-
for metric in metrics.keys():
29+
for metric in metric_names:
2730
# Each metric list is of the form [['<timeframe>', '<comma-separated values for different projects']].
2831
# The special entry ['timeframe', '<comma-separated names of projects>'] is for the csv header
2932
csv_contents[metric] = [['timeframe'] + projects]
3033

3134
for column_index, project in enumerate(projects):
35+
aggregate_output[project] = {}
3236
aggregated_data_dir = output_dir / project / 'blocks_per_entity'
3337
time_chunks, blocks_per_entity = hlp.get_blocks_per_entity_from_file(aggregated_data_dir / aggregated_data_filename)
38+
for time_chunk in time_chunks:
39+
aggregate_output[project][time_chunk] = {}
40+
3441
chunks_with_blocks = set()
3542
for block_values in blocks_per_entity.values():
3643
for tchunk, nblocks in block_values.items():
@@ -51,10 +58,21 @@ def analyze(projects, aggregated_data_filename, output_dir):
5158
result = func(time_chunk_blocks_per_entity, **args_dict) if args_dict else func(
5259
time_chunk_blocks_per_entity)
5360
csv_contents[metric][row_index + 1].append(result)
61+
aggregate_output[project][time_chunk][metric] = result
5462

5563
for metric in metrics.keys():
5664
with open(output_dir / f'{metric}.csv', 'w') as f:
5765
csv_writer = csv.writer(f)
5866
csv_writer.writerows(csv_contents[metric])
5967

60-
return list(metrics.keys())
68+
aggregate_csv_output = [['ledger', 'snapshot date'] + metric_names]
69+
for project, timeframes in aggregate_output.items():
70+
for time_chunk, results in timeframes.items():
71+
aggregate_csv_output.append([project, time_chunk])
72+
for metric in metric_names:
73+
aggregate_csv_output[-1].append(results[metric])
74+
with open(output_dir / 'output.csv', 'w') as f:
75+
csv_writer = csv.writer(f)
76+
csv_writer.writerows(aggregate_csv_output)
77+
78+
return metric_names

0 commit comments

Comments
 (0)