From 068c120fcb4a2eeacd897868aec1c4087a5530bb Mon Sep 17 00:00:00 2001 From: Andres Diaz-Pinto Date: Mon, 16 May 2022 13:55:33 +0100 Subject: [PATCH 1/2] Add original labels option Slicer UI Signed-off-by: Andres Diaz-Pinto --- plugins/slicer/MONAILabel/MONAILabel.py | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/plugins/slicer/MONAILabel/MONAILabel.py b/plugins/slicer/MONAILabel/MONAILabel.py index 801c984aa..d32ee321a 100644 --- a/plugins/slicer/MONAILabel/MONAILabel.py +++ b/plugins/slicer/MONAILabel/MONAILabel.py @@ -151,6 +151,17 @@ def __init__(self, parent): ) allowOverlapCheckBox.connect("toggled(bool)", self.onUpdateAllowOverlap) + originalLabelCheckBox = qt.QCheckBox() + originalLabelCheckBox.checked = True + originalLabelCheckBox.toolTip = "Enable this option to first read original label (predictions)" + groupLayout.addRow("Original Labels:", originalLabelCheckBox) + parent.registerProperty( + "MONAILabel/originalLabel", + ctk.ctkBooleanMapper(originalLabelCheckBox, "checked", str(qt.SIGNAL("toggled(bool)"))), + "valueAsInt", + str(qt.SIGNAL("valueAsIntChanged(int)")), + ) + developerModeCheckBox = qt.QCheckBox() developerModeCheckBox.checked = False developerModeCheckBox.toolTip = "Enable this option to find options tab etc..." @@ -1195,7 +1206,7 @@ def onNextSampleButton(self): logging.info(f"Check if file exists/shared locally: {image_file} => {local_exists}") if local_exists: - self._volumeNode = slicer.util.loadVolume(image_file) + self._volumeNode = slicer.util.loadVolume(image_file, {"discardOrientation": True}) self._volumeNode.SetName(node_name) else: download_uri = f"{self.serverUrl()}/datastore/image?image={quote_plus(image_id)}" @@ -1206,6 +1217,37 @@ def onNextSampleButton(self): nodeNames=node_name, fileNames=image_name, uris=download_uri, checksums=checksum )[0] + if slicer.util.settingsValue("MONAILabel/originalLabel", True, converter=slicer.util.toBool): + try: + download_uri = f"{self.serverUrl()}/datastore/label?label={quote_plus(image_id)}&tag=original" + logging.info(download_uri) + + sampleDataLogic = SampleData.SampleDataLogic() + + originalNode = sampleDataLogic.downloadFromURL( + nodeNames="segmentation_" + image_id, + loadFileTypes="SegmentationFile", + fileNames=image_name, + uris=download_uri, + checksums=checksum, + )[0] + + previousSegmentation = self._segmentNode.GetSegmentation() + originalSegmentation = originalNode.GetSegmentation() + + for idx, label in enumerate(self.info.get("labels")): + segmentOriginal = originalSegmentation.GetSegment(f"Segment_{idx+1}") + segmentOriginal.SetName(label) + self._segmentNode.RemoveSegment(label) + + previousSegmentation.DeepCopy(originalSegmentation) + # Delete original segmentation node + slicer.mrmlScene.RemoveNode(originalNode) + self.showSegmentationsIn3D() + + except: + print("Original label not found ... ") + self.initSample(sample) except: From 60983cb7db1ada41274c52e83af91766c2679409 Mon Sep 17 00:00:00 2001 From: Andres Diaz-Pinto Date: Mon, 16 May 2022 13:56:37 +0100 Subject: [PATCH 2/2] Update Slicer module Signed-off-by: Andres Diaz-Pinto --- plugins/slicer/MONAILabel/MONAILabel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/slicer/MONAILabel/MONAILabel.py b/plugins/slicer/MONAILabel/MONAILabel.py index d32ee321a..5958f4c8e 100644 --- a/plugins/slicer/MONAILabel/MONAILabel.py +++ b/plugins/slicer/MONAILabel/MONAILabel.py @@ -1206,7 +1206,7 @@ def onNextSampleButton(self): logging.info(f"Check if file exists/shared locally: {image_file} => {local_exists}") if local_exists: - self._volumeNode = slicer.util.loadVolume(image_file, {"discardOrientation": True}) + self._volumeNode = slicer.util.loadVolume(image_file) self._volumeNode.SetName(node_name) else: download_uri = f"{self.serverUrl()}/datastore/image?image={quote_plus(image_id)}"