Automatically run a python script from slicer for deep learning inference purpose

Hi,

I am trying to automatically run a deep learning inference from the slicer.

The link of the model is: (GitHub - AMK-AQ/CTPelvic1K: Resources of the paper “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models”.)

I have been able to automate the whole process. I have to just place the concerned CT data in a folder (which I am able to do from Slicer) and then run the script runs.py from the command window. The segmented volume is stored in another folder, which again I am able to import back into Slicer. I wanted to know how can I run this runs.py from Slicer.

Or is there any better alternatives.

Thanks

You can run external processes using this utility:

https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html?highlight=launchconsoleprocess#slicer.util.launchConsoleProcess

I tried the following code

proc = slicer.util.launchConsoleProcess(r"python runs.py", useStartupEnvironment=True)
slicer.util.logProcessOutput(proc)

But it outputs the following error:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Traceback (most recent call last):
File “”, line 1, in
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\bin\Python\slicer\util.py”, line 3459, in logProcessOutput
raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command ‘python runs.py’ returned non-zero exit status 9009.

Thanks

The useStartupEnvironment will be the one that was used to launch Slicer. If you launch from the Start menu you may get a vanilla empty environment. If you launch Slicer.exe from the same console where you can run your python script then the command should work.

I have updated the code:

def onRunAIButton(self):

    import os
    import sys

    pythonSlicerExecutablePath = os.path.dirname(sys.executable)+"/PythonSlicer"

    module = "runs"
    commandLine = [pythonSlicerExecutablePath, "-m", module]

    #proc = slicer.util.launchConsoleProcess(commandLine, useStartupEnvironment=True)
    proc = slicer.util.launchConsoleProcess(commandLine)
    slicer.util.logProcessOutput(proc)

    print('done')

The good thing is that now the slicer is able to print the things written in the python file. But its not able to run the commands in the file.

I am recieving the following output:

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Please cite the following paper when using CTPelvic1K dataset:

Pengbo, Liu, et al. “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models.” arXiv preprint arXiv: (2021).

If you have questions or suggestions, feel free to open an issue at GitHub - MIRACLE-Center/CTPelvic1K: Resources of the paper “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models”.
C:\Users\akh\Downloads\all_data_2

python inference/predict_simple.py -i C:\Users\akh\Downloads\all_data_2\nnUNet\rawdata\ipcai2021_ALL_Test -o C:\Users\akh\Downloads\all_data_2\nnUNet\rawdata\ipcai2021_ALL_Test/Task22_ipcai2021__CTPelvic1K__fold0_2d_pred -t Task22_ipcai2021 -tr nnUNetTrainer -m 2d -f 0 --num_threads_preprocessing 8 --num_threads_nifti_save 4 --gpu 0

I tried to find the reason for the error. Apparently it says that the Python is not in the path. But i am not sure if this is correct as I am able to run all the other Python commands.

Am I getting something wrong here.

Thanks

I tried to implement the suggestion given by the above error message.

It now gives another error

Please cite the following paper when using CTPelvic1K dataset:

Pengbo, Liu, et al. “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models.” arXiv preprint arXiv: (2021).

If you have questions or suggestions, feel free to open an issue at GitHub - MIRACLE-Center/CTPelvic1K: Resources of the paper “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models”.
Traceback (most recent call last):
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\lib\Python\Lib\site-packages\numpy\core_init_.py”, line 23, in
from . import multiarray
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\lib\Python\Lib\site-packages\numpy\core\multiarray.py”, line 10, in
from . import overrides
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\lib\Python\Lib\site-packages\numpy\core\overrides.py”, line 6, in
from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named ‘numpy.core._multiarray_umath’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\dev\CTPelvic1K\nnunet\inference\predict_simple.py”, line 8, in
from nnunet.inference.predict import predict_from_folder
File “C:\dev\CTPelvic1K\nnunet\inference\predict.py”, line 4, in
import numpy as np
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\lib\Python\Lib\site-packages\numpy_init_.py”, line 144, in
from . import core
File “C:\ProgramData\NA-MIC\Slicer 5.0.3\lib\Python\Lib\site-packages\numpy\core_init_.py”, line 49, in
raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  • The Python version is: Python3.10 from “C:\Users\akh\AppData\Local\Programs\Python\Python310\python.exe”
  • The NumPy version is: “1.22.1”

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named ‘numpy.core._multiarray_umath’

