ProcessAllCTInDir - SlicerLungCTAnalyzer

Hi @rbumm first of all thank you for your work!
I want to segment and analyze many lung CT images (i’m interested in emphysema features), so doing it with a batch script from command line would be the best to achieve complete automatization. I’ve seen in the SlicerLungCTAnalyzer github repo that you provide this script for this purpose, but it seems that it just automatize the analysis part, while the segmented lungs images are requested as input, am I correct? If so, is there a suggested way to segment all CT in a directory given as input? Thank you in advance,
Giulia

Hi @giurdeluca we recently implemented batch processing in both Lung CT Segmenter and Lung CT Analyzer.
There is a description in the Wiki here and here. Supports nrrd and NIFTI input.
If questions remain please ask again.
It works great, just did 100 CTs in a row without major issues …

Thank you for your prompt answer! Yes I saw that but it seems that they are executed from the Slicer GUI, while i want to interact just with the cmd (Windows). Is it possible? Maybe this is a dumb question but I’m quite new in scripting on cmd

ProcessallCTinDir is a Python script - you will need programming help to adapt it to lung CT segmented. It was written before AI tools were available in LCTA so this step had to be done manually at that time.
Now the situation is much easier, there is excellent AI for lung masks and you could adjust all parameters from the GUI. You could also batch-process the segmentations first with AI then run an adapted ProcessallCTinDir.

I think i got it now, I’ll try what you suggested.
Thank you

Dear Bumm, sorry to bother you again.
I’ve managed to execute the ProcessAllCTInDir.py script from my wsl terminal with the following command:
<path/to/Slicer.exe> --python-code "exec(open(r'path/to/processAllCTInDir.py').read())".
Note that at the end of the script I’ve added sys.exit() to make Slicer close once it has completed the execution (as you suggested here Script repository — 3D Slicer documentation)
Since i would run this script from a server for batch processing, i wish to disable any Slicer window, but when i run
<path/to/Slicer.exe> --python-code "exec(open(r'path/to/processAllCTInDir.py').read())" -no-splash -no-splash --no-main-window
the execution never ends (i do not get a new line in the terminal nor see the result.csv file in the output folder).
Where am I wrong?

The easiest is probably to install xvfb on your server and then use xvfb-run as a prefix to your Slicer command. Another alternative would be to use docker.

1 Like

So you installed Slicer in WSL and ran this command from a WSL Linux shell?

Actually I’ve followed the standard procedure to install Slicer (5.2.2 version) let’s say ‘system-wide’, but yes I ran the previous command from a WSL Linux shell

And you intend to run this on a Linux-only server? Just need to understand the scope of the problem …

Yes it’s a linux only server

To be honest I never ran Slicer (or the script) under these conditions, without a desktop environment. But I am sure others did. You should probably start these trials with very simple hello world Python scripts, then include scanning the directory, then call a simple extension logic, and see how it goes.

I understand, thank you again for your support!

You seem to be on the right track, here is the GPT-4 response to your problem:

Human:

Hi GPT, could I run 3D Slicer on a Linux server without GUI in combination with a Python script for batch processing CT data?

AI:

Sure! You can run 3D Slicer on a Linux server without a GUI and use it in combination with Python scripts for batch processing CT data. To do this, you will need to use 3D Slicer’s command-line interface (CLI) and Python scripting capabilities.

Here are the general steps to set up and run 3D Slicer on a Linux server without a GUI:

  1. Download and install 3D Slicer: Download the latest version of 3D Slicer for Linux from the official website (https://download.slicer.org/). You can use wget or curl to download the archive directly on the server, then extract it to a directory of your choice:
wget https://download.slicer.org/bitstream/ID -O slicer.tar.gz
tar -xzvf slicer.tar.gz

Replace “ID” with the actual download ID found on the website.

  1. Enable remote access and command-line operation: Make sure you can remotely access your Linux server through SSH or another remote command-line interface.
  2. Install Python and required packages: Install Python on the Linux server if it is not already installed. Also, install any required Python packages for your script using pip.
  3. Run 3D Slicer from the command line: You can run 3D Slicer from the command line using the Slicer executable found in the extracted folder. You can use the --no-main-window argument to prevent the GUI from launching, like this:
<path-to-slicer>/Slicer --no-main-window
  1. Execute Python script: You can execute your Python script with 3D Slicer by providing the --python-script argument followed by the path to your Python script:
<path-to-slicer>/Slicer --no-main-window --python-script <path-to-your-python-script>

Make sure your Python script uses Slicer’s Python API (slicer.util, slicer.modules, etc.) for batch processing and any other necessary operations on the CT data. This way, your script can take advantage of 3D Slicer’s capabilities without launching the GUI.

Please note that running 3D Slicer without a GUI may have some limitations, and certain features may not work as expected. However, for batch processing and other basic operations, this setup should work.

So it appears i should proceed as @pieper suggested before.
I really thank both of you for your fast and kind help!
I’ll try in this way and inform you on the outcome (if this might be helpful for anyone).
Giulia

1 Like

Installing 3D Slicer 5.2.2 in Windows 11 WSL Ubuntu is quite complicated, but can be accomplished by:

cd $HOME
# get the link below from 3D Slicer download page
wget https://download.slicer.org/bitstream/63f5bee68939577d9867b4c7 -O slicer.tar.gz

# decompress the archive
tar -xzvf ./slicer.tar.gz

# install libraries
sudo apt-get install libpulse-dev libnss3 libglu1-mesa
sudo apt-get install --reinstall libxcb-xinerama0

# Thank you GPT-4. It seems that 3D Slicer still requires a connection to an X server even in --no-main-window mode, 
# although it doesn't display the GUI. To work around this, you can use a virtual framebuffer (Xvfb) to 
# create a virtual X server where the application can run without the need for a physical display.
# For Debian-based systems (such as Ubuntu), you can install Xvfb using the following command:

sudo apt-get update
sudo apt-get install xvfb

# Start Xvfb:
# Run Xvfb on a specific display number (e.g., 99) and set the screen resolution and depth:

Xvfb :99 -screen 0 1024x768x24 &

# Set the DISPLAY environment variable to the display number you chose when starting Xvfb:

export DISPLAY=:99

# Now you can run 3D Slicer with the --no-main-window argument, and it should no longer throw the 
# "could not connect to display" error

This will install 3D Slicer into a new directory in your $HOME directory

image

Put a test.py script file with print("Hello") into this folder, cd to the Slicer folder and run:

./Slicer --no-main-window --python-script ./test.py

results in:

:slight_smile:

1 Like