| 19 | |
| 20 | |
| 21 | def prettify_json(args): |
| 22 | with open(args.file) as fd: |
| 23 | json_dict = json.load(fd) |
| 24 | |
| 25 | if not json_dict: |
| 26 | print('[Error] The file [{}] could not be parsed.'.format(args.file)) |
| 27 | return -1 |
| 28 | |
| 29 | progress = dictor(json_dict, '_checkpoint.progress') |
| 30 | records_table = dictor(json_dict, '_checkpoint.records') |
| 31 | sensors = dictor(json_dict, 'sensors') |
| 32 | labels_scores = dictor(json_dict, 'labels') |
| 33 | scores = dictor(json_dict, 'values') |
| 34 | |
| 35 | # compose output |
| 36 | output = "" |
| 37 | |
| 38 | if progress: |
| 39 | output += '* {}% ({}/{}) routes completed\n'.format(100.0*progress[0]/float(progress[1]), |
| 40 | progress[0], |
| 41 | progress[1]) |
| 42 | if sensors: |
| 43 | output += '* The agent used the following sensors: {}\n\n'.format(', '.join(sensors)) |
| 44 | |
| 45 | if scores and labels_scores: |
| 46 | metrics = list(zip(*[labels_scores[0:3], scores[0:3]])) |
| 47 | infractions = list(zip(*[labels_scores[3:], scores[3:]])) |
| 48 | |
| 49 | output += '=== Global average metrics: ===\n' |
| 50 | output += tabulate(metrics, tablefmt=args.format) |
| 51 | output += '\n\n' |
| 52 | output += '=== Total infractions: ===\n' |
| 53 | output += tabulate(infractions, tablefmt=args.format) |
| 54 | output += '\n\n' |
| 55 | |
| 56 | if records_table: |
| 57 | header = ['metric', 'value', 'additional information'] |
| 58 | list_statistics = [header] |
| 59 | total_duration_game = 0 |
| 60 | total_duration_system = 0 |
| 61 | total_route_length = 0 |
| 62 | for route in records_table: |
| 63 | route_completed_kms = 0.01 * route['scores']['score_route'] * route['meta']['route_length'] / 1000.0 |
| 64 | metrics_route = [[key, '{:.3f}'.format(values), ''] for key, values in route['scores'].items()] |
| 65 | infractions_route = [[key, '{:.3f} ({} occurrences)'.format(len(values)/route_completed_kms, len(values)), |
| 66 | '\n'.join(values)] for key, values in route['infractions'].items()] |
| 67 | |
| 68 | times = [['duration game', '{:.3f}'.format(route['meta']['duration_game']), 'seconds'], |
| 69 | ['duration system', '{:.3f}'.format(route['meta']['duration_system']), 'seconds']] |
| 70 | |
| 71 | route_completed_length = [ ['distance driven', '{:.3f}'.format(route_completed_kms), 'Km']] |
| 72 | |
| 73 | total_duration_game += route['meta']['duration_game'] |
| 74 | total_duration_system += route['meta']['duration_system'] |
| 75 | total_route_length += route_completed_kms |
| 76 | |
| 77 | list_statistics.extend([['{}'.format(route['route_id']), '', '']]) |
| 78 | list_statistics.extend([*metrics_route, *infractions_route, *times, *route_completed_length]) |