Running Test Class - Python Scripted Module

Hello! We have finished our code and tested it manually through the Python interactor. Now, we are attempting to run it through a module, so it is just a click of the button. In the testing class in our python file, do we actually copy and paste our algorithm into the class or do you somehow use the logic?

Also, when assigning inputVolume, is there a way to assign inputVolume to your current selected volume?


1 Like

@cpinter @lassoan @Sunderlandkyl

We especially need help setting the input and output volume. We are using CTA-cardio as our input volume and we are wondering how we should export this volume into our scripted module.

Thank You!

1 Like

To run the module test, you should call the logic methods from the test class.

Do you mean that you would like to get image that is displayed in the slice views? You can use something like this:

layoutManager =
    # Use first background volume node in any of the displayed layouts
    for layoutName in layoutManager.sliceViewNames():
      compositeNode = self.getCompositeNode(layoutName)
      if compositeNode.GetBackgroundVolumeID():
        return compositeNode.GetBackgroundVolumeID()

If you want the user to be able to select input/output nodes from the GUI, you could add qMRMLNodeComboBox to the module widget.

Okay thank you so much!

Our project requires the user to upload their own medical images, so we needed the inputVolume to be the image that they have uploaded. We will try out the compositeNode technique.

Then, we have the code that segments the vertebra. So, in the test class, we should call those methods?

Thank you!

If you want to let the users select their own inputs/outputs, you should use this approach:

Logic method should perform the actual segmentation and accept arguments for all of the inputs and outputs. In the test class you can download the sample data (CTA-cardio) and call the logic method that performs the segmentation.

Okay, we will begin working on the qMRMLNodeComboBox. Thank you so much!

I have attached the test_VertebraSegmentation1 file if you have any suggestions. We believe this is what the module runs we click reload and test. Lines 17-66 perform the actual segmentation. When we copy them into the python interactor in slicer, our desired result works(given a fiducial point has been marked on a vertebra).

The help is much appreciated! We will add in the qMRMLNodeComboBox so the user can select their own input Volume.

Furthermore, how do we instantiate a qMRMLNodeComboBox @Sunderlandkyl

We tried to reload and test and we got an error while running. We put down two fiducial points to test our code but we got an error saying that it “could not find nodes in the scene by name or id ‘F’” (our fiducials were called ‘F’). How can we fix this error @Sunderlandkyl?

Please complete this Slicer programming tutorial, which should answer all these questions. Let us know if something is still not clear.

We have done the tutorial and gone through old modules. We are very appreciative of everyone’s help. We have our code, lines 17-66 in the link two topics above. This code works perfectly when copied and pasted into the python interactor. We just don’t know how to translate that to the testing function in the testing class in our module.

We think it should be easy since we have the code, we just don’t/understand the tutorial on how to transition it.



I have just re-read through the tutorial. My understanding is that we put our chunk of code that runs the algorithm(for reference, it is lines 17-66 in the GitHub link above) in the logic class of our python text. I do not know where in the logic class we actually put it though.

Then we run the testing function in the testing class of our python text using the onApplyButton function that takes in some parameters. Is there anything we are missing? We are very confused by the whole process as it is our first time coding.

1 Like

@Sunderlandkyl @cpinter,

Do you have any solutions for this?

You should move lines 22-66 to the logic class in the run method, then change the arguments of run to accept your inputs (segmentation, fiducial node, etc.). You can call run from the test function.

1 Like

Thank you so much! And do we just remove line 17-18 where we set the masterVolumeNode because that will be the inputVolume?

1 Like

No, the test should set up the scene and test the logic without any user interaction (ie download sample data, place fiducial points, and then call the run method).

1 Like

Hello @Sunderlandkyl,

Thank you very much for the help. We still have one issue where the fiducials dissapear every time we run and we get an error saying that there is no node with id (‘F’) (the name of our fiducial). How can we fix this problem?

1 Like

Also we have set the fiducial points as an input for the run function so I am not sure what to do.

1 Like

In addition, the CTA-Cardio scan that we download deltes every time we run so there is no final volume and nothing in the subject hierarchy. This contributes to the fiducial issue above so I was wondering why this happens and what can I do to fix this?


The scene is cleared by the setUp function every time the test is run. You need to download sample data, add necessary nodes, place fiducials, etc, as needed to run the test.

Take a look at the scripted module template for an example of what should be done:

1 Like