{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "067cdb44-f8d5-4175-bb11-0447c187b5f7",
   "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']}, '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  C:\\Users\\studen\\temp\\labkeyInterface.zip: True\n"
     ]
    }
   ],
   "source": [
    "#this fills SUVanalysis\n",
    "import runStat\n",
    "#runStat.main('../templates/statistics.json')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "89889edb-533e-4bde-9514-22f40cceb2ab",
   "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  C:\\Users\\studen\\temp\\labkeyInterface.zip: True\n",
      "User: andrej studen CSRF: 9470c83e5b000aff6a021aa7ff1307a9\n",
      "True\n",
      "Failed to find segmentation for 4763/18/VISIT_1\n",
      "No segmentation for 4763/18/VISIT_1\n",
      "Failed to find segmentation for 4763/18/VISIT_2\n",
      "No segmentation for 4763/18/VISIT_2\n",
      "Failed to find segmentation for 4763/18/VISIT_3\n",
      "No segmentation for 4763/18/VISIT_3\n",
      "Failed to find segmentation for 8294/16/VISIT_1\n",
      "No segmentation for 8294/16/VISIT_1\n",
      "segmentationNotDone: 4\n",
      "backupFileOK: 0\n",
      "noSubstitute: 0\n",
      "substituteFileOK: 0\n",
      "substituteFileMissing: 0\n",
      "segmentationOK: 41\n",
      "all: 45\n"
     ]
    }
   ],
   "source": [
    "import statUtils\n",
    "import SimpleITK\n",
    "import os\n",
    "setup=statUtils.loadSetup(\"../templates/statistics.json\")\n",
    "db,fb=statUtils.connectDB('onko-nix')\n",
    "project='limfomiPET/EANM'\n",
    "blankFilter={'variable':'MTV','value':'x','oper':'isblank'}\n",
    "ds=db.selectRows(project,'study','analysis1',[blankFilter])\n",
    "\n",
    "localSeg=os.path.join(os.path.expanduser('~'),'Downloads','Segmentation_20230417')\n",
    "print(os.path.isdir(localSeg))\n",
    "\n",
    "entries=ds['rows']\n",
    "outcomes={'segmentationNotDone':0,'backupFileOK':0,'noSubstitute':0,'substituteFileOK':0,'substituteFileMissing':0,'segmentationOK':0,'all':0}\n",
    "\n",
    "doModify=False\n",
    "\n",
    "for entry in entries:\n",
    "    id=entry['ParticipantId']\n",
    "    setup.update({'db':db,'fb':fb})\n",
    "    idFilter={'variable':'ParticipantId','value':id,'oper':'eq'}\n",
    "    #visitFilter={'variable':'visitCode','value':'VISIT_1','oper':'eq'}\n",
    "    ds=db.selectRows(setup['project'],'study',setup['imagingDataset'],[idFilter])\n",
    "    rows=ds['rows']\n",
    "    for r in rows:\n",
    "        outcomes['all']+=1\n",
    "        segPaths=statUtils.getSegmentations(setup,r)\n",
    "        if not \"NONE\" in segPaths.values():\n",
    "            #print('Segmentation {}/{} OK.'.format(r['ParticipantId'],r['visitCode']))\n",
    "            outcomes['segmentationOK']+=1\n",
    "            continue\n",
    "            \n",
    "        #get file name\n",
    "        qFilter=[{'variable':x,'value':r[x],'oper':'eq'} for x in ['ParticipantId','visitCode']]\n",
    "        qFilter.append({'variable':'User','value':'1037','oper':'eq'})\n",
    "        dsSeg=db.selectRows(setup['project'],'study','Segmentations',qFilter)\n",
    "        \n",
    "        n=len(dsSeg['rows'])\n",
    "        if n==0:\n",
    "            print('No segmentation for {}/{}'.format(r['ParticipantId'],r['visitCode']))\n",
    "            outcomes['segmentationNotDone']+=1\n",
    "            continue\n",
    "        if n>1:\n",
    "            print('Multiple segmentations for {}/{}'.format(r['ParticipantId'],r['visitCode']))\n",
    "            break\n",
    "        \n",
    "        segEntry=dsSeg['rows'][0]\n",
    "        file=segEntry['latestFile']\n",
    "        localPath=os.path.join(localSeg,file)\n",
    "        found=os.path.isfile(localPath)\n",
    "        print('[{}] {}'.format(os.path.isfile(localPath),file))\n",
    "        \n",
    "        if found:\n",
    "            print('Original local file found')\n",
    "            pathList=[setup['imageDir'],segEntry['patientCode'],segEntry['visitCode'],'Segmentations']\n",
    "            remotePath=fb.buildPathURL(setup['project'],pathList)\n",
    "            remotPath+='/'+file\n",
    "            fb.writeFileToFile(localPath,remotePath)\n",
    "            outcomes['backupFileOK']+=1\n",
    "            continue\n",
    "\n",
    "        #get alternate file (one before the last)\n",
    "        fileFilter={'variable':'segmentation','value':file,'oper':'eq'}\n",
    "        dsLSeg=db.selectRows(setup['project'],'lists','segmentationList',[fileFilter])\n",
    "        key=dsLSeg['rows'][0]['Key']\n",
    "        #here we look at a previous key\n",
    "        keyFilter={'variable':'Key','value':'{}'.format(key-1),'oper':'eq'}\n",
    "        qFilter.append(keyFilter)\n",
    "        dsLSeg=db.selectRows(setup['project'],'lists','segmentationList',qFilter)\n",
    "        if len(dsLSeg['rows'])>0:\n",
    "            file1=dsLSeg['rows'][0]['segmentation']\n",
    "        else:\n",
    "            print('No subsitute found')\n",
    "            #remove vacant pointers to valid segmentations\n",
    "            if doModify:\n",
    "                db.modifyRows('delete',setup['project'],'study','Segmentations',[segEntry])\n",
    "            outcomes['noSubstitute']+=1\n",
    "            continue\n",
    "        \n",
    "        \n",
    "        localPath=os.path.join(localSeg,file1)\n",
    "        found=os.path.isfile(localPath)\n",
    "        if found:\n",
    "            print('Substitute file found')\n",
    "            pathList=[setup['imageDir'],segEntry['patientCode'],segEntry['visitCode'],'Segmentations']\n",
    "            remotePath=fb.buildPathURL(setup['project'],pathList)\n",
    "            remotePath+='/'+file1\n",
    "            fb.writeFileToFile(localPath,remotePath)\n",
    "            print('[{}] Uploaded {}'.format(fb.entryExists(remotePath),remotePath))\n",
    "            segEntry['latestFile']=file1\n",
    "            segEntry['comments']='substitute'\n",
    "            outcomes['substituteFileOK']+=1\n",
    "            if doModify:\n",
    "                db.modifyRows('update',setup['project'],'study','Segmentations',[segEntry])\n",
    "        else:\n",
    "            print('Substitute file not available')\n",
    "            #remove vacant pointers to valid segmentations\n",
    "            if doModify:\n",
    "                db.modifyRows('delete',setup['project'],'study','Segmentations',[segEntry])\n",
    "            outcomes['substituteFileMissing']+=1\n",
    "                \n",
    "                \n",
    "            #print('[{}] {}'.format(os.path.isfile(localPath),file1))\n",
    "            \n",
    "        #seg={x:SimpleITK.ReadImage(segPaths[x]) for x in segPaths}\n",
    "        #for x in seg:\n",
    "        #    ids=statUtils.getSegments(seg[x])\n",
    "        #    for id in ids:\n",
    "        #        print('{} {}'.format(id,ids[id]))\n",
    "for x in outcomes:\n",
    "    print('{}: {}'.format(x,outcomes[x]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "3be1c0f3-034c-4de0-947e-d2293693b057",
   "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  C:\\Users\\studen\\temp\\labkeyInterface.zip: True\n",
      "User: andrej studen CSRF: 0a0a0c1f37e17fbbd02c2a2670aa2879\n",
      "Segmentation_1487_17-VISIT_1_adoma_322.nrrd\n",
      "Segmentation_1645_17-VISIT_1_adoma_357.nrrd\n",
      "Segmentation_1645_17-VISIT_2_adoma_360.nrrd\n",
      "Segmentation_1731_17-VISIT_1_adoma_318.nrrd\n",
      "Segmentation_1837_16-VISIT_1_adoma_310.nrrd\n",
      "Segmentation_1837_16-VISIT_2_adoma_312.nrrd\n",
      "Segmentation_2180_17-VISIT_1_adoma_340.nrrd\n",
      "Segmentation_2294_16-VISIT_1_adoma_350.nrrd\n",
      "Segmentation_2294_16-VISIT_2_adoma_352.nrrd\n",
      "Segmentation_2589_16-VISIT_1_adoma_348.nrrd\n",
      "Segmentation_4081_16-VISIT_1_adoma_306.nrrd\n",
      "Segmentation_4081_16-VISIT_2_adoma_308.nrrd\n",
      "Segmentation_4679_17-VISIT_1_adoma_342.nrrd\n",
      "Segmentation_4679_17-VISIT_2_adoma_344.nrrd\n",
      "Segmentation_5389_16-VISIT_1_adoma_354.nrrd\n",
      "Segmentation_5916_17-VISIT_1_adoma_314.nrrd\n",
      "Segmentation_5916_17-VISIT_2_adoma_316.nrrd\n",
      "Segmentation_6435_16-VISIT_1_adoma_326.nrrd\n",
      "Segmentation_653_97-VISIT_1_adoma_346.nrrd\n",
      "Segmentation_7268_16-VISIT_1_adoma_331.nrrd\n",
      "Segmentation_7268_16-VISIT_2_adoma_333.nrrd\n",
      "Segmentation_8154_16-VISIT_1_adoma_364.nrrd\n"
     ]
    }
   ],
   "source": [
    "#correct Version numbers (forgot to do that in updates, just changed latestFile, but not numeric version)\n",
    "import statUtils\n",
    "import re\n",
    "setup=statUtils.loadSetup(\"../templates/statistics.json\")\n",
    "db,fb=statUtils.connectDB('onko-nix')\n",
    "subFilter={'variable':'comments','value':'substitute','oper':'eq'}\n",
    "dsSeg=db.selectRows(setup['project'],'study','Segmentations',[subFilter])\n",
    "for r in dsSeg['rows']:\n",
    "    f=r['latestFile']\n",
    "    print(f)\n",
    "    g=re.sub(r'^.*_([0-9]*).nrrd$',r'\\1',f)\n",
    "    key=int(g)\n",
    "    if key!=r['version']:\n",
    "        print('Version mismatch {}/{}'.format(key,r['version']))\n",
    "        r['version']=key\n",
    "        db.modifyRows('update',setup['project'],'study','Segmentations',[r])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "551c17b2-e4f4-4e63-bb8b-b5b0ff95854c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "in method 'Threshold', argument 1 of type 'itk::simple::Image const &'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-23-895f684012f3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mlabel\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mcc\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mSimpleITK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mThreshold\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mseg\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mlower\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mupper\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlabel\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mccFilter\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mSimpleITK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mConnectedComponentImageFilter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mcc1\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mccFilter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mExecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mccFilter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetObjectCount\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\studen\\appdata\\local\\programs\\python\\python39\\lib\\site-packages\\SimpleITK\\SimpleITK.py\u001b[0m in \u001b[0;36mThreshold\u001b[1;34m(image1, lower, upper, outsideValue)\u001b[0m\n\u001b[0;32m  22557\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mThreshold\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimage1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlower\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mupper\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moutsideValue\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m  22558\u001b[0m     \u001b[1;34mr\"\"\"Threshold(Image image1, double lower=0.0, double upper=1.0, double outsideValue=0.0) -> Image\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m> 22559\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0m_SimpleITK\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mThreshold\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mimage1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlower\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mupper\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moutsideValue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m  22560\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mThresholdMaximumConnectedComponentsImageFilter\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mImageFilter\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m  22561\u001b[0m     \u001b[1;34mr\"\"\"Proxy of C++ itk::simple::ThresholdMaximumConnectedComponentsImageFilter class.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: in method 'Threshold', argument 1 of type 'itk::simple::Image const &'"
     ]
    }
   ],
   "source": [
    "label=4\n",
    "cc=SimpleITK.Threshold(seg,lower=label,upper=label)\n",
    "ccFilter=SimpleITK.ConnectedComponentImageFilter()\n",
    "cc1=ccFilter.Execute(cc)\n",
    "print(ccFilter.GetObjectCount())\n",
    "ccNumpy=SimpleITK.GetArrayFromImage(cc)\n",
    "print(numpy.max(ccNumpy))\n",
    "ccNumpy[ccNumpy==label]=1\n",
    "print(len(ccNumpy[ccNumpy==1]))\n",
    "petNumpy=SimpleITK.GetArrayFromImage(pet)\n",
    "\n",
    "#matplotlib.pyplot.imshow(numpy.sum(petNumpy,1)[400:550,:])\n",
    "#matplotlib.pyplot.imshow(numpy.sum(ccNumpy[:,160:161,:],1)[400:550,:])\n",
    "matplotlib.pyplot.imshow(petNumpy[400:550,160,:])\n",
    "matplotlib.pyplot.imshow(ccNumpy[400:550,160,:],alpha=0.3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6edd77c8-57d1-4503-96ad-69ff5fd4b07d",
   "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.9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}