-
Notifications
You must be signed in to change notification settings - Fork 2
/
identify_controller.py
99 lines (68 loc) · 3.15 KB
/
identify_controller.py
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
#!/usr/bin/env python
"""This script identifies the controller and plots the results."""
# builtin
import os
import argparse
# external
import matplotlib.pyplot as plt
# local
import utils
from gait_landmark_settings import settings
PATHS = utils.config_paths()
def main(event, structure, recompute, normalize):
trial_numbers = sorted(settings.keys())
plot_dir = utils.mkdir(os.path.join(PATHS['figures_dir'],
'identification-results',
'-'.join(event.lower().split(' ')),
'-'.join(structure.split(' '))))
for trial_number in trial_numbers:
msg = 'Identifying {} controller from {} for trial #{}'
msg = msg.format(structure, event, trial_number)
print('=' * len(msg))
print(msg)
print('=' * len(msg))
trial = utils.Trial(trial_number)
if recompute:
trial.remove_precomputed_data()
trial.identify_controller(event, structure)
fig, axes = trial.plot_joint_isolated_gains(event, structure,
normalize=normalize)
solver = trial.control_solvers[event][structure]
id_num_steps = solver.identification_data.shape[0]
title = """\
{} Scheduled Gains Identified from {} Gait Cycles in Trial {}
Nominal Speed: {} m/s, Gender: {}
"""
fig.suptitle(title.format(structure.capitalize(), id_num_steps,
trial_number,
trial.meta_data['trial']['nominal-speed'],
trial.meta_data['subject']['gender']))
fig.set_size_inches((14.0, 14.0))
plt.tight_layout()
plt.subplots_adjust(top=0.85)
fig_path = os.path.join(plot_dir, 'gains-' + trial_number + '.png')
fig.savefig(fig_path, dpi=300)
print('Gain plot saved to {}'.format(fig_path))
plt.close(fig)
fig, axes = trial.plot_validation(event, structure)
fig_path = os.path.join(plot_dir, 'validation-' + trial_number + '.png')
fig.savefig(fig_path, dpi=300)
print('Validation plot saved to {}'.format(fig_path))
plt.close(fig)
if __name__ == "__main__":
desc = "Identify Controller"
parser = argparse.ArgumentParser(description=desc)
msg = ("A valid event name in the data, likely: "
"'Longitudinal Perturbation', 'First Normal Walking', "
"or 'Second Normal Walking'.")
parser.add_argument('-e', '--event', type=str, help=msg,
default='Longitudinal Perturbation')
msg = ("The desired controller structure: 'join isolated' or 'full'.")
parser.add_argument('-s', '--structure', type=str, help=msg,
default='joint isolated')
msg = ("Force recomputation of all data.")
parser.add_argument('-r', '--recompute', action="store_true", help=msg)
msg = ("Normalize gains to subject mass in plots.")
parser.add_argument('-n', '--normalize', action="store_true", help=msg)
args = parser.parse_args()
main(args.event, args.structure, args.recompute, args.normalize)