Cross Section Analysis Freezing

Hi all.

We automatizated the cross section analysis computation for our own centerlines.

The program runs like this:

def obtainDataFromGeometry(segmentationNode, segmentID, branchID, G, centerlinesDict, tableNode, 
                 fileToSaveResults, createFile):

    """
    Runs CrossSectionAnalysis and genrates a file with
    - Centerline ID
    - Centerline branch ID
    - Node ID
    - Distance
    - Diameter
    - Area
    - Curvature
    - RAS coordinates 
    
    :segmentationNode: segmentation node where the geometry is
    :segmentID: the segment name we want to operate with
    :branchID: indicates the coronary branch ('left' or 'right')
    :centerlinesDict: a dictionary with key "centerline name" and value list of 
            nodeIDs of the centerline
    :tableNode: 'vtkMRMLTableNode' to save auxiliary results
    :fileToSaveResults: path to file where to save results
    :createFile: if True, generates a new file to save resutls. If False, appends results to file
    
    :return: creates a file in fileToSaveResults with the metadata
    """
    
    logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()
    
    segmentNodeID = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentID)
    
    # Set lumen surface
    logic.setLumenSurface(segmentationNode, segmentNodeID)
    
    # Set table node
    logic.setOutputTableNode(tableNode)
    
    # To obtain RAS coordinates in different cols
    logic.coordinateSystemColumnSingle = False
    
    # Set centerline node
    centerlineNames = list(centerlinesDict.keys()) # utils.getCurveNames(centerlinesPrefix)
    
    if(createFile): # We create a new file
        with open(fileToSaveResults, "w") as file:
            file.write("CenterlineID,branchID,nodeID,Distance,Diameter(CE),Cross-sectionArea,Curvature,R,A,S\n")
    
    
    for centerlineID in centerlineNames:
        print("Extracting metadata info from " + str(centerlineID) + "...")
        
        # Find nodes 
        # numberOfValues = tableNode.GetTable().GetColumn(0).GetNumberOfValues()
        numberOfValues = len(centerlinesDict[centerlineID])
        
        # If the curve has less than 3 points, cross section analysis crashes
        doCrossSection = False
        if(numberOfValues > 2):
            doCrossSection = True
        
        if (doCrossSection):
            # avoids interpolating points
            slicer.util.getNode(centerlineID).SetNumberOfPointsPerInterpolatingSegment(1) 
            
            logic.setInputCenterlineNode(slicer.util.getNode(centerlineID))
            
            # Run CrossSectionAnalysis
            logic.run()

The issue we see few times is the following one:

  • If the scene is created and saved and we use this routine in a new Slicer window where we open the saved scene, the function sometimes freezes extracting radius and other parameters. The function blocks the interface:

  • This problem is not seemed yet when we run the the function in the same Slicer window, but we do not rule out the possibility of happen here too.

Thanks a lot for the help :slight_smile: .

Can you share an MRB file so as to reproduce and debug this over the weekend? Only reproducible execution pathways can be solved. What Slicer version are you using? Which OS?

Hi @chir.set, what is a MRB File? We usually work with mrml files… We are using 3D Slicer 5.6.2 version, in Windows 11. I would try to send you the scene without the images and then I will try to reproduce the error. If the error occurs in the scene without images I can send you whatever you need.

Thanks :slight_smile:

You can select an MRB file in the ‘Save dialog’, it’s a ZIP archive that includes anything in your scene that can be saved.

If you mean the volume node, it can be omitted. The segmentation and the centerlines must be included however. Your code is already online.

Okay, how can I share you this file?

Pd: We tried using the function without saving and opening the scene and it also freezes…

You can use any online file sharing services like Yandex disk, Google drive…

Okay, I think this should work:

How is centerlinesDict structured? What is pointed by centerlinesDict[centerlineID]?

centerlinesDict is a dictionary which contains the centerline names as keys, and the list of nodes as values for each centerline.

numberOfValues = len(centerlinesDict[centerlineID]) is the number of nodes of the selected centerline.

This code just works: first time, repeat run in same scene, in recreated scenes, after closing and restarting Slicer, i.e, always.

i

import CrossSectionAnalysis
logic = CrossSectionAnalysis.CrossSectionAnalysisLogic()

segmentation = getNode("Segmentacion")
segmentId = segmentation.GetSegmentation().GetSegmentIdBySegmentName("Paciente")
centerlines = slicer.util.getNodesByClass("vtkMRMLMarkupsCurveNode")

logic.setLumenSurface(segmentation, segmentId)
logic.coordinateSystemColumnSingle = False
# table = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLTableNode")
table = getNode("Table_4")
logic.setOutputTableNode(table)

for centerline in centerlines:
    centerline.SetNumberOfPointsPerInterpolatingSegment(1)
    logic.setInputCenterlineNode(centerline)
    logic.run()

