Exporting CSV with parenchyma analysis module

Just to let you know, @chuawm :
I have literally spent 6 hours on that problem, tried every possible way to make this work in 4.11, but had no success. The problem seems to be, that the “Parenchyma analysis” of CIP (which I am not the author of) is outdated and probably not fully compatible with 4.11. Whatever values I put into the “CIP colors” color table node, and use it during export of the “Lung CT Segmenter” segmentation and as an input into “Parenchyma analysis”, it is not associated with “right lung” and “left lung” and “left lung” histogram remains flat.

Just an example: segmentToLabelValueMapping.SetTypeToUser() is not in the vtkMRMLColorTableNode documentation. Am I / are we missing anything here ?

I could easily implement a histogram function into “LungCTSegmenter”, but as @lassoan said - this is probably not the right way to go because the specialized modules should be modernized.

Firstly, thank you @rbumm for helping! I really appreciate it!

I will probably have to make do with using the alternative you suggested for the time being, which is to use a histogram function for “LungCTSegmenter”.

Apologies as I am still new to this and I have a question about inserting the histogram function. How do I get the individual histograms for each segments (i.e. right lung, left lung)?

The script repository here that I’m using is likely generating a single histogram (I’m assuming its the whole lung).

Additionally, the count frequency and graph seem to be different when generated by CIP “Parenchyma Analysis” compared with the generic script I am using.

Did you copy that code into the python interactor or modify it before? You would need to.
The code you mention is an example of loading a brain tumor dataset and plotting a histogram …

I will add a histogram function, which will be helpful anyway, to LungCTAnalyzer during the next few days. Please check back.

I’m fixing CIP for the Slicer Preview Release (4.13) and also investigating this histogram computation issue.

I’ve fixed Chest Imaging Platform build for Slicer-4.13. It’ll probably take a few days to get the pull request merged.

I’ve also fixed the bug that cause the color map not applied during labelmap export (and therefore broke the histogram computation). This will be available in the Slicer Preview Release from Wednesday.

1 Like

Chest Imaging Platform fix has been merged today. So, in tomorrow’s Slicer Preview Release CIP should be available and it should work well with LungCTSegmenter output.

1 Like

I can confirm that in 4.13.0-2021-08-24 → CIP is available. The “Parenchyma analysis” extension of CIP now correctly detects and analyzes a labelmap generated from a segmentation created by “LungCTSegmenter” using CIP-colors. As far as I see - good histograms:

Thank you @lassoan
@Ezio_Lanza , @chuawm go for it :slight_smile:

1 Like

Hello @rbumm and @lassoan ,

Thank you again for helping! I am really excited to try this out. However, I am again encountering the same issues.

I upgraded the 3D slicer version to 4.13.0-2021-08-24 and installed CIP. I then used “LungCTSegmenter” to segment the lungs as shown.

