{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loadLibrary\n",
      "remoteSourcesURL https://git0.fmf.uni-lj.si/studen/nixSuite/raw/master/remoteResources/resources.json\n",
      "{'labkeyInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'irAEMM': {'url': 'https://git0.fmf.uni-lj.si/studen/iraemm/archive/master.zip', 'branch': 'master', 'modules': ['iraemmBrowser']}, 'SlicerLabkeyExtension': {'url': 'https://git0.fmf.uni-lj.si/studen/SlicerLabkeyExtension/archive/SlicerExtensionIndex.zip', 'branch': 'SlicerExtensionIndex', 'modules': ['labkeyBrowser']}, 'limfomiPET': {'url': 'https://git0.fmf.uni-lj.si/studen/limfomiPET/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser', 'segmentationBrowser']}, 'parseConfig': {'url': 'https://git0.fmf.uni-lj.si/studen/parseConfig/archive/master.zip', 'branch': 'master', 'modules': []}, 'orthancInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}}\n",
      "{'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}\n",
      "File  /home/studen/temp/labkeyInterface.zip: True\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import subprocess\n",
    "import json\n",
    "import config\n",
    "import getData\n",
    "import segmentation\n",
    "import importlib\n",
    "importlib.reload(segmentation)\n",
    "importlib.reload(getData)\n",
    "importlib.reload(config)\n",
    "\n",
    "sys.path.append(os.path.join(os.path.expanduser('~'),'software','src','|ixSuite','wrapper'))\n",
    "import nixWrapper\n",
    "nixWrapper.loadLibrary('labkeyInterface')\n",
    "import labkeyInterface\n",
    "import labkeyFileBrowser\n",
    "import labkeyDatabaseBrowser\n",
    "\n",
    "#manipulate segmentations\n",
    "#rewrite this\n",
    "#nim=getPatientNIM(pId)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loadLibrary\n",
      "remoteSourcesURL https://git0.fmf.uni-lj.si/studen/nixSuite/raw/master/remoteResources/resources.json\n",
      "{'labkeyInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'irAEMM': {'url': 'https://git0.fmf.uni-lj.si/studen/iraemm/archive/master.zip', 'branch': 'master', 'modules': ['iraemmBrowser']}, 'SlicerLabkeyExtension': {'url': 'https://git0.fmf.uni-lj.si/studen/SlicerLabkeyExtension/archive/SlicerExtensionIndex.zip', 'branch': 'SlicerExtensionIndex', 'modules': ['labkeyBrowser']}, 'limfomiPET': {'url': 'https://git0.fmf.uni-lj.si/studen/limfomiPET/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser', 'segmentationBrowser']}, 'parseConfig': {'url': 'https://git0.fmf.uni-lj.si/studen/parseConfig/archive/master.zip', 'branch': 'master', 'modules': []}, 'orthancInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}}\n",
      "{'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}\n",
      "File  /home/studen/temp/labkeyInterface.zip: True\n",
      "User: andrej studen CSRF: e682a2d778135e99c8f86ab87bcc8e47\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "\n",
    "fsetup='../template/cardiacSPECT.json'\n",
    "with open(fsetup,'r') as f:\n",
    "    setup=json.load(f)\n",
    "\n",
    "db,fb=getData.connectDB(setup['network'])    \n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'date': '2022/12/06 00:00:00', 'lsid': 'urn:lsid:ijs.si:Study.Data-113:5006.MM.2.0000', 'visitName': 'OBR', '_labkeyurl_nmMasterOrthancId': 'https://orthanc.fmf.uni-lj.si/series/49464cfb-e53fb7b5-f8d4f7fc-c762f334-f1dc3863/archive', '_labkeyurl_PatientId': '/labkey/dinamic_spect/Patients/study-participant.view?participantId=MM', 'PatientId': 'MM', 'ctOrthancId': 'a226533f-2ec5d437-1c74d352-17bd6dca-dc719b60', 'nmCorrDataOrthancId': '7d5a1513-67b10c30-c0decf1c-2305db38-8c0e3128', '_labkeyurl_nmCorrDataOrthancId': 'https://orthanc.fmf.uni-lj.si/series/7d5a1513-67b10c30-c0decf1c-2305db38-8c0e3128/archive', '_labkeyurl_ctOrthancId': 'https://orthanc.fmf.uni-lj.si/series/a226533f-2ec5d437-1c74d352-17bd6dca-dc719b60/archive', 'nmMasterOrthancId': '49464cfb-e53fb7b5-f8d4f7fc-c762f334-f1dc3863', 'SequenceNum': 2.0}\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "def guessSegmentation(db,setup,pId='MM',visitName='MIR',sx=32,sy=31,sz=31):\n",
    "\n",
    "   idFilter={'variable':'PatientId','value':pId,'oper':'eq'}\n",
    "   visitFilter={'variable':'visitName','value':visitName,'oper':'eq'}  \n",
    "   rows=getData.getPatients(db,setup,[idFilter,visitFilter])\n",
    "   r=rows[0] \n",
    "   pixels=segmentation.guessPixelPosition4(sx,sy,sz) \n",
    "   segmentation.updateSegmentation(db,setup,r,pixels)\n",
    "\n",
    "#guessSegmentation(db,setup,sz=32,sx=33)\n",
    "def doPlotSegmentation(db,fb,setup,pId='MM',visitName='MIR',vmax=1000):\n",
    "   idFilter={'variable':'PatientId','value':pId,'oper':'eq'}\n",
    "   visitFilter={'variable':'visitName','value':visitName,'oper':'eq'}  \n",
    "   rows=getData.getPatients(db,setup,[idFilter,visitFilter])\n",
    "   r=rows[0] \n",
    "   segmentation.plotSegmentation(db,fb,r,setup,vmax)\n",
    "\n",
    "def getRow(db,setup,pId='MM',visitName='MIR'):\n",
    "   idFilter={'variable':'PatientId','value':pId,'oper':'eq'}\n",
    "   visitFilter={'variable':'visitName','value':visitName,'oper':'eq'}  \n",
    "   rows=getData.getPatients(db,setup,[idFilter,visitFilter])\n",
    "   return rows[0] \n",
    "    \n",
    "\n",
    "pId='MM'\n",
    "visitName='OBR'\n",
    "#guessSegmentation(db,setup,pId,visitName,sx=32,sy=31,sz=33)\n",
    "#doPlotSegmentation(db,fb,setup,pId,visitName,vmax=500)\n",
    "r=getRow(db,setup,pId,visitName)\n",
    "print(r)\n",
    "x=segmentation.loadSegmentation(db,fb,r,setup)\n",
    "print(x.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******10KF_MIR******\n",
      "******10KF_OBR******\n",
      "******MM_MIR******\n",
      "******MM_MIR1******\n",
      "******MM_OBR******\n"
     ]
    },
    {
     "ename": "FileNotFoundError",
     "evalue": "[Errno 2] No such file or directory: '/home/studen/temp/dynamicSPECT/MM_OBR/MM_OBR_Segmentation.txt'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mFileNotFoundError\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[80], line 5\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m r \u001b[38;5;129;01min\u001b[39;00m rows:\n\u001b[1;32m      4\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m******\u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m******\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mformat(config\u001b[38;5;241m.\u001b[39mgetCode(r,setup)))\n\u001b[0;32m----> 5\u001b[0m     \u001b[43msegmentation\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwriteSegmentation\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdb\u001b[49m\u001b[43m,\u001b[49m\u001b[43mr\u001b[49m\u001b[43m,\u001b[49m\u001b[43msetup\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/software/src/dynamicSPECT/pythonScripts/segmentation.py:152\u001b[0m, in \u001b[0;36mwriteSegmentation\u001b[0;34m(db, r, setup)\u001b[0m\n\u001b[1;32m    149\u001b[0m     v[region,\u001b[38;5;241m0\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mfloat\u001b[39m(qr[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mz\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m    150\u001b[0m \u001b[38;5;66;03m#for i in range(len(rows)):\u001b[39;00m\n\u001b[1;32m    151\u001b[0m \u001b[38;5;66;03m#    print(v[i,:])\u001b[39;00m\n\u001b[0;32m--> 152\u001b[0m \u001b[43mnumpy\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msavetxt\u001b[49m\u001b[43m(\u001b[49m\u001b[43mos\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlocDir\u001b[49m\u001b[43m,\u001b[49m\u001b[43mfileName\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43mv\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m<__array_function__ internals>:5\u001b[0m, in \u001b[0;36msavetxt\u001b[0;34m(*args, **kwargs)\u001b[0m\n",
      "File \u001b[0;32m/usr/lib/python3/dist-packages/numpy/lib/npyio.py:1368\u001b[0m, in \u001b[0;36msavetxt\u001b[0;34m(fname, X, fmt, delimiter, newline, header, footer, comments, encoding)\u001b[0m\n\u001b[1;32m   1365\u001b[0m     fname \u001b[38;5;241m=\u001b[39m os_fspath(fname)\n\u001b[1;32m   1366\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m _is_string_like(fname):\n\u001b[1;32m   1367\u001b[0m     \u001b[38;5;66;03m# datasource doesn't support creating a new file ...\u001b[39;00m\n\u001b[0;32m-> 1368\u001b[0m     \u001b[38;5;28;43mopen\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mfname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mwt\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mclose()\n\u001b[1;32m   1369\u001b[0m     fh \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mlib\u001b[38;5;241m.\u001b[39m_datasource\u001b[38;5;241m.\u001b[39mopen(fname, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwt\u001b[39m\u001b[38;5;124m'\u001b[39m, encoding\u001b[38;5;241m=\u001b[39mencoding)\n\u001b[1;32m   1370\u001b[0m     own_fh \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
      "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: '/home/studen/temp/dynamicSPECT/MM_OBR/MM_OBR_Segmentation.txt'"
     ]
    }
   ],
   "source": [
    "#rarely used. Mostly, segmentation points are edited in LabKey\n",
    "rows=getData.getPatients(db,setup)\n",
    "for r in rows:\n",
    "    print('******{}******'.format(config.getCode(r,setup)))\n",
    "    segmentation.writeSegmentation(db,r,setup)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
