Skip to content

Commit dc680d5

Browse files
committed
ENH: modified slicer3d plugin to load multi-volume datasets
Adds ability to load multiple volumes per sample or load sequence data depending on request Signed-off-by: Cavan Riley <cavan-riley@uiowa.edu>
1 parent 057130e commit dc680d5

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

plugins/slicer/MONAILabel/MONAILabel.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,19 +1288,57 @@ def onNextSampleButton(self):
12881288
return
12891289

12901290
logging.info(sample)
1291-
image_id = sample["id"]
1291+
id = sample["id"]
12921292
image_file = sample.get("path")
1293-
image_name = sample.get("name", image_id)
1294-
node_name = sample.get("PatientID", sample.get("name", image_id))
1293+
image_name = sample.get("name", id)
1294+
node_name = sample.get("PatientID", sample.get("name", id))
12951295
checksum = sample.get("checksum")
12961296
local_exists = image_file and os.path.exists(image_file)
1297+
multichannel: bool = bool(sample.get("multichannel", False))
1298+
multi_file: bool = bool(sample.get("multi_file", False))
12971299

12981300
logging.info(f"Check if file exists/shared locally: {image_file} => {local_exists}")
12991301
if local_exists:
1300-
self._volumeNode = slicer.util.loadVolume(image_file)
1301-
self._volumeNode.SetName(node_name)
1302+
if multichannel:
1303+
# For 4D multichannel images, NOTE: slicer does not like 4D nifti images
1304+
# from https://github.com/Project-MONAI/MONAILabel/issues/241#issuecomment-1497788857
1305+
volumeSequenceNode = slicer.util.loadSequence(image_file)
1306+
volumeSequenceNode.SetName(node_name)
1307+
# Get a volume node
1308+
browserNode = slicer.modules.sequences.logic().GetFirstBrowserNodeForSequenceNode(
1309+
volumeSequenceNode
1310+
)
1311+
browserNode.SetOverwriteProxyName(
1312+
None, True
1313+
) # set the proxy node name based on the sequence node name
1314+
self._volumeNode = browserNode.GetProxyNode(volumeSequenceNode)
1315+
else:
1316+
if not multi_file:
1317+
self._volumeNode = slicer.util.loadVolume(image_file)
1318+
self._volumeNode.SetName(node_name)
1319+
else: # in the case the underlying dataset is multi_file, we load all the images in the directory
1320+
dir_path = image_file
1321+
if not os.path.isdir(dir_path):
1322+
raise ValueError(f"multi_file=True but path is not a directory: {dir_path}")
1323+
1324+
# get valid image paths
1325+
entries = sorted(os.listdir(dir_path))
1326+
image_paths = []
1327+
for name in entries:
1328+
full_path = os.path.join(dir_path, name)
1329+
if os.path.isfile(full_path):
1330+
image_paths.append(full_path)
1331+
1332+
nodes = []
1333+
for idx, image in enumerate(image_paths):
1334+
image_base_name = os.path.basename(image)
1335+
node = slicer.util.loadVolume(image)
1336+
node.SetName(image_base_name)
1337+
nodes.append(node)
1338+
1339+
self._volumeNode = nodes[0]
13021340
else:
1303-
download_uri = f"{self.serverUrl()}/datastore/image?image={quote_plus(image_id)}"
1341+
download_uri = f"{self.serverUrl()}/datastore/image?image={quote_plus(id)}"
13041342
logging.info(download_uri)
13051343

13061344
sampleDataLogic = SampleData.SampleDataLogic()
@@ -1311,7 +1349,7 @@ def onNextSampleButton(self):
13111349
if slicer.util.settingsValue("MONAILabel/originalLabel", True, converter=slicer.util.toBool):
13121350
try:
13131351
datastore = self.logic.datastore()
1314-
label_info = datastore["objects"][image_id]["labels"]["original"]["info"]
1352+
label_info = datastore["objects"][id]["labels"]["original"]["info"]
13151353
labels = label_info.get("params", {}).get("label_names", {})
13161354

13171355
if labels:
@@ -1323,7 +1361,7 @@ def onNextSampleButton(self):
13231361
labels = self.logic.info().get("labels")
13241362

13251363
# ext = datastore['objects'][image_id]['labels']['original']['ext']
1326-
maskFile = self.logic.download_label(image_id, "original")
1364+
maskFile = self.logic.download_label(id, "original")
13271365
self.updateSegmentationMask(maskFile, list(labels))
13281366
print("Original label uploaded! ")
13291367

0 commit comments

Comments
 (0)