calculate_muscle_activations = True calculate_joint_reactions = True ## # scale os.chdir('scale/') call(['opensim-cmd', 'run-tool', 'setup_scale.xml']) os.chdir(subject_dir) ## # inverse kinematics os.chdir('inverse_kinematics/') call(['opensim-cmd', 'run-tool', 'setup_ik.xml']) plot_sto_file('task_InverseKinematics.mot', 'task_InverseKinematics.pdf', 3) plot_sto_file('task_ik_marker_errors.sto', 'task_ik_marker_errors.pdf', 3) plot_sto_file('task_ik_model_marker_locations.sto', 'task_ik_model_marker_locations.pdf', 3) os.chdir(subject_dir) ## # residual reduction algorithm and model adjustment os.chdir('residual_reduction_algorithm/') rra_output = run(['opensim-cmd', 'run-tool', 'setup_rra.xml'], stdout=PIPE, stderr=PIPE, universal_newlines=True) print(rra_output.stdout) # find mass change from RRA output (perform only when RRA does not suggest much # mass change)
from subprocess import PIPE, run, call from utils import adjust_model_mass, subject_specific_isometric_force from utils import plot_sto_file # %% # subject data # 1.8m, 72 kg subject_height = 1.803 subject_dir = os.path.abspath('../') os.chdir(subject_dir) # %% # experimental data plot_sto_file('experimental_data/task.trc', 'experimental_data/task.pdf', 3) plot_sto_file('experimental_data/task_grf.mot', 'experimental_data/task_grf.pdf', 3) # %% # scale os.chdir('scale/') call(['opensim-cmd', 'run-tool', 'setup_scale.xml']) os.chdir(subject_dir) # %% # inverse kinematics os.chdir('inverse_kinematics/') call(['opensim-cmd', 'run-tool', 'setup_ik.xml'])
'inverse_dynamics/task_InverseDynamics.sto') tau_rt_file = os.path.join(output_dir, 'tau.sto') # %% # read data tau_reference = read_from_storage(tau_reference_file) tau_rt = read_from_storage(tau_rt_file) if gait_cycle: t0 = 0.6 # right heel strike tf = 1.83 # next right heel strike tau_reference = to_gait_cycle(tau_reference, t0, tf) tau_rt = to_gait_cycle(tau_rt, t0, tf) plot_sto_file(tau_rt_file, tau_rt_file + '.pdf', 3) # %% # compare d_tau_total = [] with PdfPages(output_dir + 'inverse_dynamics_comparison.pdf') as pdf: for i in range(1, tau_reference.shape[1]): # find index key = tau_reference.columns[i].replace('_moment', '').replace('_force', '') j = tau_rt.columns.get_loc(key) d_tau = rmse_metric(tau_reference.iloc[:, i], tau_rt.iloc[:, j]) if not np.isnan(d_tau): # NaN when siganl is zero d_tau_total.append(d_tau)
# %% # data gait_cycle = True subject_dir = os.path.abspath('../') output_dir = os.path.join(subject_dir, 'real_time/muscle_optimization/') fm_reference_file = os.path.join( subject_dir, 'static_optimization/task_StaticOptimization_force.sto') fm_rt_file = os.path.join(output_dir, 'fm.sto') am_rt_file = os.path.join(output_dir, 'am.sto') tauRes_rt_file = os.path.join(output_dir, 'tauRes.sto') plot_sto_file(am_rt_file, am_rt_file + '.pdf', 3) plot_sto_file(tauRes_rt_file, tauRes_rt_file + '.pdf', 3) # %% # read data fm_reference = read_from_storage(fm_reference_file) fm_rt = read_from_storage(fm_rt_file) if gait_cycle: t0 = 0.6 # right heel strike tf = 1.83 # next right heel strike fm_reference = to_gait_cycle(fm_reference, t0, tf) fm_rt = to_gait_cycle(fm_rt, t0, tf) plot_sto_file(fm_rt_file, fm_rt_file + '.pdf', 3)
# %% # read data jr_reference = read_from_storage(jr_reference_file) jr_rt = read_from_storage(jr_rt_file) # make very small number zero before plotting jr_reference[jr_reference.abs() < 1e-9] = 0 jr_rt[jr_reference.abs() < 1e-9] = 0 if gait_cycle: t0 = 0.6 # right heel strike tf = 1.83 # next right heel strike jr_reference = to_gait_cycle(jr_reference, t0, tf) jr_rt = to_gait_cycle(jr_rt, t0, tf) plot_sto_file(jr_rt_file, jr_rt_file + '.pdf', 3) # %% # compare d_jr_total = [] with PdfPages(output_dir + 'joint_reaction_comparison_fm.pdf') as pdf: for i in range(1, jr_rt.shape[1]): # find index key = jr_rt.columns[i] j = jr_reference.columns.get_loc(key) d_jr = rmse_metric(jr_reference.iloc[:, j], jr_rt.iloc[:, i]) if not np.isnan(d_jr): # NaN when siganl is zero
rotate_data_table(forces_task, [1, 0, 0], -90) # conversion of unit (f -> N, p -> mm, tau -> Nmm) mm_to_m(forces_task, 'p1') mm_to_m(forces_task, 'p2') mm_to_m(forces_task, 'm1') mm_to_m(forces_task, 'm2') # refine ground reaction forces refine_ground_reaction_wrench(forces_task, ['f1', 'p1', 'm1'], stance_threshold=50, tau=0.001) refine_ground_reaction_wrench(forces_task, ['f2', 'p2', 'm2'], stance_threshold=50, tau=0.001) # export forces (assume two force plates) time = forces_task.getIndependentColumn() forces_task = forces_task.flatten(['x', 'y', 'z']) force_sto = create_opensim_storage(time, forces_task.getMatrix(), labels_force) force_sto.setName('GRF') force_sto.printResult(force_sto, 'task_grf', output_dir, 0.01, '.mot') # plot plot_sto_file(os.path.join(output_dir, 'task.trc'), os.path.join(output_dir, 'task.pdf'), 3) plot_sto_file(os.path.join(output_dir, 'task_grf.mot'), os.path.join(output_dir, 'task_grf.pdf'), 3) ##
for muscle in muscles: # right leg labels.append('R_' + muscle) right = acq.GetAnalog('R_' + muscle).GetData().GetValues().reshape(-1) emg_raw.append(right) emg_env.append(calculate_emg_linear_envelope(right, f_s)) # left leg labels.append('L_' + muscle) left = acq.GetAnalog('L_' + muscle).GetData().GetValues().reshape(-1) emg_raw.append(left) emg_env.append(calculate_emg_linear_envelope(left, f_s)) ## # save data emg_raw_sto = create_opensim_storage( time, np_array_to_simtk_matrix(np.array(emg_raw).transpose()), labels) emg_raw_sto.setName('emg_raw') emg_raw_sto.printResult(emg_raw_sto, 'emg_raw', output_dir, 1.0 / f_s, '.sto') plot_sto_file(os.path.join(output_dir, 'emg_raw.sto'), os.path.join(output_dir, 'emg_raw.pdf'), 2) emg_env_sto = create_opensim_storage( time, np_array_to_simtk_matrix(np.array(emg_env).transpose()), labels) emg_env_sto.setName('emg_env') emg_env_sto.printResult(emg_env_sto, 'emg_env', output_dir, 1.0 / f_s, '.sto') plot_sto_file(os.path.join(output_dir, 'emg_env.sto'), os.path.join(output_dir, 'emg_env.pdf'), 2) ##
q_ddot_filtered_sp = read_from_storage(q_ddot_filtered_sp_file) if gait_cycle: t0 = 0.6 # right heel strike tf = 1.83 # next right heel strike q_reference = to_gait_cycle(q_reference, t0, tf) q_dot_reference = to_gait_cycle(q_dot_reference, t0, tf) q_ddot_reference = to_gait_cycle(q_ddot_reference, t0, tf) q_filtered = to_gait_cycle(q_filtered, t0, tf) q_dot_filtered = to_gait_cycle(q_dot_filtered, t0, tf) q_ddot_filtered = to_gait_cycle(q_ddot_filtered, t0, tf) q_filtered_sp = to_gait_cycle(q_filtered_sp, t0, tf) q_dot_filtered_sp = to_gait_cycle(q_dot_filtered_sp, t0, tf) q_ddot_filtered_sp = to_gait_cycle(q_ddot_filtered_sp, t0, tf) plot_sto_file(q_reference_file, q_reference_file + '.pdf', 2) plot_sto_file(q_filtered_file, q_filtered_file + '.pdf', 2) # %% # compare d_q_total = [] d_u_total = [] d_a_total = [] with PdfPages(output_dir + 'filter_comparison.pdf') as pdf: for i in range(1, q_reference.shape[1]): # if 'mtp' in q_reference.columns[i] or \ # 'subtalar' in q_reference.columns[i] or \ # 'ankle_angle_l' in q_reference.columns[i]: # # ankle added because it was too noisy # continue
# weight regression model if adapt_muscle_strength: subject_specific_isometric_force('../model/model_generic.osim', 'model_scaled.osim', generic_height, subject_height) os.chdir(subject_dir) ## # inverse kinematics os.chdir('inverse_kinematics/') call(['opensim-cmd', 'run-tool', 'setup_ik.xml']) plot_sto_file('task_InverseKinematics.mot', 'task_InverseKinematics.pdf', 3) plot_sto_file('task_ik_marker_errors.sto', 'task_ik_marker_errors.pdf', 3) plot_sto_file('task_ik_model_marker_locations.sto', 'task_ik_model_marker_locations.pdf', 3) os.chdir(subject_dir) ## # inverse dynamics os.chdir('inverse_dynamics/') call(['opensim-cmd', 'run-tool', 'setup_id.xml']) plot_sto_file('task_InverseDynamics.sto', 'task_InverseDynamics.pdf', 3)
'inverse_kinematics/task_InverseKinematics.mot') q_rt_file = os.path.join(output_dir, 'q.sto') # %% # read data q_reference = read_from_storage(q_reference_file) q_rt = read_from_storage(q_rt_file) if gait_cycle: t0 = 0.6 # right heel strike tf = 1.83 # next right heel strike q_reference = to_gait_cycle(q_reference, t0, tf) q_rt = to_gait_cycle(q_rt, t0, tf) plot_sto_file(q_rt_file, q_rt_file + '.pdf', 3) # %% # compare d_q_total = [] with PdfPages(output_dir + 'inverse_kinematics_comparison.pdf') as pdf: for i in range(1, q_reference.shape[1]): # find index j = q_rt.columns.get_loc(q_reference.columns[i]) # deg to rad for rotational degrees of freedom if '_tx' in q_reference.columns[i] or \ '_ty' in q_reference.columns[i] or \ '_tz' in q_reference.columns[i]: