def main(dataset='measurements.npy'):
    X = np.load(dataset, mmap_mode='r')
    print 'loaded', dataset, X.shape

    trial = X[0, 0, 0]
    fig = plt.figure()
    ax = util.axes(fig)
    lines = [ax.plot([], [], [], '.-', c='#111111')[0] for s in C.SKELETON]

    def init():
        for l in lines:
            l.set_data([], [])
            l.set_3d_properties([])
        return lines

    def draw(f):
        frame = trial[f % len(trial)]
        markers = frame[17:].reshape((-1, 4))
        for i, l in enumerate(lines):
            x, y, z = np.array(
                [util.identity(frame, markers[m, :3])
                 for m in C.SKELETON[i] if markers[m, 3] > 0]).T
            l.set_data(x, z)
            l.set_3d_properties(y)
        #ax.view_init(30, 0.3 * f)
        fig.canvas.draw()
        return lines

    a = anim.FuncAnimation(
        fig, draw, init_func=init, frames=240, interval=10, blit=False)
    #a.save('/tmp/trial.mp4', fps=15, extra_args=['-vcodec', 'libx264'])

    util.set_limits(ax, center=(0, 0, 0))
    plt.show()
def main(dataset='measurements.npy'):
    data = np.load(dataset, mmap_mode='r')
    print 'loaded', dataset, data.shape

    fig = plt.figure()
    ax = util.axes(fig, 111)

    trial = data[15, 1, 5]
    for f in range(0, len(trial), 300):
        util.plot_skeleton(ax, trial[f], alpha=1)
    x, y, z = trial[:, TARGET].T
    ax.plot(x, z, y, 'o-', color='#111111', alpha=0.5)

    util.set_limits(ax, center=(0, 0, 1), span=1)

    ax.w_xaxis.set_pane_color((1, 1, 1, 1))
    ax.w_yaxis.set_pane_color((1, 1, 1, 1))
    ax.w_zaxis.set_pane_color((1, 1, 1, 1))

    #plt.gcf().set_size_inches(12, 10)
    #plt.savefig('single-trial.pdf', dpi=600)
    plt.show()
def main(dataset='measurements.npy'):
    data = np.load(dataset, mmap_mode='r')
    print 'loaded', dataset, data.shape

    plots = list(range(N * N))
    frames = [[] for _ in plots]
    for subj in data:
        for block in subj[1:]:
            for trial in block:
                if trial[0, C.col('trial-hand')] == C.right:
                    for frame in trial:
                        for i in plots:
                            if within_region(frame, i):
                                frames[i].append(frame)
                                break

    u, v = np.mgrid[0:2 * np.pi:11j, 0:np.pi:7j]
    sphx = np.cos(u) * np.sin(v)
    sphy = np.sin(u) * np.sin(v)
    sphz = np.cos(v)

    fig = plt.figure()
    for i, postures in enumerate(frames):
        if not postures:
            continue
        if i != 2:
            continue

        postures = np.array(postures)
        for m in range(50):
            marker = postures[:, 17+m*4:17+(m+1)*4]
            drops = marker[:, 3] < 0
            marker[drops, :3] = marker[~drops, :3].mean(axis=0)
        means = postures.mean(axis=0)
        stds = postures.std(axis=0)

        #ax = util.axes(fig, 111)
        #for frame in postures[::5]:
        #    util.plot_skeleton(ax, frame, alpha=0.1)
        ax = util.axes(fig, 110 * N + i + 1)
        util.plot_skeleton(ax, means, alpha=1.0)
        for m in range(50):
            mx, my, mz = means[17+m*4:20+m*4]
            sx, sy, sz = stds[17+m*4:20+m*4] / 2
            ax.plot_wireframe(sphx * sx + mx, sphz * sz + mz, sphy * sy + my,
                              color=C.MARKER_COLORS[m], alpha=0.3)

        #tgtx, tgty, tgtz = postures.mean(axis=0)[
        #    C.cols('target-x', 'target-y', 'target-z')]
        #ax.plot([tgtx], [tgtz], [tgty], 'o', color='#111111')

        #for m in range(50):
        #    marker = postures[:, 17 + 4 * m:17 + 4 * (m+1)]
        #    position = marker.mean(axis=0)
        #    size = marker.std(axis=0)
        #    ax.plot_surface()

        util.set_limits(ax, center=(0, -0.5, 1), span=1)
        ax.w_xaxis.set_pane_color((1, 1, 1, 1))
        ax.w_yaxis.set_pane_color((1, 1, 1, 1))
        ax.w_zaxis.set_pane_color((1, 1, 1, 1))
        ax.set_title(['Top Right', 'Top Left', 'Bottom Right', 'Bottom Left'][i])

    #for m in range(50):
    #    x, z, y = frame[m*4:m*4+3]
    #    ax.text(x, y, z, str(m))

    plt.gcf().set_size_inches(12, 10)
    #plt.savefig('reach-targets-with-variance.pdf', dpi=600)
    plt.show()