{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "4783a1dd-e2b3-461c-8e61-5e6a36e41a7a", "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': []}, 'dynamicSPECT': {'url': 'https://git0.fmf.uni-lj.si/studen/dynamicSPECT/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser']}}\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", "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': []}, 'dynamicSPECT': {'url': 'https://git0.fmf.uni-lj.si/studen/dynamicSPECT/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser']}}\n", "{'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}\n", "File C:\\Users\\studen\\temp\\orthancInterface.zip: True\n" ] } ], "source": [ "#load required libraries\n", "import sys\n", "import os\n", "import chardet\n", "import json\n", "import re\n", "import datetime\n", "\n", "#you should get nixSuite via git clone https://git0.fmf.uni-lj.si/studen/nixSuite.git\n", "#if you don't put it to $HOME/software/src/, you should update the path\n", "nixSuite=os.path.join(os.path.expanduser('~'),'software','src','nixSuite')\n", "sys.path.append(os.path.join(nixSuite,'wrapper'))\n", "import nixWrapper\n", "nixWrapper.loadLibrary('labkeyInterface')\n", "import labkeyInterface\n", "import labkeyDatabaseBrowser\n", "import labkeyFileBrowser\n", "nixWrapper.loadLibrary('orthancInterface')\n", "import orthancInterface\n", "import orthancDatabaseBrowser\n", "\n", "def connectDB(server):\n", " #check connectivity. This checks the configuration in $HOME/.labkey/network.json, \n", " #where paths to certificates are stored\n", " net=labkeyInterface.labkeyInterface()\n", " fconfig=os.path.join(os.path.expanduser('~'),'.labkey','{}.json'.format(server))\n", " net.init(fconfig)\n", " #this reports the certificate used\n", " try:\n", " print('Using: {}'.format(net.connectionConfig['SSL']['user']))\n", " except KeyError:\n", " pass\n", " #This gets a deafult CSRF code; It should report user name plus a long string of random hex numbers\n", " net.getCSRF()\n", " db=labkeyDatabaseBrowser.labkeyDB(net)\n", " fb=labkeyFileBrowser.labkeyFileBrowser(net)\n", " return db,fb\n", "\n", "def modifyRowsWithCheck(db,project,schema,query,entry,testFields):\n", " mode='insert'\n", " filters=[{'variable':x,'value':'{}'.format(entry[x]),'oper':'eq'} for x in testFields]\n", " ds=db.selectRows(project,schema,query,filters)\n", " finalEntry={}\n", " if len(ds['rows'])==1:\n", " finalEntry=ds['rows'][0]\n", " mode='update'\n", " for q in entry:\n", " finalEntry[q]=entry[q];\n", " print(db.modifyRows(mode,project,schema,query,[finalEntry]))\n", "\n", "def connectOrthanc(server):\n", " net=orthancInterface.orthancInterface()\n", " fconfig=os.path.join(os.path.expanduser('~'),'.labkey','{}.json'.format(server))\n", " net.init(fconfig)\n", " db=orthancDatabaseBrowser.orthancDB(net)\n", " return db\n", "\n" ] }, { "cell_type": "code", "execution_count": 16, "id": "b2682976-a44b-450d-88c2-ad69a3fde570", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User: andrej studen CSRF: ea2151487c571c34c2860833e3685f60\n", "8294/16: 2017-07-07\n" ] } ], "source": [ "setup={}\n", "setup['server']='onko-nix'\n", "setup['project']='limfomiPET/Study2023'\n", "setup['query']='missingTransfers'\n", "#setup['view']='Missing visit 1'\n", "setup['view']='Missing pre-treatment'\n", "\n", "def listOrthanc(setup):\n", " db,fb=connectDB(setup['server'])\n", " ds=db.selectRows(setup['project'],'study',setup['query'],[],setup['view'])\n", " rows=ds['rows']\n", " for r in rows:\n", " imgs={}\n", " idFilter={'variable':'PatientId','value':r['ParticipantId'],'oper':'eq'}\n", " ds=db.selectRows('Orthanc/Database','study','Imaging',[idFilter])\n", " for x in ds['rows']:\n", " date=datetime.datetime.strptime(x['studyDate'],'%Y/%m/%d %H:%M:%S')\n", " try:\n", " imgs[date].append(x)\n", " except KeyError:\n", " imgs[date]=[x]\n", " #print('{} {}'.format(date.strftime('%Y-%m-%d'),x['seriesDescription']))\n", " #print(r['ParticipantId'])\n", " dates=[d.strftime('%Y-%m-%d') for d in imgs]\n", " print('{}: {}'.format(r['ParticipantId'],','.join(dates)))\n", " \n", " \n", "\n", "listOrthanc(setup)" ] }, { "cell_type": "code", "execution_count": 5, "id": "9740dbc3-17c0-4ab9-9680-44961590a0ab", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User: andrej studen CSRF: 3fd61436be2b1f4f01f9c5be197d96eb\n", "Deleting 0 rows\n", "User: andrej studen CSRF: 4bb37bc5125387f1a054d520f1a6333e\n", "Inserting 1400 rows\n", "User: andrej studen CSRF: 9099dbaacb01d92900e4eb39b899a398\n" ] } ], "source": [ "def copyDataset(modify=False):\n", " db,fb=connectDB('onko-nix')\n", " sourceProject='limfomiPET/Study'\n", " sourceQuery='SUVanalysis'\n", " targetProject='limfomiPET/Study'\n", " targetQuery='SUVanalysisBackup'\n", " \n", " #prepare (and clear) target\n", " ds=db.selectRows(targetProject,'study',targetQuery,[])\n", " rows=ds['rows']\n", " print('Deleting {} rows'.format(len(rows)))\n", " if modify:\n", " db.modifyRows('delete',targetProject,'study',targetQuery,rows)\n", " \n", " #copy from source\n", " ds=db.selectRows(sourceProject,'study',sourceQuery,[])\n", " rows=ds['rows']\n", " #insert to target\n", " print('Inserting {} rows'.format(len(rows)))\n", " if modify:\n", " db.modifyRows('insert',targetProject,'study',targetQuery,rows)\n", " \n", "copyDataset(True)\n", " " ] }, { "cell_type": "code", "execution_count": 36, "id": "09d73d56-8d4a-488c-a11f-e8fb7e581ab1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User: andrej studen CSRF: 4762143c71425ffc69605ce515e80d93\n", "True\n", "Segmentation_2763_07-VISIT_1_adoma_220.nrrd 1: 5394\n", "Segmentation_2763_07-VISIT_1_adoma_220.nrrd 2: 0\n", "Segmentation_2763_07-VISIT_1_adoma_220.nrrd 4: 86125\n" ] } ], "source": [ "import SimpleITK\n", "import numpy\n", "def getSegmentation():\n", " db,fb=connectDB('onko-nix')\n", " p='1965/16'\n", " p='2763/07'\n", " #p='4464/16'\n", " pcode=p.replace('/','_')\n", " idFilter={'variable':'ParticipantId','value':p,'oper':'eq'}\n", " v='VISIT_1'\n", " visitFilter={'variable':'visitCode','value':v,'oper':'eq'}\n", " user='1037'\n", " userFilter={'variable':'User','value':user,'oper':'eq'}\n", " project='limfomiPET/Study'\n", " ds=db.selectRows(project,'study','Segmentations',[idFilter,visitFilter,userFilter])\n", " for r in ds['rows']:\n", " url=fb.buildPathURL(project,['preprocessedImages',pcode,v,'Segmentations'])\n", " remoteURL='/'.join([url,r['latestFile']])\n", " print(fb.entryExists(remoteURL))\n", " localPath=os.path.join(os.path.expanduser('~'),'Downloads',r['latestFile'])\n", " fb.readFileToFile(remoteURL,localPath)\n", " im=SimpleITK.ReadImage(localPath)\n", " ar=SimpleITK.GetArrayFromImage(im)\n", " for seg in [1,2,4]:\n", " print('{} {}: {}'.format(r['latestFile'],seg,numpy.sum(ar==seg)))\n", " \n", "getSegmentation()" ] }, { "cell_type": "code", "execution_count": null, "id": "93f25f6e-344c-45ca-9c51-b292856eb5d4", "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 }