{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n\nMocap Data\n==========\n\nSimple example.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom pydygp.linlatentforcemodels import MLFMAdapGrad\nfrom pydygp.pydygp_examples import MocapExample\nfrom pydygp.liealgebras import so\nfrom pydygp.linlatentforcemodels import MLFMAdapGrad\nfrom pydygp.probabilitydistributions import Normal\nfrom sklearn.gaussian_process.kernels import RBF\nfrom scipy.interpolate import interp1d\n\n\nData = MocapExample.load_data('lhumerus')\n\nmotions = [str(i) for i in range(1, 6)]\n\ntt = Data['times']\nY = np.column_stack([Data[m].T.ravel() for m in motions])\n\n\nmlfm = MLFMAdapGrad(so(4), R=2, lf_kernels=[RBF(),]*2)\n\n# Define the priors\nbeta_prior = Normal(scale=5)*((mlfm.dim.R+1)*mlfm.dim.D)\nmap_res = mlfm.fit(tt, Y,\n                   beta_prior=beta_prior,\n                   logtau_is_fixed=False)\n\nfig = plt.figure()\nfor k in range(mlfm.dim.K):\n    ax = fig.add_subplot(2, 2, k+1)\n    for m in motions:\n        ax.plot(Data['times'], Data[m][:, k], '+')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Demonstrate the reconstruction\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ttd = np.linspace(tt[0], tt[-1], 50)\n\nu = [interp1d(tt, g, kind='cubic', fill_value='extrapolate')\n     for g in map_res.g]\nx0 = sum(Data[m][0, :] for m in motions) / len(motions)\n\nreconstruction, _ = mlfm.sim(x0, ttd, beta=map_res.beta, latent_forces=u)\n\nfig, ax = plt.subplots()\nfor m in motions:\n    ax.plot(Data['times'], Data[m][:, 0], '+')\n\nax.plot(ttd, reconstruction[:, 0], 'k-', alpha=0.5, label='reconstruction')\nax.legend()\n\nfig2, ax2 = plt.subplots()\nax2.plot(tt, map_res.g.T, '+')\n\nEg, SDg = mlfm.predict_lf(ttd, return_std=True)\n\nfor eg in Eg:\n    ax2.plot(ttd, eg, '-', alpha=0.5)\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}