|
6 | 6 | # [1] https://www.slicer.org # |
7 | 7 | # # |
8 | 8 | #-------------------------------------------------------------------------------------# |
9 | | -# Slicer 5.4.0 # |
10 | | -# Updated: 18.11.2023 # |
| 9 | +# Slicer 5.6 # |
| 10 | +# Updated: 17.3.2024 # |
11 | 11 | #-------------------------------------------------------------------------------------# |
12 | 12 | #TODO: check Documentation/Nightly/Developers/Tutorials/MigrationGuide # |
13 | 13 | #-------------------------------------------------------------------------------------# |
|
25 | 25 |
|
26 | 26 | # Non Slicer libs |
27 | 27 | 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 |
29 | 29 | from shutil import copyfile |
30 | 30 |
|
31 | 31 | from six.moves.urllib.request import urlretrieve |
@@ -75,6 +75,14 @@ def tstSum(self, x,y): |
75 | 75 | print("testing") |
76 | 76 | return x+y |
77 | 77 |
|
| 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 | + |
78 | 86 | # vsExtension = 0: Cochlea, vsExtension = 1: Spine |
79 | 87 | def setGlobalVariables(self,vsExtension): |
80 | 88 | # define global variables as a dictonary |
@@ -196,18 +204,16 @@ def checkVisSimTools(self,vtVars,vsExtension ): |
196 | 204 | print(" Downloading VisSim Tools ... ") |
197 | 205 | try: |
198 | 206 | 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) |
201 | 210 | print (" Extracting to user home ") |
202 | 211 | zip_ref = zipfile.ZipFile(vissimZip, 'r') |
203 | | - zip_ref.extractall(os.path.expanduser("~/")) |
| 212 | + zip_ref.extractall(os.path.expanduser("~")) |
204 | 213 | zip_ref.close() |
205 | 214 | #remove the downloaded zip file |
206 | 215 | 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! ") |
211 | 217 | except Exception as e: |
212 | 218 | print(" Error: can not download and extract VisSimTools ...") |
213 | 219 | print(e) |
@@ -463,18 +469,16 @@ def runCropping(self, inputVolume, pointT,croppingLengthT, samplingLengthT, hrCh |
463 | 469 | SlicerBinPath="" |
464 | 470 | ResampleBinPath="" |
465 | 471 | ## this produces error in windows |
466 | | - #resamplingCommand = slicer.modules.resamplescalarvolume.path |
467 | | - #os.system(resamplingCommand) |
468 | | - #TODO: Get Slicer PATH |
469 | 472 | SlicerPath = os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.sys.executable, os.pardir)), os.pardir)) |
470 | 473 | 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 |
472 | 479 | if sys.platform == 'win32': |
473 | 480 | 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}"' |
478 | 482 |
|
479 | 483 | print(resamplingCommand) |
480 | 484 | si = None |
@@ -511,15 +515,24 @@ def runCropping(self, inputVolume, pointT,croppingLengthT, samplingLengthT, hrCh |
511 | 515 | def runElastix(self, elastixBinPath, fixed, moving, output, parameters, verbose, line): |
512 | 516 | print ("************ Compute the Transform **********************") |
513 | 517 | 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 | + |
516 | 521 | errStr="No error!" |
517 | 522 | 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 | + |
523 | 536 | elif currentOS in ["linux","linux2"]: |
524 | 537 | print(" elastix is running in Linux :) !!!") |
525 | 538 | CmdList = Cmd.split() |
@@ -553,12 +566,13 @@ def runElastix(self, elastixBinPath, fixed, moving, output, parameters, verbose, |
553 | 566 | def runTransformix(self,transformixBinPath, img, output, parameters, verbose, line): |
554 | 567 | print ("************ Apply transform **********************") |
555 | 568 | 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 | + |
557 | 571 | #if subprocess.mswindows: |
558 | 572 | errStr="No error!" |
559 | | -# if hasattr(subprocess, 'mswindows'): |
560 | 573 | if currentOS in ["win32","msys","cygwin"]: |
561 | 574 | print(" transformix is running in Windows :( !!!") |
| 575 | + Cmd = f'"{transformixBinPath}" -tp "{parameters}" -in "{img}" -out "{output}" -def all ' |
562 | 576 | print(Cmd) |
563 | 577 | si = subprocess.STARTUPINFO() |
564 | 578 | si.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
|
0 commit comments