Paw Analysis /

import matplotlib.pyplot as plt
import numpy as np

import data
import analysis

def main():
    for measurement in data.dogs['/Krupp Krulle']:

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( + ' ' +
    return fig

def annotate_paw_prints(measurement, ax=None):
    if ax is None:
        ax = plt.gca()

    # Display all paw impacts (sum over time)
    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')
    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_xlabel('Time (ms) Since Beginning of Experiment')
    ax.set_title('Periods of Paw Contact')

    return ax

if __name__ == '__main__':