{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "from data.load_all_datasets import load_data_train_test\n",
    "\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "np.random.seed(1337)  # Random seed for reproducibility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load in all datasets and sort them by complexity\n",
    "metadata = sorted(\n",
    "    load_data_train_test(), \n",
    "    key=lambda x: x['train']['n_samples']**2*x['train']['n_features']**3  # O(n**2 * m**3)\n",
    ")\n",
    "dataset = metadata[0]  # Take the dataset which is expected to take the least long "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read in the datafiles, split them into features and labels\n",
    "train_df = pd.read_csv(dataset['train']['data_path'])\n",
    "test_df = pd.read_csv(dataset['test']['data_path'])\n",
    "X_train = train_df.drop('target', axis=1)\n",
    "y_train = train_df['target']\n",
    "X_test = test_df.drop('target', axis=1)\n",
    "y_test = test_df['target']\n",
    "\n",
    "# Map the labels to the range [0, ..., C-1] with C the number of classes\n",
    "map_dict = {}\n",
    "for j, c in enumerate(np.unique(y_train)):\n",
    "    map_dict[c] = j\n",
    "y_train = y_train.map(map_dict) \n",
    "y_test = y_test.map(map_dict)\n",
    "\n",
    "# Convert everything to numpy arrays\n",
    "X_train = X_train.values\n",
    "X_test = X_test.values\n",
    "y_train = y_train.values\n",
    "y_test = y_test.values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. Brute-force algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from brute_force import BruteForceExtractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "timeseries: 100%|██████████| 67/67 [02:47<00:00,  2.50s/it]\n"
     ]
    }
   ],
   "source": [
    "bfe = BruteForceExtractor()\n",
    "# Let's extract shapelets of a specific length, else it would take quite a while...\n",
    "shapelets = bfe.extract(X_train, y_train, nr_shapelets=3, min_len=10, max_len=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32132 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc29e459b70>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "for shap in shapelets:\n",
    "    plt.plot(range(len(shap)), shap)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Fast Shapelets (a faster brute-force algorithm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fast import FastExtractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "timeseries: 100%|██████████| 67/67 [00:48<00:00,  1.38it/s]\n"
     ]
    }
   ],
   "source": [
    "fe = FastExtractor()\n",
    "shapelets = fe.extract(X_train, y_train, nr_shapelets=3, min_len=10, max_len=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32132 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc29e463a20>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "for shap in shapelets:\n",
    "    plt.plot(range(len(shap)), shap)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. SAX Shapelets (approximative algorithm with SAX representations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sax import SAXExtractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "length: 100%|██████████| 1/1 [00:17<00:00, 17.92s/it]\n"
     ]
    }
   ],
   "source": [
    "se = SAXExtractor()\n",
    "shapelets = se.extract(X_train, y_train, nr_shapelets=3, min_len=10, max_len=11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28180 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc29dbae2b0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "for shap in shapelets:\n",
    "    plt.plot(range(len(shap)), shap)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. Particle Swarm Optimization (a bio-inspired algorithm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pso import ParticleSwarmExtractor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gen\tevals\tavg    \tstd     \tmax     \n",
      "0  \t50   \t0.16075\t0.102844\t0.413352\n",
      "1  \t50   \t0.149065\t0.116951\t0.413352\n",
      "2  \t50   \t0.156983\t0.103128\t0.413352\n",
      "3  \t50   \t0.152819\t0.10086 \t0.413352\n",
      "4  \t50   \t0.151428\t0.103662\t0.413352\n",
      "5  \t50   \t0.158191\t0.104338\t0.413352\n"
     ]
    }
   ],
   "source": [
    "pse = ParticleSwarmExtractor()\n",
    "shapelets = pse.extract(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22068 characters]\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fc29da95b00>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(10, 5))\n",
    "for shap in shapelets:\n",
    "    plt.plot(range(len(shap)), shap)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
