Creating a module with existing python code

Hi all

I’m new to 3D slicer and I’m looking for ways to create a plug-in (extension) in this software which can utilize already existing python codes. I can’t really find anything helpful online about this. Anybody here who can help me out?

Thanks,
Jesse

I think this resource is supposed to be the extension developer starting point: https://www.slicer.org/wiki/Documentation/Nightly/Developers/Tutorials/BuildTestPackageDistributeExtensions

Did you see it? Or you did see it, but it was not helpful?

I did see it, but I still don’t quite understand. I have never used slicer before.
I also don’t quite understand the difference between a scripted module (which is written in python) and a python based CLI. Which would be the best choice for my application?

It depends on what your application is. CLIs are suitable for non-interactive and batch processing tools.

It can help if you start with an extension that has similar functionality. If you share the details of what you are trying to do, we may be able to point you to similar extensions/modules, or give more specific advice.

Hi fedorov. The idea is to manually delineate tumors in some MRI slices and then the extension should delineate the remaining slices resulting in a complete delineation. A first version of the python code to do this is already available. I’m looking if it is feasible to implement this existing python code in a plugin/extension, so basically an implementation of the code. Thanks.

Take a look at Segment Editor: https://slicer.readthedocs.io/en/latest/user_guide/module_segmenteditor.html

There is already “Fill between slices” effect. This should be a good place to start.

1 Like

As @fedorov suggested above, you would normally implement such features as an effect in Segment Editor module. You can see an example of an extension that adds several new effects here. You can fork this extension and then use “Extension Wizard” module in Slicer to add a new module of scriptedsegmenteditoreffect type.

2 Likes

So if I’m understanding correctly, I can use the Segment Editor module to delineate some slices and add my own python code as an extra module to propagate the delineations to the other remaining slices? And so create an extension.

Yes. Slicer’s Segment Editor module already has a number of effects that require an approximate segmentation as input (“Grow from seeds” effect requires seeds inside segments as input; “Fill between slices” requires accurate segmentation in a few slices) and generates full segmentation from that.

You can add modules (implemented in Python) that add similar effects to the Segment Editor. For example, SegmentEditorExtraEffects extension adds Watershed and Fast marching effects to the Segment Editor, which takes seed regions and generate full segmentation from it.

Probably a good way to start is to learn what Segment Editor can do, check out Slicer programming tutorials, have a look at existing Segment Editor effect implementations in Slicer core and in SegmentEditorExtraEffects and then create a new effect using Extension Wizard and put your code into it.

If you want Slicer users to easily access your new tool, you can submit your extension to the Slicer appstore where people can download and install by a few clicks.

1 Like

Thank you for this information, I think this will be helpful! This morning, I also stumbled upon https://docs.google.com/presentation/d/1JXIfs0rAM7DwZAho57Jqz14MRn2BIMrjB17Uj_7Yztc/edit#slide=id.g41f90baec_028
I was following it and I think this is a template to create your own extension, is that correct?

Although the presentation is 5 years old, it is still mostly valid. There have been a number of important improvements in Slicer, so my recommendations would be somewhat different in these details:

  • I would not recommend using the simple “scripted” template type for new modules but use “scripteddesigner” for general-purpose modules and “scriptedsegmenteditoreffect” for Segment Editor effects.
  • I would not use the legacy chart infrastructure for 2D diagrams but use the new plots infrastructure instead.

You can find updated tutorial for scripteddesigner template based projects here.

I still find it difficult to understand the codes behind the segment editor modules, may be due to my lack of experience in python. What would you recommend me to due to try to understand these codes?

It is certainly not simple task to implement such features. You need to be somewhat familiar with VTK and Qt libraries and have a basic understanding of MRML. I would recommend to study the source code of existing segment editor effects, find one that does something similar to what you would need, and modify that. You can implement effects either in C++ or Python. If you have any specific question then we are here to help.

1 Like

Do you know a good place where I can get familiar with VTK and Qt libraries?
I also already looked a bit into MRML because it seemed important but I couldn’t find something immediately that made clear what it is
I also looked at the source code of “Fill in between slices” because the concept is quite similar but to my surprice this was only 46 lines long. To me this seems very short.

VTK and other medical image computing related libraries: see this post.
Qt: there are many online tutorials and examples.

That’s the point. If you already have a method that computes missing segmentation between segmented slices, then you need to write a few ten lines of code to make it available in Slicer.

1 Like