{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Sakoe-Chiba band\n\nThis example explains how to set the `window_size` parameter of the Sakoe-Chiba\nband when computing the Dynamic Time Warping (DTW) with\n``method == \"sakoechiba\"``. The Sakoe-Chiba region is defined through a\n`window_size` parameter which determines the largest temporal shift allowed\nfrom the diagonal in the direction of the longest time series. It is\nimplemented in :func:`pyts.metrics.sakoe_chiba_band`. The window size can\nbe either set relatively to the length of the longest time series as a ratio\nbetween 0 and 1, or manually if an integer is given.\n\nThis example visualizes the Sakoe-Chiba band in different scenarios:\n  * the degenerate case: ``window_size = 0``,\n  * a relative size: ``window_size = 0.4``, and\n  * an absolute size: ``window_size = 4``.\n\nThe last two cases are equivalent since ``0.4 * 10 = 4``.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Author: Hicham Janati <hicham.janati@inria.fr>\n#         Johann Faouzi <johann.faouzi@gmail.com>\n# License: BSD-3-Clause\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom pyts.metrics import sakoe_chiba_band\nfrom pyts.metrics.dtw import _check_sakoe_chiba_params\n\n\n# #####################################################################\n# We write a function to visualize the sakoe-chiba band for different\n# time series lengths.\n\n\ndef plot_sakoe_chiba(n_timestamps_1, n_timestamps_2, window_size=0.5, ax=None):\n    \"\"\"Plot the Sakoe-Chiba band.\"\"\"\n    region = sakoe_chiba_band(n_timestamps_1, n_timestamps_2, window_size)\n    scale, horizontal_shift, vertical_shift = \\\n        _check_sakoe_chiba_params(n_timestamps_1, n_timestamps_2, window_size)\n    mask = np.zeros((n_timestamps_2, n_timestamps_1))\n    for i, (j, k) in enumerate(region.T):\n        mask[j:k, i] = 1.\n\n    plt.imshow(mask, origin='lower', cmap='Wistia', vmin=0, vmax=1)\n\n    sz = max(n_timestamps_1, n_timestamps_2)\n    x = np.arange(-1, sz + 1)\n    lower_bound = scale * (x - horizontal_shift) - vertical_shift\n    upper_bound = scale * (x + horizontal_shift) + vertical_shift\n    plt.plot(x, lower_bound, 'b', lw=2)\n    plt.plot(x, upper_bound, 'g', lw=2)\n    diag = (n_timestamps_2 - 1) / (n_timestamps_1 - 1) * np.arange(-1, sz + 1)\n    plt.plot(x, diag, 'black', lw=1)\n\n    for i in range(n_timestamps_1):\n        for j in range(n_timestamps_2):\n            plt.plot(i, j, 'o', color='green', ms=1)\n\n    ax.set_xticks(np.arange(-.5, n_timestamps_1, 1), minor=True)\n    ax.set_yticks(np.arange(-.5, n_timestamps_2, 1), minor=True)\n    plt.grid(which='minor', color='b', linestyle='--', linewidth=1)\n    plt.xticks(np.arange(0, n_timestamps_1, 2))\n    plt.yticks(np.arange(0, n_timestamps_2, 2))\n    plt.xlim((-0.5, n_timestamps_1 - 0.5))\n    plt.ylim((-0.5, n_timestamps_2 - 0.5))\n\n\nwindow_sizes = [0, 0.4, 4]\n\nrc = {\"font.size\": 14, \"axes.titlesize\": 10,\n      \"xtick.labelsize\": 8, \"ytick.labelsize\": 8}\nplt.rcParams.update(rc)\n\n\nlengths = [(10, 10), (10, 5), (5, 10)]\ny_coordinates = [0.915, 0.60, 0.35]\n\nplt.figure(figsize=(10, 8))\n\nfor i, ((n1, n2), y) in enumerate(zip(lengths, y_coordinates)):\n    for j, window_size in enumerate(window_sizes):\n        ax = plt.subplot(3, 3, i * 3 + j + 1)\n        plot_sakoe_chiba(n1, n2, window_size, ax)\n        plt.title('window_size = {}'.format(window_size))\n        if j == 1:\n            plt.figtext(0.5, y, 'sakoe_chiba_band({}, {})'.format(n1, n2),\n                        ha='center')\nplt.subplots_adjust(hspace=0.4)\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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}