{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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: 14c633f5ee0ecbc6da0fd1343cb5a5e6\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import subprocess\n",
    "import scipy.stats\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','Summary',[])\n",
    "patients=[row['aliasID'] for row in ds['rows']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getValues(db,project,p,nclass,option,sigma2,var):\n",
    "    idFilter={'variable':'aliasID','value':p,'oper':'eq'}\n",
    "    cFilter={'variable':'nclass','value':str(nclass),'oper':'eq'}\n",
    "    oFilter={'variable':'option','value':option,'oper':'eq'}    \n",
    "    sFilter={'variable':'sigma2','value':str(sigma2),'oper':'eq'}    \n",
    "    ds=db.selectRows(project,'study','Summary',[idFilter,cFilter,oFilter,sFilter])\n",
    "    nr=len(ds['rows'])\n",
    "    k1=numpy.zeros(nr)\n",
    "    for r in ds['rows']:\n",
    "        k1[int(r['regionId'])]=60*r[var]\n",
    "    return k1\n",
    "\n",
    "def getAllValues(db,project,patientList,nclass,option,sigma2,var):\n",
    "    for p in patientList:\n",
    "        try:\n",
    "            k1=numpy.append(k1,getValues(db,project,p,nclass,option,sigma2,var))\n",
    "        except UnboundLocalError:\n",
    "            k1=getValues(db,project,p,nclass,option,sigma2,var)\n",
    "    return k1\n",
    "\n",
    "def plotSummary(db,project,patientList,option,var):\n",
    "    fig, ax = matplotlib.pyplot.subplots(3,3,figsize=(20,10))\n",
    "    i=0\n",
    "    for s in [0.1,1.0,4.0]:\n",
    "        k1m=getAllValues(db,project,patientList,16,'D',s,'mean')\n",
    "        j=0\n",
    "        for c in [10,20,30]:\n",
    "            k1c=getAllValues(db,project,patientList,c,option,0.1,var)\n",
    "            select=[all(tup) for tup in zip(k1m<1,k1c<1)]\n",
    "            k1mp=k1m[select]\n",
    "            k1c=k1c[select]\n",
    "            ax[i,j].scatter(k1mp,k1c)\n",
    "            fr,fp=scipy.stats.pearsonr(k1mp,k1c)\n",
    "            fy=max(k1c)\n",
    "            ax[i,j].text(0.01,0.9*fy,'[{}]/{:.2f}'.format(c,s),fontsize='x-large')\n",
    "            ax[i,j].text(0.01,0.8*fy,'R2={:.2f}'.format(fr*fr),fontsize='x-large')\n",
    "            j+=1\n",
    "        i+=1\n",
    "    matplotlib.pyplot.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "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",
      "/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"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 79460 characters]\n",
      "text/plain": [
       "<Figure size 1440x720 with 9 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "patientList=list(set(patients))\n",
    "plotSummary(db,project,patientList,'W','mean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "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",
      "/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"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 83668 characters]\n",
      "text/plain": [
       "<Figure size 1440x720 with 9 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "patientList=list(set(patients))\n",
    "plotSummary(db,project,patientList,'M','mean')"
   ]
  },
  {
   "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
}
