{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d69d2abf",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-03-24 12:30:42.246729: 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-03-24 12:30:42.246750: 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 library.analysis as A \n",
    "from library.exercise import plotCloud\n",
    "from library.generators import *\n",
    "from library.dataset import DataSet\n",
    "from library.timing import timing\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f3364eda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Load 'data_input/folding_abalone9-18'\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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d0ea209e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def avg(x):\n",
    "    return sum(x) / len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a11a121e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def testHisto(descTrainCount):\n",
    "    print(f\"======[ {descTrainCount} ]======\")\n",
    "    t = timing(f\"train with {descTrainCount} extra rounds\")\n",
    "    t.start()\n",
    "    g = ConvGAN(data.data1.shape[1], neb_epochs=10, withMajorhoodNbSearch=True)\n",
    "    g.reset()\n",
    "    g.train(data, descTrainCount)\n",
    "    t.stop()\n",
    "    print(t)\n",
    "    t0 = g.predictReal(data.data0)\n",
    "    print(\"majority \" + str((min(t0), avg(t0), max(t0))))\n",
    "    t1 = g.predictReal(data.data1)\n",
    "    print(\"minority \" + str((min(t1), avg(t1), max(t1))))\n",
    "    t2 = g.predictReal(g.generateData(data.data0.shape[0]))\n",
    "    print(\"synthetic \" + str((min(t2), avg(t2), max(t2))))\n",
    "    plt.hist(t0, label=\"majority\")\n",
    "    #plt.hist(t1, label=\"minority\")\n",
    "    plt.hist(t2, label=\"synthetic\")\n",
    "    plt.show()\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "662817c4",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======[ 0 ]======\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-03-24 12:30:44.032119: 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-03-24 12:30:44.032141: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)\n",
      "2022-03-24 12:30:44.032157: 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-03-24 12:30:44.032352: 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": [
      "train with 0 extra rounds: #1 44.1003s\n",
      "majority (0.36754203, 0.431634900921142, 0.60214233)\n",
      "minority (0.41898528, 0.5370993649675733, 0.8336862)\n",
      "synthetic (0.42654964, 0.5366197719369814, 0.8290475)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 4672 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 1 ]======\n",
      "train with 1 extra rounds: #1 72.5592s\n",
      "majority (0.054442644, 0.27122818303385043, 0.84951097)\n",
      "minority (0.2057668, 0.6238270089739845, 0.9983149)\n",
      "synthetic (0.20576864, 0.635948086987386, 0.99831486)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5928 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 2 ]======\n",
      "train with 2 extra rounds: #1 101.5201s\n",
      "majority (0.00344944, 0.15321989588882823, 0.94366026)\n",
      "minority (0.10768956, 0.6254928175892148, 0.9991397)\n",
      "synthetic (0.20021954, 0.6810118817712122, 0.9968319)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5600 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 3 ]======\n",
      "train with 3 extra rounds: #1 129.5592s\n",
      "majority (0.0005763471, 0.10390776793738754, 0.9897444)\n",
      "minority (0.04233092, 0.6437406504438037, 0.9999985)\n",
      "synthetic (0.15036112, 0.7553514468825607, 0.99995005)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5328 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 4 ]======\n",
      "train with 4 extra rounds: #1 158.6489s\n",
      "majority (0.0010927618, 0.09106668258959047, 0.99234223)\n",
      "minority (0.032084554, 0.652064446182478, 1.0)\n",
      "synthetic (0.10200524, 0.7617625614036151, 0.9999969)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5292 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 5 ]======\n",
      "train with 5 extra rounds: #1 188.0797s\n",
      "majority (0.00028133392, 0.10637742319889096, 0.9951868)\n",
      "minority (0.039069414, 0.6762245524497259, 1.0)\n",
      "synthetic (0.039138883, 0.7555334710763397, 0.9999926)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5296 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 6 ]======\n",
      "train with 6 extra rounds: #1 217.5180s\n",
      "majority (0.00011443569, 0.10461087292720267, 0.98728174)\n",
      "minority (0.028799742, 0.687773618669737, 0.99999905)\n",
      "synthetic (0.036519766, 0.728148201769772, 0.99999905)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5324 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 7 ]======\n",
      "train with 7 extra rounds: #1 247.2812s\n",
      "majority (5.7400057e-06, 0.07812106153224706, 0.995649)\n",
      "minority (0.017223567, 0.6630145311355591, 0.9999995)\n",
      "synthetic (0.09074798, 0.7825938416928788, 0.9999931)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5284 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 8 ]======\n",
      "train with 8 extra rounds: #1 274.0284s\n",
      "majority (0.0003386438, 0.10330177780851743, 0.9970418)\n",
      "minority (0.02112639, 0.6864672005176544, 1.0)\n",
      "synthetic (0.06407535, 0.8349364148169021, 0.99999785)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5308 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "======[ 9 ]======\n",
      "train with 9 extra rounds: #1 306.6393s\n",
      "majority (4.8418256e-05, 0.08757061694664493, 0.9976011)\n",
      "minority (0.03271407, 0.6800935970885413, 1.0)\n",
      "synthetic (0.12129837, 0.7845661920023588, 0.9999973)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 5292 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "for n in range(10):\n",
    "    testHisto(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19babca7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "05929c92",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A.runExercise(dataSetName, None, \"test\", A.generators[\"convGAN\"])"
   ]
  }
 ],
 "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
}
