Skip to content

Commit c2ca13c

Browse files
author
idhamari
committed
update scripts for slicer 5
1 parent 3f3b5bd commit c2ca13c

3 files changed

Lines changed: 65 additions & 75 deletions

File tree

CochleaReg/CochleaReg.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@
1818
# [4] https://mtixnat.uni-koblenz.de #
1919
# #
2020
#-------------------------------------------------------------------------------------#
21-
# Slicer 4.11.0 #
22-
# Updated: 18.1.2022 #
21+
# Slicer 5.0.3 #
22+
# Updated: 6.8.2022 #
2323
#======================================================================================
2424

25-
import os, time, logging, unittest
25+
# Non Slicer libs
26+
from __future__ import print_function
27+
import os, sys, time, re, shutil, math, unittest, logging, zipfile, platform, subprocess, hashlib
28+
from shutil import copyfile
29+
30+
from six.moves.urllib.request import urlretrieve
2631
import numpy as np
27-
from __main__ import qt, ctk, slicer
32+
import SimpleITK as sitk
33+
34+
# Slicer related
35+
from __main__ import vtk, qt, ctk, slicer
2836
from slicer.ScriptedLoadableModule import *
37+
import sitkUtils
2938
import SampleData
30-
39+
import SegmentStatistics
40+
import Elastix
3141
import VisSimCommon
3242

3343
# TODO:
@@ -37,7 +47,7 @@
3747
# Terminology
3848
# img : ITK image
3949
# imgNode : Slicer Node
40-
# imgName : Filename without the path and without extension
50+
# imgName : Filename without the path and without extension
4151
# imgPath : wholePath + Filename and extension
4252

4353

@@ -50,14 +60,7 @@ def __init__(self, parent):
5060
parent.title = "Cochlea Registration"
5161
parent.categories = ["VisSimTools"]
5262
parent.dependencies = []
53-
parent.contributors = [
54-
"Christopher Guy",
55-
"Ibraheem Al-Dhamari",
56-
"Michel Peltriauxe",
57-
"Anna Gessler",
58-
"Jasper Grimmig",
59-
"Pepe Eulzer"
60-
]
63+
parent.contributors = ["Ibraheem Al-Dhamari"]
6164
parent.helpText = " This module uses ACIR method to auatomatically register cochlea images"
6265
parent.acknowledgementText = " This work is sponsored by Cochlear as part of COMBS project "
6366
self.parent = parent
@@ -71,7 +74,7 @@ class CochleaRegWidget(ScriptedLoadableModuleWidget):
7174
def setup(self):
7275
print(" ")
7376
print("=======================================================")
74-
print(" Automatic Cochlea Image Registration ")
77+
print(" Automatic Cochlea Image Registration and Fusion ")
7578
print("=======================================================")
7679

