Ver código fonte

Merge branch 'master' of wiscigt.powertheword.com:oil/EMBRACE

Andrej 3 anos atrás
pai
commit
b6f74771d8
1 arquivos alterados com 293 adições e 0 exclusões
  1. 293 0
      slicerScripts/embrace.ipynb

+ 293 - 0
slicerScripts/embrace.ipynb

@@ -0,0 +1,293 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "704c900c-9af8-4a68-95e5-10a13a749a22",
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "importDicom ver: 1.0.2\n",
+      "loadDicom ver: 1.0.0\n",
+      "loadPatient.load ver: 1.0.3\n",
+      "User: andrej studen CSRF: 014d1d34b354fe0645d621f760feec80\n",
+      "\n",
+      "Adding series 1 f=http://onko-nix.onko-i.si:8080/labkey/_webdav/Test/Transfer/%40files/dicom/1.2.840.113704.1.1762661776.985.1253733606.8/2.16.840.1.114337.23840299283.22957.20190906170313.0/2.16.840.1.114337.23840299283.22957.20190906170313.0.0\n",
+      "...................................................................................................Got 1 series\n",
+      "Got 1 series\n",
+      "....................................................................................................Added 100 files\n"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "C:\\Users\\studen\\AppData\\Local\\NA-MIC\\Slicer 4.11.20200930\\lib\\Python\\Lib\\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: a2559dbbd284436b12f91b98557e3fdf\n",
+      "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "C:\\Users\\studen\\AppData\\Local\\NA-MIC\\Slicer 4.11.20200930\\lib\\Python\\Lib\\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": [
+      "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+     ]
+    },
+    {
+     "name": "stderr",
+     "output_type": "stream",
+     "text": [
+      "C:\\Users\\studen\\AppData\\Local\\NA-MIC\\Slicer 4.11.20200930\\lib\\Python\\Lib\\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": [
+      "xxxxxxxx"
+     ]
+    }
+   ],
+   "source": [
+    "import sys\n",
+    "import os\n",
+    "import json\n",
+    "import pydicom\n",
+    "import importlib\n",
+    "import numpy\n",
+    "import Elastix \n",
+    "import vtk\n",
+    "import DicomRtImportExportPlugin\n",
+    "import glob\n",
+    "import zipfile\n",
+    "\n",
+    "#just load nix suite\n",
+    "sys.path.append(os.path.join(os.path.expanduser('~'),'software','src','nixSuite','wrapper'))\n",
+    "import nixWrapper\n",
+    "\n",
+    "nixWrapper.loadLibrary('labkeyInterface')\n",
+    "import labkeyInterface\n",
+    "\n",
+    "#DB access at ONKO-NIX\n",
+    "net=labkeyInterface.labkeyInterface()\n",
+    "fconfig=os.path.join(os.path.expanduser('~'),'.labkey','network.json')\n",
+    "net.init(fconfig)\n",
+    "\n",
+    "#DMR are at merlin\n",
+    "netDMR=labkeyInterface.labkeyInterface()\n",
+    "fconfigDMR=os.path.join(os.path.expanduser('~'),'.labkey','merlin.json')\n",
+    "netDMR.init(fconfigDMR)\n",
+    "\n",
+    "import labkeyDatabaseBrowser\n",
+    "db=labkeyDatabaseBrowser.labkeyDB(net)\n",
+    "\n",
+    "import labkeyFileBrowser\n",
+    "fb=labkeyFileBrowser.labkeyFileBrowser(net)\n",
+    "fbDMR=labkeyFileBrowser.labkeyFileBrowser(netDMR)\n",
+    "\n",
+    "#dicom tools\n",
+    "nixWrapper.loadLibrary('DICOMtools')\n",
+    "try:\n",
+    "    importlib.reload(importDicom)\n",
+    "except NameError:\n",
+    "    import importDicom\n",
+    "    \n",
+    "try:\n",
+    "    importlib.reload(loadDicom)\n",
+    "except NameError:\n",
+    "    import loadDicom\n",
+    "\n",
+    "    \n",
+    "nixWrapper.loadLibrary('EMBRACE')\n",
+    "try:\n",
+    "    importlib.reload(loadPatient)\n",
+    "except NameError:\n",
+    "    import loadPatient\n",
+    "\n",
+    "project='EMBRACE/studija'\n",
+    "iDicom=importDicom.importDicom()\n",
+    "lDicom=loadDicom.loadDicom()\n",
+    "\n",
+    "try:\n",
+    "    del elastix\n",
+    "except NameError:\n",
+    "    pass\n",
+    "\n",
+    "elastix = Elastix.ElastixLogic()\n",
+    "\n",
+    "registrationIndex = 29 #targeted EMBRACE\n",
+    "registrationIndex = 30 #rigid EMBRACE\n",
+    "RegistrationPresets_ParameterFilenames = 5\n",
+    "parameterFilenames = elastix.getRegistrationPresets()[registrationIndex]\\\n",
+    "    [RegistrationPresets_ParameterFilenames]\n",
+    "\n",
+    "ids=range(1,87)\n",
+    "ids=range(6,7)\n",
+    "\n",
+    "shn = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)\n",
+    "baseDir=os.path.join('z:\\\\','EMBRACE')\n",
+    "if not os.path.isdir(baseDir):\n",
+    "    os.makedirs(baseDir)\n",
+    "\n",
+    "transformLogic = slicer.vtkSlicerTransformLogic()\n",
+    "\n",
+    "exporter = DicomRtImportExportPlugin.DicomRtImportExportPluginClass()\n",
+    "\n",
+    "for id in ids:\n",
+    "\n",
+    "    label = 'LJU'+'{:03d}'.format(id)\n",
+    "    loadDicom.clearNodes()\n",
+    "    \n",
+    "    nodes=loadPatient.load(db,fb,fbDMR,iDicom,lDicom,project,label)\n",
+    "    print('Segmentations: {}'.format(len(slicer.util.getNodesByClass(\"vtkMRMLSegmentationNode\"))))\n",
+    "    \n",
+    "    #debug \n",
+    "    #seg=nodes['CTRS'].GetSegmentation()\n",
+    "    #n=seg.GetNumberOfSegments()\n",
+    "    #for i in numpy.arange(n):\n",
+    "    #    sId=seg.GetNthSegmentID(int(i))\n",
+    "    #    print(sId)\n",
+    "    #continue\n",
+    "\n",
+    "    try:\n",
+    "        fixedVolumeNode = nodes['DMR']\n",
+    "        movingVolumeNode = nodes['CT']\n",
+    "    except KeyError:\n",
+    "        continue\n",
+    "\n",
+    "    transformNodeName = label+'_T2_DF'\n",
+    "    #\n",
+    "    transformNode=slicer.vtkMRMLGridTransformNode()\n",
+    "    transformNode.SetName(transformNodeName)\n",
+    "    slicer.mrmlScene.AddNode(transformNode)\n",
+    "    \n",
+    "    elastix.registerVolumes(fixedVolumeNode, movingVolumeNode, \\\n",
+    "                            parameterFilenames = parameterFilenames, outputVolumeNode = None, \n",
+    "                            outputTransformNode = transformNode, \\\n",
+    "                            fixedVolumeMaskNode = None, movingVolumeMaskNode = None, \n",
+    "                            forceDisplacementFieldOutputTransform = True)\n",
+    "\n",
+    "    \n",
+    "    #dataManager.export(network, 'EMBRACE/Studija', patientID)\n",
+    "    #create output dicom with MRI and migrated CTRS\n",
+    "    oDir=os.path.join(baseDir,label)\n",
+    "    if not os.path.isdir(oDir):\n",
+    "        os.makedirs(oDir)\n",
+    "    #transformNode=slicer.util.loadTransform(os.path.join(oDir,'Transform_R.h5'))\n",
+    "    \n",
+    "        \n",
+    "    #remove all prior dicom files\n",
+    "    dcmList=glob.glob(os.path.join(oDir,'*.dcm'))\n",
+    "    for f in dcmList:\n",
+    "        os.remove(f)\n",
+    "    \n",
+    "    for mode in ['CT','DMR']:\n",
+    "        slicer.util.saveNode(nodes[mode],os.path.join(oDir,label+'_'+mode+'.nrrd'))\n",
+    "    slicer.util.saveNode(transformNode,os.path.join(oDir,label+'_DF.nrrd'))\n",
+    "    seg=nodes['CTRS'].GetSegmentation()\n",
+    "    n=seg.GetNumberOfSegments()\n",
+    "    \n",
+    "    nodes['CTRS'].SetAndObserveTransformNodeID(transformNode.GetID())\n",
+    "    \n",
+    "    #transformLogic.hardenTransform(nodes['CTRS'])\n",
+    "    for i in numpy.arange(n):\n",
+    "        sId=seg.GetNthSegmentID(int(i))\n",
+    "        print(sId)\n",
+    "        seg.GetSegment(sId)\n",
+    "        fname=os.path.join(oDir,label+'_{}.nrrd'.format(sId))\n",
+    "        labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLLabelMapVolumeNode')\n",
+    "        \n",
+    "        idArray = vtk.vtkStringArray()\n",
+    "        idArray.InsertNextValue(sId)\n",
+    "        slicer.vtkSlicerSegmentationsModuleLogic.ExportSegmentsToLabelmapNode(\n",
+    "                  nodes['CTRS'], idArray, labelmapVolumeNode, nodes['DMR'])\n",
+    "        slicer.util.saveNode(labelmapVolumeNode, fname)\n",
+    "    \n",
+    "    exportables=[]\n",
+    "    for mode in ['CTRS','DMR']:\n",
+    "        itemId=shn.GetItemByDataNode(nodes[mode])\n",
+    "        exportables.extend(exporter.examineForExport(itemId))\n",
+    "                                           \n",
+    "    for exp in exportables:\n",
+    "        exp.directory=oDir\n",
+    "        exp.setTag('PatientID',label)\n",
+    "        exp.setTag('PatientName',label)\n",
+    "        exp.setTag('StudyDescription','contoursAligned')\n",
+    "        exp.setTag('PatientID',label)\n",
+    "        if exp.tag('Modality')=='CT':\n",
+    "            exp.setTag('Modality','MR')\n",
+    "            exp.setTag('ScanningSequence',nodes['metadataDMR']['scanningSequence'])\n",
+    "            exp.setTag('SequenceVariant',nodes['metadataDMR']['sequenceVariant'])\n",
+    "            \n",
+    "    \n",
+    "    exporter.export(exportables)\n",
+    "   "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "id": "f2399104-5f88-4151-80fa-59ab4446a69e",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "ids=range(3,5)\n",
+    "for id in ids:\n",
+    "    label = 'LJU'+'{:03d}'.format(id)\n",
+    "    oDir=os.path.join(baseDir,label)\n",
+    "    dcmList=glob.glob(os.path.join(oDir,'*.dcm'))\n",
+    "    ozip=os.path.join(baseDir,label+'.zip')\n",
+    "    with zipfile.ZipFile(ozip, 'w') as zf:\n",
+    "        for f in dcmList:\n",
+    "            zf.write(f)\n",
+    "\n",
+    "    rDir=fb.buildPathURL(project,[label])\n",
+    "    rZip='{}/{}.zip'.format(rDir,label)\n",
+    "    fb.writeFileToFile(ozip,rZip)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "3943dc9d-3070-4cdb-90e8-c79e2100745b",
+   "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": 5
+}