Resample Scalar Volume Automatization

Hi all,

I am attempting to automate a volume resampling process using the reslice function. The issue I am encountering is that the resampling process takes a few seconds (approximately 20 seconds), and I need the code to wait until this process finishes. To address this, I have implemented the waitUntilReslice function. However, I seem to be encountering problems with the resampling status; it never changes to “Completed,” regardless of how long I wait. It consistently remains in the “Completing” state.

Following the resampling process, I also need to adjust the window level. However, I am encountering a “NoneType” error for the “reslicedVolumeNode.” Does anyone have any suggestions on how I can resolve this issue?

Code:

def waitUntilReslice(timeout, res, period=0.25):
  mustend = time.time() + timeout

  while time.time() < mustend:
    if (res.GetStatusString() == 'Completed') or \
        (res.GetStatusString() == 'Completing'): # I want to use only "Completed"
        print(res.GetStatusString())
        return True
    time.sleep(period)
  return False

def reslice(volumeNode, resolution=0.25, name='resliced'):
    
    global res

    reslicedVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode", name)
    parameters = {
        "outputPixelSpacing":"{:},{:},{:}".format(*[resolution]*3),
        "InputVolume":volumeNode.GetID(),
        "interpolationMode":'bspline',
        "referenceVolume": volumeNode.GetID(),
        "OutputVolume":reslicedVolumeNode.GetID()}
    
    res = slicer.cli.run(slicer.modules.resamplescalarvolume, None, parameters)
    
    waitUntilReslice(60, res)
    
    # reslicedVolumeNode = slicer.util.getNode(name)
    
    if(not reslicedVolumeNode == None):
           reslicedVolumeNode.GetDisplayNode().SetAutoWindowLevel(0)
           reslicedVolumeNode.GetDisplayNode().SetWindowLevel(800,100)

    return reslicedVolumeNode

Thanks,

Belén

One option is to use the runSync option like in this example.

Or you can observe the node for changes like this one.

It’s always best to avoid using sleep for things like this. If you do need time-based processing, it’s better to use QTimer since it works with the Slicer event loop.

Solved with runSync. Thanks :slight_smile:

Hi,

I have a similar issue regarding the “wait” topic. I want the program to stop until the user presses some key.

Example:

...
# Ask the user to segment 
print("Now segment the teeth...")

# Wait until the user has segmented and continue the execution
while(key "y" not pressed):
     wait

Now what I’m doing is:

# Ask the user to segment 
input("Now segment the teeth and press key to continue...")

But this implies that the user has to go back to the python interactor and I want to avoid this.

I was thinking about using something with the below code. But still don’t know how to wait in the code.

import qt
import slicer

def moveForward():
    print("Continue pressed")
    continueVariable = True
        
shortcut1 = qt.QShortcut(slicer.util.mainWindow())
shortcut1.setKey(qt.QKeySequence("y"))
shortcut1.connect( 'activated()', moveForward)

How can I wait until a key is pressed?