I then proceeded to go to “segment editor” to change the label values of the segments to match CIP conventions and export it as a lablelmap (mentioned by @lassoan here
Exporting CSV with parenchyma analysis module - #17 by lassoan)

Then I went to “Parenchyma Analysis” to use the generate the histogram using the segmentation labelmap I created. But I still get a flat histogram

I noticed that your segmentation colours are the default ones generated by “LungCTSegmenter” (i.e. green and yellow). I also tried using the default colours but the same flat histogram is still present.

Did I do something wrong?

This is still a bit complicated, we will make this easier soon.

You need to right-click on the newly created lung segmentation, then go → “Edit properties”

image

This will open the segmentations extension
→ go “Export/Import models and labelmaps”
→ click "Advanced

image

→ Check the checkbox “Use color table values”
→ Press “Export” button

and you are done.

Under “Data” you will find a new labelmap “Lung segmentation-label” with correct label specifications for CIP

image

“Parenchyma analysis” with an input of the new labelmap will produce:

image

@lassoan will probably know how to transform segmentations to labelmaps using a color table via script.

1 Like

Thank you so much!!! It works now :grinning:
Your steps were clear so it was not that complicated

2 Likes

CIP modules should be updated to take segmentations directly instead of requiring labelmap conversion.

@rbumm it would be awesome if you could review and modernize CIP modules. They contain excellent algorithms but they don’t seem to be actively maintained.

1 Like

Quite a difficult task, no promises concerning a time frame, but I will dig into this @lassoan.

Allowing selfecting segmentation or labelmap volume as input should be straightforward. You can list multiple node types in the input selector combobox and if you receive a segmentation node then you can convert to labelmap node with the appropriate color table.

But I fully agree that overall the modernization of the CIP extension is significant effort. Any amount of work that you can put into it (and other extensions) are greatly appreciated.

It could help to look at how it’s done in SlicerRadiomics.

How Do I use ExportSegmentsToLabelmapNode correctly with a color node ?

        # to create labelmap compatibility with Chest Imaging Platform
 
        segmentToLabelValueMapping = slicer.util.getFirstNodeByClassByName("vtkMRMLColorTableNode", "CIP colors")
        if not segmentToLabelValueMapping:
            segmentToLabelValueMapping = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLColorTableNode", "CIP colors")
            segmentToLabelValueMapping.SetTypeToUser()
            segmentToLabelValueMapping.HideFromEditorsOff()
            segmentToLabelValueMapping.SetNumberOfColors(69)
            segmentToLabelValueMapping.SetColor( 0, "background", 0.0, 0.0, 0.0, 0.0)
            segmentToLabelValueMapping.SetColor( 1, "whole lung", 0.42, 0.38, 0.75, 1.0)
            segmentToLabelValueMapping.SetColor( 2, "right lung", 0.26, 0.64, 0.10, 1.0)
            segmentToLabelValueMapping.SetColor( 3, "left lung",  0.80, 0.11, 0.36, 1.0)
            segmentToLabelValueMapping.SetColor(58, "trachea",    0.49, 0.49, 0.79, 1.0)
            segmentToLabelValueMapping.NamesInitialisedOn()
        
        segmentIds = [self.rightLungSegmentId, self.leftLungSegmentId, self.tracheaSegmentId]
        labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLLabelMapVolumeNode")
        slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(self.outputSegmentation, segmentIds, labelmapVolumeNode, self.inputVolume, slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY, segmentToLabelValueMapping)

        # end for compatibility with CIP 

Running this results in:
Failed to compute results: “arguments do not match any overloaded methods”

vtkSlicerSegmentationsModuleLogic::ExportSegmentsToLabelmapNode methods expect segment IDs as a string vector reference or as a vtkStringArray. A list of Python strings can only be used when the argument is a string vector (not a reference). Therefore right now, you need to copy your strings into a vtkStringArray and use that as input to the method:

segmentIds = vtk.vtkStringArray()
for segmentId in segmentIdList:
  segmentIds.InsertNextValue(segmentId)
slicer.modules.segmentations.logic().ExportSegmentsToLabelmapNode(..., segmentIds, ...)
1 Like

I investigated this more and it turns out that VTK is supposed to support string vector references, there was just a bug in VTK that broke this for cases when there were multiple overloads for a method. I’ve reported the error to VTK developers, they fixed it, now we just need to backport these fixes in to Slicer’s VTK. Once that is done, you will not need to create a vtkStringArray but you can directly pass a Python string array to the ExportSegmentsToLabelmapNode method.

1 Like

Could you notify me when the backport is finished?

The fixes will be available in the Slicer Preview Release from tomorrow.

Dear all,

I realized that the Parenchymal Analysis Module is no longer available on the later versions of Slicer Preview Release (4.13.0-2021-12-09). Is this permanent or temporary? If permanent, is there another module that can analyze and create histogram of lung density similar to Parenchymal Analysis Module?

Thank you.