loadDicom.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. dicomFiles=sNet.listDir(dir)
  34. #filelist=[os.path.join(dir,f) for f in os.listdir(dir)]
  35. filelist=[]
  36. for f in dicomFiles:
  37. localPath=sNet.GetFile(f)
  38. f0=localPath
  39. f1=f0+"1"
  40. subprocess.call(dicomModify+" "+f0+" "+f1+" && mv "+f1+" "+f0+";", shell=True)
  41. filelist.append(localPath)
  42. try:
  43. loadables=self.volumePlugin.examineForImport([filelist])
  44. except AttributeError:
  45. self.volumePlugin=slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
  46. loadables=self.volumePlugin.examineForImport([filelist])
  47. for loadable in loadables:
  48. #TODO check if it makes sense to load a particular loadable
  49. if loadable.name.find('imageOrientationPatient')>-1:
  50. continue
  51. volumeNode=self.volumePlugin.load(loadable)
  52. if volumeNode != None:
  53. vName='Series'+dicomValue(loadable.files[0],"0020,0011")
  54. volumeNode.SetName(vName)
  55. try:
  56. loadableRTs=self.RTPlugin.examineForImport([filelist])
  57. except:
  58. self.RTPlugin=plugin=slicer.modules.dicomPlugins['DicomRtImportExportPlugin']()
  59. loadableRTs=self.RTPlugin.examineForImport([filelist])
  60. for loadable in loadableRTs:
  61. segmentationNode=self.RTPlugin.load(loadable)
  62. #if segmentationNode!= None:
  63. # segmentationNode.SetName('SegmentationBR')
  64. if not doRemove:
  65. return
  66. for f in filelist:
  67. os.remove(f)
  68. def dicomValue(file,tag):
  69. dcmdump=os.path.join(os.environ['SLICER_HOME'],"bin")
  70. dcmdump=os.path.join(dcmdump,"dcmdump")
  71. try:
  72. out=subprocess.check_output([dcmdump,'+P',tag,file])
  73. out=re.sub(r'^.*\[(.*)\].*\n$',r'\1',out)
  74. return out
  75. except:
  76. return None
  77. def clearNodes():
  78. nodes=[]
  79. nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode"))
  80. nodes.extend(slicer.util.getNodesByClass("vtkMRMLScalarVolumeDisplayNode"))
  81. nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationNode"))
  82. nodes.extend(slicer.util.getNodesByClass("vtkMRMLSegmentationDisplayNode"))
  83. for node in nodes:
  84. slicer.mrmlScene.RemoveNode(node)