# Resample Scalar Volume Automatization

**URL:** <https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [October 3, 2023, 9:08am UTC](https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008 "2023-10-03T09:08:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 3, 2023, 9:08am UTC](https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008/1 "2023-10-03T09:08:21Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 3, 2023, 10:52pm UTC](https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008/2 "2023-10-03T22:52:06Z")

</div>

One option is to use the `runSync` option like in [this example](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-run-a-cli-module-from-python).

Or you can observe the node for changes like [this one](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#running-cli-in-the-background).

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.

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 6, 2023, 8:36am UTC](https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008/3 "2023-10-06T08:36:12Z")

</div>

Solved with runSync. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![bserrano](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/bserrano/32/17034_2.png) [@bserrano](https://discourse.slicer.org/u/bserrano)\
**Post date:** [October 24, 2023, 2:42pm UTC](https://discourse.slicer.org/t/resample-scalar-volume-automatization/32008/4 "2023-10-24T14:42:54Z")

</div>

Hi,

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

Example:

```auto
...
# 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:

```auto
# 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.

```auto
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?
