How to run python based CLI modules?

I have gone through this
https://www.slicer.org/wiki/Documentation/Nightly/Developers/SlicerExecutionModel/Python

So, what I understand about python based CLI is:
there is an xml file which will produce GUI for application and the same xml is used by the .py file to parse elements.
the execute function is in the xml file that will be run by slicer and the name of the script .py file is within this function.

how to run and test the python based cli module?

Thank you

Regards,
Saima Safdar

Hi Saima -

For a new style python scripted cli, you can run like you would any other cli.

https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Running_a_CLI_from_Python

E.g.

node = slicer.cli.runSync(slicer.modules.slicerradiomicscli)
print(node)

HTH,
Steve

How to run and test newly created cli modules?
and how do you get the GUI for newly created CLI module?

I am new in this and I want to create a python based CLI module. The module will take input image send it to external python file for execution (as python 3 external libraries are required) and return the resultant image.

Please help me as I could not understand how to do this?

I have found this script
https://www.slicer.org/wiki/Documentation/Nightly/Developers/SlicerExecutionModel/Python

I need to do this but I do not know how I can do this in slicer?

Do I need to build with cmake. then create a scripted module for the cli and then run thought the scripted module?

Please guide how can I create python based CLI modules

Hi @Saima -

As it says at the top of the page you linked that older style was only supported in Slicer 3.x. There is a new style for Slicer 4.x that you can follow like in this repository.

The description of the parameters goes in the .xml file, and the script to run is the .py file.

You can also look at the PyCLIModule tests here.

I haven’t tried running python3 - you will probably run into environment variable issues. @ihnorton did you have any suggestions for that?

Thank you so much for replying.

I will look into the things you suggested and will come back again.

Hi,
How can you put a newly written python based cli into slicer?

Thank you

Regards,
Saima Safdar

You should be able to just add the directory to the module path like you would with a scripted module (module path selector in the preferences dialog)

I added the folder “newfuzzypython” containing the example .py and .xml. Like below in the screen shot.
Is it correct? How can I see this in slicer. I dont see any new module added to module section.
Sorry, if i am asking general questions. I am new to this environment. Trying to understand.
image

Could you upload your source code to GitHub and post the link here so that we can have a look?

Sorry I was away. the issue to load the cli module is resolved. will come back again if I have problems.

Please would you tell me one thing. if I made changes in cli module I have to restart slicer to see those changes. is it the only way to test the new module again and again.

There is no need to restart Slicer to test changes. You can reload your module by clicking “Reload” button at the top of your scripted module user interface. Make sure you use a module template generated by a recent version of Extension Wizard module, and enable “Developer mode” in application settings.

Dear Andras,
when I create scripted python module I do find restore and the changes reflect but when I am working with cli based python module I could not see a reload button. the template produces this as below:
image

For Python CLIs you don’t even need to click Reload, as when you click Apply then always the current version of the code is used.

If you update the GUI (the XML description) then you need to restart Slicer to see the changes, but since it is just a descriptive text, it should not require frequent modifications or debugging.

Hello,

I was able to follow the instructions above and it worked fine for the moment. I just had a quick question:

I’m using a script to automate extraction of radiomics features after histogram matching. I’m using the radiomics CLI module for this. However, I’m not sure how to import a params.yaml file into slicer so I can use it for radiomics extraction. Should I directly import it using a python function or is there a slicer function I can use e.g. the “loadNodeFromFile” function

Thanks

I don’t think you need to load the file, just pass the filename. This automatic test could be a useful example.

1 Like

Yes that worked out. Thank you!

However, now I have a new problem. I have attached my code below. I’m trying to extract radiomics features from a node called “Pref_hist” using a segmentation node but the parameter file is not considered during the extraction of raiomics features. I manually extracted radiomics using the interative module and compared the values and it turns out that the parameter set supplied in the “param” argument is not considered (or is being ignored). I have also attached a copy of the params file as a jpg here.


Please let me know if you have any suggestions.

#Create a new blank table to store radiomics
tableNode=slicer.vtkMRMLTableNode()
tableNode.SetName(“Pre”)

slicer.mrmlScene.AddNode(tableNode)
radiomics=slicer.modules.slicerradiomicscli

#Parameters for radiomics extraction
parameters_rad={}
parameters_rad[“Image”]=Pre_hist
parameters_rad[“Mask”]=getNode(‘Segmentation’)
parameters_rad[“param”]={Location of file “Parameter_File.yaml” attached in screenshot}
parameters_rad[“out”]=tableNode
node_rad=slicer.cli.runSync(radiomics,None,parameters_rad)

1 Like

You can have a look at the application log to see the command-line arguments that are generated from your parameters_rad variable. Compare it to the command-line arguments that are generated when you run the interactive module.

1 Like

hi Lassoan!
3D slicer is very convinnent in analysis medical images, specially show 3D models.
recently, I want to combine my deep learning code python with 3D slicer.
it is the algorithm for segmenting the liver in CT images on pytorch.
but I don’t know how to do this work. can you give me instructions, specific documents, links… for it.
thankyou!!!

You can install PyTorch directly in slicer like this:

Or you can use your model in something like the MONAI Label server:

1 Like

hi Pieper!
thankyou so much.