Use existing slicer modules with python and cli

Hi,
I followed this tutorial on how to use existing modules in slicer through cli library:

It sounds not bad (considering my understanding in cli is close to 0…)
Anyway, since I want to use registration module (such as BRAINSfit) I’d like to have the option to cancel the operation after running it. but the cli cancel command is not implemented yet in the cli.py file that I’ve found.

so my questions:

  1. Is using slicer.cli is a good way execute existing modules proggramatically? If not- what is?
  2. How can I cancel the running process with cli?

Thanks!

Here is part of the code- the apply btton, executing the add Scalar Volume module (but can’t stop in the middle):

   def onApplyButton(self):

        # allow cancel the process
        if self.InProgress:
            self.InProgress = False
            self.abortRequested = True
            raise ValueError("User requested cancel.")
            # self.cliNode.cancel() # not implemented
            self.applyutton.text = "Cancelling..."
            self.applyButton.enabled = False
            return

        self.InProgress = True
        self.applyButton.text = "Cancel"

        fixedImage  = self.fixedSelector.currentNode()
        movingImage = self.moveImgSelector.currentNode()
        outputImage = self.outputSelector.currentNode()

        if self.registratoinMethod == 'addScalarVolumes':
          moduleVar = slicer.modules.addscalarvolumes
          parameters = {}
          parameters["inputVolume1"] = fixedImage.GetID()
          parameters["inputVolume2"] = movingImage.GetID()
          parameters["outputVolume"] = outputImage.GetID()
          parameters["interpolation order"] = 0

        # get the module as object
        slicer.app.processEvents()
        self.cliNode = slicer.cli.run(moduleVar,None, parameters)
        print self.cliNode.cancel.
        self.bar.setCommandLineModuleNode(self.cliNode)

    # logic.run(self.fixedSelector.currentNode(),
        #           self.outputSelector.currentNode())
        # logic.run(fixedImage,movingImage,outputImage)

        # make the output volume appear in all the slice views
        slicer.util.setSliceViewerLayers(
            background=outputImage)
  1. Yes

  2. Did you try calling self.cliNode.Cancel()? (that should probably be added to cli.py)

Hi, thanks! followed your suggestion- I did, but it didn’t work.
begginner question- maybe there is a way to access the cancel of the module itself? (the cancel button exist in some modules)

Are you sure it didn’t work? In your example code you have cancel but the method is Cancel with a capital C. Calling Cancel should be the same as clicking the Cancel button on CLI interface widget.

Thanks!! This fixed the problem!!
I changed it to:

if self.InProgress:
self.InProgress = False
self.abortRequested = True
self.cliNode.Cancel()
self.applyButton.text = “Cancelling…”
self.applyButton.enabled = False
return
now it cancel the progress.

Could you help me with another question?

  1. Is there a way to make something happen after the process is over? like change the name of the botton after it finishes.

  2. How can I get through the cli the log, that is ussualy apear in the consol window while running the algorithm with the original loadable module?

I couldn’t find an explanation here:
https://www.slicer.org/wiki/Documentation/4.1/Modules/BRAINSFit

Great - glad that worked.

If you observe events from the cliNode you can tell what state the process is in and react accordingly. You can review this documentation with the printStatus example:

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

Also to get the log info you can call cliNode.GetOutputText() and cliNode.GetErrorText()

(I just updated the wiki with this info).

1 Like