James Kuszmaul | 61a971f | 2020-01-01 15:06:18 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | # Sample usage: |
| 3 | # bazel run -c opt //frc971/analysis:plot -- --logfile /tmp/log.fbs --config gyro.pb |
| 4 | import argparse |
| 5 | import json |
| 6 | import os.path |
| 7 | from pathlib import Path |
| 8 | import sys |
| 9 | |
| 10 | from frc971.analysis.py_log_reader import LogReader |
| 11 | from frc971.analysis.plot_config_pb2 import PlotConfig, Signal |
| 12 | from google.protobuf import text_format |
| 13 | |
| 14 | import matplotlib |
| 15 | from matplotlib import pyplot as plt |
| 16 | |
| 17 | |
| 18 | class Plotter: |
| 19 | def __init__(self, plot_config: PlotConfig, reader: LogReader): |
| 20 | self.config = plot_config |
| 21 | self.reader = reader |
| 22 | # Data streams, indexed by alias. |
| 23 | self.data = {} |
| 24 | |
| 25 | def process_logfile(self): |
| 26 | aliases = set() |
| 27 | for channel in self.config.channel: |
| 28 | if channel.alias in aliases: |
| 29 | raise ValueError("Duplicate alias " + channel.alias) |
| 30 | aliases.add(channel.alias) |
| 31 | if not self.reader.subscribe(channel.name, channel.type): |
| 32 | raise ValueError("No such channel with name " + channel.name + |
| 33 | " and type " + channel.type) |
| 34 | |
| 35 | self.reader.process() |
| 36 | |
| 37 | for channel in self.config.channel: |
| 38 | self.data[channel.alias] = [] |
| 39 | for message in self.reader.get_data_for_channel( |
| 40 | channel.name, channel.type): |
| 41 | valid_json = message[2].replace('nan', '"nan"') |
| 42 | parsed_json = json.loads(valid_json) |
| 43 | self.data[channel.alias].append((message[0], message[1], |
| 44 | parsed_json)) |
| 45 | |
| 46 | def plot_signal(self, axes: matplotlib.axes.Axes, signal: Signal): |
| 47 | if not signal.channel in self.data: |
| 48 | raise ValueError("No channel alias " + signal.channel) |
| 49 | field_path = signal.field.split('.') |
| 50 | monotonic_time = [] |
| 51 | signal_data = [] |
| 52 | for entry in self.data[signal.channel]: |
| 53 | monotonic_time.append(entry[0] * 1e-9) |
| 54 | value = entry[2] |
| 55 | for name in field_path: |
| 56 | value = value[name] |
| 57 | # Catch NaNs and convert them to floats. |
| 58 | value = float(value) |
| 59 | signal_data.append(value) |
| 60 | label_name = signal.channel + "." + signal.field |
| 61 | axes.plot(monotonic_time, signal_data, label=label_name) |
| 62 | |
| 63 | def plot(self): |
| 64 | for figure_config in self.config.figure: |
| 65 | fig = plt.figure() |
| 66 | num_subplots = len(figure_config.axes) |
| 67 | for ii in range(num_subplots): |
| 68 | axes = fig.add_subplot(num_subplots, 1, ii + 1) |
| 69 | axes_config = figure_config.axes[ii] |
| 70 | for signal in axes_config.signal: |
| 71 | self.plot_signal(axes, signal) |
| 72 | axes.legend() |
| 73 | axes.set_xlabel("Monotonic Time (sec)") |
| 74 | if axes_config.HasField("ylabel"): |
| 75 | axes.set_ylabel(axes_config.ylabel) |
| 76 | |
| 77 | |
| 78 | def main(argv): |
| 79 | parser = argparse.ArgumentParser( |
| 80 | description="Plot data from an aos logfile.") |
| 81 | parser.add_argument( |
| 82 | "--logfile", |
| 83 | type=str, |
| 84 | required=True, |
| 85 | help="Path to the logfile to parse.") |
| 86 | parser.add_argument( |
| 87 | "--config", |
| 88 | type=str, |
| 89 | required=True, |
| 90 | help="Name of the plot config to use.") |
| 91 | parser.add_argument( |
| 92 | "--config_dir", |
| 93 | type=str, |
| 94 | default="frc971/analysis/plot_configs", |
| 95 | help="Directory to look for plot configs in.") |
| 96 | args = parser.parse_args(argv[1:]) |
| 97 | |
| 98 | if not os.path.isdir(args.config_dir): |
| 99 | print(args.config_dir + " is not a directory.") |
| 100 | return 1 |
| 101 | config_path = os.path.join(args.config_dir, args.config) |
| 102 | if not os.path.isfile(config_path): |
| 103 | print(config_path + |
| 104 | " does not exist or is not a file--available configs are") |
| 105 | for file_name in os.listdir(args.config_dir): |
| 106 | print(os.path.basename(file_name)) |
| 107 | return 1 |
| 108 | |
| 109 | config = PlotConfig() |
| 110 | with open(config_path) as config_file: |
| 111 | text_format.Merge(config_file.read(), config) |
| 112 | |
| 113 | if not os.path.isfile(args.logfile): |
| 114 | print(args.logfile + " is not a file.") |
| 115 | return 1 |
| 116 | |
| 117 | reader = LogReader(args.logfile) |
| 118 | |
| 119 | plotter = Plotter(config, reader) |
| 120 | plotter.process_logfile() |
| 121 | plotter.plot() |
| 122 | plt.show() |
| 123 | |
| 124 | return 0 |
| 125 | |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | sys.exit(main(sys.argv)) |