1. Joe Kington
  2. Paw Analysis

Source

Paw Analysis / plotting.py

"""
Basic visualizations of the paw impact data.
"""

__author__ = 'Joe Kington <jkington@geology.wisc.edu>'
__license__ = """
Copyright (C) 2010 by The Free Software Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import matplotlib.pyplot as plt
import numpy as np

import data
import analysis

def main():
    #view_dogs_measurements('Krupp Krulle')
    walk_through_all_measurements()

def walk_through_all_measurements():
    for measurement in data.measurements:
        plot_measurement(measurement)
        plt.show()

def view_dogs_measurements(dogname):
    # I need to rebuild data.hdf5 so that there aren't leading
    # slashes before the dog names!!!
    for measurement in data.dogs['/' + dogname]:
        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()