{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fill Modes (for Missing Dimensions)\n\nAn example plot of the different fill modes of :class:`sksfa.SFA`.\n\nThe input data does only have effective dimension 2 (the other dimensions are linearly dependent),\nbut 3 slow features are set to be extracted. The different fill_mode options will fill the missing\nfeatures in different ways.\nSetting 'fill_mode' to None, would make the transformer throw an exception.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom sksfa import SFA\nimport matplotlib.pyplot as plt\n\n\nn_samples = 500\nn_slow_features = 3\ndim = 4\nfill_modes = [\"noise\", \"zero\", \"fastest\"]\n\n# Generate different randomly shifted time-scales\nt = np.linspace(0, 2*np.pi, n_samples).reshape(n_samples, 1)\n\n# Generate latent cosine signals\nx = np.hstack([np.cos(t), 0.5 * np.cos(t), np.cos(2 * t), 1.5 * np.cos(t)])\n\n# Compute random affine mapping of cosines (observed)\nA = np.random.normal(0, 1, (dim, dim))\nb = np.random.normal(0, 2, (1, dim))\ndata = np.dot(x, A) + b\n\n# Extract slow features from observed data\n\n# Plot cosines, mapped data, and extracted features\nfig, ax = plt.subplots(2 + len(fill_modes), 1, sharex=True)\nfig.set_size_inches(8, 18)\nfig.subplots_adjust(hspace=0.5)\nfor d in range(n_slow_features):\n    ax[0].plot(x[:, d])\nax[1].plot(data)\nfor idx, fill_mode in enumerate(fill_modes):\n    sfa = SFA(n_slow_features, fill_mode=fill_mode)\n    slow_features = sfa.fit_transform(data)\n    ax[2 + idx].plot(slow_features[:, :-1])\n    ax[2 + idx].plot(slow_features[:, -1], linestyle=\":\", color=\"purple\")\n    ax[2 + idx].set_title(f\"Extracted features, fill_mode='{fill_mode}'\")\n    ax[2 + idx].set_xlabel(\"Time t\")\nax[0].set_title(\"x(t)\")\nax[1].set_title(\"A\u22c5x(t) + b\")\nfor idx in range(2 + len(fill_modes)):\n    ax[idx].set_ylabel(\"Features\")\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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}