Hi folks,
I’m just getting started with slicer and python and I’m hoping to get some help regarding how to access the underlying logic of Slicer modules from within a python script? (i.e. the Python interactor or the python code of a scripted module)
Specifically, I’ve got a scripted module I made using the Extension Wizard (after working through these two tutorials on python and python modules). In the ‘run’ section of my scripted module, I’d like to call the Simple Filters module to use the ConnectedThresholdImageFilter and then call the Model Maker module to turn that segmentation into a model.
I’ve found this example showing the use of the Model Maker module via python, but I’m hoping to have an understanding that’s more generalizable to other modules (i.e. are all modules accessed via “slicer.cli.run( …)”? How would I figure out which parameters need to be passed to the module if I didn’t know them in advance?).
You can get the parameters of CLI modules as described in the wiki.
I think you can have access to the logic of Slicer modules like this: >>> slicer.vtkSlicerTransformLogic() (vtkCommonCorePython.vtkSlicerTransformLogic)0x138f47738 >>> slicer.modules.transforms.logic() (vtkCommonCorePython.vtkSlicerTransformLogic)0x138f476d0
I don’t think you need to use the Simple Filters module if you’re doing Python. You can just use SimpleITK in your code.
Hi Fernando,
Thanks so much for your help! I’ve been able to sort out the details of using the model maker CLI module to go from label map to model as well as using SimpleITK to perform the segmentation.
My only remaining hiccup is that in trying to connect the two, the filter in SimpleITK returns a scalar volume node while the model maker module requires a label-map to behave as intended (when I pass it a volume node it still runs but it appears to be trying to make a model out of the entire volume rather than the segmented area only). I can see the Volumes module can do this with the ‘Convert to Label Map’ button but I’m unsure how to call that button from a python script (unlike module maker, the Volumes appears to be a loadable module rather than a CLI one).
Fernando, thanks again for your help! I’m also pretty new to github so I hadn’t realized you could search an entire repository. I’ve now got the pipeline of segmentation and model making working as intended.
Andras, I already had the model making process sorted out so I won’t bother re-factoring this time around but I looked at your suggested approach and I agree it seems much simpler. I’ll be sure to keep that in mind for future.