Looking at your code, there seem to be confusion between ‘number of points’ and ‘number of centerlines’:

numberOfValues = len(centerlinesDict[centerlineID])
...
# If the curve has less than 3 points, cross section analysis crashes
...
    if(numberOfValues > 2):

because you precise:

centerlinesDict is a dictionary which contains the centerline names as keys, and the list of nodes as values for each centerline

This is confusing, it suggests that one key references a collection of centerline curves. Is it a simple

dict[“centerlineName”] = centerlineNode

mapping?

Regardless, if

slicer.util.getNode(centerlineID)

returns a curve node, it should not hang.

You seem to be getting the curves using a custom function in your project:

# utils.getCurveNames(centerlinesPrefix)

Check whether all nodes in centerlinesDict for a given key is a curve; for example:

print(centerlineID, slicer.util.getNode(centerlineID).GetClassName())

It does not hang on my system (Linux) in any circumstances. In general, it’s user code, or some particulars of your system.

2 Likes

Hi @chir.set we have been testing the functions (your version and also our version). The behaviour seemed was:

· Both functions works when we execute it alone in Python’s terminal in Slicer.

· None of them works when we execute it in the whole routine. This routine contains the following steps:

With a button driven interface we generate the centerlines, smooth them and let the user modify it (with the same name per each centerline node). Then the centerlines are removed and re-generated using the modified points. Once this process is done, we call the function you provide and Slicer hangs.

This routine sometimes works and sometimes hangs depending on the geometry and the centerlines. We also see that the message ‘Waiting for background jobs…’ is related with taking the geometry.

Do you spot any possible mistake here?

Thanks :slight_smile:

That’s hard to say without seeing the full routine that you can’t obviously disclose (no problem with this).

Since your code works in the Python console too, and not in your routine, the problem remains in the way your code is using CrossSectionAnalysis.

You are repurposing the centerline curves, namely smoothing the curve and recreating points. The initial points are associated with scalar arrays coming right from VMTK and ‘Extract centerline’. If you do not end up with the same number of points and/or if their coordinates change (they will change), the scalar arrays will be out-of-sync with the curve. Your curves miss the important ‘Radius’ array for example. They are being used as arbitrary curves, which is perfectly legitimate too.

Have the curves in your sample file online been repurposed already?

The perpetual ‘Waiting for background jobs…’ message indicates that execution instance is still in the C++ pathway, probably in an infinite loop (and probably with a high CPU load). It contains 2 ‘for’ loops that you could debug so as to understand further. Please note that the scalar arrays of the centerline curves are not used by this C++ code.

I just noticed that your curves in the sample online file are of ‘Linear’ type. The curve is processed by vtkParallelTransportFrame in the C++ pathway to generate tangents along a spline, which is the default type for a markups curve. I don’t know how much this processing may be related with the hangs you are observing. Try with curves of ‘Spline’ type.

Hi @chir.set , by parts:

· We didn’t notice high CPU usage (by the way is very low)
· Indeed, our centerlines in the online example were modified and processed.
· We don’t use extract centerlines, so we create in each iteration the centerlines from arrays.
· Our centerlines are Linear type, yes, and we use the command: curve_node.SetCurveTypeToLinear(). We don’t know how to create centerlines with ‘Spline’, cause is not an option in SetCurveType functions.

We thank a lot your help.

We were exploring and reading the problematic function vtkCrossSectionCompute::UpdateTable in vtkCrossSectionCompute.cxx and the only possibility we manage now is that the thread is never free to be selected, so the module waits indef for it or problems in synchronization between the main thread and the workers threads.

This could be weird because the only execution the big routine do different with execute directly the function in the terminal is keep in the slicer interface a qt dockInterfaceWidget (no subprocess are running during CSAnalysis) and the usage of CPU is quite low (less than 10% before start the CSAnalysis). The memory remains low too, around the 50% of the total memory available is used.

At this point we are concern with the problem and don’t know what to do.

I don’t really understand what it means. Joinable threads are spawn, the main thread will wait for their completion. Each thread will stop when there’s nothing more to process. The main thread cannot ‘select’ a running thread to release/free it AFAIU.

Me too, me too… All I can say is that it’s an out-of-intent/design usage. If you can find a solution that can satisfy your specific requirements without breaking anything, it can be further considered.

1 Like

We saw a completely arbitrary behavior in the well procedure or not in this function:

If we make some minimal changes in the geometry, like minimal smoothings or cut a little bit ends of some vessels, it looks to work again sometimes… A completely no sense…

Thank for your help in this process.

PD: could you tell us how to make our centerlines ‘Spline’?

Since your centerlines are created outside of Slicer, I can’t really say how to draw them as spline.

In Slicer, the spline is the default curve type. As you know the coordinates of your centerline points, you just have to add points to a newly created curve:

mycurve = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode")
mycurve.AddControlPointWorld(0.0, 0.0, 0.0)
1 Like