{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "modified-scott",
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy as np\n",
    "#import seaborn as sns\n",
    "import library.analysis as A \n",
    "#import matplotlib.pyplot as plt\n",
    "import tensorflow as tf\n",
    "from library.exercise import plotCloud\n",
    "from library.generators import Autoencoder, SimpleGan, ConvGAN, ProWRAS\n",
    "from library.dataset import DataSet\n",
    "from library.timing import timing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4ba7f95a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Load 'data_input/folding_car_good'\n",
      "from pickle file\n",
      "Data loaded.\n"
     ]
    }
   ],
   "source": [
    "dataSetName = \"imblearn_ozone_level\"\n",
    "dataSetName = \"folding_abalone_17_vs_7_8_9_10\"\n",
    "dataSetName = \"folding_abalone9-18\"\n",
    "dataSetName = \"folding_yeast4\"\n",
    "dataSetName = \"folding_car_good\"\n",
    "data = A.loadDataset(\"data_input/\" + dataSetName)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "front-broadcast",
   "metadata": {},
   "source": [
    "data = DataSet(\n",
    "    np.array([\n",
    "    [0,1],\n",
    "    [0,2],\n",
    "    [0,3],\n",
    "    [0,4],\n",
    "    [1,0],\n",
    "    [1,1],\n",
    "    [1,2],\n",
    "    [1,3],\n",
    "    [1,4],\n",
    "    [2,0],\n",
    "    [2,1],\n",
    "    [2,2],\n",
    "    [2,3],\n",
    "    [2,4],\n",
    "    [3,0],\n",
    "    [3,1],\n",
    "    [3,2],\n",
    "    [3,3],\n",
    "    [3,4],\n",
    "    ])\n",
    "    , np.array([\n",
    "    [0.5, 0.5],\n",
    "    [1.5, 1.5],\n",
    "    [0.5, 1.5],\n",
    "    [1.5, 0.5]\n",
    "    ]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5918ec68",
   "metadata": {},
   "outputs": [],
   "source": [
    "def statistic(points):\n",
    "    n = 0\n",
    "    center = 0.0 * points[0]\n",
    "    for p in points:\n",
    "        center = center + p\n",
    "        n += 1\n",
    "    center *= (1.0 / n)\n",
    "    \n",
    "    radius = 0.0\n",
    "    for p in points:\n",
    "        d = math.sqrt(sum([(a-b)*(a-b) for (a,b) in zip(center, p)]))\n",
    "        radius = max(radius, d)\n",
    "    \n",
    "    print(f\"center = {center}\")\n",
    "    print(f\"radius = {radius}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2d3beac6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "center = [0.5 0.5]\n",
      "radius = 0.7071067811865476\n"
     ]
    }
   ],
   "source": [
    "statistic(np.array([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0], [1.0, 1.0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "02a5eedd",
   "metadata": {},
   "outputs": [],
   "source": [
    "def showSamplePlot(gan, name):\n",
    "    print(\"\")\n",
    "    print(f\"======== {name} ========\")\n",
    "    \n",
    "    t = timing(\"duration\")\n",
    "    t.start()\n",
    "    gan.reset()\n",
    "    gan.train(data)\n",
    "    syntheticPoints = gan.generateData(max(\n",
    "        1, data.data0.shape[0] - data.data1.shape[0]))\n",
    "    t.stop()\n",
    "    print(t)\n",
    "    statistic(data.data1)\n",
    "    statistic(syntheticPoints)\n",
    "    \n",
    "    di = None\n",
    "    dx = None\n",
    "    for x in syntheticPoints:\n",
    "        d = distanceToSet(x, data.data1)\n",
    "        if di is None:\n",
    "            di = d\n",
    "            dx = d\n",
    "        else:\n",
    "            di = min(di, d)\n",
    "            dx = max(dx, d)\n",
    "            \n",
    "    print(f\"distance: {di} ... {dx}\")\n",
    "    \n",
    "    plotCloud(data.data0\n",
    "              , data.data1\n",
    "              , syntheticPoints\n",
    "              , outputFile=f\"images/example_{dataSetName}_{name}.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "objective-fifth",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dist(x, y):\n",
    "    return math.sqrt(sum([(z[0] - z[1]) * (z[0] - z[1]) for z in zip(x,y)]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fallen-guyana",
   "metadata": {},
   "outputs": [],
   "source": [
    "def distanceToSet(point, dataSet):\n",
    "    d = None\n",
    "    for p in dataSet:\n",
    "        if d is None:\n",
    "            d = dist(point, p)\n",
    "        else:\n",
    "            d = max(d, dist(point, p))\n",
    "    return d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "60137f8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "gans = {\n",
    "    #\"LoRAS\": SimpleGan(data.data1.shape[1]),\n",
    "    #\"ProWRAS\": ProWRAS(),\n",
    "    \"simpleGAN\": SimpleGan(data.data1.shape[1], noiseSize=3, epochs=15, dLayers=[6], gLayers=[6], withTanh=True),\n",
    "    #\"convGAN\": ConvGAN(data.data1.shape[1])\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "japanese-beach",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== basis data ========\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 451448 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======== simpleGAN ========\n",
      "Epoch 1/15\n",
      "Epoch 2/15\n",
      "Epoch 3/15\n",
      "Epoch 4/15\n",
      "Epoch 5/15\n",
      "Epoch 6/15\n",
      "Epoch 7/15\n",
      "Epoch 8/15\n",
      "Epoch 9/15\n",
      "Epoch 10/15\n",
      "Epoch 11/15\n",
      "Epoch 12/15\n",
      "Epoch 13/15\n",
      "Epoch 14/15\n",
      "Epoch 15/15\n",
      "duration: #1 63.4990s\n",
      "center = [1.33333333 1.33333333 1.56521739 1.47826087 0.95652174 1.13043478]\n",
      "radius = 2.366627172917898\n",
      "center = [0.9974712  0.9970208  0.9937588  0.9908424  0.43686813 0.70359564]\n",
      "radius = 1.6334166782496207\n",
      "distance: 2.8306527671086847 ... 3.918209259576518\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 608800 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "print(f\"======== basis data ========\")\n",
    "plotCloud(data.data0, data.data1, dataNew=None\n",
    "          , outputFile=f\"images/example_{dataSetName}_basisData.pdf\"\n",
    "          , title=\"basis data\")\n",
    "\n",
    "for name in gans.keys():\n",
    "    showSamplePlot(gans[name], name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "silent-seeking",
   "metadata": {
    "scrolled": false
   },
   "source": [
    "a = Autoencoder(data.data0.shape[1], 2)\n",
    "a.reset()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "regulation-leisure",
   "metadata": {},
   "source": [
    "a.train(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adapted-anger",
   "metadata": {},
   "source": [
    "showSamplePlot(Autoencoder(data.data0.shape[1], 3), \"autoencoder\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "insured-large",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/5\n",
      "Epoch 2/5\n",
      "Epoch 3/5\n",
      "Epoch 4/5\n",
      "Epoch 5/5\n",
      "[0.9979738  0.93783873 0.9901252  0.9959885  0.99767363 0.99118567] → 2.8616228976928197\n",
      "[0.99476796 0.9121443  0.9822218  0.9908665  0.9945598  0.97986954] → 2.881501722105176\n",
      "[0.9970843  0.92875004 0.9893062  0.9945789  0.99691665 0.98613864] → 2.867595464792821\n",
      "[0.9996704  0.9682388  0.9981135  0.99923086 0.99961984 0.996805  ] → 2.8424038644545138\n",
      "[0.99918157 0.9545885  0.99538296 0.99815524 0.9989927  0.9956878 ] → 2.849859246974232\n",
      "[0.999127   0.95439833 0.99586743 0.998161   0.99903095 0.99385214] → 2.8502471224786374\n",
      "[0.9987227  0.9488785  0.9917154  0.99740124 0.99848914 0.99375063] → 2.8553086990821104\n",
      "[1.         0.99829906 0.9999923  0.99999917 0.99999964 0.99999106] → 2.829037709167106\n",
      "[0.9999798  0.9886596  0.99970084 0.9999309  0.9999689  0.9997081 ] → 2.8327841201789283\n",
      "[0.99957615 0.96655065 0.99580824 0.99903125 0.9994556  0.9973578 ] → 2.8444584078317923\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 896584 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "g = SimpleGan(data.data0.shape[1], noiseSize=3, epochs=5, dLayers=[6,4,3], gLayers=[3,4,6], withTanh=True)\n",
    "g.reset()\n",
    "g.train(data)\n",
    "s = g.generateData(10)\n",
    "for x in s:\n",
    "    d = distanceToSet(x, data.data1)\n",
    "    print(f\"{x} → {d}\")\n",
    "plotCloud(data.data0, data.data1, s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ahead-regulation",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "indonesian-hormone",
   "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
