{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d521079d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import numpy as np\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "from torch.utils.data import DataLoader, Dataset\n",
    "import torch.nn.functional as F\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d40fc35",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('220831_output_set276_e100_8-DOl9191923-1best.pickle', 'rb') as s:\n",
    "    train, validation, abnormal, train_ori, validation_ori, abnormal_ori, train_loss, validation_loss, abnormal_loss = pickle.load(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8397e786",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a62cf8a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(validation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8d42e55",
   "metadata": {},
   "outputs": [],
   "source": [
    "len(abnormal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9326dc0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import roc_curve\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.metrics import confusion_matrix, plot_confusion_matrix\n",
    "from sklearn import metrics\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import cross_val_score, cross_validate\n",
    "import itertools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e231820",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_roc_curve(fper, tper):\n",
    "    plt.figure(figsize = (11, 10))\n",
    "    plt.plot(fper, tper, color = 'red', label= 'ROC')\n",
    "    plt.plot([0, 1], [0, 1], color = 'green', linestyle = 'dashed')\n",
    "    plt.xlabel(\"False Positive Rate\", fontsize = 18)\n",
    "    plt.ylabel(\"True Positive Rate\", fontsize = 18)\n",
    "    plt.title(\"Receiver Operating Characteristic Curve\", fontsize = 28, fontweight = 'bold')\n",
    "    plt.legend(loc = 'best', fontsize = 18)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ef3a577",
   "metadata": {},
   "outputs": [],
   "source": [
    "def roc_score(val_diff, ab_diff, point):\n",
    "    score = []\n",
    "    real_norm = np.zeros(len(val_diff))\n",
    "    real_ab = np.ones(len(ab_diff))\n",
    "    real = np.concatenate([real_norm, real_ab])\n",
    "    for i in range(point): \n",
    "        out_norm = np.array(val_diff) > i\n",
    "        out_ab = np.array(ab_diff) > i\n",
    "        out = np.concatenate([out_norm, out_ab])\n",
    "        fper, tper, thresholds = roc_curve(real, out)\n",
    "        score.append(roc_auc_score(real, out))\n",
    "    best = np.min(np.argwhere(score ==np.max(score)))\n",
    "    out_norm_best = np.array(val_diff) > best\n",
    "    out_ab_best = np.array(ab_diff) > best\n",
    "    out = np.concatenate([out_norm_best, out_ab_best])\n",
    "    fper, tper, thresholds = roc_curve(real, out)\n",
    "    print(metrics.confusion_matrix(real, out))\n",
    "    #plot_confusion_matrix()\n",
    "    print(f'Recall: {metrics.recall_score(real, out)}')\n",
    "    print(f'F1 Score: {metrics.f1_score(real, out)}')\n",
    "    plot_roc_curve(fper, tper)\n",
    "        \n",
    "    return real, np.concatenate([np.array(val_diff), np.array(ab_diff)]), np.min(np.argwhere(score ==np.max(score))+1), np.max(np.argwhere(score ==np.max(score))+1), np.max(score), #score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb0b5b42",
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "real_labels, X, argmin, argmax, auroc = roc_score(validation_loss, abnormal_loss, 30000)\n",
    "print(f'AUROC: {auroc}')\n",
    "print(argmin)\n",
    "print(argmax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7a50947",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_confusion_matrix2(con_mat, labels, title='Confusion Matrix', cmap=plt.cm.get_cmap('Blues'), normalize=False):\n",
    "    plt.imshow(con_mat, interpolation='nearest', cmap=cmap)\n",
    "    plt.title(title, size = 18, fontweight = 'bold')\n",
    "    plt.colorbar()\n",
    "    marks = np.arange(len(labels))\n",
    "    nlabels = []\n",
    "    for k in range(len(con_mat)):\n",
    "        n = sum(con_mat[k])\n",
    "        nlabel = '{0}(n={1})'.format(labels[k],n)\n",
    "        nlabels.append(nlabel)\n",
    "    plt.xticks(marks, labels)\n",
    "    plt.yticks(marks, nlabels)\n",
    "\n",
    "    thresh = con_mat.max() / 2.\n",
    "    if normalize:\n",
    "        for i, j in itertools.product(range(con_mat.shape[0]), range(con_mat.shape[1])):\n",
    "            plt.text(j, i, '{0}%'.format(con_mat[i, j] * 100 / n), horizontalalignment=\"center\", color=\"white\" if con_mat[i, j] > thresh else \"black\")\n",
    "    else:\n",
    "        for i, j in itertools.product(range(con_mat.shape[0]), range(con_mat.shape[1])):\n",
    "            plt.text(j, i, con_mat[i, j], horizontalalignment=\"center\", color=\"white\" if con_mat[i, j] > thresh else \"black\")\n",
    "    plt.tight_layout()\n",
    "    plt.ylabel('True', size = 13)\n",
    "    plt.xlabel('Predicted', size = 13)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bea8411",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "def clf(x):\n",
    "    result = x > (argmin+argmax)/2\n",
    "    result = result.astype(np.int32)\n",
    "    return result\n",
    "    \n",
    "real_labels_list = real_labels.astype(np.int32).tolist()\n",
    "pred_labels_list = clf(X).tolist()\n",
    "confusion_matrix = confusion_matrix(real_labels_list, pred_labels_list)\n",
    "plot_confusion_matrix2(confusion_matrix, labels=['Normal', 'Abnormal'], normalize=False)"
   ]
  }
 ],
 "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.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
