{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "modified-scott",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-02-23 17:36:44.293123: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory\n",
      "2022-02-23 17:36:44.293144: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.\n"
     ]
    }
   ],
   "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.SimpleGan import SimpleGan\n",
    "from library.convGAN import ConvGAN\n",
    "from library.LoRAS_ProWRAS import ProWRAS\n",
    "from library.dataset import DataSet\n",
    "from library.timing import timing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bright-creation",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "#plotCloud([[0,0], [1,1], [2,2]], [[0,1],[1,0]], [[2,0]]\n",
    "#          , outputFile=\"images/test.pdf\"\n",
    "#          , title=\"test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "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": "code",
   "execution_count": 4,
   "id": "c5c12a5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#data0 = data = np.array([\n",
    "#    np.array([float(x) for x in np.random.randint(1000, size=10)])\n",
    "#    for _i in range(10000)])\n",
    "#\n",
    "#data1 = data = np.array([\n",
    "#    np.array([float(x) for x in np.random.randint(1000, size=10)])\n",
    "#    for _i in range(100)])\n",
    "#\n",
    "#data = DataSet(data0, data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "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": 6,
   "id": "2d3beac6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "center = [0.525 0.525]\n",
      "radius = 0.8131727983645297\n"
     ]
    }
   ],
   "source": [
    "statistic(np.array([[1.0, 0.0], [0.0, 1.0], [0.0, 0.0], [1.1, 1.1]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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(syntheticPoints)\n",
    "    plotCloud(data.data0\n",
    "              , data.data1\n",
    "              , syntheticPoints\n",
    "              , outputFile=f\"images/example_{dataSetName}_{name}.pdf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "60137f8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "gans = {\n",
    "    #\"LoRAS\": SimpleGan(data.data0.shape[1]),\n",
    "    \"ProWRAS\": ProWRAS(),\n",
    "    \"simpleGAN\": SimpleGan(data.data0.shape[1]),\n",
    "    \"convGAN\": ConvGAN(data.data0.shape[1])\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9fd46db5",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======== basis data ========\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 348888 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======== ProWRAS ========\n",
      "duration: #1 12.8430s\n",
      "center = [1.34143114 1.31194586 1.51595098 1.4713993  0.94137914 1.1345912 ]\n",
      "radius = 2.0653264115717667\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 1970888 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======== simpleGAN ========\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-02-23 17:37:01.678542: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory\n",
      "2022-02-23 17:37:01.678576: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)\n",
      "2022-02-23 17:37:01.678591: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (sbi-klabautermann): /proc/driver/nvidia/version does not exist\n",
      "2022-02-23 17:37:01.678755: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA\n",
      "To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "duration: #1 21.4286s\n",
      "center = [ 1.  1.  1.  1. -1.  1.]\n",
      "radius = 2.274368289511074e-06\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 381244 characters]\n",
      "text/plain": [
       "<Figure size 6000x6000 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======== convGAN ========\n",
      "Model: \"model_1\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                   Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      " input_2 (InputLayer)           [(None, 6)]          0           []                               \n",
      "                                                                                                  \n",
      " tf.reshape (TFOpLambda)        (1, 5, 6)            0           ['input_2[0][0]']                \n",
      "                                                                                                  \n",
      " conv1d (Conv1D)                (1, 3, 6)            114         ['tf.reshape[0][0]']             \n",
      "                                                                                                  \n",
      " flatten (Flatten)              (1, 18)              0           ['conv1d[0][0]']                 \n",
      "                                                                                                  \n",
      " dense_8 (Dense)                (1, 25)              475         ['flatten[0][0]']                \n",
      "                                                                                                  \n",
      " reshape (Reshape)              (1, 5, 5)            0           ['dense_8[0][0]']                \n",
      "                                                                                                  \n",
      " tf.math.reduce_sum (TFOpLambda  (1, 5)              0           ['reshape[0][0]']                \n",
      " )                                                                                                \n",
      "                                                                                                  \n",
      " lambda (Lambda)                (1, 5)               0           ['tf.math.reduce_sum[0][0]']     \n",
      "                                                                                                  \n",
      " tf.math.reciprocal (TFOpLambda  (1, 5)              0           ['lambda[0][0]']                 \n",
      " )                                                                                                \n",
      "                                                                                                  \n",
      " multiply (Multiply)            (1, 5, 5)            0           ['tf.math.reciprocal[0][0]',     \n",
      "                                                                  'reshape[0][0]']                \n",
      "                                                                                                  \n",
      " tf.__operators__.getitem (Slic  (5, 5)              0           ['multiply[0][0]']               \n",
      " ingOpLambda)                                                                                     \n",
      "                                                                                                  \n",
      " tf.compat.v1.transpose (TFOpLa  (5, 5)              0           ['tf.__operators__.getitem[0][0]'\n",
      " mbda)                                                           ]                                \n",
      "                                                                                                  \n",
      " tf.linalg.matmul (TFOpLambda)  (5, 6)               0           ['tf.compat.v1.transpose[0][0]', \n",
      "                                                                  'input_2[0][0]']                \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 589\n",
      "Trainable params: 589\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n",
      "None\n",
      "\n",
      "\n",
      "Model: \"model_2\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " input_3 (InputLayer)        [(None, 6)]               0         \n",
      "                                                                 \n",
      " dense_9 (Dense)             (None, 250)               1750      \n",
      "                                                                 \n",
      " dense_10 (Dense)            (None, 125)               31375     \n",
      "                                                                 \n",
      " dense_11 (Dense)            (None, 2)                 252       \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 33,377\n",
      "Trainable params: 0\n",
      "Non-trainable params: 33,377\n",
      "_________________________________________________________________\n",
      "None\n",
      "\n",
      "\n",
      "Model: \"model_3\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                   Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      " input_4 (InputLayer)           [(None, 6)]          0           []                               \n",
      "                                                                                                  \n",
      " lambda_1 (Lambda)              (None, 6)            0           ['input_4[0][0]']                \n",
      "                                                                                                  \n",
      " model_1 (Functional)           (5, 6)               589         ['lambda_1[0][0]']               \n",
      "                                                                                                  \n",
      " lambda_2 (Lambda)              (None, 6)            0           ['input_4[0][0]']                \n",
      "                                                                                                  \n",
      " tf.concat (TFOpLambda)         (None, 6)            0           ['model_1[0][0]',                \n",
      "                                                                  'lambda_2[0][0]']               \n",
      "                                                                                                  \n",
      " model_2 (Functional)           (None, 2)            33377       ['tf.concat[0][0]']              \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 33,966\n",
      "Trainable params: 589\n",
      "Non-trainable params: 33,377\n",
      "__________________________________________________________________________________________________\n",
      "None\n",
      "\n",
      "\n",
      "10 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "20 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "30 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "40 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "50 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "60 neighbourhood batches trained; running neighbourhood epoch 1\n",
      "Neighbourhood epoch 1 complete\n",
      "70 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "80 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "90 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "100 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "110 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "120 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "130 neighbourhood batches trained; running neighbourhood epoch 2\n",
      "Neighbourhood epoch 2 complete\n",
      "140 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "150 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "160 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "170 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "180 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "190 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "200 neighbourhood batches trained; running neighbourhood epoch 3\n",
      "Neighbourhood epoch 3 complete\n",
      "210 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "220 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "230 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "240 neighbourhood batches trained; running neighbourhood epoch 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "250 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "260 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "270 neighbourhood batches trained; running neighbourhood epoch 4\n",
      "Neighbourhood epoch 4 complete\n",
      "280 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "290 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "300 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "310 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "320 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "330 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "340 neighbourhood batches trained; running neighbourhood epoch 5\n",
      "Neighbourhood epoch 5 complete\n",
      "350 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "360 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "370 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "380 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "390 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "400 neighbourhood batches trained; running neighbourhood epoch 6\n",
      "Neighbourhood epoch 6 complete\n",
      "410 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "420 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "430 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "440 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "450 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "460 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "470 neighbourhood batches trained; running neighbourhood epoch 7\n",
      "Neighbourhood epoch 7 complete\n",
      "480 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "490 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "500 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "510 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "520 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "530 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "540 neighbourhood batches trained; running neighbourhood epoch 8\n",
      "Neighbourhood epoch 8 complete\n",
      "550 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "560 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "570 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "580 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "590 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "600 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "610 neighbourhood batches trained; running neighbourhood epoch 9\n",
      "Neighbourhood epoch 9 complete\n",
      "620 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "630 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "640 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "650 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "660 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "670 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "680 neighbourhood batches trained; running neighbourhood epoch 10\n",
      "Neighbourhood epoch 10 complete\n",
      "690 neighbourhood batches trained; running neighbourhood epoch 11\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 160216 characters]\n",
      "text/plain": [
       "<Figure size 1152x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "duration: #1 80.4662s\n",
      "center = [1.1970533 1.185401  1.3975617 1.3076869 0.8633356 1.0025151]\n",
      "radius = 2.8725808476855828\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 1534044 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)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
