loadDicom.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import slicer
  2. import os
  3. import subprocess
  4. import re
  5. dicomModify=os.getenv("HOME")
  6. if not dicomModify==None:
  7. dicomModify+="/software/install/"
  8. dicomModify+="dicomModify/bin/dicomModify"
  9. class loadDicom(slicer.ScriptedLoadableModule.ScriptedLoadableModule):
  10. def __init__(self,parent):
  11. slicer.ScriptedLoadableModule.ScriptedLoadableModule.__init__(self, parent)
  12. self.className="loadDicom"
  13. self.parent.title="loadDicom"
  14. self.parent.categories = ["Examples"]
  15. self.parent.dependencies = []
  16. self.parent.contributors = ["Andrej Studen (UL/FMF)"] # replace with "Firstname Lastname (Organization)"
  17. self.parent.helpText = """
  18. utilities for parsing dicom entries
  19. """
  20. self.parent.acknowledgementText = """
  21. Developed within the medical physics research programme of the Slovenian research agency.
  22. """ # replace with organization, grant and thanks.
  23. class loadDicomWidget(slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget):
  24. """Uses ScriptedLoadableModuleWidget base class, available at:
  25. https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
  26. """
  27. def setup(self):
  28. slicer.ScriptedLoadableModule.ScriptedLoadableModuleWidget.setup(self)
  29. class loadDicomLogic(slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic):
  30. def __init__(self,parent):
  31. slicer.ScriptedLoadableModule.ScriptedLoadableModuleLogic.__init__(self, parent)
  32. def load(self,sNet,dir,doRemove=True):
  33. print("Loading dir {}").format(dir)
  34. dicomFiles=sNet.listDir(dir)
  35. #filelist=[os.path.join(dir,f) for f in os.listdir(dir)]
  36. filelist=[]
  37. for f in dicomFiles:
  38. localPath=sNet.GetFile(f)
  39. f0=localPath
  40. f1=f0+"1"
  41. if not dicomModify==None:
  42. subprocess.call(dicomModify+" "+f0+" "+f1+" && mv "+f1+" "+f0+";", shell=True)
  43. filelist.append(localPath)
  44. try:
  45. loadables=self.volumePlugin.examineForImport([filelist])
  46. except AttributeError:
  47. self.volumePlugin=slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
  48. loadables=self.volumePlugin.examineForImport([filelist])
  49. for loadable in loadables:
  50. #TODO check if it makes sense to load a particular loadable
  51. if loadable.name.find('imageOrientationPatient')>-1:
  52. continue
  53. volumeNode=self.volumePlugin.load(loadable)
  54. if volumeNode != None:
  55. vName='Series'+dicomValue(loadable.files[0],"0020,0011")
  56. volumeNode.SetName(vName)
  57. try:
  58. loadableRTs=self.RTPlugin.examineForImport([filelist])
  59. except:
  60. self.RTPlugin=plugin=slicer.modules.dicomPlugins['DicomRtImportExportPlugin']()
  61. loadableRTs=self.RTPlugin.examineForImport([filelist])
  62. for loadable in loadableRTs:
  63. segmentationNode=self.RTPlugin.load(loadable)
  64. #if segmentationNode!= None:
  65. # segmentationNode.SetName('SegmentationBR')
  66. if not doRemove:
  67. return
  68. for f in filelist:
  69. os.remove(f)
  70. def dicomValue(file,tag):
  71. dcmdump=os.path.join(os.environ['SLICER_HOME'],"bin")
  72. dcmdump=os.path.join(dcmdump,"dcmdump")
  73. try:
  74. out=subprocess.check_output([dcmdump,'+P',tag,file])
  75. out=re.sub(r'^.*\[(.*)\].*\n$',r'\1',out)
  76. return out
  77. except:
  78. return None
  79. def clearNodes():
  80. nodes=[]
  81. nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode"))
  82. nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeDisplayNode"))
  83. nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationNode"))
  84. nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationDisplayNode"))
  85. for node in nodes:
  86. slicer.mrmlScene.RemoveNode(node)