How can I write a python CLI code using slicer internal module

Hi,

I am new to slice and working on incorporating our own 3D segmentation model with slicer. And we would like to assume the slicer user does not know anything about code, so intend to write a python CLI code to do everything. I am wondering is it possible? If not, can you provide me some lights that what should I do?

  1. The user specifies the input volume path, output volume path, and some other parameters, then our python code computes the segmentation. This can be done as shown here: SlicerPythonCLIExample/BlurImage.py at master · lassoan/SlicerPythonCLIExample · GitHub

  2. But I would like to use this external python code (note not the interpreter provided in slicer) to render /plot/show the segmentation mask in slicer, is it possible? I know how to do this with the interpreter provided in slicer but not sure whether I can do this with totally external python code?

Thanks!

I don’t know how to edit the original post so I reply here.

After more search, I think CLI is designed to not interact with slicer core. I find an alternative way to do it.
I only want the computed label map to be received by Slicer, currently, I did this by return the label map as a grayscale volume and convert it manually through the volume module. Is there a way to explicitly denote the returned object as a label map in Slicer Execution Model?

Thanks!

CLI modules are Python scripts or command-line applications that do not rely on Slicer. They can be executed within Slicer’s environment or without Slicer. What Slicer offers is displaying automatically-generated GUI, writing all inputs to file, executing the application, continuous progress reporting, and in the end reading back all outputs into the scene.

The automatically generated GUI is typically convenient enough for development, testing, and research/engineering use. For clinical use, often a Python scripted loadable module is developed as a front-end, which provides a more sophisticated GUI and calls the CLI module in the background.

To designate an output image as labelmap, specify type="label", as it is done for example in ModelToLabelMap module:

Hi Andras, Thanks for pointing out the XML file. It helps a lot!

I have another question, how could I run the CLI algorithm using my own python environments instead of slicer internal python environments?

Thanks!

Another possible way is to install the packages I required in slicer internal environment? What is the correct way for doing this? Thanks!

BTW, on MAC, I cannot install packages through the python interaction by “pip.main([…])”

AttributeError: ‘PythonQtStdInRedirect’ object has no attribute ‘isatty’ this error occurs and it seems to have something to do with the mac python framework.

When using Slicer 4.11 and newer you can use the following method to install python packages into Slicer’s environment.
https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#install-a-python-package

You need to activate Slicer’s Python environment before you can run any Python executables (this is required in any other Python or Conda virtual environments, too). You can activate the Slicer Python environment for an executable by launching it using the PythonSlicer launcher (it sets up the Python enrivonment and starts the executable).

However, most Python executables just call python -m ModuleName ..., so instead of using that executable, it is simply to directly run PythonSlicer -m ModuleName .... This is what the slicer.util.pip_install convenience function calls internally:

Installing the required Python packages is easier, because you don’t need to ask your users to set up a new Python environment.