Please cite the following paper when using CTPelvic1K dataset:

Pengbo, Liu, et al. “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models.” arXiv preprint arXiv: (2021).

If you have questions or suggestions, feel free to open an issue at GitHub - MIRACLE-Center/CTPelvic1K: Resources of the paper “Deep Learning to Segment Pelvic Bones: Large-scale CT Datasets and Baseline Models”.
C:\Users\akh\Downloads\all_data_2

python inference/predict_simple.py -i C:\Users\akh\Downloads\all_data_2\nnUNet\rawdata\ipcai2021_ALL_Test -o C:\Users\akh\Downloads\all_data_2\nnUNet\rawdata\ipcai2021_ALL_Test/Task22_ipcai2021__CTPelvic1K__fold0_2d_pred -t Task22_ipcai2021 -tr nnUNetTrainer -m 2d -f 0 --num_threads_preprocessing 8 --num_threads_nifti_save 4 --gpu 0

You are running Slicer’s python environment, which is not the same as the one you get in a console. To run the console version you’ll need to set an environment that matches the console’s so the correct python environment is used.

Maybe you should set up a batch file first that exactly does what you expect - just for testing and outside Slicer.

For example, create a file “runai.bat” in your Windows home directory with the following content (adjust the path to the Python script directory and put your file “runs.py” there) :

cd "C:\Users\YourName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Scripts"
python runs.py
pause

Double-click runai.bat.

Is this working? If yes we could provide the next steps.

The batch file is working. I had to do some more scripting there, but it’s working really nice.

Thanks.

Very good.

So you could copy your “runs.py” script into the Slicer Python script directory and do the following from within Slicer or a Slicer extension now:

# get current directory 
beforeDir = os.getcwd()

# get Slicer home bin folder
slicerHomeBinDir = slicer.app.slicerHome
slicerHomeDir = slicerHomeBinDir.removesuffix('/bin/..')

# change to Slicer script directory
os.chdir(slicerHomeDir + r"/lib/Python/Scripts")
                
# run your script from Slicer script directory
proc = slicer.util.launchConsoleProcess(r"python ./runs.py")
slicer.util.logProcessOutput(proc)

# change to before directory
os.chdir(beforeDir)

The Python script directory for Slicer 5.0.3 in Windows would be at

C:\Users\Yourname\AppData\Local\NA-MIC\Slicer 5.0.3\lib\Python\Scripts

Thanks for the script. I tried to implement this in Slicer. It starts the process perfectly, but after sometime the whole system crashes and the system restarts. I didn’t faced this problem when working from the anaconda prompt or from the bash file etc. Do you have any suggestion what might be the cause?

Not really, try to debug this by using a very basic Python script “runs.py” initially with a print("Hello") and make this more complex later, see where it fails …

Are you working with the latest stable version of 3D Slicer?

Yes, I am using the latest stable version of Slicer (5.0.3).

I ran the code in Slicer 4.11.20210226 and it worked perfectly with this version.

So my code works with old version of Slicer. But it crashes with Slicer 5.0.3 and restarts the computer. Is there any way I can make it work with the latest version also?

Thanks.

We’ve recently been integrating a call to a complex deep learning tool (TotalSegmentator) in our Lung CT Segmenter extension without crashing 5.0.3 stable.

See the code segment here. You could even test the extension yourself.

As you probably do not want to publish your code yet, I fear you would need to debug the code step by step and identify the part that crashes Slicer.

If your code is available online please provide a link.