From e5cd0c7536c7b9db8177d42c099e46b5d280de85 Mon Sep 17 00:00:00 2001 From: Théo de la Hogue Date: Fri, 20 May 2022 11:53:05 +0200 Subject: Adding a plot method to TimeStampedBuffer class. Removing plot support from MiscFeatures.py. --- src/argaze/DataStructures.py | 19 ++++++ src/argaze/utils/MiscFeatures.py | 11 ---- src/argaze/utils/export_tobii_segment_plots.py | 89 ++++++-------------------- 3 files changed, 39 insertions(+), 80 deletions(-) diff --git a/src/argaze/DataStructures.py b/src/argaze/DataStructures.py index ead5c48..f66ad78 100644 --- a/src/argaze/DataStructures.py +++ b/src/argaze/DataStructures.py @@ -3,7 +3,10 @@ import collections import json import bisect + import pandas +import matplotlib.pyplot as mpyplot +import matplotlib.patches as mpatches class TimeStampedBuffer(collections.OrderedDict): """Ordered dictionary to handle timestamped data. @@ -105,3 +108,19 @@ class TimeStampedBuffer(collections.OrderedDict): except: raise RuntimeError(f'Can\' write {filepath}') + + def plot(self, names=[], colors=[], split={}): + + df = self.as_dataframe(split=split) + legend_patches = [] + + for name, color in zip(names, colors): + + markerline, stemlines, baseline = mpyplot.stem(df.index, df[name]) + mpyplot.setp(markerline, color=color, linewidth=1, markersize = 1) + mpyplot.setp(stemlines, color=color, linewidth=1) + mpyplot.setp(baseline, color=color, linewidth=1) + + legend_patches.append(mpatches.Patch(color=color, label=name.upper())) + + return legend_patches diff --git a/src/argaze/utils/MiscFeatures.py b/src/argaze/utils/MiscFeatures.py index ba0b1b6..56a77bb 100644 --- a/src/argaze/utils/MiscFeatures.py +++ b/src/argaze/utils/MiscFeatures.py @@ -1,8 +1,5 @@ #!/usr/bin/env python -import matplotlib.pyplot as mpyplot -import matplotlib.patches as mpatches - # Print iterations progress def printProgressBar (iteration:int, total:int, prefix:str = '', suffix:str = '', decimals:int = 1, length:int = 100, fill:str = '█', printEnd:str = "\r"): """Print iterations progress. @@ -25,14 +22,6 @@ def printProgressBar (iteration:int, total:int, prefix:str = '', suffix:str = '' if iteration == total: print() -# Plot timestamped dataframe as stem -def plotTimestampedDataframe(df, name: str, color='#000000'): - - markerline, stemlines, baseline = mpyplot.stem(df.index, df[name]) - mpyplot.setp(markerline, color=color, linewidth=1, markersize = 1) - mpyplot.setp(stemlines, color=color, linewidth=1) - mpyplot.setp(baseline, color=color, linewidth=1) - # Handle exit event class ExitSignalHandler(): diff --git a/src/argaze/utils/export_tobii_segment_plots.py b/src/argaze/utils/export_tobii_segment_plots.py index 13a6ead..94e88e7 100644 --- a/src/argaze/utils/export_tobii_segment_plots.py +++ b/src/argaze/utils/export_tobii_segment_plots.py @@ -72,47 +72,35 @@ def main(): # Edit figure figure = mpyplot.figure(figsize=(4 * tobii_segment_video.get_duration() / 1e6, 35)) - # Access to timestamped pupil diameter data buffer - df_ts_pupil_diameters = tobii_segment_data['PupilDiameter'].as_dataframe() - # Plot pupil diameter data subplot = figure.add_subplot(711) subplot.set_title('Pupil diameter', loc='left') + subplot.set_ylim(0, 10) - mpyplot.ylim(0, 10) - - MiscFeatures.plotTimestampedDataframe(df_ts_pupil_diameters, 'value', color='#FFD800') + patches = tobii_segment_data['PupilDiameter'].plot(names=['value'], colors=['#FFD800']) - value_patch = mpatches.Patch(color='#FFD800', label='DIAMETER') - subplot.legend(handles=[value_patch], loc='upper left') + subplot.legend(handles=patches, loc='upper left') - # Access to timestamped event data buffer + # Annotate events df_ts_events = tobii_segment_data['Event'].as_dataframe() - # Annotate events if len(df_ts_events) > 0: - + for ts, event_type, event_tag in zip(df_ts_events.index, df_ts_events.type, df_ts_events.tag): subplot.annotate(f'{event_type}\n{event_tag}', xy=(ts, 7), horizontalalignment="left", verticalalignment="top") subplot.vlines(ts, 0, 6, color="tab:red", linewidth=1) # Access to timestamped pupil center data buffer - df_ts_pupil_centers = tobii_segment_data['PupilCenter'].as_dataframe(split={'value':['x','y', 'z']}) + df_ts_pupil_centers = tobii_segment_data['PupilCenter'].as_dataframe(split={'value':['x','y','z']}) # Plot pupil center data subplot = figure.add_subplot(712) subplot.set_title('Pupil center', loc='left') + subplot.set_ylim(-40, -20) - mpyplot.ylim(-40, -20) - - MiscFeatures.plotTimestampedDataframe(df_ts_pupil_centers, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_pupil_centers, 'y', color='#9427B6') - MiscFeatures.plotTimestampedDataframe(df_ts_pupil_centers, 'z', color='#888888') + patches = tobii_segment_data['PupilCenter'].plot(names=['x','y','z'], colors=['#276FB6','#9427B6','#888888'], split={'value':['x','y','z']}) - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - z_patch = mpatches.Patch(color='#888888', label='Z') - subplot.legend(handles=[x_patch, y_patch, z_patch], loc='upper left') + subplot.legend(handles=patches, loc='upper left') # Access to timestamped gaze position data buffer df_ts_gaze_positions = tobii_segment_data['GazePosition'].as_dataframe(split={'value':['x','y']}) @@ -120,67 +108,35 @@ def main(): # Plot gaze position data subplot = figure.add_subplot(713) subplot.set_title('Gaze position', loc='left') + subplot.set_ylim(0., 1.) - mpyplot.ylim(0., 1.) + patches = tobii_segment_data['GazePosition'].plot(names=['x','y'], colors=['#276FB6','#9427B6'], split={'value':['x','y']}) - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_positions, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_positions, 'y', color='#9427B6') - - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - subplot.legend(handles=[x_patch, y_patch], loc='upper left') - - # Access to timestamped gaze direction data buffer - df_ts_gaze_directions = tobii_segment_data['GazeDirection'].as_dataframe(split={'value':['x','y', 'z']}) + subplot.legend(handles=patches, loc='upper left') # Plot gaze direction data subplot = figure.add_subplot(714) subplot.set_title('Gaze direction', loc='left') - #mpyplot.ylim(0., 1.) - - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_directions, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_directions, 'y', color='#9427B6') - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_directions, 'z', color='#888888') + patches = tobii_segment_data['GazeDirection'].plot(names=['x','y','z'], colors=['#276FB6','#9427B6','#888888'], split={'value':['x','y','z']}) - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - z_patch = mpatches.Patch(color='#888888', label='Z') - subplot.legend(handles=[x_patch, y_patch, z_patch], loc='upper left') - - # Access to timestamped gaze position 3d data buffer - df_ts_gaze_positions_3d = tobii_segment_data['GazePosition3D'].as_dataframe(split={'value':['x','y', 'z']}) + subplot.legend(handles=patches, loc='upper left') # Plot gaze direction data subplot = figure.add_subplot(715) subplot.set_title('Gaze position 3D', loc='left') - #mpyplot.ylim(0., 1.) - - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_positions_3d, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_positions_3d, 'y', color='#9427B6') - MiscFeatures.plotTimestampedDataframe(df_ts_gaze_positions_3d, 'z', color='#888888') - - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - z_patch = mpatches.Patch(color='#888888', label='Z') - subplot.legend(handles=[x_patch, y_patch, z_patch], loc='upper left') + patches = tobii_segment_data['GazePosition3D'].plot(names=['x','y','z'], colors=['#276FB6','#9427B6','#888888'], split={'value':['x','y','z']}) - # Access to timestamped accelerometer data buffer - df_ts_accelerometers = tobii_segment_data['Accelerometer'].as_dataframe(split={'value':['x','y', 'z']}) + subplot.legend(handles=patches, loc='upper left') # Plot accelerometer data subplot = figure.add_subplot(716) subplot.set_title('Accelerometer', loc='left') - MiscFeatures.plotTimestampedDataframe(df_ts_accelerometers, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_accelerometers, 'y', color='#9427B6') - MiscFeatures.plotTimestampedDataframe(df_ts_accelerometers, 'z', color='#888888') + patches = tobii_segment_data['Accelerometer'].plot(names=['x','y','z'], colors=['#276FB6','#9427B6','#888888'], split={'value':['x','y','z']}) - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - z_patch = mpatches.Patch(color='#888888', label='Z') - subplot.legend(handles=[x_patch, y_patch, z_patch], loc='upper left') + subplot.legend(handles=patches, loc='upper left') # Access to timestamped gyroscope data buffer df_ts_gyroscopes = tobii_segment_data['Gyroscope'].as_dataframe(split={'value':['x','y', 'z']}) @@ -189,14 +145,9 @@ def main(): subplot = figure.add_subplot(717) subplot.set_title('Gyroscope', loc='left') - MiscFeatures.plotTimestampedDataframe(df_ts_gyroscopes, 'x', color='#276FB6') - MiscFeatures.plotTimestampedDataframe(df_ts_gyroscopes, 'y', color='#9427B6') - MiscFeatures.plotTimestampedDataframe(df_ts_gyroscopes, 'z', color='#888888') + patches = tobii_segment_data['Gyroscope'].plot(names=['x','y','z'], colors=['#276FB6','#9427B6','#888888'], split={'value':['x','y','z']}) - x_patch = mpatches.Patch(color='#276FB6', label='X') - y_patch = mpatches.Patch(color='#9427B6', label='Y') - z_patch = mpatches.Patch(color='#888888', label='Z') - subplot.legend(handles=[x_patch, y_patch, z_patch], loc='upper left') + subplot.legend(handles=patches, loc='upper left') # Export data plots mpyplot.tight_layout() -- cgit v1.1