{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/studen/software/install/Slicer/lib/Python/lib/python3.6/site-packages/urllib3/connection.py:395: 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",
      "  SubjectAltNameWarning,\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: d070f9c0c9d1624c76a72190dfeb82fd\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'d070f9c0c9d1624c76a72190dfeb82fd'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "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()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/studen/software/install/Slicer/lib/Python/lib/python3.6/site-packages/urllib3/connection.py:395: 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",
      "  SubjectAltNameWarning,\n"
     ]
    }
   ],
   "source": [
    "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','Imaging',[])\n",
    "patients=[row['aliasID'] for row in ds['rows']]\n",
    "for p in patients:\n",
    "    pass\n",
    "    #patientId='7TMMIR'\n",
    "    #files=downloadPatient.readPatient(fb,locDir,project,p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['10KFMIR', '10KFOBR', '11ZMMIR', '11ZMOBR', '2SBMIR', '2SMobr', '3ZFMIR', '3ZFOBR', '5MIMIR', '5MIOBR', '7TMMIR', '7TMOBR', '8JSMIR', '8JSOBR']\n"
     ]
    }
   ],
   "source": [
    "print(patients)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: e3eac520b926622281349e3fd84a911a\n",
      "Uploading 5MIMIR_sliceView.png\n",
      "User: andrej studen CSRF: 2f9c2b33df616b5e542a94e05b9b4723\n"
     ]
    }
   ],
   "source": [
    "import ScreenCapture\n",
    "cap = ScreenCapture.ScreenCaptureLogic()\n",
    "ds=db.selectRows(project,'study','Imaging',[])\n",
    "patients=[row['aliasID'] for row in ds['rows']]\n",
    "patients1=[patients[8]]\n",
    "#patients1=patients\n",
    "for p in patients1:\n",
    "    [slicer.mrmlScene.RemoveNode(v) for v in slicer.util.getNodesByClass('vtkMRMLVolumeNode')]\n",
    "    [slicer.mrmlScene.RemoveNode(v) for v in slicer.util.getNodesByClass('vtkMRMLSegmentationNode')]\n",
    "    \n",
    "    fCT=os.path.join(locDir,p,p+'CT.nrrd')\n",
    "    nodeCT=slicer.util.loadNodeFromFile(fCT,'VolumeFile')\n",
    "    fV19=os.path.join(locDir,p,p+'Volume19.nrrd')\n",
    "    nodeV19=slicer.util.loadNodeFromFile(fV19,'VolumeFile')\n",
    "    nodeV19.GetScalarVolumeDisplayNode().SetAndObserveColorNodeID('vtkMRMLColorTableNodeFileInferno.txt')\n",
    "    fSegm=os.path.join(locDir,p,p+'_Segm.nrrd')\n",
    "    nodeSegm=slicer.util.loadNodeFromFile(fSegm,'SegmentationFile')\n",
    "    slicer.util.setSliceViewerLayers(background=nodeCT,foreground=nodeV19,label=nodeSegm,foregroundOpacity=0.5)\n",
    "    \n",
    "    r=[row for row in ds['rows'] if row['aliasID']==p][0]\n",
    "    fz=r['baseZ']\n",
    "    dir=[0,0,0]\n",
    "    origin=nodeV19.GetOrigin()\n",
    "    sp=nodeV19.GetSpacing()\n",
    "    nodeV19.GetJToRASDirection(dir)\n",
    "    off=[fz*sp[2]*x for x in dir]\n",
    "    pz=[x+y for (x,y) in zip(origin,off)]\n",
    "    \n",
    "    view=slicer.app.layoutManager().sliceWidget('Red')\n",
    "    view.sliceController().setSegmentationOpacity(0.1)\n",
    "    view.sliceLogic().SetSliceOffset(pz[2])\n",
    "    viewCap = cap.viewFromNode(slicer.mrmlScene.GetNodeByID('vtkMRMLSliceNodeRed'))\n",
    "    viewFileName=p+'_sliceView.png'\n",
    "    viewFile=os.path.join(locDir,p,viewFileName)\n",
    "    cap.captureImageFromView(viewCap,viewFile)\n",
    "    fb.writeFileToFile(viewFile,fb.buildPathURL(project,[p])+'/'+viewFileName)\n",
    "    print('Uploading {}'.format(viewFileName))\n",
    "    r['sliceView']=viewFileName\n",
    "    db.modifyRows('update',project,'study','Imaging',[r])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[slicer.mrmlScene.RemoveNode(v) for v in slicer.util.getNodesByClass('vtkMRMLVolumeNode')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-11.6860873538232, 475.06586290468414, 1525.948169558067]\n",
      "[0.09892055255379667, -0.06777229572872773, -0.9927847904828667]\n"
     ]
    }
   ],
   "source": [
    "dir=[0,0,0]\n",
    "origin=slicer.util.getFirstNodeByName('10KFMIRVolume19*').GetOrigin()\n",
    "sp=slicer.util.getFirstNodeByName('10KFMIRVolume19*').GetSpacing()\n",
    "slicer.util.getFirstNodeByName('10KFMIRVolume19*').GetJToRASDirection(dir)\n",
    "off=[32.5*sp[1]*x for x in dir]\n",
    "pz=[x+y for (x,y) in zip(origin,off)]\n",
    "print(pz)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'vtkMRMLColorTableNodeFileInferno.txt'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "node.GetScalarVolumeDisplayNode().GetColorNodeID()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "slicer.app.layoutManager().sliceWidget('Red').sliceLogic().SetSliceOffset(1500)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Slicer 4.11",
   "language": "python",
   "name": "slicer-4.11"
  },
  "language_info": {
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
