Hi,
can you please tell me how I can run a python script as a subprocess from Slicer?
I would like to lauch a python file from the logic of my module.
Thank you
Hi,
can you please tell me how I can run a python script as a subprocess from Slicer?
I would like to lauch a python file from the logic of my module.
Thank you
Maybe slicer.util.launchConsoleProcess
could work for this
You can also run your Python script as a CLI module. Slicer automatically generates a GUI for specifying inputs and outputs; and you can also run the script as any other CLI module. See a complete example here: GitHub - lassoan/SlicerPythonCLIExample: Example extension for 3D Slicer that demonstrates how to make a Python script available as a CLI module
Thank you for your reply.
I noticed that the python script runned as a CLI module is much slower than if I run it from an external command prompt.
Do you know any possible reason?
Thank you
You can get definitive answer by using a Python profiler, but a likely root cause is excessive logging. See more details here:
Thank you.
I also tried to run a simple os.system from the logic of my Slicer module:
import os
.
.
.
path_to_python = "python_path//"
os.system(f"{path_to_python}python.exe myscript.py")
but I am getting this error:
module 'os' has no attribute 'add_dll_directory'
While if I do the same from a Windows command prompt it works well:
import os
path_to_python = "python_path//"
os.system(f"{path_to_python}python.exe myscript.py")
Do you know the reason for this behaviour?
Thank you
os.system
is a very rudimentary tool, you cannot control the used environment, output redirections, ownership of thr process, etc. Use subprocess
instead. Set example here.
There is also the slicer.util.launchConsoleProcess
function that conveniently redirects outputs, sets the environment, hides the console, gives access to the process so that you can log its outputs or if user requests then terminate the process.
If you want to run Python then you can use Slicer’s Python environment as shown here.
Hi Andras,
I have created a slicerpythonscriptedcli module. Is it possible to call subprocess_check_out to run a program using anaconda.
I am using some libraries that are installed in an enviornment in anaconda thats why I am calling the external python to run a program.
Please let me know how can i do it.
I receive the following error:
Traceback (most recent call last):
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/Scheduler/scheduler/scheduler.py”, line 68, in
main(sys.argv[1], float(sys.argv[2]), sys.argv[3], sys.argv[4], sys.argv[5])
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/Scheduler/scheduler/scheduler.py”, line 50, in main
command_result = check_output(command_line, env=slicer.util.startupEnvironment())
File “/home/useradmin/Slicer-4.11.20210226-linux-amd64/bin/Python/slicer/util.py”, line 106, in startupEnvironment
startupEnv = slicer.app.startupEnvironment()
AttributeError: module ‘slicer’ has no attribute ‘app’
regards,
Saima
slicer.util.startupEnvironment()
requires that you run the script using ./Slicer
executable (and not using ./PythonSlicer
executable).
You could get the startup environment by calling the ctkAppLauncherEnvironment::environment(0)
method in C++, but the only Python-wrapped class that exposes this method is qSlicerCoreApplication. If you start your Python script using ./PythonSlicer
then no Qt application class is created, so you would need to add a new C++ class that allows you to call this function.
It would be much simpler to install the libraries using PyPI into Slicer’s Python environment. What are these libraries?
Its a toolkit, bratstoolkit for tumour segmentation.
regards,
Saima
Hi andras,
can I run check_output or run without env variable and run the process outside the slicer in an anaconda enviornment and get the results and avoid running into error due to startupenviornment.
At this stage I have to not use anything from slicer but after the processing the output is a volume(.nrrd), which I will export and do some processing on it.
regards,
Saima Safdar
You can pip install it in Slicer’s Python environment (PythonSlicer.exe -m pip install BraTS-Toolkit
). There should be no need to mess with anaconda.
There is a problem that this package pins a very specific version of SimpleITK (and a couple of other Python packages), which makes Slicer remove the special SimpleITK distribution that is bundled with Slicer, so modules that rely on SimpleITK may stop working. See Did sitkUtils.PullVolumeFromSlicer() break with Slicer 5.2? - #9 by lassoan for more details.
Another bad thing about this bratstoolkit is that it comes with the strictest possible copyleft license. If you use this code then you must make all other code in the same project public. Probably you can find other segmentation toolkits that provide similar features and do not come with such heavy restrictions.
Hi Andras,
For the bratstoolkit, may be I could run it outside the slicer and get the results back into the slicer using the subprocess command and make the scripted module.
Is this ok? because if i dont install within slicer I will not have the SimpleITK issue.
The only thing I need for scripted module is how cani display the output or errors like in the
pythonCLi based module. That is a desperate post.
Can you suggest some other tumour segmentation toolkits ?
regards,
Saima
Hi Andras,
I have submitted the batchBrainMRSegmentation extension. The suggestion is to make CLI modules for each of the files I am calling through the main scripted python module.
I started making the cli module. It did install the bratstoolkit but I am getting this error now. Please see below. How can I proceed further. Please help.
Error response from daemon: No such container: greedy_elephant
Error: No such container: greedy_elephant
Error response from daemon: No such container: greedy_elephant
docker: Error response from daemon: could not select device driver “” with capabilities: [[gpu]].
Error: No such container: greedy_elephant
Error: No such container: greedy_elephant
Error: No such container: greedy_elephant
Error: No such container: greedy_elephant
Traceback (most recent call last):
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/socketio/client.py”, line 280, in connect
self.eio.connect(url, headers=headers, transports=transports,
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/engineio/client.py”, line 194, in connect
return getattr(self, ‘connect’ + self.transports[0])(
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/engineio/client.py”, line 300, in _connect_polling
raise exceptions.ConnectionError(
engineio.exceptions.ConnectionError: Connection refused by the server
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/SlicerBratsPreprocessor/BratsPreprocessor/BratsPreprocessor.py”, line 79, in
main(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7])
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/SlicerBratsPreprocessor/BratsPreprocessor/BratsPreprocessor.py”, line 60, in main
prep.single_preprocess(t1File=t1File, t1cFile=t1cFile, t2File=t2File, flaFile=flaFile, outputFolder=outputDir, mode=“gpu”, confirm=True, skipUpdate=False, gpuid=‘0’)
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/brats_toolkit/preprocessor.py”, line 127, in single_preprocess
self.batch_preprocess(
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/brats_toolkit/preprocessor.py”, line 168, in batch_preprocess
self._connect_client()
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/brats_toolkit/preprocessor.py”, line 172, in _connect_client
self.sio.connect(“http://localhost:5000”)
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/socketio/client.py”, line 283, in connect
self._trigger_event(
File “/media/useradmin/Disk2/Slicer-5.7.0-2024-03-25-linux-amd64(1)/Slicer-5.7.0-2024-03-25-linux-amd64/lib/Python/lib/python3.9/site-packages/socketio/client.py”, line 555, in _trigger_event
return self.handlers[namespace]event
TypeError: connect_error() takes 0 positional arguments but 1 was given