{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "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: e38e7c8505b76f18934e3fb0f5a0f262\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\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)\n",
    "ds=db.selectRows(project,'study','ImagingData',[])\n",
    "patients=list(set([row['PatientId'] for row in ds['rows']]))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39312 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 37916 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39476 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "patients1=[patients[2]]\n",
    "v1='k1'\n",
    "var='Median'\n",
    "for p in patients1:\n",
    "    for nclass in [10,20,30]:\n",
    "        rows=[r for r in ds['rows'] if r['PatientId']==p and abs(r['SequenceNum']-nclass)<1]\n",
    "        nseg=len(rows)\n",
    "        fx=100/nseg*2*numpy.pi*numpy.array([r['SequenceNum']-nclass for r in rows])\n",
    "        fy=60*numpy.array([r[v1+'MIR'+var] for r in rows])\n",
    "        fyerr=60*numpy.array([r[v1+'MIR'+var+'Std'] for r in rows])\n",
    "        fy1=60*numpy.array([r[v1+'OBR'+var] for r in rows])\n",
    "        fy1err=60*numpy.array([r[v1+'OBR'+var+'Std'] for r in rows])\n",
    "        fig = matplotlib.pyplot.figure()\n",
    "        ax = matplotlib.pyplot.axes(polar=True)\n",
    "        ax.errorbar(fx, fy, yerr=fyerr, capsize=0)\n",
    "        ax.errorbar(fx, fy1, yerr=fy1err, capsize=0)\n",
    "        #ax.set_rorigin(-0.3)\n",
    "        matplotlib.pyplot.yticks(0.1*numpy.arange(2, 10,2))\n",
    "        matplotlib.pyplot.show()\n",
    "        print(len(rows))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38124 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 37232 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34636 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 34588 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35432 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35228 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35716 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 35908 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36132 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 33740 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36520 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 37872 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "#plot comparisson by class\n",
    "patients1=patients\n",
    "v1='v19'\n",
    "varCorrect=60\n",
    "if v1=='v19':\n",
    "    varCorrect=1e-3\n",
    "var='Median'\n",
    "for p in patients1:\n",
    "    fig = matplotlib.pyplot.figure()\n",
    "    ax = matplotlib.pyplot.axes(polar=True)\n",
    "    for nclass in [10,20,30]:\n",
    "        rows=[r for r in ds['rows'] if r['PatientId']==p and abs(r['SequenceNum']-nclass)<1]\n",
    "        nseg=len(rows)\n",
    "        fx=100/nseg*2*numpy.pi*numpy.array([r['SequenceNum']-nclass for r in rows])\n",
    "        fy=varCorrect*numpy.array([r[v1+'MIR'+var] for r in rows])\n",
    "        try:\n",
    "            fyerr=varCorrect*numpy.array([r[v1+'MIR'+var+'Std'] for r in rows])\n",
    "        except KeyError:\n",
    "            fyerr=numpy.zeros(fy.shape)\n",
    "        ax.errorbar(fx, fy, yerr=fyerr, capsize=0)\n",
    "        ax.set_rlim(0,1)\n",
    "    \n",
    "    matplotlib.pyplot.show()\n",
    "    fig = matplotlib.pyplot.figure()\n",
    "    ax = matplotlib.pyplot.axes(polar=True)\n",
    "    for nclass in [10,20,30]:\n",
    "        rows=[r for r in ds['rows'] if r['PatientId']==p and abs(r['SequenceNum']-nclass)<1]\n",
    "        nseg=len(rows)\n",
    "        fy1=varCorrect*numpy.array([r[v1+'OBR'+var] for r in rows])\n",
    "        try:\n",
    "            fy1err=varCorrect*numpy.array([r[v1+'OBR'+var+'Std'] for r in rows])\n",
    "        except KeyError:\n",
    "            fy1err=numpy.zeros(fy1.shape)\n",
    "        ax.errorbar(fx, fy1, yerr=fy1err, capsize=0)\n",
    "        ax.set_rlim(0,1)\n",
    "    matplotlib.pyplot.show()\n",
    "    #ax.set_rorigin(-0.3)\n",
    "    print(len(rows))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 47008 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 54576 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 42248 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 43768 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 39736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 36324 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    }
   ],
   "source": [
    "#plot comparisson by class\n",
    "patients1=patients\n",
    "v1='k1'\n",
    "var='90p'\n",
    "for p in patients1:\n",
    "    fig = matplotlib.pyplot.figure()\n",
    "    ax = matplotlib.pyplot.axes(polar=True)\n",
    "    for nclass in [10,20,30]:\n",
    "        rows=[r for r in ds['rows'] if r['PatientId']==p and abs(r['SequenceNum']-nclass)<1]\n",
    "        nseg=len(rows)\n",
    "        fx=100/nseg*2*numpy.pi*numpy.array([r['SequenceNum']-nclass for r in rows])\n",
    "        fy=60*numpy.array([r[v1+'MIR'+var] for r in rows])\n",
    "        try:\n",
    "            fyerr=60*numpy.array([r[v1+'MIR'+var+'Std'] for r in rows])\n",
    "        except KeyError:\n",
    "            fyerr=numpy.zeros(fy.shape)\n",
    "        fy1=60*numpy.array([r[v1+'OBR'+var] for r in rows])\n",
    "        try:\n",
    "            fy1err=60*numpy.array([r[v1+'OBR'+var+'Std'] for r in rows])\n",
    "        except KeyError:\n",
    "            fy1err=numpy.zeros(fy1.shape)\n",
    "        fq=fy1/fy;\n",
    "        fe=numpy.sqrt(numpy.power(fyerr/fy,2)+numpy.power(fy1err/fy1,2))*fq\n",
    "        ax.errorbar(fx, fq, yerr=fe, capsize=0)\n",
    "        ax.set_rlim(0,5)\n",
    "    matplotlib.pyplot.show()\n",
    "    #ax.set_rorigin(-0.3)\n",
    "    print(len(rows))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 9)\n",
      "0.21027698286315857\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 28956 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6761352546353504\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 33488 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.2885292919290429\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 35128 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.32453629820917174\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34100 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.3057394935669287\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 28476 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.38411353004904286\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 32768 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#plot errors (as function of nclass)\n",
    "patients1=patients\n",
    "v1='k1'\n",
    "var='Median'\n",
    "\n",
    "regions=list(set([r['regionId'] for r in ds['rows']]))\n",
    "classes=list(set([r['nclass'] for r in ds['rows']]))\n",
    "\n",
    "fy=numpy.zeros((len(classes),len(patients),len(regions)))\n",
    "fy1=numpy.zeros((len(classes),len(patients),len(regions)))\n",
    "print(fy.shape)\n",
    "ip=0\n",
    "for p in patients1:\n",
    "    rows=[r for r in ds['rows'] if r['PatientId']==p]\n",
    "    regions=list(set([r['regionId'] for r in rows]))\n",
    "    classes=list(set([r['nclass'] for r in rows]))\n",
    "    \n",
    "    #average over class\n",
    "    favg=numpy.zeros(len(regions))\n",
    "    favg1=numpy.zeros(len(regions))\n",
    "    for rid in regions:\n",
    "        rows1=[r for r in rows if r['regionId']==rid]\n",
    "        favg[rid-1]=60*numpy.mean(numpy.array([r[v1+'MIR'+var] for r in rows1]))\n",
    "        favg1[rid-1]=60*numpy.mean(numpy.array([r[v1+'OBR'+var] for r in rows1]))\n",
    "    \n",
    "    ic=0    \n",
    "    for nclass in classes:\n",
    "        rows=[r for r in ds['rows'] if r['PatientId']==p and abs(r['SequenceNum']-nclass)<1]\n",
    "        nseg=len(rows)\n",
    "        fx=100/nseg*2*numpy.pi*numpy.array([r['SequenceNum']-nclass for r in rows])\n",
    "        fy[ic,ip,:]=60*numpy.array([r[v1+'MIR'+var+'Std'] for r in rows])/favg\n",
    "        fy1[ic,ip,:]=60*numpy.array([r[v1+'OBR'+var+'Std'] for r in rows])/favg1\n",
    "        #matplotlib.pyplot.plot(fx, fyerr)\n",
    "        #matplotlib.pyplot.plot(fx, fy1err)\n",
    "        ic+=1\n",
    "    #matplotlib.pyplot.ylim(0,1)\n",
    "    #matplotlib.pyplot.title('This is a title')\n",
    "    \n",
    "    #matplotlib.pyplot.show()\n",
    "    #ax.set_rorigin(-0.3)\n",
    "    ip+=1\n",
    "    \n",
    "for ic in numpy.arange(fy.shape[0]):\n",
    "    for ip in numpy.arange(fy.shape[1]):\n",
    "        matplotlib.pyplot.plot(fx,fy[ic,ip,:])\n",
    "    matplotlib.pyplot.ylim(0,1)\n",
    "    print(numpy.mean(fy[ic,:,:]))\n",
    "    matplotlib.pyplot.show()\n",
    "    for ip in numpy.arange(fy1.shape[1]):\n",
    "        matplotlib.pyplot.plot(fx,fy1[ic,ip,:])\n",
    "    print(numpy.mean(fy1[ic,:,:]))\n",
    "    matplotlib.pyplot.ylim(0,1)\n",
    "    matplotlib.pyplot.show()"
   ]
  },
  {
   "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
