Set segmentation master volume from Python

I have a question related to this topic (sorry if I should have posted a new topic!)

I am trying to auto-create segmentations from multiple master volumes using python, but when trying to do the second segmentation it has trouble setting the active segmentation with this code:

segmentEditorWidget.setSegmentationNode(segmentationNode) segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

Interestingly, calling setSegmentationNode(segmentationNode) twice sets the node properly, but the master volume node setting is for some reason disabled, as shown:

I’m not sure if I can simply enable that menu (or how to do that) or if I’m missing some sort of ‘reset’ I need to do to access this menu.

Thanks,

Colin

After setting segmentation node, you should be able to set the master node by simply calling setMasterVolumeNode. Are you using the latest nightly version of Slicer?

Nope, I’ll try it out.
I agree and for the first iteration of my code that method works perfectly. I’ll update after I try in the newest nightly…

Update: still not working. I’m wondering if I’m not setting up the widget properly as I do a few things within the loop.

I set this up outside of the loop:
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()

then within
for volNum, vol in enumerate(vols):

# ... code for setup of segmentation...

segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.vtkMRMLSegmentEditorNode()
slicer.mrmlScene.AddNode(segmentEditorNode)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

# ... after seed growth and at end of loop...
slicer.mrmlScene.RemoveNode(segmentEditorNode)

Then the error results when I attempt to grow from seeds during the second iteration of the loop.

@che85 did this in another module - can you point @Colin_McCurdy to the relevant part in QuantitativeReporting Christian?

1 Like

Also, have a look at the simple example script repository. Does that work correctly?

My code is based off of the SegmentGrowCutSimple code.

It’s moreso using the segmentEditorWidget within a loop that seems to be the issue.

If you don’t let events to be processed (because you don’t return from your loop) then it might be an issue. Try if adding slicer.app.processEvents() in your loop fixes the problems.

Is your module on GitHub? If you send the repository URL then I can have a look.

not quite a module, just a script for now.
But you can view it here:
https://github.com/colinmccurdy/dtiAnalysis/blob/master/dtiAnalysisNrrd.py , let me know if you see any glaring issues !

also, I tried processEvents in a few locations and it did not make a difference.

Another note: even when I run a single iteration I get the following errors:

void __cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onSegmentSelectionChanged(const class QItemSelection &,const class QItemSelection &) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onSegmentationNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::onMasterVolumeNodeChanged(class vtkMRMLNode *) : Invalid segment editor parameter set node
void __cdecl qMRMLSegmentEditorWidget::updateWidgetFromMRML(void) : Invalid segment editor parameter set node
vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: PhantomVolume
vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: Background
vtkSlicerSegmentationsModuleLogic::GetSegmentForSegmentSubjectHierarchyItem: Segmentation does not contain segment with given ID: Feature

The code seems to still execute though with a single one.