Total segmentator in 3d Slicer with python script

Hi there, I am trying to write a python script that uses Totalsegmentator in 3dSlicer, but so far I am not able to make good progress. The aim of using it in 3dslicer instead of standalone is to be able to exctract SUV values from pet data later using the same script. So far the import seems to work, but I wasn`t able to figure out how to call totalsegmentator. Also, since the DICOM includes ct and pet files, the script should figure out which of the files (=CT) to use as input (?)
So far my script is as follows, can someone help me out how to continue from here?

import os
import sys
import TotalSegmentator
from DICOMLib import DICOMUtils

dicomDataDir = “C:/Users/user1/sample/dicom” # input folder with DICOM files

Check if the folder exists

if not os.path.exists(dicomDataDir):
sys.exit(“Aborting the script. Folder is required.”)

loadedNodeIDs =

with DICOMUtils.TemporaryDICOMDatabase() as db:
DICOMUtils.importDicom(dicomDataDir, db)
patientUIDs = db.patients()
for patientUID in patientUIDs:
loadedNodeIDs.extend(DICOMUtils.loadPatientByUID(patientUID))

slicer.util.selectModule(“TotalSegmentator”)

@Sergio maybe? From the search results I noticed you did something similar (?)

(Btw, the formatting in my post didnt work, especially with the hashtag before “check if folder exists”, but I hope its still understandable…

try the following

TotalSegmentator = slicer.util.getModuleLogic('TotalSegmentator') 
TotalSegmentator.process(inputVolumeNode, outputSegmentation, fast=False, cpu=False, task="tissue_types", subset=None, interactive=False)

You just need to update the nodes and parameters

Thank you very much for your reply!
The process is running well with that code, however it seems to ignore the “fast=False” parameter and runs with ‘–fast’ included? Do you know why that might be?
Also, the segmentation does not appear in the scene, it only appears to be in the temp folder? Is it possible to save it properly in that function?

Hello, I have a few questions,
What makes you say that the code is running in fast mode ? The computation can be “quick” (a few minutes) even without fast mode

What version of slicer/Total segmentator are you using ? The syntaxe changed between version (I think between version 5.4 and 5.6 but I’m not sure)

Hi, thanks for your time!
3D Slicer V 5.6.2 r32448 with totalsegmentator V 221f84d (2024-09-20)

TotalSegmentator log seems to imply it`s using fast mode:
Total Segmentator arguments: [‘-i’, ‘C:/Users/XXXXXXX/AppData/Local/Temp/Slicer/__SlicerTemp__2024-12-16_16+39+15.344/total-segmentator-input.nii’, ‘-o’, ‘C:/Users/XXXXXXX/AppData/Local/Temp/Slicer/__SlicerTemp__2024-12-16_16+39+15.344/segmentation’, ‘–fast’]

It might have something to do with my GPU that has only 5 GB of RAM. When running TS from the GUI, however, it asks if I want to enable fast mode and then runs without the --fast flag after I click “no” (and it`s slower)

It’s hard to help you without having access to your machine, but you confirm that:
-You manage to make totalsegmentator run on your PC in “slow” mode from the GUI
-You can make TotalSegmentator run in fast mode from your code
-When putting “fast” to false, totalsegmentator still run in fast mode

My advice would be to install a debugger and to check what turn fast mode on, but I’m very surprised that all of the above propositions are true