-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
110 lines (86 loc) · 3.49 KB
/
plotter.py
File metadata and controls
110 lines (86 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import h5py
import sys
import numpy as np
import matplotlib.pyplot as plt
class Plotter:
"""
Loads logged hdf5 data for plotting.
"""
def __init__(self, file_id):
self.f = h5py.File(f"{file_id}.h5", 'r')
def plot(self, group):
"""
Plot a single logger.
"""
# Get columns for group
if group not in self.f:
raise ValueError(f"no such group {group}")
# Get the display names out of the group attributes
labels = {}
for key, value in self.f[group].attrs.items():
labels[key] = value
columns = list(self.f[group].keys())
columns.remove('t')
if len(columns) == 0:
raise IOError(f"dataset {group} contained no columns except time")
# Create subplots for each column except t
self.fig, self.ax = plt.subplots(len(columns), 1)
self.fig.suptitle(group)
t = self.f[group]['t'][:]
for i, column in enumerate(columns):
if column == 't':
continue
self.ax[i].plot(t, self.f[group][column][:])
# Optionally set the label (if a label is given)
if column in labels:
self.ax[i].set_ylabel(labels[column])
else:
self.ax[i].set_ylabel(column)
self.ax[-1].set_xlabel('t')
def show(self):
plt.show()
def analyze(
self,
group='low_rate_log',
error_column='e',
setpoint_column='setpoint',
settling_threshold=0.05,
):
"""
Compute and display performance metrics on an error signal.
This method is a bit of a hack. It would be nice
"""
e = self.f[group][error_column][:] # Assuming 'e' is the error signal
setpoint = self.f[group][setpoint_column][:] # Assuming 'setpoint' is the setpoint signal
t = self.f[group]['t'][:]
# Calculate RMSE from error
rmse = np.sqrt(np.mean(e ** 2))
# Settling time: find the time it takes for the error to fall to within settling_threshold
# percent of the setpoint.
threshold = np.max(np.abs(setpoint)) * settling_threshold
within_threshold_indices = np.abs(e) <= threshold
# Start at the end of within_threshold_indices and work backwards until we find
# one that isn't True
for index in range(len(within_threshold_indices) - 1, -1, -1):
if not within_threshold_indices[index]:
break
else:
threshold_index = index
settling_time = t[threshold_index] if len(within_threshold_indices) > 0 else None
# Peak response time: find first peak in error signal
peak_response_time = t[np.argmax(np.abs(e))]
# Overshoot: find percentage of time points where e is less than zero
overshoot = np.sum(e < 0) / len(e)
# Print the metrics
print(f"RMSE: {rmse}")
print(f"Peak Response Time: {peak_response_time} time units")
print(f"Settling Time: {settling_time} time units (within {100 * settling_threshold}% of setpoint)")
print(f"Overshoot: {100 * overshoot}%")
if __name__ == "__main__":
p = Plotter('data')
for log in ['low_rate_log', 'high_rate_log']:
print(f"plotting {log}")
p.plot(log)
# Can also print out a few performance metrics for the PID controller.
p.analyze('low_rate_log', 'e')
p.show()