Skip to content

Commit dcd1d59

Browse files
author
idhamari
committed
fix a windows bug when running elastix
1 parent 5c58e2b commit dcd1d59

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ This is a [3D Slicer](https://gaithub.com/Slicer/Slicer) plugin that uses [elast
1717

1818
# Tested on
1919

20-
* Slicer 5.0.3 [for windows](https://slicer-packages.kitware.com/api/v1/file/62d5d2ebe911182f1dc285b2/download) and [for linux](https://slicer-packages.kitware.com/api/v1/file/62cc52d2aa08d161a31c1af2/download). Tested on Windows 10 and Ubuntu 20.04
20+
* Slicer 5.6, Windows 10 and Ubuntu 20.04
2121

2222

2323
This project contains three modules:
2424

2525
1. Cochlea image registration and fusion.
2626
2. Cochlea image segmentation and analysis.
27+
3. VisSimCommon for common functions.
2728

2829
# How to use:
2930

VisSimCommon/VisSimCommon.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# [1] https://www.slicer.org #
77
# #
88
#-------------------------------------------------------------------------------------#
9-
# Slicer 5.4.0 #
10-
# Updated: 18.11.2023 #
9+
# Slicer 5.6 #
10+
# Updated: 17.3.2024 #
1111
#-------------------------------------------------------------------------------------#
1212
#TODO: check Documentation/Nightly/Developers/Tutorials/MigrationGuide #
1313
#-------------------------------------------------------------------------------------#
@@ -25,7 +25,7 @@
2525

2626
# Non Slicer libs
2727
from __future__ import print_function, unicode_literals
28-
import os, sys, time, re, shutil, math, unittest, logging, zipfile, platform, subprocess, hashlib
28+
import os, sys, glob, time, re, shutil, math, unittest, logging, zipfile, platform, subprocess, hashlib
2929
from shutil import copyfile
3030

3131
from six.moves.urllib.request import urlretrieve
@@ -75,6 +75,14 @@ def tstSum(self, x,y):
7575
print("testing")
7676
return x+y
7777

78+
# Get slicer lib path automatically
79+
def getSlicerLibPath(SlicerPath):
80+
# Search for directories that match the "Slicer-*" pattern within the lib directory
81+
slicer_dirs = glob.glob(os.path.join(os.path.join(SlicerPath, "lib"), "Slicer-*"))
82+
83+
# Assuming there's only one Slicer directory per installation, take the first match
84+
return slicer_dirs[0]
85+
7886
# vsExtension = 0: Cochlea, vsExtension = 1: Spine
7987
def setGlobalVariables(self,vsExtension):
8088
# define global variables as a dictonary
@@ -196,18 +204,16 @@ def checkVisSimTools(self,vtVars,vsExtension ):
196204
print(" Downloading VisSim Tools ... ")
197205
try:
198206
print(" Downloading VisSimTools others ...")
199-
vissimZip = os.path.expanduser("~/VisSimToolsTmp.zip")
200-
uFile = urlretrieve(othersWebLink,vissimZip)
207+
vissimZip = os.path.join(os.path.expanduser("~"),"VisSimToolsTmp.zip")
208+
print("vissimZip: ",vissimZip)
209+
uFile = urlretrieve(othersWebLink, vissimZip)
201210
print (" Extracting to user home ")
202211
zip_ref = zipfile.ZipFile(vissimZip, 'r')
203-
zip_ref.extractall(os.path.expanduser("~/"))
212+
zip_ref.extractall(os.path.expanduser("~"))
204213
zip_ref.close()
205214
#remove the downloaded zip file
206215
os.remove(vissimZip)
207-
# moved the folder: fix bug related to windows
208-
# TODO: bug related to slicer, it works in python interactor but not in the extension
209-
shutil.move(os.path.join(os.path.expanduser("~/"),"VisSimToolsCochlea") , os.path.join(os.path.expanduser("~/"),"VisSimTools") )
210-
print (" done! ")
216+
print (" Extracting to user home ... done! ")
211217
except Exception as e:
212218
print(" Error: can not download and extract VisSimTools ...")
213219
print(e)
@@ -463,18 +469,16 @@ def runCropping(self, inputVolume, pointT,croppingLengthT, samplingLengthT, hrCh
463469
SlicerBinPath=""
464470
ResampleBinPath=""
465471
## this produces error in windows
466-
#resamplingCommand = slicer.modules.resamplescalarvolume.path
467-
#os.system(resamplingCommand)
468-
#TODO: Get Slicer PATH
469472
SlicerPath = os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.sys.executable, os.pardir)), os.pardir))
470473
SlicerBinPath = os.path.join(SlicerPath,"Slicer")
471-
ResampleBinPath = os.path.join(SlicerPath,"lib","Slicer-5.4" , "cli-modules","ResampleScalarVolume" )
474+
SlicerLibPath = VisSimCommonLogic.getSlicerLibPath(SlicerPath)
475+
#ResampleBinPath = os.path.join(SlicerPath,"lib","Slicer-5.4" , "cli-modules","ResampleScalarVolume" )
476+
ResampleBinPath = os.path.join(SlicerLibPath, "cli-modules","ResampleScalarVolume" )
477+
#ResampleBinPath = os.path.join(SlicerPath,"lib","Slicer-5.6" , "cli-modules","ResampleScalarVolume" )
478+
resamplingCommand = ResampleBinPath
472479
if sys.platform == 'win32':
473480
ResampleBinPath + ".exe"
474-
resamplingCommand = SlicerBinPath + " --launch " + ResampleBinPath
475-
else:
476-
#note: in windows, no need to use --launch
477-
resamplingCommand = ResampleBinPath
481+
resamplingCommand = f'"{SlicerBinPath}" --launch "{ResampleBinPath}"'
478482