7780
ScriptedLoadableModuleWidget.setup(self)
@@ -310,11 +313,11 @@ def run(self, fixedVolumeNode, fixedFiducialNode, movingVolumeNode, movingFiduci
310313

311314
print("=================== Cropping =====================")
312315
self.vsc.vtVars['fixedCropPath'] = self.vsc.runCropping(fixedVolumeNode, fixedPointT,self.vsc.vtVars['croppingLength'], self.vsc.vtVars['RSxyz'], self.vsc.vtVars['hrChk'],0)
313-
[success, croppedFixedNode] = slicer.util.loadVolume(self.vsc.vtVars['fixedCropPath'], returnNode=True)
316+
[success, croppedFixedNode] = slicer.util.loadVolume(self.vsc.vtVars['fixedCropPath'])
314317
croppedFixedNode.SetName(fixedVolumeNode.GetName()+"_F_Crop")
315318

316319
self.vsc.vtVars['movingCropPath'] = self.vsc.runCropping(movingVolumeNode, movingPointT,self.vsc.vtVars['croppingLength'], self.vsc.vtVars['RSxyz'], self.vsc.vtVars['hrChk'],0)
317-
[success, croppedMovingNode] = slicer.util.loadVolume(self.vsc.vtVars['movingCropPath'], returnNode=True)
320+
[success, croppedMovingNode] = slicer.util.loadVolume(self.vsc.vtVars['movingCropPath'])
318321
croppedMovingNode.SetName(movingVolumeNode.GetName()+"_M_Crop")
319322
print ("************ Register cropped moving image to cropped fixed image **********************")
320323
cTI = self.vsc.runElastix(self.vsc.vtVars['elastixBinPath'],self.vsc.vtVars['fixedCropPath'], self.vsc.vtVars['movingCropPath'], self.vsc.vtVars['outputPath'], self.vsc.vtVars['parsPath'], self.vsc.vtVars['noOutput'], "336")
@@ -325,19 +328,19 @@ def run(self, fixedVolumeNode, fixedFiducialNode, movingVolumeNode, movingFiduci
325328
os.rename(resOldDefPath,resDefPath)
326329

327330
print ("************ Load deformation field Transform **********************")
328-
[success, vtTransformNode] = slicer.util.loadTransform(resDefPath, returnNode = True)
331+
[success, vtTransformNode] = slicer.util.loadTransform(resDefPath)
329332
vtTransformNode.SetName(transNodeName)
330333
print ("************ Transform The Original Moving image **********************")
331334
movingVolumeNode.SetAndObserveTransformNodeID(vtTransformNode.GetID())
332335
#export seg to lbl then export back with input image as reference
333336
slicer.vtkSlicerTransformLogic().hardenTransform(movingVolumeNode) # apply the transform
334337
fnm = os.path.join(self.vsc.vtVars['outputPath'] , movingVolumeNode.GetName()+"_Registered.nrrd")
335338
sR = slicer.util.saveNode(movingVolumeNode, fnm )
336-
[success, registeredMovingVolumeNode] = slicer.util.loadVolume(fnm, returnNode = True)
339+
[success, registeredMovingVolumeNode] = slicer.util.loadVolume(fnm)
337340
registeredMovingVolumeNode.SetName(movingVolumeNode.GetName()+"_Registered")
338341
#remove the tempnode and load the original
339342
slicer.mrmlScene.RemoveNode(movingVolumeNode)
340-
[success, movingVolumeNode] = slicer.util.loadVolume(movingPath, returnNode = True)
343+
[success, movingVolumeNode] = slicer.util.loadVolume(movingPath)
341344
movingVolumeNode.SetName(os.path.splitext(os.path.basename(movingVolumeNode.GetStorageNode().GetFileName()))[0])
342345
if (cTI==0) and (cTR==0):
343346
print("No error is reported during registeration ...")

CochleaSeg/CochleaSeg.py

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,34 @@
1818
# [4] https://mtixnat.uni-koblenz.de #
1919
# #
2020
#-------------------------------------------------------------------------------------#
21-
# Slicer 4.11.0 #
22-
# Updated: 24.6.2019 #
21+
# Slicer 5.0.3 #
22+
# Updated: 6.8.2022 #
2323
#======================================================================================
24+
# Non Slicer libs
2425
from __future__ import print_function
25-
import os,sys, time, unittest, logging
26+
import os, sys, time, re, shutil, math, unittest, logging, zipfile, platform, subprocess, hashlib
2627
from shutil import copyfile
28+
29+
from six.moves.urllib.request import urlretrieve
2730
import numpy as np
28-
from __main__ import qt, ctk, slicer
31+
import SimpleITK as sitk
32+
33+
# Slicer related
34+
from __main__ import vtk, qt, ctk, slicer
2935
from slicer.ScriptedLoadableModule import *
36+
import sitkUtils
3037
import SampleData
31-
38+
import SegmentStatistics
39+
import Elastix
3240
import VisSimCommon
3341

34-
#TODO:
35-
36-
# 1. solve right model problem:
37-
# - it seems hardening transform does not work after loading the segmentation. Slicer crashes.
38-
# with error : Failed to get reference image geometry
39-
# - the problem above solved by exporting to label with model as reference then export to .seg.
40-
# but still we have bad results. More testing is needed.
41-
# 2. cleaning, removing temp nodes and files
42-
# 3. add alternative links for models and sample images.
43-
# 4. local testing with different machines.
44-
45-
# Later:
46-
# - Checking if all above are needed
47-
# - Cleaning, optimizing, commenting.
48-
# - Testing in both Windows and Linux.
49-
# - Supporting DICOM.
50-
# - Supporting illegal filename.
51-
# - Add alternative to use elastix binaries directly by downloading the binary release.
52-
# - Visualizing the interimediate steps.
53-
#
54-
#
55-
#
42+
# TODOS:
43+
# Update the models
44+
5645
# Terminology
5746
# img : ITK image
5847
# imgNode : Slicer Node
59-
# imgName : Filename without the path and without extension
48+
# imgName : Filename without the path and without extension
6049
# imgPath : wholePath + Filename and extension
6150

6251
#===================================================================
@@ -69,13 +58,7 @@ def __init__(self, parent):
6958
parent.title = "Cochlea Segmentation"
7059
parent.categories = ["VisSimTools"]
7160
parent.dependencies = []
72-
parent.contributors = ["Christopher Guy",
73-
"Ibraheem Al-Dhamari",
74-
"Michel Peltriauxe",
75-
"Anna Gessler",
76-
"Jasper Grimmig",
77-
"Pepe Eulzer"
78-
]
61+
parent.contributors = ["Ibraheem Al-Dhamari"]
7962
self.parent.helpText += self.getDefaultModuleDocumentationLink()
8063
parent.acknowledgementText = " This work is sponsored by Cochlear as part of COMBS project "
8164
self.parent = parent
@@ -91,7 +74,7 @@ class CochleaSegWidget(ScriptedLoadableModuleWidget):
9174
def setup(self):
9275
print(" ")
9376
print("=======================================================")
94-
print(" Automatic Cochlea Image Segmentation ")
77+
print(" Automatic Cochlea Image Segmentation and Analysis ")
9578
print("=======================================================")
9679

9780
ScriptedLoadableModuleWidget.setup(self)
@@ -323,10 +306,10 @@ def run(self, inputVolumeNode, inputFiducialNode, cochleaSide):
323306
# rename fthe file:
324307
os.rename(resOldDefPath,resDefPath)
325308
print ("************ Load deformation field Transform **********************")
326-
[success, vtTransformNode] = slicer.util.loadTransform(resDefPath, returnNode = True)
309+
vtTransformNode = slicer.util.loadTransform(resDefPath)
327310
vtTransformNode.SetName(transNodeName)
328311
print ("************ Transform The Segmentation **********************")
329-
[success, vtSegNode] = slicer.util.loadSegmentation(modelSegPath, returnNode = True)
312+
vtSegNode = slicer.util.loadSegmentation(modelSegPath)
330313
vtSegNode.SetName(segNodeName)
331314
vtSegNode.SetAndObserveTransformNodeID(vtTransformNode.GetID())
332315
#export seg to lbl then export back with input image as reference
@@ -336,10 +319,9 @@ def run(self, inputVolumeNode, inputFiducialNode, cochleaSide):
336319
sR = slicer.util.saveNode(vtSegNode, fnm )
337320

338321
print ("************ Transform The Scala Points **********************")
339-
#TODO:check the right side model
340-
#TODO:add scala vestibuli model
341322
# transform the Scala Tympani Points for length Computation
342-
[success, vtImgStNode] = slicer.util.loadMarkupsFiducialList (modelImgStPath, returnNode = True)
323+
vtImgStNode = slicer.util.loadMarkupsFiducialList (modelImgStPath, returnNode=True)
324+
343325
vtImgStNode.GetDisplayNode().SetSelectedColor(1,0,0)
344326
vtImgStNode.GetDisplayNode().SetTextScale(0.5)
345327
vtImgStNode.SetName(stpNodeName)

VisSimCommon/VisSimCommon.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# [1] https://www.slicer.org #
77
# #
88
#-------------------------------------------------------------------------------------#
9-
# Slicer 4.11 #
10-
# Updated: 18.1.2022 #
9+
# Slicer 5.0.3 #
10+
# Updated: 6.8.2022 #
1111
#-------------------------------------------------------------------------------------#
1212
#TODO: check Documentation/Nightly/Developers/Tutorials/MigrationGuide #
1313
#-------------------------------------------------------------------------------------#
@@ -22,16 +22,21 @@
2222
# slicer.util.reloadScriptedModule("VisSimCommon")
2323
# slicer.util.reloadScriptedModule(self.moduleName)
2424
#======================================================================================
25+
26+
# Non Slicer libs
2527
from __future__ import print_function
26-
import os, re, sys, math, unittest, logging, zipfile, platform, subprocess, hashlib
28+
import os, sys, time, re, shutil, math, unittest, logging, zipfile, platform, subprocess, hashlib
29+
from shutil import copyfile
30+
2731
from six.moves.urllib.request import urlretrieve
2832
import numpy as np
2933
import SimpleITK as sitk
30-
import sitkUtils
34+
35+
# Slicer related
3136
from __main__ import vtk, qt, ctk, slicer
3237
from slicer.ScriptedLoadableModule import *
38+
import sitkUtils
3339
import SampleData
34-
3540
import SegmentStatistics
3641
import Elastix
3742

@@ -43,6 +48,7 @@ def __init__(self, parent):
4348
ScriptedLoadableModule.__init__(self, parent)
4449
parent.title = "VisSim Common"
4550
parent.categories = ["VisSimTools"]
51+
parent.contributors = ["Ibraheem Al-Dhamari"]
4652
self.parent = parent
4753
#enddef
4854
#endclass
@@ -97,7 +103,6 @@ def setGlobalVariables(self,vsExtension):
97103
slicer.mrmlScene.AddDefaultNode(msn)
98104

99105
if vsExtension == 0: #0=cochlea
100-
self.vtVars['othersUniKoWebLink'] = ("https://cloud.uni-koblenz-landau.de/s/XYXPb4Fepms2JeC/download")
101106
self.vtVars['othersWebLink'] = ("https://github.com/MedicalImageAnalysisTutorials/VisSimData/raw/master/VisSimToolsCochlea.zip")
102107
self.OthersSHA256 = '763be6b5b11f0f6a3ed73d1a5ef5df34cdbbf46a3e1728195e79e8dcd26313d1'
103108
self.vtVars['parsPath'] = os.path.join(self.vtVars['vissimPath'] , "pars","parCochSeg.txt")
@@ -117,7 +122,6 @@ def setGlobalVariables(self,vsExtension):
117122
#Only for Cervical Spine
118123
elif vsExtension == 1: # Cervical Spine
119124
print("VisSimCommonLogic: initializing global variables:")
120-
self.vtVars['othersUniKoWebLink'] = "https://cloud.uni-koblenz-landau.de/s/yfwcdymS9QfqKc9/download"
121125
self.vtVars['othersWebLink'] = "https://github.com/MedicalImageAnalysisTutorials/VisSimData/raw/master/VisSimToolsCervicalSpine.zip"
122126
self.OthersSHA256 = 'fbcd25344b649cb3055674ff740be305f0c975781726c353ef11566be1b545c0'
123127
self.vtVars['parsPath'] = os.path.join(self.vtVars['vissimPath'] , "pars","parSpiSeg.txt" )
@@ -193,13 +197,14 @@ def checkVisSimTools(self,vtVars,vsExtension ):
193197
uFile = urlretrieve(othersWebLink,vissimZip)
194198
print (" Extracting to user home ")
195199
zip_ref = zipfile.ZipFile(vissimZip, 'r')
196-
zip_ref.extractall(os.path.expanduser("~/"))
200+
zip_ref.extract(os.path.expanduser("~/"))
197201
zip_ref.close()
198202
#remove the downloaded zip file
199203
os.remove(vissimZip)
200204
# moved the folder: fix bug related to windows
201-
import shutil
202-
shutil.move(os.path.join(os.path.expanduser("~/"),"VisSimToolsCochlea","VisSimTools") , os.path.join(os.path.expanduser("~/"),"VisSimTools") )
205+
# TODO: bug related to slicer, it works in python interactor but not in the extension
206+
shutil.move(os.path.join(os.path.expanduser("~/"),"VisSimToolsCochlea","VisSimTools") , os.path.expanduser("~/") )
207+
os.rmdir(os.path.join(os.path.expanduser("~/"),"VisSimToolsCochlea"))
203208
print (" done! ")
204209
except Exception as e:
205210
print(" Error: can not download and extract VisSimTools ...")
@@ -444,12 +449,12 @@ def runCropping(self, inputVolume, pointT,croppingLengthT, samplingLengthT, hrCh
444449
#endfor
445450
croppingBounds = [lower,upper]
446451
# Call SimpleITK CropImageFilter
447-
print("Cropping with " + str(croppingBounds[0]) + " and " + str(croppingBounds[1]) + ".")
448-
#self.inputCropPath = "bla blaa blaaa"
449-
450452
inputImage = sitkUtils.PullVolumeFromSlicer(inputVolume.GetID())
451453
cropper = sitkUtils.sitk.CropImageFilter()
452-
croppedImage = cropper.Execute(inputImage, croppingBounds[0], croppingBounds[1])
454+
cropper.SetLowerBoundaryCropSize(croppingBounds[0])
455+
cropper.SetUpperBoundaryCropSize(croppingBounds[1])
456+
print("Cropping with " + str(croppingBounds[0]) + " and " + str(croppingBounds[1]) + ".")
457+
croppedImage = cropper.Execute(inputImage)
453458
# Make a node with cropped image
454459
sitkUtils.PushVolumeToSlicer(croppedImage, None, nodeName , 'vtkMRMLScalarVolumeNode' )
455460
crNodes = slicer.util.getNodesByClass("vtkMRMLScalarVolumeNode")

0 commit comments

Comments
 (0)