{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/urllib3/connection.py:391: SubjectAltNameWarning: Certificate for merlin.fmf.uni-lj.si has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/urllib3/urllib3/issues/497 for details.)\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 2e2ad486aae3991259a5a9499f48a7dc\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import imageio\n",
    "sys.path.append(os.path.join(os.path.expanduser('~'),'software','src','nixSuite','wrapper'))\n",
    "import nixWrapper\n",
    "nixWrapper.loadLibrary('labkeyInterface')\n",
    "import labkeyInterface\n",
    "net=labkeyInterface.labkeyInterface()\n",
    "fconfig=os.path.join(os.path.expanduser('~'),'.labkey','network.json')\n",
    "net.init(fconfig)\n",
    "net.getCSRF()\n",
    "import labkeyFileBrowser\n",
    "sys.path.append(os.getcwd())\n",
    "import downloadPatient\n",
    "fb=labkeyFileBrowser.labkeyFileBrowser(net)\n",
    "project='dinamic_spect/Patients'\n",
    "locDir=os.path.join(os.path.expanduser('~'),'temp','dynamicSPECT')\n",
    "import labkeyDatabaseBrowser\n",
    "db=labkeyDatabaseBrowser.labkeyDB(net)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/lib/python3/dist-packages/urllib3/connection.py:391: SubjectAltNameWarning: Certificate for merlin.fmf.uni-lj.si has no `subjectAltName`, falling back to check for a `commonName` for now. This feature is being removed by major browsers and deprecated by RFC 2818. (See https://github.com/urllib3/urllib3/issues/497 for details.)\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16984 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17752 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18368 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17484 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17748 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 90dc721765507858188e9664d4a23104\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17000 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18080 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 95145a867bd9007341b2a3147bf460e4\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16672 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17356 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16864 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15932 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17320 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 73b7611429bfdd47eb6bfdf130135ce1\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17880 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17368 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17100 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18072 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15256 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18848 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: c484448a0bbdf80d335cd97c2332133e\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16616 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16700 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17028 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15968 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18136 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 5f920112e2329782b7434c6997a79daa\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18480 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16500 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16316 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16888 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17168 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: e2a882f47ef755dad6a66a01c48de50a\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16040 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17280 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17124 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16048 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 24c95498d8a757a96f608eeb437630b5\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18420 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16828 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17644 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 229d509661adfb65595cc2602e568145\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17684 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 15112 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17456 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17120 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17284 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 337b60b744c3aa70557066f5308d0e1e\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16864 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16216 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17144 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18308 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16860 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: a23bb15c3488b0ee5a79124c0725a7c5\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16880 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16812 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17544 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18920 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: af71a46371356ba77a358491f0931d64\n",
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18332 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17564 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16604 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17784 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16716 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(656, 875, 3)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 18956 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 8b4e7238e39701a63c3c528ef8955f3e\n"
     ]
    }
   ],
   "source": [
    "ds=db.selectRows(project,'study','Imaging',[])\n",
    "patients=list(set([row['aliasID'] for row in ds['rows']]))\n",
    "print(len(patients))\n",
    "\n",
    "patients1=[patients[11]]\n",
    "for pId in patients:\n",
    "    if pId.find('8')==0:\n",
    "        continue\n",
    "    r=[row for row in ds['rows'] if row['aliasID']==pId][0]\n",
    "    segmFile=os.path.join(locDir,pId,pId+'_Segm.nrrd')\n",
    "    imS=SimpleITK.ReadImage(segmFile)\n",
    "    nS=SimpleITK.GetArrayFromImage(imS)\n",
    "    \n",
    "    fUnit=numpy.ones(nS.shape)\n",
    "    \n",
    "    idFilter={'variable':'PatientId','value':r['PatientId'],'oper':'eq'}\n",
    "    dsQ=db.selectRows(project,'study','ImagingData',[idFilter])\n",
    "    regionIds=[int(r['regionId']) for r in dsQ['rows']]\n",
    "    \n",
    "    nCenter=[]\n",
    "    maxCenter=[]\n",
    "    for i in numpy.arange(10):\n",
    "        centerFile=os.path.join(locDir,pId,pId+'_center'+str(i+1)+'.nrrd')\n",
    "        imCenter=SimpleITK.ReadImage(centerFile)\n",
    "        nCenter.append(SimpleITK.GetArrayFromImage(imCenter))\n",
    "    \n",
    "    for region in regionIds:\n",
    "        vs=[]\n",
    "        for i in numpy.arange(10):\n",
    "            vc=nCenter[i][nS==region]\n",
    "            val=numpy.sum(vc)\n",
    "            #print('[{}]: reg {} center {}: {}'.format(pId,region,i,numpy.sum(vc)))\n",
    "            vs.append(val)\n",
    "        #print('{}/{}'.format(numpy.max(vs),numpy.argmax(vs)))\n",
    "        maxCenter.append(numpy.argmax(vs))\n",
    "    \n",
    "    maxCenter=list(set(maxCenter))\n",
    "    mc=[str(x) for x in maxCenter]\n",
    "    r['centers']=';'.join(mc)\n",
    "    \n",
    "    for i in maxCenter:\n",
    "        im = imageio.imread(os.path.join(locDir,pId,'centers'+str(i+1)+'.png'))\n",
    "        print(im.shape)\n",
    "        matplotlib.pyplot.imshow(im)\n",
    "        matplotlib.pyplot.show()\n",
    "        \n",
    "    db.modifyRows('update',project,'study','Imaging',[r])\n",
    "        \n",
    "                                \n",
    "        \n",
    "                                \n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
