Created
June 13, 2023 16:02
-
-
Save pieper/17cf79b8ea852693bde0b63bafbaa74f to your computer and use it in GitHub Desktop.
Example of assigning segment names from table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import numpy | |
import os | |
import random | |
includeEdema = True | |
exec(open("/opt/data/nac/neurosurgicalatlas/MONAI/labels.py").read()) | |
tureSynthSegGlob = "/opt/data/nac/neurosurgicalatlas/MONAI/Ture-subset-2022-12-03-nii-seg/Ture-*-*.nrrd_synthseg.nii.gz" | |
tureVolumePattern = "/opt/data/nac/neurosurgicalatlas/MONAI/Ture-subset-2022-12-03-nii/Ture-%03d-%s.nrrd.nii.gz" | |
tureMONIAPattern = "/opt/data/nac/neurosurgicalatlas/MONAI/Ture-subset-2022-12-03/24-NEW-Ture-cases/Ture-%03d.nii.gz" | |
saveDir = "/opt/data/nac/neurosurgicalatlas/MONAI/Ture-subset-2022-12-03+ss+ml-with-edema" | |
savePattern = f"{saveDir}/Ture-%03d-%s+ss+ml.seg.nrrd" | |
os.system(f"mkdir -p {saveDir}") | |
synthSegLabelsByValue = {} | |
for label,value in synthSegLabels.items(): | |
synthSegLabelsByValue[value] = label | |
synthSegLabelsByValue[10000] = "Brain tumor + necrotic" | |
synthSegLabelsByValue[10001] = "Edema" | |
segLogic = slicer.modules.segmentations.logic() | |
for synthSegPath in glob.glob(tureSynthSegGlob): | |
slicer.mrmlScene.Clear() | |
caseSplit = os.path.basename(synthSegPath).split("-") | |
caseString = caseSplit[1] | |
caseNumber = int( caseString[1:] if caseString[0] == "0" else caseString ) | |
caseContrast = caseSplit[2].split(".")[0] | |
print(f"processing {caseString} -> {caseNumber}, {caseContrast}") | |
tureVolumePath = tureVolumePattern % (caseNumber, caseContrast) | |
tureMONIAPath = tureMONIAPattern % caseNumber | |
print(tureVolumePath) | |
if not os.path.exists(tureVolumePath): | |
print("failed!") | |
break | |
print(synthSegPath) | |
if not os.path.exists(synthSegPath): | |
print("failed!") | |
break | |
print(tureMONIAPath) | |
if not os.path.exists(tureMONIAPath): | |
print("failed!") | |
break | |
tureVolume = slicer.util.loadVolume(tureVolumePath) | |
ssColors = slicer.mrmlScene.CreateNodeByClass("vtkMRMLColorTableNode") | |
ssColors.SetType(slicer.vtkMRMLColorNode.User) | |
slicer.mrmlScene.AddNode(ssColors) | |
ssColors.SetName(f"ssColors") | |
ssColors.SetNumberOfColors(10002) | |
random.seed(42) | |
for value,label in synthSegLabelsByValue.items(): | |
ssColors.SetColor(value, label, random.random(), random.random(), random.random(), 1.0) | |
ssColors.SetNamesFromColors() | |
for value,label in synthSegLabelsByValue.items(): | |
ssColors.SetColorName(value, label) | |
ssColors.SetColor(10000, "Brain tumor + necrotic", random.random(), random.random(), random.random(), 1.0) | |
ssColors.SetColor(10001, "Edema", random.random(), random.random(), random.random(), 1.0) | |
ssSegmentation = slicer.util.loadSegmentation(synthSegPath, {"colorNodeID": ssColors.GetID()}) | |
ssSegmentation.SetReferenceImageGeometryParameterFromVolumeNode(tureVolume) | |
monaiLabel = slicer.util.loadLabelVolume(tureMONIAPath, {"colorNodeID": ssColors.GetID()}) | |
monaiLabel.SetName(f"{'Ture-%03d' % caseNumber}-monaiLabel") | |
monaiSeg = slicer.mrmlScene.CreateNodeByClass("vtkMRMLSegmentationNode") | |
slicer.mrmlScene.AddNode(monaiSeg) | |
monaiSeg.SetName(f"{'Ture-%03d' % caseNumber}-monaiSeg") | |
monaiSeg.SetReferenceImageGeometryParameterFromVolumeNode(tureVolume) | |
segLogic.ImportLabelmapToSegmentationNode(monaiLabel, monaiSeg) | |
segLogic.ExportSegmentsToLabelmapNode( | |
monaiSeg, | |
monaiSeg.GetSegmentation().GetSegmentIDs(), | |
monaiLabel, | |
tureVolume, | |
slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY, | |
monaiLabel.GetDisplayNode().GetColorNode()) | |
mergedLabel = slicer.mrmlScene.CreateNodeByClass("vtkMRMLLabelMapVolumeNode") | |
slicer.mrmlScene.AddNode(mergedLabel) | |
mergedLabel.SetName(f"mergedLabel") | |
segLogic.ExportSegmentsToLabelmapNode( | |
ssSegmentation, | |
ssSegmentation.GetSegmentation().GetSegmentIDs(), | |
mergedLabel, | |
tureVolume, | |
slicer.vtkSegmentation.EXTENT_REFERENCE_GEOMETRY, | |
ssColors) | |
mergedLabel.GetDisplayNode().SetAndObserveColorNodeID(ssColors.GetID()) | |
ssLabelArray = slicer.util.arrayFromVolume(mergedLabel) | |
ns16LabelArray = slicer.util.arrayFromVolume(monaiLabel) | |
ns16TumorLabel = ns16Labels["Brain tumor + necrotic"] | |
ns16EdemaLabel = ns16Labels["Edema"] | |
ssLabelArray[ns16LabelArray == ns16TumorLabel] = 10000 | |
if includeEdema: | |
ssLabelArray[ns16LabelArray == ns16EdemaLabel] = 10001 | |
slicer.util.arrayFromVolumeModified(mergedLabel) | |
mergedSeg = slicer.mrmlScene.CreateNodeByClass("vtkMRMLSegmentationNode") | |
mergedSeg = slicer.mrmlScene.AddNode(mergedSeg) | |
mergedSeg.SetName(f"{'Ture-%03d' % caseNumber}-merged") | |
mergedSeg.SetReferenceImageGeometryParameterFromVolumeNode(tureVolume) | |
segLogic.ImportLabelmapToSegmentationNode(mergedLabel, mergedSeg) | |
slicer.util.saveNode(mergedSeg, savePattern % (caseNumber,caseContrast)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment