def plot_pos_data(self, title: str, y_label: str, kine_var: str, fig_num: int, add_sd: bool = True, clip_graph: bool = False) -> matplotlib.figure.Figure: """Plot overlayed raw, filled, and smoothed marker position data.""" fig = plt.figure(num=fig_num) ax = fig.subplots(3, 1, sharex=True) lines_filled = marker_graph_init(ax, self.filled.means.pos, y_label, x_data=self.frames, color='red') lines_raw = marker_graph_add(ax, self.raw.means.pos, self.frames, color='indigo', marker='.') lines_smoothed = marker_graph_add(ax, getattr(self.smoothed.means, kine_var), self.filtered_frames, color='limegreen', lw=1) if add_sd: marker_graph_add_cov(ax, self.smoothed.means.pos, self.smoothed.covars.pos, self.filtered_frames, 'limegreen') if clip_graph: for c_ax in ax: c_ax.set_xlim(self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) else: add_vicon_start_stop(ax, self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) plt.tight_layout(pad=1.0, w_pad=1.0, h_pad=0) fig.suptitle(title, x=0.25, fontsize=11, fontweight='bold') plt.subplots_adjust(top=0.94) fig.legend((lines_raw[0], lines_filled[0], lines_smoothed[0]), ('Raw', 'Filled', 'Smoothed'), 'upper right', labelspacing=0.1, ncol=3, columnspacing=0.3) make_interactive() return fig
def plot_marker_data_diff_hist(self, title: str, x_label: str, fig_num: int, clip_graph: bool = True) \ -> matplotlib.figure.Figure: """Plot histogram of smoothed marker positions - raw marker positions.""" if clip_graph: endpts = np.zeros((2,), dtype=np.int) if self.vicon_endpts[0] > self.filtered.endpts[0]: endpts[0] = self.vicon_endpts[0] - self.filtered.endpts[0] if self.vicon_endpts[1] < self.filtered.endpts[1]: endpts[1] = self.vicon_endpts[1] - self.filtered.endpts[0] else: endpts[1] = self.filtered.endpts[1] - self.filtered.endpts[0] smoothed_diff = self.smoothed_pos_diff[endpts[0]:endpts[1]] else: smoothed_diff = self.smoothed_pos_diff fig = plt.figure(num=fig_num) ax = fig.subplots(1, 3, sharey=True) lines_smoothed = marker_diff_his_init(ax, smoothed_diff, x_label, 'limegreen') fig.legend([lines_smoothed[0]], ['Smoothed'], 'upper right', labelspacing=0.1) marker_graph_title(fig, title) make_interactive() return fig
def plot_marker_data(self, title: str, y_label: str, kine_var: str, fig_num: int, add_sd: bool = True, clip_graph: bool = False, marker: str = '') -> matplotlib.figure.Figure: """Plot raw, filtered, and smoothed kinematic (position, velocity, acceleration) marker data, allowing the addition of confidence bounds.""" fig = plt.figure(num=fig_num) ax = fig.subplots(3, 1, sharex=True) lines_raw = marker_graph_init(ax, getattr(self.raw.means, kine_var), y_label, x_data=self.frames, color='indigo', marker=marker) lines_filtered = marker_graph_add(ax, getattr(self.filtered.means, kine_var), self.filtered_frames, color='red') lines_smoothed = marker_graph_add(ax, getattr(self.smoothed.means, kine_var), self.filtered_frames, color='limegreen') if add_sd: marker_graph_add_cov(ax, getattr(self.filtered.means, kine_var), getattr(self.filtered.covars, kine_var), self.filtered_frames, 'red') marker_graph_add_cov(ax, getattr(self.smoothed.means, kine_var), getattr(self.smoothed.covars, kine_var), self.filtered_frames, 'limegreen') if clip_graph: for c_ax in ax: c_ax.set_xlim(self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) else: add_vicon_start_stop(ax, self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) fig.legend((lines_raw[0], lines_filtered[0], lines_smoothed[0]), ('Raw', 'Filtered', 'Smoothed'), 'upper right', labelspacing=0.1) marker_graph_title(fig, title) make_interactive() return fig
def plot_biplane_vicon(self, title: str, fig_num: int, vicon_field: str, vicon_type: str) \ -> matplotlib.figure.Figure: """Plot overlayed marker position data as measured via Vicon and biplane fluoroscopy.""" fig = plt.figure(num=fig_num) ax = fig.subplots(3, 1, sharex=True) lines_vicon = marker_graph_init(ax, getattr(self, vicon_field), 'Distance (mm)', self.vicon_frames, color='limegreen', marker='.', lw=1, ms=2) lines_biplane = marker_graph_add(ax, self.biplane_data, self.vicon_frames, color='indigo', marker='.') fig.legend((lines_biplane[0], lines_vicon[0]), ('Biplane', vicon_type + ' Vicon'), 'upper right', ncol=3, columnspacing=0.3, handlelength=1.0) marker_graph_title(fig, title) make_interactive() return fig
def plot(self) -> List[matplotlib.figure.Figure]: """Plot marker_data onto 3 row subplots on Figure 0.""" fig = plt.figure(num=0) ax = fig.subplots(3, 1, sharex=True) marker_graph_init(ax, self.marker_pos_labeled, 'Pos (mm)', x_data=self.frame_nums, color='blue') add_vicon_start_stop(ax, self.vicon_endpts[0] + 1, self.vicon_endpts[1]) marker_graph_title(fig, self.trial_name + ' ' + self.marker_name) make_interactive() return [fig]
def plot_marker_data_diff(self, title: str, y_label: str, fig_num: int) -> matplotlib.figure.Figure: """Plot smoothed marker positions - raw marker positions.""" fig = plt.figure(num=fig_num) ax = fig.subplots(3, 1, sharex=True) lines_smoothed = marker_graph_init(ax, self.smoothed_pos_diff, y_label, x_data=self.filtered_frames, color='limegreen') fig.legend([lines_smoothed[0]], ['Smoothed'], 'upper right', labelspacing=0.1) add_vicon_start_stop(ax, self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) marker_graph_title(fig, title) make_interactive() return fig
def plot_marker_data_smooth(self, title: str, y_label: str, kine_var: str, fig_num: int) \ -> matplotlib.figure.Figure: """Plot smoothed kinematic (position, velocity, acceleration) marker data.""" fig = plt.figure(num=fig_num) ax = fig.subplots(3, 1, sharex=True) lines_smoothed = marker_graph_init(ax, getattr(self.smoothed.means, kine_var), y_label, x_data=self.filtered_frames, color='limegreen') fig.legend([lines_smoothed[0]], ['Smoothed'], 'upper right', labelspacing=0.1) add_vicon_start_stop(ax, self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) marker_graph_title(fig, title) make_interactive() return fig
def plot_cov(self, title: str, y_labels: Sequence[str], fig_num: int) -> matplotlib.figure.Figure: """Plot overlayed variance of filtered and smoothed kinematic variables (position, velocity, acceleration) in separate rows with 3 columns for each spatial dimension (3x3). """ fig = plt.figure(num=fig_num) ax = fig.subplots(3, 3, sharex='all', sharey='row') lines_filtered = cov_trend_graph_init(ax, self.filtered.covars, self.filtered_frames, y_labels, np.sqrt, color='red') lines_smooth = cov_trend_graph_add(ax, self.smoothed.covars, self.filtered_frames, np.sqrt, color='limegreen') fig.legend((lines_filtered[0][0], lines_smooth[0][0]), ('Filtered', 'Smoothed'), 'upper right', labelspacing=0.1, ncol=2) add_vicon_start_stop(ax, self.vicon_frame_endpts[0], self.vicon_frame_endpts[1]) marker_graph_title(fig, title) make_interactive() return fig
def plot_all_diff(self, title: str, fig_num: int) -> matplotlib.figure.Figure: """Overlay plot differences in marker position as measured via Vicon and biplane fluoroscopy, and differences in marker position as measured Vicon (then smoothed) and biplane fluoroscopy.""" fig = plt.figure(num=fig_num) ax = fig.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [2, 1]}) lines_xyz_raw = marker_diff_graph(ax[0], self._diff_raw, 'Distance (mm)', x_label=None, x_data=self.vicon_frames, ls='--') line_scalar_raw = marker_diff_graph(ax[1], self._diff_raw_scalar, 'Distance (mm)', 'Frame Number', self.vicon_frames, color='indigo', ls=':') # reset colors ax[0].set_prop_cycle(None) lines_xyz_smooth = marker_diff_graph(ax[0], self._diff_smoothed, 'Distance (mm)', x_label=None, x_data=self.vicon_frames) line_scalar_smooth = marker_diff_graph(ax[1], self._diff_smoothed_scalar, 'Distance (mm)', 'Frame Number', self.vicon_frames, color='indigo') leg1 = fig.legend(lines_xyz_raw + line_scalar_raw, ('X (Raw)', 'Y', 'Z', '$\\mid \\mid$'), loc='lower left', handletextpad=0.1, ncol=4, columnspacing=0.5, handlelength=1.0, bbox_to_anchor=(0.0, 0.0)) fig.legend(lines_xyz_smooth + line_scalar_smooth, ('X (Smooth)', 'Y', 'Z', '$\\mid \\mid$'), loc='lower right', handletextpad=0.1, ncol=4, columnspacing=0.5, handlelength=1.0, bbox_to_anchor=(1.0, 0.0)) fig.add_artist(leg1) fig.suptitle(title, fontsize=11, fontweight='bold') plt.tight_layout(pad=1.0, h_pad=0, rect=(0, 0.015, 1, 1)) # add RMS, MAE, Max raw_text = 'RMS: {:.2f} MAE: {:.2f} Max: {:.2f}'\ .format(self.biplane_vicon_diff.raw_rms_scalar, self.biplane_vicon_diff.raw_mae_scalar, self.biplane_vicon_diff.raw_max_scalar) smooth_text = 'RMS: {:.2f} MAE: {:.2f} Max: {:.2f}'\ .format(self.biplane_vicon_diff.smoothed_rms_scalar, self.biplane_vicon_diff.smoothed_mae_scalar, self.biplane_vicon_diff.smoothed_max_scalar) ax[0].text(0.01, 0.99, raw_text, ha='left', va='top', transform=fig.transFigure, fontweight='bold', bbox=dict(ec='indigo', fc='None', boxstyle='round', ls=':', lw=2)) ax[0].text(0.99, 0.99, smooth_text, ha='right', va='top', transform=fig.transFigure, fontweight='bold', bbox=dict(ec='indigo', fc='None', boxstyle='round', lw=2)) make_interactive() return fig
def plot_diff(self, title: str, fig_num: int, vicon_fields: Sequence[str], diff_field: str) \ -> matplotlib.figure.Figure: """Plot difference between marker position data as measured via Vicon and biplane fluoroscopy.""" fig = plt.figure(num=fig_num) ax = fig.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [2, 1]}) lines_xyz = marker_diff_graph(ax[0], getattr(self, vicon_fields[0]), 'Distance (mm)', x_label=None, x_data=self.vicon_frames) line_scalar = marker_diff_graph(ax[1], getattr(self, vicon_fields[1]), 'Distance (mm)', None, self.vicon_frames, color='indigo') fig.legend(lines_xyz + line_scalar, ('X', 'Y', 'Z', '| |'), loc='lower center', ncol=4, columnspacing=0.3, handlelength=1.0) fig.suptitle(title, fontsize=11, fontweight='bold') plt.tight_layout(pad=1.0, h_pad=0, rect=(0, 0.015, 1, 1)) # add RMS, MAE, Max for each individual x, y, z text_align = [(0.01, 0.99, 'left', 'top'), (0.99, 0.99, 'right', 'top'), (0.01, 0.01, 'left', 'bottom')] cc = plt.rcParams['axes.prop_cycle'].by_key()['color'] for n in range(3): xyz_text = 'RMS: {:.2f} MAE: {:.2f} Max: {:.2f}'\ .format(getattr(self.biplane_vicon_diff, diff_field + '_rms')[n], getattr(self.biplane_vicon_diff, diff_field + '_mae')[n], getattr(self.biplane_vicon_diff, diff_field + '_max')[n]) ax[0].text(text_align[n][0], text_align[n][1], xyz_text, ha=text_align[n][2], va=text_align[n][3], transform=fig.transFigure, fontweight='bold', bbox=dict(ec=cc[n], fc='None', boxstyle='round', lw=2)) # add RMS, MAE, Max for scalar scalar_text = 'RMS: {:.2f} MAE: {:.2f} Max: {:.2f}'\ .format(getattr(self.biplane_vicon_diff, diff_field + '_rms_scalar'), getattr(self.biplane_vicon_diff, diff_field + '_mae_scalar'), getattr(self.biplane_vicon_diff, diff_field + '_max_scalar')) ax[0].text(0.99, 0.01, scalar_text, ha='right', va='bottom', transform=fig.transFigure, fontweight='bold', bbox=dict(ec='indigo', fc='None', boxstyle='round', lw=2)) make_interactive() return fig
tracking_markers = np.stack([ trial.filled[marker] for marker in StaticTorsoSegment.TRACKING_MARKERS ], 0) torso_vicon = compute_trajectory( trial.subject.torso.static_markers_intrinsic, tracking_markers) torso_fluoro = change_cs(trial.subject.f_t_v, torso_vicon) torso_truncated = torso_fluoro[trial.vicon_endpts[0]:trial.vicon_endpts[1]] quat = quaternion.as_float_array( quaternion.from_rotation_matrix(torso_truncated[:, :3, :3], nonorthogonal=False)) torso_pos_quat = np.concatenate((torso_truncated[:, :3, 3], quat), 1) # retrieve reference data ref_data = pd.read_csv( Path(params.matlab_threejs_dir) / subject_id / (params.trial_name + '.csv')) # since the negative of a quaternions represents the same orientation make the appropriate sign flip so graphs align mask = extended_dot(torso_pos_quat[:, 3:], ref_data.iloc[:, 3:7].to_numpy()) < 0 torso_pos_quat[mask, 3:] = -torso_pos_quat[mask, 3:] # graph init_graphing() for n in range(7): plt.figure(n + 1) plt.plot(torso_pos_quat[:, n], 'r') plt.plot(ref_data.iloc[:, n], 'g') make_interactive() plt.show()