loadDicom.py 3.7 KB

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