Programming a script to automate Slicer Extension Module for DiceComputation (CLI)

Hello all,

I am currently trying to program a script that will automate thresholding of scalar volumes then compute the DSC values of these segmentations when compared with previously manually segmented ones. While the coding for Threshold worked seamlessly with CLI, I have been struggling to figure out the parameters for DiceComputation (the extension).

Documentation here:
https://www.slicer.org/wiki/Documentation/Nightly/Modules/DiceComputation

The script runs beautifully in Slicer from Command Line, but then gives me a NoneType object attribute error because the parameter name is clearly wrong and cannot associate with the value I give it in the code. I am convinced it is only a parameter naming problem as all of my other code works well.

When checking the documentation online, I couldn’t find any of the attribute names. Therefore, I have been mostly guessing them based on what I know from other CLI parameters in different modules. I have also tried looking for the original source code of DiceComputation both on GitHub as well as from my own computer after installing the module. However, while I had no luck for the former, I only found .so (compiled Shared Object files) on my computer in its directory.

If anyone knows what the parameters are or have worked in CLI for DiceComputation before, please let me know. All tips and tricks are warmly welcomed. I don’t know, but perhaps DiceComputation doesn’t have the capabilities for CLI?

Thank you,
Phoenix

The source code for the DiceComputation module is here: https://github.com/lchauvin/DiceComputation

This is a loadable module, not a CLI. However, computing the Dice coefficient is implemented in the logic for the module, and so should be easily accessible through python. See: Help with Accessing Module Logic in Python Scripted Modules

Thank you! I will check this all out and hope it solves my problems.

Hi Phoenix,

Here is a function written for this purpose, to interact with the DiceComputation widget.

def calculateDiceCoefficient(self, inputVolume1, inputVolume2):
    dc = slicer.modules.dicecomputation.widgetRepresentation()
    dc.onLabelMapNumberChanged(2)
    # set label maps
    labelMaps = dc.findChild('ctkCollapsibleButton','LabelMapsFrame').findChildren('qSlicerDiceComputationLabelMapSelectorWidget')
    item1 = labelMaps[0].findChild('qMRMLNodeComboBox')
    item1.setCurrentNode(inputVolume1)
    item2 = labelMaps[1].findChild('qMRMLNodeComboBox')
    item2.setCurrentNode(inputVolume2)
    dc.computeDiceCoefficient()
    # get dsc
    outFrame = dc.findChild('ctkCollapsibleButton','OutputFrame')
    table = outFrame.findChild('QTableWidget','OutputResultsTable')
    dsc = float(table.item(0,1).text())

    return dsc

inputVolume1 and inputVolume2 are both label maps

Best,

Michael

Thank you Michael.

I had tried using this in a previous iteration of the code before Threshold was working. I’ll try again.

Best,
Phoenix

Note that Dice metric is a very poor metric for comparing segmentations. The main issue that the value has no absolute meaning - 99% value may be unacceptably low and a 80% value may be perfectly good, depending on the shape of the compared segments. Instead, I would recommend to use Hausdorff distance as a metric. Both Dice and Hausdorff distance metrics are computed by Segment Comparison module (available in SlicerRT extension).

1 Like