Hi all,
We are working on an extension to 3D Slicer that often uses a large number of small images (>100). However I came across some consistent slowness that is seen when using the SetBackgroundVolumeID()
function. I was able to create some Python code that can replicate the problem using a small PNG image.
import time
# Import lots of data
for i in range(200):
slicer.util.loadVolume("/path/to/test.png")
layoutManager = slicer.app.layoutManager()
shNode = slicer.mrmlScene.GetSubjectHierarchyNode()
# Get ids of loaded data
dataIDs = []
shNode.GetItemChildren(shNode.GetSceneItemID(), dataIDs)
# Fuction to time duration of SetBackgroundVolumeID() call
def timeFunction(colour, dataID):
sliceWidget = layoutManager.sliceWidget(colour)
compositeNode = sliceWidget.mrmlSliceCompositeNode()
print(f"Slice orientation: {sliceWidget.sliceOrientation}")
s = time.time()
compositeNode.SetBackgroundVolumeID(str(dataID))
e = time.time()
timeElapsed = e - s
print(f"Time to call SetBackgroundVolumeID(): {timeElapsed}")
return timeElapsed
# Code to determine the average time per each slice view
yellowTimes = []
greenTimes = []
redTimes = []
for id in dataIDs:
yellowTimes.append(timeFunction("Yellow", id))
greenTimes.append(timeFunction("Green", id))
redTimes.append(timeFunction("Red", id))
print(f"Average time to call SetBackgroundVolumeID() for yellow slice view: {sum(yellowTimes) / len(yellowTimes)}")
print(f"Average time to call SetBackgroundVolumeID() for green slice view: {sum(greenTimes) / len(greenTimes)}")
print(f"Average time to call SetBackgroundVolumeID() for red view: {sum(redTimes) / len(redTimes)}")
I found the following results:
200 images
Average time to call SetBackgroundVolumeID() for yellow slice view: 0.016587159633636474
Average time to call SetBackgroundVolumeID() for green slice view: 0.0006015706062316895
Average time to call SetBackgroundVolumeID() for red view: 0.0005157172679901123
1000 images
Average time to call SetBackgroundVolumeID() for yellow slice view: 0.3258379149436951
Average time to call SetBackgroundVolumeID() for green slice view: 0.0015380308628082276
Average time to call SetBackgroundVolumeID() for red view: 0.0014937591552734374
The slowness can be quite noticeable when 500+ images are loaded. It wouldn’t be an issue if the speed was the same across each slice view but the Yellow slice view seems to slow at greater rate than the other slice views. Would anyone be able to help debug this? Thank you!
test.png