loadDicom.py 3.7 KB

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