Select segment IDs in a Segmentation with multiple segments

Hi to everyone. Today I’m trying to solve a very concerning problem. The situation is the next one:

suffix = '_manualSegmentation'
segmentationName = f"{patientID}{suffix}"
segmentName = f"{patientID}_coronaryTree{suffix}"
namesCorrect = False
  
while(not namesCorrect):
    try:
        segmentationNode = slicer.util.getNode(segmentationName)
        segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentName)
        
        if (not segmentId == ''):
            namesCorrect = True
            
    except:
        input("Check that segmentation name and segment name are: " + 
              segmentationName + " and " + segmentName + ",respectively."
              + "And press key to continue...")
        namesCorrect = False

This code works properly to be sure that the user puts fine the segmentation and segment names. But only when I have one segment. If I have more than one, the code crashes and Slicer stops working… Am I doing something wrong ?

Thanks to everyone :slight_smile:

I think the code is just getting caught in an infinite while loop where namesCorrect is never True.

I’ve tested the code on a segmentation with multiple segments. If the segment name is found then it works fine, but if “segmentName” is not the name of a segment, there will be an infinite loop.

1 Like