{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import torch\n",
    "import Loaders\n",
    "from torchvision import transforms\n",
    "import defs\n",
    "import Models as M\n",
    "import csv\n",
    "import cv2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "\n",
    "Dataset = Loaders.RARP_DatasetCreator(\n",
    "    \"./DataSet_Ando_All\",\n",
    "    FoldSeed=505,\n",
    "    createFile=True,\n",
    "    SavePath=\"./DataSet_Ando_All\",\n",
    "    Fold=5,\n",
    "    removeBlackBar=True\n",
    ")\n",
    "\n",
    "Dataset.CreateFolds()\n",
    "    \n",
    "Dataset.mean, Dataset.std = ([30.38144216, 42.03988769, 97.8896116], [40.63141752, 44.26910074, 50.29294373])\n",
    "fullDataset = pd.read_csv(Dataset.CVS_File, usecols=[\"name\", \"class\"])\n",
    "\n",
    "foldfile = np.load(Dataset.CVS_File.parent/\"Folds.npy\", allow_pickle=True)\n",
    "folds = np.array_split(foldfile, len(foldfile)/3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "lastID = 224\n",
    "rootFile = Path(\"./DataSet_big/\")\n",
    "cNotNVB, cNVB = (0, 0)\n",
    "with open(Dataset.CVS_File, \"a\", newline='') as csvfile:\n",
    "    writerOBJ = csv.writer(csvfile)\n",
    "    for i, file in enumerate(rootFile.glob(\"**/*.tiff\")):\n",
    "        id = lastID + i\n",
    "        if not len (fullDataset.loc[(fullDataset[\"class\"] == file.parent.name) & (fullDataset[\"name\"] == file.name)]):\n",
    "            tempImg = cv2.imread(str(file), cv2.IMREAD_COLOR)\n",
    "            tempImg = Dataset._removeBlackBorder(tempImg)\n",
    "            \n",
    "            cNotNVB += 1 if file.parent.name == \"NOT_NVB\" else 0\n",
    "            cNVB += 1 if file.parent.name == \"NVB\" else 0\n",
    "            numFold = (cNotNVB if file.parent.name == 'NOT_NVB' else cNVB) % 5\n",
    "            \n",
    "            writerOBJ.writerow([lastID + i, 0 if file.parent.name == \"NOT_NVB\" else 1 , file.parent.name, \"\", file.name, \"0\", \"0\", \"0\", \"0\", \"0\", \"0\"])\n",
    "            folds[numFold][2].append(id)\n",
    "            \n",
    "            np.save(Dataset.CVS_File.parent.parent/f\"fold_{numFold}/test/{file.parent.name.replace('T', '')}/Img0-{id}.npy\", tempImg)\n",
    "            \n",
    "    csvfile.close()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "folds[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "dataset = pd.read_csv(Dataset.CVS_File, usecols=[\"id\", \"label\", \"class\", \"name\"])\n",
    "listNames = pd.read_excel(Path(\"./List Review labels.xlsx\", header=0,))\n",
    "\n",
    "foldNum, th, epoch = (4, 0.50, 104)\n",
    "model = M.RARP_NVB_ResNet50_VAN.load_from_checkpoint(Path(f\"./log_X12_VAN_Review/lightning_logs/version_{foldNum}/checkpoints/RARP-epoch={epoch}.ckpt\"))\n",
    "model.eval()\n",
    "model.to(device)\n",
    "\n",
    "dumpCVS = Dataset.CVS_File.parent/\"analisis.csv\"\n",
    "\n",
    "rootFile = Dataset.CVS_File.parent.parent/f\"fold_{foldNum}\"\n",
    "\n",
    "testtransform =  torch.nn.Sequential(\n",
    "    transforms.Resize(256, antialias=True, interpolation=transforms.InterpolationMode.BICUBIC),\n",
    "    transforms.CenterCrop(224),\n",
    "    transforms.Normalize(Dataset.mean, Dataset.std)\n",
    ").to(device)\n",
    "\n",
    "foldDataset = dataset.loc[dataset[\"id\"].isin(folds[foldNum][2])] #[fold][train|eval|test]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "with torch.no_grad():\n",
    "    fileMode = \"x\" if not dumpCVS.exists() else \"a\"\n",
    "    \n",
    "    with open(dumpCVS, fileMode, newline='') as csvfile: \n",
    "        writerOBJ = csv.writer(csvfile)\n",
    "        if fileMode == \"x\":\n",
    "            writerOBJ.writerow([\"fold\", \"th\", \"name\", \"P(x)\", \"old Label (GT)\", \"new Label (GT)\", f\"prediction label (TH)\", \"prediction label (0.5)\"])\n",
    "        \n",
    "        for _, row in foldDataset.iterrows():\n",
    "            imgPath = rootFile/f\"test/{'NO_NVB' if row['class'] == 'NOT_NVB' else row['class']}/Img0-{row['id']}.npy\"\n",
    "            rawIMG = defs.load_file_tensor(imgPath).to(device)\n",
    "            \n",
    "            rawIMG = testtransform(rawIMG)\n",
    "            rawIMG = rawIMG.repeat(1, 1, 1, 1)\n",
    "            \n",
    "            pred, _, _ = model((rawIMG, rawIMG))\n",
    "            pred = pred.flatten()\n",
    "            pred = torch.sigmoid(pred)\n",
    "            \n",
    "            newInfo = listNames.loc[listNames[\"列1\"].astype(str)+\".tiff\" == row[\"name\"]]\n",
    "            newLabel = newInfo[\"再判定\"].to_list()\n",
    "            \n",
    "            if len(newLabel) == 0:\n",
    "                newLabel = None\n",
    "            else:\n",
    "                if pd.isna(newInfo[\"再判定\"]).values[0]:\n",
    "                    newLabel = \"*\"\n",
    "                else:\n",
    "                    newLabel = \"no_nvb\" if newInfo[\"再判定\"].to_list()[0] == \"なし\" else \"nvb\"\n",
    "            writerOBJ.writerow([foldNum, th, row[\"name\"], pred.item(), \"nvb\" if row[\"label\"] == 1 else \"no_nvb\", newLabel, \"NVB\" if pred > th else \"NO_NVB\", \"NVB\" if pred > 0.5 else \"NO_NVB\", ])\n",
    "    \n",
    "        csvfile.close()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "torch.hub.load('facebookresearch/dino:main', 'dino_resnet50')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pyRARP",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
