# VMTK SlicerExtension Multiprocessing

**URL:** <https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099>\
**Category:** Support\
**Created:** [January 15, 2025, 9:57pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099 "2025-01-15T21:57:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tracy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tracy/32/78179_2.png) [@Tracy](https://discourse.slicer.org/u/Tracy)\
**Post date:** [January 15, 2025, 9:57pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/1 "2025-01-15T21:57:40Z")

</div>

Hi,

I have a python pipeline that utilizes several functions, such as ExtractCenterline and ClipVessel, from the SlicerExtension-VMTK ([GitHub - vmtk/SlicerExtension-VMTK](https://github.com/vmtk/SlicerExtension-VMTK)) for analyzing vessel segmentation. Since I need to process each segment in the segmentation, I am interested in using multiprocessing to speed up the analysis.

I’ve tried using SlicerParallelProcessing ([GitHub - pieper/SlicerParallelProcessing: Slicer modules for running subprocesses to operate on data in parallel](https://github.com/pieper/SlicerParallelProcessing/tree/master)), but it doesn’t seem to work, possibly due to the presence of vtkMRMLNode in SlicerExtension-VMTK.

Is there any suggestion or other method to implement multiprocessing in this context?  
BTW, I am working with SlicerJupyter and the Slicer 5.6 environment.

Thank you so much!

Best,  
Tracy

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 15, 2025, 10:04pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/2 "2025-01-15T22:04:26Z")

</div>

By default the SlicerParallelProcessing just [launches PythonSlicer](https://github.com/pieper/SlicerParallelProcessing/blob/master/Processes/Processes.py#L227) which is the same python shell as Slicer but without the modules or extensions.

You should be able to create a subshell that has all the paths defined so that VMTK logic libraries are included and can be used. Perhaps [`lauchConsoleProcess`](https://github.com/Slicer/Slicer/blob/main/Base/Python/slicer/util.py#L3787), but I haven’t tried.

---

<div class="post-metadata">

**Author:** ![Tracy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tracy/32/78179_2.png) [@Tracy](https://discourse.slicer.org/u/Tracy)\
**Post date:** [January 21, 2025, 8:37pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/3 "2025-01-21T20:37:42Z")

</div>

Hi Pieper,

Thanks for your advice.

I’m encountering an issue (which I believe is related to the environment) when trying to use launchConsoleProcess.

Here is the command

command = [  
‘PythonSlicer’, ‘test\_script.py’, # The Python interpreter and the script name  
‘–segmentationName’, ‘segment\_merge.nii.gz’,  
‘–segmentName’, ‘Segment\_1’,  
‘–centerlineName’, ‘test’,  
‘–point’, ‘145.66666666666666, 315.3333333333333, 301.6666666666667’ # Second 3D coordinate  
]

process = slicer.util.launchConsoleProcess(command)  
slicer.util.logProcessOutput(process)

When I use ‘python3’ as the executable, I get the error:  
**ModuleNotFoundError: No module named numpy**  
Additionally, all the SlicerExtension-VMTK modules seem to be missing.

On the other hand, if I use ‘PythonSlicer’, I encounter this error:  
**FileNotFoundError: [Errno 2] No such file or directory: ‘PythonSlicer’**

Do you have any suggestions for resolving these issues?  
If you need any further information, please let me know.  
Thanks a lot.

Best,  
Tracy

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 21, 2025, 9:01pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/4 "2025-01-21T21:01:43Z")

</div>

Try `launchConsoleProcess(["PythonSlicer"], useStartupEnvironment=False)`

---

<div class="post-metadata">

**Author:** ![Tracy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tracy/32/78179_2.png) [@Tracy](https://discourse.slicer.org/u/Tracy)\
**Post date:** [January 21, 2025, 9:25pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/5 "2025-01-21T21:25:58Z")

</div>

Thanks for quick reply.

However, I face some other error (vtkMRMLNode)  
Please refer to the attached image.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/9/494f34f13d8d12361d0fc7bbccfc480d809769d6.png)

Here is my function if useful  
def centerline\_extraction(segmentationName, segmentName, centerlineName, points=None):

```
segmentationNode = slicer.util.getNode(segmentationName)
segmentID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentName)

# Init the VMTK ExtracCenterline class
extractLogic = ExtractCenterline.ExtractCenterlineLogic()

# Preprocess the surface
inputSurfacePolyData = extractLogic.polyDataFromNode(segmentationNode, segmentID)
targetNumberOfPoints = 5000.0
decimationAggressiveness = 3.5
subdivideInputSurface = False
preprocessedPolyData = extractLogic.preprocess(inputSurfacePolyData, targetNumberOfPoints, decimationAggressiveness, subdivideInputSurface)

endPointsMarkupsNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode", "Centerline endpoints")
if not np.any(points):
    # Auto-detect the endpoints      
    networkPolyData = extractLogic.extractNetwork(preprocessedPolyData, endPointsMarkupsNode)
    startPointPosition=None
    endpointPositions = extractLogic.getEndPoints(networkPolyData, startPointPosition)
    endPointsMarkupsNode.RemoveAllMarkups()
    for position in endpointPositions:
      endPointsMarkupsNode.AddControlPoint(vtk.vtkVector3d(position))

else:
    assert points.shape[1] == 3
    # Self define start and end points
    endPointsMarkupsNode.RemoveAllMarkups()
    for p in points:
        endPointsMarkupsNode.AddControlPoint(vtk.vtkVector3d(p))
    
        #endPointsMarkupsNode.AddControlPoint(vtk.vtkVector3d(points[-1]))

# Extract the centerline
curveName = centerlineName+"Curve"
modelName = centerlineName+"Model"

centerlineCurveNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode", curveName)

centerlinePolyData, voronoiDiagramPolyData = extractLogic.extractCenterline(preprocessedPolyData, endPointsMarkupsNode)
centerlinePropertiesTableNode = None
extractLogic.createCurveTreeFromCenterline(centerlinePolyData, centerlineCurveNode, centerlinePropertiesTableNode)

centerlineModelNode = slicer.modules.models.logic().AddModel(centerlinePolyData)
centerlineModelNode.SetName(modelName)

return centerlineCurveNode, centerlineModelNode

```

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [January 21, 2025, 9:34pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/6 "2025-01-21T21:34:57Z")

</div>

PythonSlicer is more just a python shell and not a full instance of Slicer. Maybe for what you want you can use `Slicer --no-main-window`. Since it runs the application you’ll need to explicitly `exit()` when you are done processing.

---

<div class="post-metadata">

**Author:** ![Enkh-Orchlon\_Batbaya](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/enkh-orchlon_batbaya/32/79162_2.png) [@Enkh-Orchlon\_Batbaya](https://discourse.slicer.org/u/Enkh-Orchlon_Batbaya)\
**Post date:** [January 22, 2025, 12:54am UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/7 "2025-01-22T00:54:13Z")

</div>

Dear Steve Pieper,  
My writing is not belong to this conversation. Could you kindly review and approve my post request? I am new to the community and currently seeking assistance. Thank you for your time and understanding.

---

<div class="post-metadata">

**Author:** ![Tracy](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/tracy/32/78179_2.png) [@Tracy](https://discourse.slicer.org/u/Tracy)\
**Post date:** [January 28, 2025, 8:40pm UTC](https://discourse.slicer.org/t/vmtk-slicerextension-multiprocessing/41099/8 "2025-01-28T20:40:27Z")

</div>

Thanks for suggestion Pieper. It works with subprocess.Popen()  
Just sharing this for anyone else who might encounter the same issue.

command = [‘Slicer-5.6.2-linux-amd64/Slicer’, ‘–no-main-window’,  
‘–python-script’, ‘test.py’,  
‘–segmentation\_path’, segmentation\_path,  
‘–output\_path’, output\_path  
]

process\_dic = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
