1. Joe Kington
  2. Paw Analysis

Commits

Joe Kington  committed 402fc44

Added some basic plotting examples

  • Participants
  • Parent commits ab19880
  • Branches default

Comments (0)

Files changed (1)

File plotting.py

View file
+import matplotlib.pyplot as plt
+import numpy as np
+
+import data
+import analysis
+
+def main():
+    for measurement in data.dogs['/Krupp Krulle']:
+        plot_measurement(measurement)
+        plt.show()
+
+def plot_measurement(measurement, fig=None):
+    if fig is None:
+        fig = plt.figure()
+    ax1 = fig.add_subplot(2,1,1)
+    annotate_paw_prints(measurement, ax1)
+    ax2 = fig.add_subplot(2,1,2)
+    plot_paw_contacts(measurement, ax2)
+    fig.suptitle(measurement.dog.name + ' ' + measurement.name)
+    return fig
+
+def annotate_paw_prints(measurement, ax=None):
+    if ax is None:
+        ax = plt.gca()
+
+    # Display all paw impacts (sum over time)
+    ax.imshow(measurement.data.sum(axis=2).T)
+    limits = ax.axis()
+
+    # Annotate each impact with which paw it is
+    # (Relative to the first paw to hit the sensor)
+    x, y = [], []
+    for impact in measurement.impacts:
+        # Get x,y center of slice...
+        x0, y0 = impact.x.mean(), impact.y.mean()
+        x.append(x0); y.append(y0)
+
+        # Annotate the paw impacts         
+        ax.annotate(impact.paw, (x0, y0),  
+            color='white', ha='center', va='bottom')
+
+    # Plot line connecting paw impacts
+    x, y = np.array(x), np.array(y)
+    ax.plot(x,y, '-wo')
+    ax.set_title('Order of Steps')
+    ax.axis(limits)
+    ax.set_ylim(ax.get_ylim()[::-1])
+    
+    return ax
+
+def plot_paw_contacts(measurement, ax=None):
+    if ax is None:
+        ax = plt.gca()
+    # Group impacts by paw...
+    for impact in measurement.impacts:
+        paw_number = analysis.INV_PAW_CODE_LUT[impact.paw] + 1
+        # Draw a bar over the time interval where each paw is in contact
+        ax.barh(bottom=paw_number, width=impact.time.ptp(), height=0.2, 
+                left=impact.time.min(), align='center', color='red')
+    ax.set_yticks(range(1, 5))
+    ax.set_yticklabels(analysis.PAW_CODE_LUT.values())
+    ax.set_xlabel('Time (ms) Since Beginning of Experiment')
+    ax.yaxis.grid(True)
+    ax.set_title('Periods of Paw Contact')
+
+    return ax
+
+if __name__ == '__main__':
+    main()