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

**URL:** <https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411>\
**Category:** Support\
**Created:** [September 23, 2022, 9:44am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411 "2022-09-23T09:44:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 23, 2022, 9:44am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/1 "2022-09-23T09:44:40Z")

</div>

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”.](https://github.com/AMK-AQ/CTPelvic1K))

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

---

<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:** [September 23, 2022, 12:05pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/2 "2022-09-23T12:05:14Z")

</div>

You can run external processes using this utility:

[https://slicer.readthedocs.io/en/latest/developer\_guide/slicer.html?highlight=launchconsoleprocess#slicer.util.launchConsoleProcess](https://slicer.readthedocs.io/en/latest/developer_guide/slicer.html?highlight=launchconsoleprocess#slicer.util.launchConsoleProcess)

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 23, 2022, 1:26pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/3 "2022-09-23T13:26:21Z")

</div>

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

---

<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:** [September 23, 2022, 3:50pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/4 "2022-09-23T15:50:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 26, 2022, 8:40am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/5 "2022-09-26T08:40:23Z")

</div>

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”.](https://github.com/ICT-MIRACLE-lab/CTPelvic1K)  
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

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 26, 2022, 9:35am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/6 "2022-09-26T09:35:32Z")

</div>

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”.](https://github.com/ICT-MIRACLE-lab/CTPelvic1K)  
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”.](https://github.com/ICT-MIRACLE-lab/CTPelvic1K)  
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

---

<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:** [September 26, 2022, 4:52pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/7 "2022-09-26T16:52:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 27, 2022, 7:08am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/8 "2022-09-27T07:08:40Z")

</div>

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) :

```auto
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.

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 27, 2022, 9:40am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/9 "2022-09-27T09:40:22Z")

</div>

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

Thanks.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 27, 2022, 9:53am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/10 "2022-09-27T09:53:00Z")

</div>

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:

```auto
# 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

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

```

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 27, 2022, 11:51am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/11 "2022-09-27T11:51:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 27, 2022, 12:05pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/12 "2022-09-27T12:05:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 27, 2022, 12:25pm UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/13 "2022-09-27T12:25:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![AMK](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@AMK](https://discourse.slicer.org/u/AMK)\
**Post date:** [September 30, 2022, 8:31am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/14 "2022-09-30T08:31:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 30, 2022, 9:18am UTC](https://discourse.slicer.org/t/automatically-run-a-python-script-from-slicer-for-deep-learning-inference-purpose/25411/15 "2022-09-30T09:18:44Z")

</div>

We’ve recently been integrating a call to a complex deep learning tool ([TotalSegmentator](https://github.com/wasserth/TotalSegmentator)) in our Lung CT Segmenter extension without crashing 5.0.3 stable.

See the [code segment here](https://github.com/rbumm/SlicerLungCTAnalyzer/blob/master/LungCTSegmenter/LungCTSegmenter.py#L1532:~:text=elif%20self.,Segmentation%20done.%22). You could even [test the extension yourself](https://github.com/rbumm/SlicerLungCTAnalyzer/wiki/Step-2-Using-Lung-CT-Segmenter-with-experimental-AI).

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.
