Hello Slicer Community,
I have some scripts written for a client about 9 or 10 years ago for automated centerline extraction (and calculating metrics for those centerlines) that I developed with VMTK. The client recently decided they wanted to start using this functionality again (I don’t think they have used it for three or four years. The code is run inside a docker container in an old version of ubuntu which no longer builds (which is different than VMTK Extract Centerline with python console where doing the extraction inside Slicer is ok).
My first approach was to try and get conda or pip installs of vtk and VMTK to work together but it seems like the packages have not been kept up to date. I saw in a thread (which I don’t have handy at the moment) that Andras suggested that the Slicer team’s focus has been on the Slicer project and not supporting side projects like VMTK. Fair enough, so it seems like I am at a crossroads between centerline extraction automation like in this thread VMTK Extract Centerline with python console - #7 by mikebind and having a contained Slicer environment like this thread Running Slicer without GUI? - #16 by jakehockman. I have taken the slicer notebook docker container as a template SlicerDocker/slicer-notebook at main · Slicer/SlicerDocker · GitHub updated it a little bit to a newer bullseye image and removed the Juypter notebook pieces. It seems like I have an image with slicer installed with my python scripts (updated to python 3 since the original was in 2.7). I’ve seen instructions to run scripts in Slicer something like this:
xvfb-run -a /opt/slicer/Slicer/Slicer --no-main-window --python-script /opt/app/Python/app.py
Can I run python scripts directly with the Slicer python interpreter though and get around xvfb and Slicer? Something like this:
/opt/slicer/Slicer/ /opt/app/Python/app.py
I’m hoping that I’m very close, since I have scripts that worked in vmtk several years ago.
From my understanding of the documentation and this comment from the man himself, yes unless you need the slicer modules. SlicerPython is like a prepackaged virtual environment, it includes vmtk and can be run from cmd but you’ll need to have the user interface open for access to slicer.app, slicer.mrmlScene, slicer.modules, slicer.moduleNames. You can also use other flags to help your automation routine like --no-main-window and --exit-after-startup.
Thank you Kyle,
I missed that page in the documentation though, it’s mostly geared towards using scripts inside of Slicer but between that and Andras’ comment it seems like I figured out how to get my dependencies added but still I get this error:
from vmtk import vtkvmtk
ModuleNotFoundError: No module named 'vmtk'
It was my understanding that slicer came packaged with vmtk, do I need to load vmtk seperately somehow?
Thank you Kyle,
Looks like I’m missing the SlicerVMTK extension. Kyle, did you install it via the install.sh script in the docker examples or somehow directly?
It looks like I was able to get the SlicerVTMK extension to install (after adding many apt-get calls to the docker container. So I have something like this in install.h
...
echo "Install SlicerVMTK extension"
$slicer_executable -c '
em = slicer.app.extensionsManagerModel()
extensionName = "SlicerVMTK"
if int(slicer.app.revision) >= 30893:
# Slicer-5.0.3 or later
em.updateExtensionsMetadataFromServer(True, True)
if not em.downloadAndInstallExtensionByName(extensionName, True):
raise ValueError(f"Failed to install {extensionName} extension")
# Wait for installation to complete
# (in Slicer-5.4 downloadAndInstallExtensionByName has a waitForComplete flag
# so that could be enabled instead of running this wait loop)
import time
while not em.isExtensionInstalled(extensionName):
slicer.app.processEvents()
time.sleep(0.1)
...
However when I try to run my script I still get an error that vmtk isn’t available:
File "/opt/app/Python/CenterLines.py", line 7, in <module>
from vmtkCustomSeedSelector2 import vmtkBranchSeedSelector
File "/opt/app/Python/vmtkCustomSeedSelector2.py", line 9, in <module>
from vmtk import vtkvmtk
ModuleNotFoundError: No module named 'vmtk'
So I did some searching and it seems like SlicerVMTK has some different ways of referencing vmtk. So in the SlicerVMTK extension the there is an ExtractCenterline.py fiie
...
def extractCenterline(self, surfacePolyData, endPointsMarkupsNode, curveSamplingDistance=1.0):
"""Compute centerline.
This is more robust and accurate but takes longer than the network extraction.
:param surfacePolyData:
:param endPointsMarkupsNode:
:return:
"""
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
...
But when I try to import it in my scripts I get an error:
import vtkvmtkComputationalGeometryPython as vtkvmtkComputationalGeometry
ModuleNotFoundError: No module named 'vtkvmtkComputationalGeometryPython'
Does anyone have any advice on what I’m doing wrong here?