{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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  /home/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  /home/studen/temp/orthancInterface.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",
    "\n",
    "import segmentation\n",
    "import importlib\n",
    "import datetime\n",
    "\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",
    "import labkeyFileBrowser\n",
    "import labkeyDatabaseBrowser\n",
    "\n",
    "nixWrapper.loadLibrary('orthancInterface')\n",
    "import orthancInterface\n",
    "import orthancFileBrowser\n",
    "import orthancDatabaseBrowser\n",
    "\n",
    "def configFile(server):\n",
    "    return os.path.join(os.path.expanduser('~'),'.labkey',\"{}.json\".format(server))\n",
    "    \n",
    "\n",
    "def connectDB(server):\n",
    "    fconfig=configFile(server)\n",
    "    net=labkeyInterface.labkeyInterface()\n",
    "    net.init(fconfig)\n",
    "    net.getCSRF()\n",
    "    return labkeyDatabaseBrowser.labkeyDB(net)\n",
    "\n",
    "def connectOrthanc(server):\n",
    "    fconfig=configFile(server)\n",
    "    net=orthancInterface.orthancInterface()\n",
    "    net.init(fconfig)\n",
    "    return orthancDatabaseBrowser.orthancDB(net)\n",
    "    \n",
    "#manipulate segmentations\n",
    "#rewrite this\n",
    "#nim=getPatientNIM(pId)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#figure out and set studyId from seriesId\n",
    "def assignFromSeriesId():\n",
    "    db=connectDB('merlin')\n",
    "    orthanc=connectOrthanc('merlin')\n",
    "    project='/dinamic_spect/Patients'\n",
    "    dataset='Imaging1'\n",
    "\n",
    "    ds=db.selectRows(project,'study',dataset,'')\n",
    "    for r in ds['rows']:\n",
    "        se=orthanc.getSeriesData(r['nmCorrDataOrthancId'])\n",
    "        r['studyOrthancId']=se['ParentStudy']\n",
    "        db.modifyRows('update',project,'study',dataset,[r])\n",
    "\n",
    "\n",
    "def sortStudies(orthanc,xs,f):\n",
    "    xm={}\n",
    "    for x in xs:\n",
    "        sd=f(orthanc.getStudyData(x))\n",
    "        try:\n",
    "            xm[sd].append(x)\n",
    "        except KeyError:\n",
    "            xm[sd]=[x]\n",
    "    return xm\n",
    "\n",
    "#extractors as 3rd arguments to sortStudies;\n",
    "#they operate on sdata as returned by orthancDatabaseBrowser\n",
    "\n",
    "def getStudyDate(sdata):\n",
    "    return sdata['MainDicomTags']['StudyDate']\n",
    "\n",
    "def getBirthYear(sdata):\n",
    "    return sdata['PatientMainDicomTags']['PatientBirthDate'][0:4]\n",
    "\n",
    "#implementation of sortStudies by studyDate\n",
    "def studyMap(orthanc):\n",
    "    xs=orthanc.getList('studies')\n",
    "    return sortStudies(orthanc,xs,getStudyDate)\n",
    "    \n",
    "    \n",
    "        \n",
    "        \n",
    "def assignFromDate():\n",
    "    db=connectDB('labkey-ukc')\n",
    "    orthanc=connectOrthanc('labkey-ukc')\n",
    "    project='dynamicSPECT/cardiacSPECT'\n",
    "    dataset='Imaging1'\n",
    "    oStudies=studyMap(orthanc)\n",
    "    ds=db.selectRows(project,'study',dataset,'')\n",
    "    for r in ds['rows']:\n",
    "        sid=r['studyOrthancId']\n",
    "        patientId=r['patientCode']\n",
    "        if sid:\n",
    "            print(f'{patientId} ID:{sid}')\n",
    "            continue\n",
    "        sdate=r['imagingDate']\n",
    "        studies=oStudies[sdate]\n",
    "        if len(studies)==1:\n",
    "            r['studyOrthancId']=studies[0]\n",
    "            ds=db.modifyRows('update',project,'study',dataset,[r])\n",
    "            continue\n",
    "        xm=sortStudies(orthanc,studies,getBirthYear)\n",
    "        bdate=patientId[2:]\n",
    "        #filter on bdate\n",
    "        xmPrime=xm[bdate]\n",
    "        #unique solution\n",
    "        if len(xmPrime)==1:\n",
    "            r['studyOrthancId']=xmPrime[0]\n",
    "            ds=db.modifyRows('update',project,'study',dataset,[r])\n",
    "            continue\n",
    "        print(xm)\n",
    "            \n",
    "        \n",
    "assignFromDate()    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 7350f9dcc87c17afddc8c65618089763\n",
      "User: andrej studen CSRF: 7ddb2a798e2815d2cc6344b560e9a8e1\n",
      "User: andrej studen CSRF: 523db9b5e52a7a6c294b36d8e86b81d4\n"
     ]
    }
   ],
   "source": [
    "def isCT(orthanc,sedata):\n",
    "    #base criteria - is CT and SeriesDescription contains AC\n",
    "    mtags=sedata['MainDicomTags']\n",
    "    if not mtags['Modality']=='CT':\n",
    "        return False\n",
    "    try:\n",
    "        if not mtags['SeriesDescription'].find('AC')==0:\n",
    "            return False\n",
    "    except KeyError:\n",
    "        pass\n",
    "    #backup - more than a single instance\n",
    "    n=len(sedata['Instances'])\n",
    "    if n==1:\n",
    "        return False\n",
    "    return True\n",
    "\n",
    "def isMasterNM(orthanc,sedata):\n",
    "    mtags=sedata['MainDicomTags']\n",
    "    if not mtags['Modality']=='NM':\n",
    "        return False\n",
    "    n=len(sedata['Instances'])\n",
    "    if not n==1:\n",
    "        return False\n",
    "    idata=orthanc.getData('instances',sedata['Instances'][0])\n",
    "    imtags=idata['MainDicomTags']\n",
    "    iframes=int(imtags['NumberOfFrames'])\n",
    "    if iframes<500:\n",
    "        return False\n",
    "    #print(idata)\n",
    "    return True\n",
    "    \n",
    "def isCorrDataNM(orthanc,sedata):\n",
    "    mtags=sedata['MainDicomTags']\n",
    "    if not mtags['Modality']=='NM':\n",
    "        return False\n",
    "    n=len(sedata['Instances'])\n",
    "    if n==1:\n",
    "        return False\n",
    "    #idata=orthanc.getData('instances',sedata['Instances'][0])\n",
    "    #imtags=idata['MainDicomTags']\n",
    "    itype=orthanc.getDicomField(sedata['Instances'][0],'0028-0051')\n",
    "    if itype.find('SCAT')==-1:\n",
    "        return False\n",
    "    #print(itype)\n",
    "    return True\n",
    "            \n",
    "\n",
    "def getSeries():\n",
    "    db=connectDB('labkey-ukc')\n",
    "    orthanc=connectOrthanc('labkey-ukc')\n",
    "    project='dynamicSPECT/cardiacSPECT'\n",
    "    dataset='Imaging1'\n",
    "    ds=db.selectRows(project,'study',dataset,'')\n",
    "    for r in ds['rows']:\n",
    "        if r['nmMasterOrthancId']:\n",
    "            continue\n",
    "        series=orthanc.getStudyData(r['studyOrthancId'])['Series']\n",
    "        for s in series:\n",
    "            sedata=orthanc.getSeriesData(s)\n",
    "            mtags=sedata['MainDicomTags']\n",
    "            #print('{} {}'.format(mtags['Modality'],mtags['SeriesDescription']))\n",
    "            if isCT(orthanc,sedata):\n",
    "                r['ctOrthancId']=s\n",
    "                continue\n",
    "            if isMasterNM(orthanc,sedata):\n",
    "                r['nmMasterOrthancId']=s\n",
    "                continue\n",
    "            if isCorrDataNM(orthanc,sedata):\n",
    "                r['nmCorrDataOrthancId']=s\n",
    "                continue\n",
    "            \n",
    "            #print(sedata)\n",
    "        #break\n",
    "        ds=db.modifyRows('update',project,'study',dataset,[r])\n",
    "        #break\n",
    "getSeries() \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: eda92fe6acfc645dcc20e64451de2bcc\n",
      "User: andrej studen CSRF: ccdd6bfe993c84237648879c32922324\n",
      "User: andrej studen CSRF: 1eefebcb86155da277327e320c6d855e\n",
      "User: andrej studen CSRF: 97ac1e100819769b7852e9a7dd3290fd\n",
      "User: andrej studen CSRF: ed40684a2b8537c23fc358ed4d900a47\n",
      "User: andrej studen CSRF: 34ad147b1b7dd2dd3e61e67158a34afb\n",
      "User: andrej studen CSRF: 5f51b6f277e43102b44c97658b8a11c8\n",
      "User: andrej studen CSRF: 8b66c1e79af33886680486734ab0b10b\n",
      "User: andrej studen CSRF: 0c4c51e3f4b05f46f5fe68b35a1d6e21\n",
      "User: andrej studen CSRF: 73f023fe6494ee80f446212df52f6702\n",
      "User: andrej studen CSRF: 479c20de89dc3d47e250677aab54db53\n",
      "User: andrej studen CSRF: 375be6798203b87d56a61eafdcc2fdc4\n",
      "User: andrej studen CSRF: 13553704c3885f2667711e1bb5bd8100\n",
      "User: andrej studen CSRF: 84442ae1e529d6e74d730fe96342a64f\n"
     ]
    }
   ],
   "source": [
    "#get imagingDate from studyOrthancId\n",
    "def getImagingDate():\n",
    "    db=connectDB('labkey-ukc')\n",
    "    orthanc=connectOrthanc('labkey-ukc')\n",
    "    project='/dynamicSPECT/cardiacSPECT'\n",
    "    dataset='Imaging1'\n",
    "    ds=db.selectRows(project,'study',dataset,'')\n",
    "    rows=ds['rows']\n",
    "    for r in rows:\n",
    "        sid=r['studyOrthancId']\n",
    "        patientId=r['PatientId']\n",
    "        if not sid:\n",
    "            print(f'No study id for {patientId}')\n",
    "            continue\n",
    "        sd=orthanc.getStudyData(r['studyOrthancId'])\n",
    "        r['imagingDate']=sd['MainDicomTags']['StudyDate']\n",
    "        db.modifyRows('update',project,'study',dataset,[r])\n",
    "        \n",
    "def setStudyDate():\n",
    "    db=connectDB('labkey-ukc')\n",
    "    orthanc=connectOrthanc('labkey-ukc')\n",
    "    project='/dynamicSPECT/cardiacSPECT'\n",
    "    dataset='Imaging1'\n",
    "    ds=db.selectRows(project,'study',dataset,[])\n",
    "    rows=ds['rows']\n",
    "    for r in rows:\n",
    "        dt=datetime.datetime.strptime(r['imagingDate'],'%Y%m%d')\n",
    "        r['studyDate']=dt.strftime('%Y/%m/%d')\n",
    "        db.modifyRows('update',project,'study',dataset,[r])\n",
    "        \n",
    "setStudyDate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: ef76bf934ffbf0af9fc0547fc7871983\n",
      "User: andrej studen CSRF: 816ac23d327ba77af1abb18b9c0ba3e1\n",
      "User: andrej studen CSRF: 1be64b3e1b0d3ed0b6853d56ac70e6a5\n",
      "User: andrej studen CSRF: d5b958584db13089e2731feb37334487\n",
      "User: andrej studen CSRF: 9ecdad5e798ef004b999fce6727c4e0f\n",
      "User: andrej studen CSRF: 19850f8c5b0c4217d483bea59a6fe743\n",
      "User: andrej studen CSRF: 3ecb54e610afb5605c92a7fa19fb3326\n",
      "User: andrej studen CSRF: 602968a1373dac5c14d55af8dd1c475b\n",
      "User: andrej studen CSRF: 16eb98eddbaa6b58f4a5d733a270c013\n",
      "User: andrej studen CSRF: 207b55bf76fcd1a88fb316cd832d88a4\n",
      "User: andrej studen CSRF: ecf8895ef0da5c1f092a6ec3a64f7d9c\n",
      "User: andrej studen CSRF: 94735fb33035a678e5836a38f7cdcdb6\n",
      "User: andrej studen CSRF: 9cbec531e64fed313135398a92341495\n",
      "User: andrej studen CSRF: 2387d8b279dfa87b4a1d6cadf1db7d3f\n"
     ]
    }
   ],
   "source": [
    "#set patientCode\n",
    "db=connectDB('labkey-ukc')\n",
    "project='/dynamicSPECT/cardiacSPECT'\n",
    "dataset='Imaging1'\n",
    "ds=db.selectRows(project,'study',dataset,'')\n",
    "rows=ds['rows']\n",
    "for r in rows:\n",
    "    r['patientCode']=r['PatientId']\n",
    "    db.modifyRows('update',project,'study',dataset,[r])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 4f341fbc6f8b6681df54039307becbd7\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100  9.8M    0  9.8M    0     0  1017k      0 --:--:--  0:00:09 --:--:-- 2100k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 992c0733-b219b554-7ffbb1eb-e29d0606-63a62c51 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100  9.9M  100 23131  100  9.8M   3353  1468k  0:00:06  0:00:06 --:--:-- 1524k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored AlreadyStored\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 9262k    0 9262k    0     0   369k      0 --:--:--  0:00:25 --:--:-- 1015k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 8de576d0-e88601c2-5317457b-47705c87-60c15eee to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 9283k  100 21233  100 9262k   3544  1546k  0:00:05  0:00:05 --:--:-- 1745k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 11.7M    0 11.7M    0     0   346k      0 --:--:--  0:00:34 --:--:-- 37959\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 137d9e53-0e77d03e-55f56fbf-55358d85-f473d2c5 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 11.7M  100 33199  100 11.7M   4014  1455k  0:00:08  0:00:08 --:--:-- 1347k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.9M    0 12.9M    0     0   359k      0 --:--:--  0:00:36 --:--:-- 2426k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 3dd2760f-412cbfba-192c1d12-3832dc3c-321651a5 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.9M  100 35515  100 12.9M   3549  1326k  0:00:10  0:00:10 --:--:--  307k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.1M    0 12.1M    0     0   539k      0 --:--:--  0:00:22 --:--:--  123k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 07cb4e32-30da5d01-75d7067f-f393540f-432489e1 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.1M  100 34743  100 12.1M   4214  1503k  0:00:08  0:00:08 --:--:--  939k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 11.7M    0 11.7M    0     0   857k      0 --:--:--  0:00:14 --:--:--  964k\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 80bb7c25-6e4d4890-f6f3c46e-f0a7bdfd-2be580d2 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "100 11.7M  100 33199  100 11.7M   4160  1508k  0:00:07  0:00:07 --:--:-- 1091k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.4M    0 12.4M    0     0   659k      0 --:--:--  0:00:19 --:--:-- 2216k\n",
      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
      "  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloaded study 1a759996-7bdd0109-c5e8f833-c6857dfe-84eaed66 to /home/studen/temp/Study.zip\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.4M    0     0  100 12.4M      0  1671k  0:00:07  0:00:07 --:--:-- 1762k"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success Success\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100 12.5M  100 35515  100 12.4M   4102  1474k  0:00:08  0:00:08 --:--:--  910k\n"
     ]
    }
   ],
   "source": [
    "#copy files from one orthanc instance to another\n",
    "db=connectDB('merlin')\n",
    "orthanc=connectOrthanc('merlin')\n",
    "project='/dinamic_spect/Patients'\n",
    "dataset='Imaging1'\n",
    "\n",
    "ds=db.selectRows(project,'study',dataset,'')\n",
    "src=configFile('merlin')\n",
    "target=configFile('kclj')\n",
    "path=['software','src','orthancInterface','scripts','moveDicom.sh']\n",
    "path.insert(0,os.path.expanduser('~'))\n",
    "script=os.path.join(*path)\n",
    "for r in ds['rows']:\n",
    "    subprocess.run([script,src,target,r['studyOrthancId']])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: ad0e9a315e6fac8a19f892af1e550d39\n",
      "User: andrej studen CSRF: e89f9c751f9ca44877fceac804e4909f\n",
      "User: andrej studen CSRF: f7b3b5f0ddf5198e98a29b7ba31072bd\n",
      "User: andrej studen CSRF: d34f232a12afc576f9d0477842f9d099\n",
      "User: andrej studen CSRF: 16deee5f2eb2c59edb604416b8a71bab\n",
      "User: andrej studen CSRF: fab98bb2b2fc0bb9c7c356ed7d468963\n",
      "User: andrej studen CSRF: 20974f8994412e7c209edb0c100b59bc\n",
      "User: andrej studen CSRF: 84cff63141f897248071e16692de5338\n",
      "User: andrej studen CSRF: a80d3d28add7108dd4287c5fbf4b65b8\n"
     ]
    }
   ],
   "source": [
    "#copy dataset from one instance to another\n",
    "src=connectDB('merlin')\n",
    "target=connectDB('kclj')\n",
    "srcProject='dinamic_spect/Patients'\n",
    "targetProject='dynamicSPECT/cardiacSPECT'\n",
    "schema='study'\n",
    "dataset='Imaging1'\n",
    "\n",
    "def copyDatasetDesign(src,srcProject,target,targetProject,schema,dataset):\n",
    "    minProperties=['name','label','rangeURI','lookupSchema',\n",
    "                       'lookupContainer','lookupQuery']\n",
    "    dsgn=target.getQueryDesign(targetProject,'study',dataset)\n",
    "    if 'exception' in dsgn:\n",
    "        print('Missing {} {}'.format(schema,dataset))\n",
    "        #impossible to create list copy them by hand\n",
    "        if schema=='lists':\n",
    "            print('create lists by list archive export')\n",
    "            return\n",
    "        design=src.getQueryDesign(srcProject,schema,dataset)\n",
    "\n",
    "        fields=[]\n",
    "        for f in design['fields']:\n",
    "            #skip Key\n",
    "            if f['name']=='Key':\n",
    "                continue\n",
    "            #use subselection\n",
    "            q={p:f[p] for p in minProperties}\n",
    "            fields.append(q)\n",
    "\n",
    "        print(target.addQuery(targetProject,schema,dataset,fields))\n",
    "        print('Created {}'.format(dataset))\n",
    "\n",
    "def internalField(v):\n",
    "    if v.find('_')==0:\n",
    "        return True\n",
    "    if v=='lsid':\n",
    "        return True\n",
    "    return False\n",
    "        \n",
    "def copyDatasetData(src,srcProject,target,targetProject,schema,dataset):\n",
    "    ds=src.selectRows(srcProject,schema,dataset,[])\n",
    "    fVar=['PatientId','SequenceNum']\n",
    "    skipVar=['lsid']\n",
    "    for r in ds['rows']:\n",
    "        qfilter=[{'variable':x,'value':'{}'.format(r[x]),'oper':'eq'} for x in fVar]\n",
    "        ds1=target.selectRows(targetProject,schema,dataset,qfilter)\n",
    "        if len(ds1['rows'])>0:\n",
    "            outRow=ds1['rows'][0]\n",
    "            mode='update'\n",
    "        else:\n",
    "            mode='insert'\n",
    "            outRow={}\n",
    "        for x in r:\n",
    "            if internalField(x):\n",
    "                #print('{}/Skip'.format(x))\n",
    "                continue\n",
    "            #print('{}/Use'.format(x))\n",
    "            outRow[x]=r[x]\n",
    "        target.modifyRows(mode,targetProject,schema,dataset,[outRow])\n",
    "        #break\n",
    "#copyDatasetDesign(src,srcProject,target,targetProject,schema,dataset)\n",
    "copyDatasetData(src,srcProject,target,targetProject,schema,dataset)"
   ]
  },
  {
   "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
}