479483
print(resamplingCommand)
480484
si = None
@@ -511,15 +515,24 @@ def runCropping(self, inputVolume, pointT,croppingLengthT, samplingLengthT, hrCh
511515
def runElastix(self, elastixBinPath, fixed, moving, output, parameters, verbose, line):
512516
print ("************ Compute the Transform **********************")
513517
currentOS = sys.platform
514-
print(currentOS)
515-
Cmd = elastixBinPath + ' -f ' + fixed + ' -m ' + moving + ' -out ' + output + ' -p ' + parameters
518+
print("currentOS: ",currentOS)
519+
Cmd = elastixBinPath + " -f " +fixed+" -m "+ moving +" -out "+ output +" -p "+ parameters
520+
516521
errStr="No error!"
517522
if currentOS in ["win32","msys","cygwin"]:
518-
print(" elastix is running in Windows :( !!!")
519-
print(Cmd)
520-
si = subprocess.STARTUPINFO()
521-
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
522-
cTI = subprocess.call(Cmd , shell = (sys.platform == currentOS) , startupinfo=si )
523+
print(" elastix is running in Windows :( !!!")
524+
Cmd = f'"{elastixBinPath}" -f "{fixed}" -m "{moving}" -out "{output}" -p "{parameters}"'
525+
526+
print(Cmd)
527+
Cmd = f'"{elastixBinPath}" -f "{fixed}" -m "{moving}" -out "{output}" -p "{parameters}"'
528+
si = subprocess.STARTUPINFO()
529+
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
530+
process = subprocess.Popen(Cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, startupinfo=si)
531+
stdout, stderr = process.communicate()
532+
cTI = process.returncode
533+
# print(f"STDOUT: {stdout.decode()}")
534+
# print(f"STDERR: {stderr.decode()}")
535+
523536
elif currentOS in ["linux","linux2"]:
524537
print(" elastix is running in Linux :) !!!")
525538
CmdList = Cmd.split()
@@ -553,12 +566,13 @@ def runElastix(self, elastixBinPath, fixed, moving, output, parameters, verbose,
553566
def runTransformix(self,transformixBinPath, img, output, parameters, verbose, line):
554567
print ("************ Apply transform **********************")
555568
currentOS = sys.platform
556-
Cmd = transformixBinPath + ' -tp ' + parameters + ' -in ' + img +' -out ' + output +' -def '+ ' all '
569+
Cmd = transformixBinPath + " -tp " + parameters + " -in " + img +" -out " + output + " -def all "
570+
557571
#if subprocess.mswindows:
558572
errStr="No error!"
559-
# if hasattr(subprocess, 'mswindows'):
560573
if currentOS in ["win32","msys","cygwin"]:
561574
print(" transformix is running in Windows :( !!!")
575+
Cmd = f'"{transformixBinPath}" -tp "{parameters}" -in "{img}" -out "{output}" -def all '
562576
print(Cmd)
563577
si = subprocess.STARTUPINFO()
564578
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW

0 commit comments

Comments
 (0)