{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "664095be-333a-4da6-b019-616325ab4762",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "import sparseconvnet as scn\n",
    "import numpy as np\n",
    "import csv\n",
    "import uproot\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import argparse\n",
    "import math\n",
    "import re\n",
    "import sys\n",
    "from timeit import default_timer as timer\n",
    "\n",
    "import os\n",
    "import shutil\n",
    "\n",
    "from tqdm import tqdm\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14ed9a1d-536c-40cd-b654-9b7528a4dd60",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_name = \"train_v1\"\n",
    "\n",
    "train_test_file = \"voxelized_toy_data_v1.pkl\"\n",
    "train_frac = 0.5\n",
    "\n",
    "ntrain = 100\n",
    "nval = 20\n",
    "nepoch = 2\n",
    "start_epoch = 0\n",
    "resolution = 1\n",
    "loose_cut = 2\n",
    "lr0 = 1e-5\n",
    "lr_decay = 0.05\n",
    "\n",
    "vertex_assign_cut = 0.0\n",
    "\n",
    "nIn = 1\n",
    "device = \"cpu\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c8cb5cd8-c583-4963-ada1-b2ad3c95ac9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "reps = 2 # Conv block repetition factor\n",
    "m = 16 # Unet number of features\n",
    "nPlanes = [m, 2*m, 4*m, 8*m, 16*m] # UNet number of features per level\n",
    "\n",
    "class DeepVtx(nn.Module):\n",
    "    '''\n",
    "    spatialSize seems need to be 2^n\n",
    "    '''\n",
    "    def __init__(self,  dimension = 3, device = 'cuda', spatialSize = 4096, nIn = 3, nClasses = 2):\n",
    "        nn.Module.__init__(self)\n",
    "        self.sparseModel = scn.Sequential().add(\n",
    "            scn.InputLayer(dimension, torch.LongTensor([spatialSize]*3), mode=3)).add(\n",
    "            scn.SubmanifoldConvolution(dimension, nIn, m, 3, False)).add(\n",
    "            scn.UNet(dimension, reps, nPlanes, residual_blocks=False, downsample=[2,2])).add(\n",
    "            scn.BatchNormReLU(m)).add(\n",
    "            scn.OutputLayer(dimension)).to(device)\n",
    "        self.inputLayer = scn.InputLayer(dimension, torch.LongTensor([spatialSize]*3), mode=3)\n",
    "        self.linear = nn.Linear(m, nClasses).to(device)\n",
    "    def forward(self,x):\n",
    "        x=self.sparseModel(x)\n",
    "        x=self.linear(x)\n",
    "        x=torch.sigmoid(x)\n",
    "        return x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6885ad38-963a-4e36-9342-734694bc04f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def scheduler_exp(optimizer, lr0, gamma, epoch):\n",
    "    lr = lr0*math.exp(-gamma*epoch)\n",
    "    for param_group in optimizer.param_groups:\n",
    "        param_group['lr'] = lr\n",
    "    return optimizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "188b7047-b631-4982-9eda-5a0f91b13f3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(train_test_file, \"rb\") as f:\n",
    "    all_dics, true_vtxs = pickle.load(f)\n",
    "\n",
    "\n",
    "train_index = round(len(all_dics) * train_frac)\n",
    "\n",
    "train_dics = all_dics[:train_index]\n",
    "test_dics = all_dics[train_index:]\n",
    "\n",
    "\n",
    "\n",
    "train_true_vtxs = true_vtxs[:train_index]\n",
    "test_true_vtxs = true_vtxs[train_index:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "87e024a4-1118-465b-bacc-27039ca61d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = \"train_files/\" + train_name\n",
    "if os.path.exists(path):\n",
    "    shutil.rmtree(path)\n",
    "\n",
    "os.mkdir(path)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f36e0037-aa81-4f72-bffb-e6206a774b60",
   "metadata": {},
   "outputs": [],
   "source": [
    "torch.set_num_threads(1)\n",
    "\n",
    "model = DeepVtx(dimension=3, nIn=nIn, device=device)\n",
    "model.train()\n",
    "\n",
    "criterion = nn.MSELoss().to(device)\n",
    "\n",
    "optimizer = optim.Adam(model.parameters(), lr=lr0)\n",
    "\n",
    "outfile_loss = open(path + '/loss.csv','a+')\n",
    "outfile_log = open(path + \"/log\", \"a+\")\n",
    "\n",
    "print('lr: {:.2e}*exp-{:.2e}*epoch start: {} ntrain: {} nval: {} device: {} nIn: {} resolution:{} loose_cut: {}'.format(\n",
    "        lr0, lr_decay, start_epoch, ntrain, nval, device, nIn, resolution, loose_cut\n",
    "    ), file=outfile_log, flush=True)\n",
    "print('train_test_file: {} '.format(\n",
    "    train_test_file\n",
    "), file=outfile_log, flush=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e8e80174-d0c8-4932-af0e-1d9b2e5668fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "55\n",
      "2\n",
      "17\n",
      "0\n",
      "89\n",
      "1\n",
      "23\n",
      "0\n",
      "84\n",
      "0\n",
      "1\n",
      "1\n",
      "154\n",
      "0\n",
      "34\n",
      "1\n",
      "39\n",
      "1\n",
      "90\n",
      "0\n",
      "48\n",
      "1\n",
      "39\n",
      "2\n",
      "30\n",
      "2\n",
      "28\n",
      "2\n",
      "26\n",
      "0\n",
      "78\n",
      "0\n",
      "59\n",
      "2\n",
      "34\n",
      "1\n",
      "70\n",
      "2\n",
      "64\n",
      "0\n",
      "108\n",
      "2\n",
      "18\n",
      "2\n",
      "69\n",
      "2\n",
      "39\n",
      "1\n",
      "63\n",
      "0\n",
      "6\n",
      "1\n",
      "13\n",
      "1\n",
      "1\n",
      "1\n",
      "76\n",
      "0\n",
      "3\n",
      "0\n",
      "103\n",
      "1\n",
      "35\n",
      "1\n",
      "56\n",
      "1\n",
      "5\n",
      "1\n",
      "69\n",
      "0\n",
      "28\n",
      "2\n",
      "47\n",
      "1\n",
      "20\n",
      "0\n",
      "6\n",
      "0\n",
      "147\n",
      "1\n",
      "56\n",
      "2\n",
      "19\n",
      "1\n",
      "137\n",
      "1\n",
      "101\n",
      "2\n",
      "23\n",
      "0\n",
      "100\n",
      "1\n",
      "24\n",
      "0\n",
      "45\n",
      "1\n",
      "73\n",
      "2\n",
      "111\n",
      "0\n",
      "55\n",
      "2\n",
      "17\n",
      "0\n",
      "89\n",
      "1\n",
      "23\n",
      "0\n",
      "84\n",
      "0\n",
      "1\n",
      "1\n",
      "154\n",
      "0\n",
      "34\n",
      "1\n",
      "39\n",
      "1\n",
      "90\n",
      "0\n",
      "48\n",
      "1\n",
      "39\n",
      "2\n",
      "30\n",
      "2\n",
      "28\n",
      "2\n",
      "26\n",
      "0\n",
      "78\n",
      "0\n",
      "59\n",
      "2\n",
      "34\n",
      "1\n",
      "70\n",
      "2\n",
      "64\n",
      "0\n",
      "108\n",
      "2\n",
      "18\n",
      "2\n",
      "69\n",
      "2\n",
      "39\n",
      "1\n",
      "63\n",
      "0\n",
      "6\n",
      "1\n",
      "13\n",
      "1\n",
      "1\n",
      "1\n",
      "76\n",
      "0\n",
      "3\n",
      "0\n",
      "103\n",
      "1\n",
      "35\n",
      "1\n",
      "56\n",
      "1\n",
      "5\n",
      "1\n",
      "69\n",
      "0\n",
      "28\n",
      "2\n",
      "47\n",
      "1\n",
      "20\n",
      "0\n",
      "6\n",
      "0\n",
      "147\n",
      "1\n",
      "56\n",
      "2\n",
      "19\n",
      "1\n",
      "137\n",
      "1\n",
      "101\n",
      "2\n",
      "23\n",
      "0\n",
      "100\n",
      "1\n",
      "24\n",
      "0\n",
      "45\n",
      "1\n",
      "73\n",
      "2\n",
      "111\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(start_epoch, start_epoch+nepoch):\n",
    "    \n",
    "    optimizer = scheduler_exp(optimizer, lr0, lr_decay, epoch)\n",
    "\n",
    "    epoch_loss = 0\n",
    "    epoch_crt = np.zeros([2,2,2])\n",
    "    epoch_pur = 0; epoch_eff = 0; epoch_loose = 0\n",
    "    batch_list = []\n",
    "        \n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    ntry = 0\n",
    "    npass =  0\n",
    "    nfail = np.zeros(10)\n",
    "    \n",
    "    for i, dic in enumerate(train_dics): # looping over training events\n",
    "        \n",
    "        charges_list = list(dic.values())\n",
    "                \n",
    "        coords = torch.LongTensor(list(dic.keys()))\n",
    "        charges = torch.FloatTensor(np.array(charges_list).reshape(len(charges_list), 1)).to(device)\n",
    "        truth = torch.FloatTensor(train_true_vtxs[i]).to(device)\n",
    "\n",
    "        prediction = model([coords, charges])\n",
    "        \n",
    "        pred_np = prediction.cpu().detach().numpy()\n",
    "                \n",
    "        pred_np = pred_np[:,1] - pred_np[:,0]\n",
    "        truth_np = truth.cpu().detach().numpy()\n",
    "        truth_idx = np.argmax(truth_np)\n",
    "        pred_idx = np.argmax(pred_np)\n",
    "        \n",
    "        #print(truth_idx)\n",
    "        #print(pred_idx)\n",
    "\n",
    "        \n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "036778e0-5a68-4a38-b32e-db74bb17144c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f15dcd55-1a5c-470a-a2ab-0c91fdec319e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2de5c305-1cc6-4ec9-ad9f-221457b83493",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
