James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import json |
| 4 | import pathlib |
| 5 | |
| 6 | import matplotlib.pyplot as plt |
| 7 | import pandas as pd |
| 8 | import sys |
| 9 | |
| 10 | # Load data |
| 11 | filename = pathlib.Path(sys.argv[1]) |
| 12 | |
| 13 | UNIT_TO_ABBREVIATION = { |
| 14 | "Meters": "m", |
| 15 | "Feet": "ft", |
| 16 | "Inches": "in", |
| 17 | "Degrees": "deg", |
| 18 | "Rotations": "rot", |
| 19 | "Radians": "rad", |
| 20 | } |
| 21 | |
| 22 | # Make DataFrame to facilitate plotting |
| 23 | if filename.suffix == ".json": |
| 24 | raw_data = json.loads(filename.read_text()) |
| 25 | unit = raw_data["units"] |
| 26 | |
| 27 | # Get Unit |
| 28 | try: |
| 29 | abbreviation = UNIT_TO_ABBREVIATION[unit] |
| 30 | except KeyError: |
| 31 | raise ValueError("Invalid Unit") |
| 32 | |
| 33 | # Make Columns |
| 34 | columns = ["Timestamp (s)", "Test"] |
| 35 | if "Drive" in raw_data["test"]: |
| 36 | columns.extend( |
| 37 | [ |
| 38 | "Left Volts (V)", |
| 39 | "Right Volts (V)", |
| 40 | f"Left Position ({abbreviation})", |
| 41 | f"Right Position ({abbreviation})", |
| 42 | f"Left Velocity ({abbreviation}/s)", |
| 43 | f"Right Velocity ({abbreviation}/s)", |
| 44 | "Gyro Position (deg)", |
| 45 | "Gyro Rate (deg/s)", |
| 46 | ] |
| 47 | ) |
| 48 | unit_columns = columns[4:8] |
| 49 | else: |
| 50 | columns.extend( |
| 51 | ["Volts (V)", f"Position ({abbreviation})", f"Velocity ({abbreviation}/s)"] |
| 52 | ) |
| 53 | unit_columns = columns[3:] |
| 54 | |
| 55 | prepared_data = pd.DataFrame(columns=columns) |
| 56 | for test in raw_data.keys(): |
| 57 | if "-" not in test: |
| 58 | continue |
| 59 | formatted_entry = [[pt[0], test, *pt[1:]] for pt in raw_data[test]] |
| 60 | prepared_data = pd.concat( |
| 61 | [prepared_data, pd.DataFrame(formatted_entry, columns=columns)] |
| 62 | ) |
| 63 | |
| 64 | units_per_rot = raw_data["unitsPerRotation"] |
| 65 | |
| 66 | for column in unit_columns: |
| 67 | prepared_data[column] *= units_per_rot |
| 68 | else: |
| 69 | prepared_data = pd.read_csv(filename) |
| 70 | |
| 71 | # First 2 columns are Timestamp and Test |
| 72 | for column in prepared_data.columns[2:]: |
| 73 | # Configure Plot Labels |
| 74 | plt.figure() |
| 75 | plt.xlabel("Timestamp (s)") |
| 76 | plt.ylabel(column) |
| 77 | |
| 78 | # Configure title without units |
| 79 | print(column) |
| 80 | end = column.find("(") |
| 81 | plt.title(f"{column[:end].strip()} vs Time") |
| 82 | |
| 83 | # Plot data for each test |
| 84 | for test in pd.unique(prepared_data["Test"]): |
| 85 | test_data = prepared_data[prepared_data["Test"] == test] |
| 86 | plt.plot(test_data["Timestamp (s)"], test_data[column], label=test) |
| 87 | plt.legend() |
| 88 | |
| 89 | plt.show() |